简体   繁体   English

CSS:替代垂直对齐?

[英]CSS : alternative to vertical-align?

Is there any alternative to vertical-align? 除了垂直对齐,还有其他选择吗?

For using vertical align I am having to set the display to table-cell. 为了使用垂直对齐,我必须将显示设置为表格单元。 When I have the display set to table-cell the height of the div that I have set does not work. 当我将显示设置为table-cell时,我设置的div高度不起作用。 I have overflow-y set to auto on that div. 我在该div上将溢出Y设置为自动。 What I am trying to do is align the content inside the div from the bottom of that div... I am not able to do that.. Any alternatives? 我想做的是将div中的内容从该div的底部对齐...我无法做到这一点。是否有其他选择?

This is what I have right now: 这就是我现在所拥有的:

#container{
height:375px;
border:1px solid #000;
position:relative;
overflow-y:auto;
display:table-cell;
vertical-align:bottom;
}

#container > div{
margin:0;
margin-bottom:5px;
width:660px;
position:relative;
}

There are 2 alternatives, one is to set line-height .. and other one is to set the parent element to position: relative; 有两种选择,一种是设置line-height ..另一种是将父元素设置为position: relative; and than set your child element to position: absolute; 然后将您的子元素设置为position: absolute; and later, use top: 50%; 然后使用top: 50%; and left: 50%; left: 50%; and than deduct the margins which will be 1/2 of the total height and width of the absolute element itself... 然后减去边距,该边距将是absolute元素本身总heightwidth的1/2 ...

.parent {
   position: relative;
}

.child {
   position: absolute;
   top: 50%;
   left: 50%;
   margin-top: -100px; /* Assuming height of the element is 200 */
   margin-left: -200px; /* Assuming width of the element is 400 */
}

Here's a catch though, using absolute will require fixed dimensions of the element you are trying to align vertically center 这里有一个catch虽然,使用absolute你试图元素都需要固定尺寸align垂直center


Vertical Aligning an element using display: table-cell; 使用display: table-cell;垂直对齐元素display: table-cell;

Demo 演示版

.parent {
    height: 200px;
    background: #eee;
    display: table-cell;
    width: 300px;
    vertical-align: bottom;
}

.child {
    height: 20px;
    background: #aaa;
}

Also it would be better if you use display: table; 如果使用display: table;也会更好display: table; as a wrapping element. 作为包装元素。

try this: 尝试这个:

#container{
height:375px;
line-height:375px;
}

#container > div{
display:inline-block;
line-height:1;
}

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

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