简体   繁体   English

绝对/相对定位会干扰相邻div的对齐

[英]Absolute/relative positioning disrupts alignment of adjacent div

I am using absolute and relative positioning to horizontally and vertically center a div in a container div. 我正在使用绝对和相对定位在容器div中水平和垂直居中放置div。 Adjacent to this container is another div which should fit neatly beside the container inside the top-level container div. 与该容器相邻的是另一个div,它应整齐地放置在顶级容器div内的容器旁边。 But instead, it moves down, almost completely out of the boundary of the top-level div. 但是,它向下移动,几乎完全脱离了顶层div的边界。 Source code: 源代码:

 #top-level { background: #90c0ff; height: 400px; width: 600px; } #container { background: #bbffbb; height: 400px; width: 400px; display: inline-block; position: relative; text-align: center; } #inner { height: 200px; width: 200px; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); border: 1px solid black; } #adjacent { background: #ff5050; height: 395px; width: 195px; display: inline-block; } 
 <div id="top-level"> <div id="container"> <div id="inner"> Internal Text </div> </div> <div id="adjacent"> Sample text </div> </div> 

Example fiddle here 示例在这里摆弄

Any ideas on why the adjacent div doesn't align properly? 关于相邻div为何无法正确对齐的任何想法?

You could use flex on the parent instead of inline-block on the children, would solve the issue of the second box being pushed down if there isn't enough space: 您可以在父级上使用flex而不是在子级上使用inline-block,这样可以解决如果空间不足的话第二个盒子被压下的问题:

 #top-level { background: #90c0ff; height: 400px; width: 600px; } #container { background: #bbffbb; height: 400px; width: 400px; position: relative; text-align: center; display: inline-block; vertical-align:top; } #inner { height: 200px; width: 200px; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); border: 1px solid black; } #adjacent { background: #ff5050; height: 395px; width: 195px; display: inline-block; vertical-align:top; } 
 <div id="top-level"> <div id="container"> <div id="inner"> Internal Text </div> </div> <div id="adjacent"> Sample text </div> </div> 

If you want to know the actual reason for your alignment issues, it's because you have two inline block elements that are different heights next to each other. 如果您想知道对齐问题的实际原因,那是因为您有两个高度不同的内联块元素。

The default vertical alignment for inline block elements is baseline which means that you get the effect that you see. 内联块元素的默认垂直对齐方式是基线,这意味着您将获得所看到的效果。

If you change the vertical align to top for both the container and the adjacent, your code will work as you want: 如果将容器和相邻容器的垂直对齐方式都更改为顶部对齐,则代码将按需要工作:

 #top-level { background: #90c0ff; height: 400px; width: 600px; /* add te following */ display: flex; justify-content: space-between; } #container { background: #bbffbb; height: 400px; width: 400px; position: relative; text-align: center; } #inner { height: 200px; width: 200px; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); border: 1px solid black; } #adjacent { background: #ff5050; height: 395px; width: 195px; } 
 <div id="top-level"> <div id="container"> <div id="inner"> Internal Text </div> </div> <div id="adjacent"> Sample text </div> </div> 

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

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