【问题标题】:How can I make this picture always stay in the center of the div?我怎样才能让这张图片始终保持在 div 的中心?
【发布时间】:2016-02-13 22:09:08
【问题描述】:

我正在使用引导程序来构建自己的网站。我的 PAGE1 分为 3 列。在第三列中,我想在中心放置一张图片,但只能水平放置。我想知道为什么“margin-left:auto;”和“margin-right:auto;”不要做这项工作。请帮我把这张图片居中。

这是相关代码:

HTML:

<div id="PAGE1" class="container-fluid">
        <div id="backgroundDiv1"></div>
        <div id="backgroundDiv2"></div>
        <div style="height:100%" class="row">
            <div id="reihe1"class="col-md-3"></div>
            <div id="reihe2" class="col-md-3"></div>
            <div id="reihe3"class="col-md-6">
                <div id="googleDiv" data-toggle="tooltip" data-placement="left" title="google+"><img id="googleplus" src="google+.png"></div>
                <div id="titel">
                    <p>*******</p>
                    <p>*******</p>
                    <p id="designer">*********</p>
                    <img src="pfeil.png" id="arrow">
                </div>
            </div>
        </div>
    </div>

CSS:

#PAGE1{
    overflow:hidden;
}
#backgroundDiv1{
    position: absolute;
    background-color:#848484;
    width: 1200px;
    height: 500px;
    z-index: 1;
    bottom:0;
    border-top-right-radius:700px;
    border-top-left-radius:200px;
    border-right: solid black 20px; 
}
#backgroundDiv2{
    position: absolute;
    background-color:#742B22;
    width: 800px;
    height: 400px;
    z-index: 2;
    bottom:0;
    border-top-right-radius:400px;
    border-top-left-radius:200px;
    border-right: solid black 20px;
}
#titel{
    position: relative;
    width:100%;
    height:100%;
    text-align:center;
    font-size:450%;
    color:#222222;
    padding-right: 50px;
    padding-top: 100px;
}
#designer{
    font-size: 0.6em!important;
    padding-left: 300px;
}
#arrow{
    margin-left: auto;
    margin-right: auto;
}

【问题讨论】:

    标签: jquery html css twitter-bootstrap


    【解决方案1】:

    它不起作用,因为没有声明图像的宽度。 Margin:auto 仅在对象的宽度已知时有效,通过width:Npx CSS 标签或在 img 标签的情况下,宽度属性。

    【讨论】:

    • 我刚刚添加了css属性“width:100px;”。不过还是遇到了同样的问题。
    • 父 div 可能还需要一个宽度属性。我已经有一段时间没有解决这类问题了,但我确实记得margin:auto 的“魔法”只有在明确声明宽度时才有效。
    • 嗯,放图片的行应该有宽度,因为class="col-md-6"。
    • 应用显示:块;边距:0 自动;在您的图像上
    【解决方案2】:

    要么对 img 应用宽度,要么将图像包装在 div 中,然后将 text-align:center 应用到 div。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 2010-12-31
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多