简体   繁体   English

垂直对齐中间表单元格以响应文本

[英]Vertical align middle table cell responsive with text

I want to centre content in a div using the table approach --- see first section here 我想使用表格方法在div中将内容居中--- 请参阅此处的第一部分

This is all good, however I have an issue. 一切都很好,但是我有一个问题。

I have 2 divs, each taking up 50% width. 我有2个div,每个div占用50%的宽度。 The left div contains an image which is px` in length. 左div包含长度为px`的图像。 The right div contains text. 右边的div包含文本。 I can set a height to the div containing the text and it works, however, when I make the screen smaller it is not responsive. 我可以为包含文本的div设置一个高度,它可以工作,但是,当我缩小屏幕时,它没有响应。 Can anyone tell me, if there is one, the technique to decrease the height of the div with the text in it as the screen reduces in size? 谁能告诉我(如果有的话)一种随着屏幕尺寸减小而减小带有文本的div高度的技术?

I want to retain the 2 divs side by side, and reduce the height of the div on the right as the screen gets smaller, at the same rate that the image on the right decreases in size. 我想并排保留2个div,并随着屏幕变小来减小右侧div的高度,其速率与右侧图像的减小相同。

See jsfiddle HERE and code below to demonstrate: 请参阅jsfiddle HERE和下面的代码进行演示:

HTML 的HTML

<div class="left">
   <img src="http://www.profightdb.com/img/wrestlers/thumbs-600/1414330df8brethart.jpg">
</div>

<div class="right">
  <div class="table">
    <div class="table-cell">
      <p>Test</p>
      <p>Lorem ipsu se skio sas nie lorem dtakorem ipsu se skio sas nie lorem dtak lorem ipsu se skio sas nie lorem dtak</p>
    </div>
  </div>
</div>

CSS 的CSS

img {
  width: 100%;
}

.left, .right {
  float: left;
  width: 50%;
}

.table {
  height: 400px;
  display: table;
}

.table-cell {
  display: table-cell;
  vertical-align: middle;
}

Why do u use table for this? 你为什么要用桌子呢? Use a flex. 使用伸缩。

 img { width: 100%; } .left, .right { width: 50%; /* float: left; */ } /* .table { height: 400px; display: table; } .table-cell { display: table-cell; vertical-align: middle; } */ body { display: flex; align-items: center; } 
 <div class="left"> <img src="http://www.profightdb.com/img/wrestlers/thumbs-600/1414330df8brethart.jpg"> </div> <div class="right"> <div class="table"> <div class="table-cell"> <p>Test</p> <p>Lorem ipsu se skio sas nie lorem dtakorem ipsu se skio sas nie lorem dtak lorem ipsu se skio sas nie lorem dtak</p> </div> </div> </div> 

Try flexbox . 尝试flexbox Updated fiddle here . 更新小提琴这里

 .wrapper { display: flex; align-items: center; } .child { flex: 1; } img { width: 100%; } 
 <div class="wrapper"> <div class="child"> <img src="http://www.profightdb.com/img/wrestlers/thumbs-600/1414330df8brethart.jpg"> </div> <div class="child"> <p>Test</p> <p>Lorem ipsu se skio sas nie lorem dtakorem ipsu se skio sas nie lorem dtak lorem ipsu se skio sas nie lorem dtak</p> </div> </div> 

I have find the following revised html structure is working 我发现以下修改后的html结构有效

<div class="table">
    <div class="table-cell">
      <img src="http://www.profightdb.com/img/wrestlers/thumbs-600/1414330df8brethart.jpg">
    </div>
    <div class="table-cell">
      <p>Test</p>
      <p>Lorem ipsu se skio sas nie lorem dtakorem ipsu se skio sas nie lorem dtak lorem ipsu se skio sas nie lorem dtak</p>
    </div>
</div>

while you have to change the height of .table to height: auto; 当您必须将.table的高度更改为height: auto;

Or maybe you would like to use the unit vh of height which represent the viewpoint height: try height: 50vh; 或者,也许您想使用代表视点高度的高度单位vh :试试height: 50vh;

 <div class="table clearfix">
        <div class="left">
           <img src="http://www.profightdb.com/img/wrestlers/thumbs-600/1414330df8brethart.jpg">
        </div>

        <div class="right">
            <div class="table-cell">
              <p>Test</p>
              <p>Lorem ipsu se skio sas nie lorem dtakorem ipsu se skio sas nie lorem dtak lorem ipsu se skio sas nie lorem dtak</p>
            </div>
        </div>
    </div>
 .clearfix:after {
                    visibility: hidden;
                    display: block;
                    font-size: 0;
                    content: " ";
                    clear: both;
                    height: 0;
                }
                .clearfix {
                    display: inline-block;
                }
                * html .clearfix {
                    height: 1%;
                }
                .clearfix {
                    display: block;
                }
                .table {
                    width: 1000px;
                    margin: auto;
                }
                .left img {
                    width: 100%;
                    position: absolute;
                    top: 0;
                    bottom: 0;
                    margin: auto;
                }
                .left,
                .right {
                    float: left;
                    width: 50%;
                    position: relative;
                    height: 100%;
                }
                .right {
                    display: table;
                }
                .table-cell {
                    display: table-cell;
                    vertical-align: middle;
                }

This code may help you. 此代码可以为您提供帮助。

I updated your fiddle: https://jsfiddle.net/9bjan951/4/ 我更新了您的小提琴: https : //jsfiddle.net/9bjan951/4/

Basically what I've done: 基本上我已经做了:

  • Added a wrapper-element 添加了一个包装元素
  • Clear the floating, so the wrapper gets a height 清除浮动,使包装器达到高度
  • Set the right column to absolute position and give it always 100% height of its parent 将右栏设置为绝对位置,并始终为其父栏设置100%的高度
  • Set the right table to 100% height 将右桌子设置为100%的高度
  • For narrow screens the text will overflow with a scrollbar 对于狭窄的屏幕,文本将带有滚动条溢出

Most of the magic happens here: 大多数魔术都发生在这里:

.right {
  height: 100%;
  background: #c54a8d;
  position: absolute;
  top: 0;
  left: 50%;
  right: 0;
  bottom: 0;
  overflow: hidden;
  overflow-y: auto;
} 

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM