[英]Vertically center a div inside another div
I'm trying to vertical-align: middle
a div inside another div, but for some reason it's not working properly.我正在尝试
vertical-align: middle
一个 div 放在另一个 div 的vertical-align: middle
,但由于某种原因,它无法正常工作。 What am I doing wrong?我究竟做错了什么?
#wrapper { border: 1px solid red; width: 500px; height: 500px; } #block { border: 1px solid blue; width: 500px; height: 250px; vertical-align: middle; }
<div id = 'wrapper'> <div id = 'block'> I'm Block </div> <div>
PS: This is just an example, so no, I don't actually know the actual width and height of the divs, as they are dynamic, according to their contents, so please no margin-top: 125px, or padding-top: 125px answers, or answers like that.
PS:这只是一个例子,所以不,我实际上并不知道 div 的实际宽度和高度,因为它们是动态的,根据它们的内容,所以请不要使用 margin-top: 125px 或 padding-top: 125px 的答案,或类似的答案。
The vertical-align
property applies only to inline-level and table-cell elements ( source ). vertical-align
属性仅适用于行内级和表格单元格元素 ( source )。 In your code you're working with block-level elements.在您的代码中,您正在使用块级元素。
Try this flexbox alternative:试试这个 flexbox 替代方案:
#wrapper { border: 1px solid red; width: 500px; height: 500px; display: flex; /* establish flex container */ align-items: center; /* vertically center flex items */ } #block { border: 1px solid blue; width: 500px; height: 250px; /* vertical-align: middle; */ }
<div id='wrapper'> <div id='block'> I'm Block </div> <div>
Learn more about flex alignment here: Methods for Aligning Flex Items在此处了解有关 Flex 对齐的更多信息:对齐 Flex 项目的方法
Browser support: Flexbox is supported by all major browsers, except IE < 10 .浏览器支持:除 IE < 10 外,所有主流浏览器都支持Flexbox。 Some recent browser versions, such as Safari 8 and IE10, requirevendor prefixes .
一些最新的浏览器版本,例如 Safari 8 和 IE10,需要供应商前缀。 For a quick way to add prefixes use Autoprefixer .
要快速添加前缀,请使用Autoprefixer 。 More details in this answer .
此答案中的更多详细信息。
Here is how I normally do this.这是我通常的做法。
#wrapper { border: 1px solid red; width: 500px; height: 500px; position: relative; } #block { border: 1px solid blue; width: 500px; height: 250px; position: absolute; top: 50%; transform: translateY(-50%); }
<div id="wrapper"> <div id="block"></div> </div>
Easy way to make it dynamic.使其动态化的简单方法。
You can do it this way:你可以这样做:
#wrapper {
border: 1px solid red;
width: 500px;
height: 500px;
}
#block {
border: 1px solid blue;
width: 500px;
height: 250px;
position: relative;
top: 50%;
transform: translateY(-50%);
}
Here a live view: https://jsfiddle.net/w9bpy1t4/这里有一个实时视图: https : //jsfiddle.net/w9bpy1t4/
You can do this:你可以这样做:
#block {
border: 1px solid blue;
margin: 25% 0;
width: 500px;
height: 250px;
vertical-align: middle;
}
But that's not what you looking for!但这不是你要找的!
or like this:或者像这样:
#wrapper {
border: 1px solid red;
width: 500px;
height: 500px;
display: table-cell;
vertical-align: middle;
}
#block {
border: 1px solid blue;
display: inline-block;
width: 500px;
height: 250px;
}
if u know the height of the inner div then u can use position relative on wrapper and position absolute in inner div with some margin.如果你知道内部 div 的高度,那么你可以使用包装器上的相对位置和内部 div 中的绝对位置,并带有一些边距。 So css can be this
所以css可以是这个
#wrapper {
border: 1px solid red;
width: 500px;
height: 500px;
position: relative;
}
#block {
border: 1px solid blue;
width: 500px;
height: 250px;
vertical-align: middle;
position: absolute;
margin-top: 50%;
top: -125px;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.