【问题标题】:Center content in the middle of div container将内容居中放在 div 容器的中间
【发布时间】:2014-04-06 10:42:19
【问题描述】:

我正在尝试创建一个“平铺”(方形块),其内容完全居中在它的中间。

这是 HTML

  <div class="tile-facebook">
    <h5>Facebook</h5>
    <div class="tile-notification">4</div>
    <h4>notifications</h4>
  </div>

还有 CSS

.tile-facebook{
  width:175px;
  height:175px;
  background: #3b5998 ;
  text-align: center;
  border-radius: 10px;
  border-style: solid;
  border-color: #ccc;
  border-width:1px;
  color: white;
}

.tile-notification{
  font-size:80px;
  font-weight:bold;
  padding:10px 0px;

}

我将文本放在块的中间,但我希望它直接位于中间,顶部和底部的填充相同。想法?

【问题讨论】:

  • 这是您尝试做的吗? jsfiddle.net/cnq82
  • 是的,你明白了!谢谢!
  • oki,刚刚通过一些关于垂直填充/边距的链接回答了它:)

标签: html css


【解决方案1】:

您可能不设置高度,但使用伪元素或额外元素从框的任何宽度绘制正方形。

100% + inline-block 的垂直填充是一种绘制正方形并在其中间添加内容的方法。

<div class="tile-facebook">
    <div class="wrapper">
        <h5>
            Facebook
        </h5>
        <div class=" tile-notification ">
            4
        </div>
         <h4>
             notifications
        </h4>
    </div>
</div>
.tile-facebook {
    width:175px;
    background: #3b5998;
    text-align: center;
    border-radius: 10px;
    border-style: solid;
    border-color: #ccc;
    border-width:1px;
    color: white;
}
.tile-notification {
    font-size:80px;
    font-weight:bold;
}
.tile-facebook .wrapper * {
    margin:0;
}
.tile-facebook:before {
    padding-top:100%;
    content:'';
}
.tile-facebook:before,
.wrapper {
    display:inline-block;
    vertical-align:middle;
}

http://jsfiddle.net/cnq82/

关于垂直百分比填充或边距的一些解释:http://www.w3.org/TR/CSS2/box.html#padding-properties & http://www.w3.org/TR/CSS2/box.html#propdef-margin-top

所以,(希望它让它更清楚一点:))

如果您提供 100% 的垂直填充,则其高度将等于父级的宽度。

如果您希望高度大于 20px,您可以这样做:padding:100% 0 20px 0 ;padding:20px 0 100% 0;

如果您想要一个比例为 4:3 的盒子,只需执行 padding-top:75%;padding:50% 0 25% 0;

伪元素或额外元素可以是浮动的,也可以是用于垂直对齐的内联块。

您不需要在父级的 CSS 中设置宽度。

【讨论】:

  • 您应该添加一些关于如何更改外框高度的注释(就像 OP 在他的小提琴中所做的那样),顺便说一句,我们必须更改 :before 元素的 padding-top相反,OP 应该注意这一点,以防他想稍微增加或减少高度。
  • W3C 上的链接,如果阅读说明一切:) 100% on padding 给出 100% 的宽度 padding-top 和 bottom 允许混合 % 和任何其他值。 100% 的比例为 1:1 等... :) 我相信问题是关于一个正方形的 :)
【解决方案2】:

此修复要求内容高度永远不会改变,您需要添加另一个&lt;div&gt;

<div class="tile-facebook">
  <div class="center">
    <h5>Facebook</h5>
    <div class="tile-notification">4</div>
    <h4>notifications</h4>
  </div>
</div>

并添加 CSS:

.title-facebook {
    position: relative;    
}

.center {
    position: absolute;
    top: 50%;
    margin-top: -[half height];
    left:0;
    width: 100%;
}

其中[half height].center div 高度的一半。

【讨论】:

  • 在 jsfiddle 中运行它会使文本严重溢出蓝色 div 的底部和左侧边缘。
  • 我添加了left: 0; width: 100%; 来解决这个问题。我只关注高度而忘记了其他维度......
  • 最好使用transform:translate而不是手动设置margin-top,这不是很灵活,但是使用转换更简单,需要我们添加一些不同版本的供应商前缀,以便css工作跨浏览器。
【解决方案3】:

在此处将 margin: -30px; 添加到您的 CSS:

.tile-notification {
    font-size:80px;
    font-weight: bold;
    padding: 10px 0px;
    margin: -30px;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2023-04-06
    • 2015-04-18
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多