css - 在这种特定情况下如何将文本居中放置在 div 中?

标签 css image responsive-design centering

<分区>

如何使该框内的文本居中?

下面是我希望如何整理元素:

enter image description here

在较小的屏幕中,元素相互堆叠,包含我想要居中的文本的 .text-box div 具有固定的高度。对于较大的宽度,.text-box div 的高度应该等于较大图像的高度减去较短图像的高度

See the Fiddle here

HTML

  <div id="wrapper">
    <div class="box">
      <img class="img-large" src="http://placekitten.com/900/800" alt=""/>
    </div>
    <div class="box">
        <img src="http://placekitten.com/900/400" alt=""/>    
    </div>
    <div class="box-text">
        <div class="vcenter-outer">
            <div class="vcenter-inner">
                <p>center this text vertically</p>  
            </div>
        </div>
    </div>
  </div>

CSS

#wrapper {
    background: #ffeaea;
    height: 100vh;
}
img {
    width: 100%;
    display: block;
}
.img-large {
    height: 100vh;
    width: auto;
}
.box {
    position: relative;
    width: 100%;
    overflow: hidden;
}
.box-text {
    text-align: center;
    background: yellow;
    height: 100%;
}

@media only screen and (min-width: 768px) {
    .box {
        float: left;
        width: 50%;
    }
}

.vcenter-outer {
    background: yellow;
    display: table;
}

.vcenter-inner {
    background: lightblue;
    display: table-cell;
    vertical-align: middle;
}

为什么 .text-box div 跨越整个包装器?

最佳答案

基本上你可以同时使用 display:flex 和 float

.trio {
  height:100vh;
  width:100vh;
}
.trio>div {
  float:left;
  display:flex;
  align-items:center;
  justify-content:center;
  width:50vh;
  height:50vh;
  box-shadow:inset 0 0 0 2px;
}
.trio .first {
  height:100vh;
}
<div class="trio">
  <div class="first">
    <p>Center</p>
  </div>
  <div class="next">
    <p>Center</p>
  </div>
  <div class="next">
    <p>Center</p>
  </div>
</div>

display:table 也可以像你一样使用额外级别的 div 来工作,同样的想法:float + vh

codepen to play with

关于css - 在这种特定情况下如何将文本居中放置在 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440879/

上一篇:JQuery 设置事件链接

下一篇:css - 不适用于 ASP.NET MVC 站点的样式

相关文章:

html - 如何让这个 Sprite 按照我想要的方式运行?

java - 如何将 CLOB 转换为图像,反之亦然?

xcode - 设置UIButton图像并保持透明度

javascript - JavaScript 中的多媒体查询

html - 试图删除标签和组合框之间的填充

CSS:第 nth-of-type(2) 未应用于类

html - CSS 问题,位置 : absolute

javascript - 渐进式宽度调整 : is this for aesthetic or technical reasons?

responsive-design - Zurb 基金会 : Does the basic grid work without javascript?

html - 移动优先响应式网页设计和响应式图像问题