简体   繁体   English

将div与顶部内联对齐

[英]align divs with the top inline

i've got a div containing two other divs that should be displayed with the tops inline. 我有一个包含两个其他div的div,这些div应该与顶部的行一起显示。 I've got the problem that they are intended to be on one level at the top, but when i fill them with a different number of lines of text one is getting pushed upwards. 我有一个问题,他们打算将它们放在顶部的一个层次上,但是当我用不同数量的文本行填充它们时,一个文本就会被向上推。 It would be awesome if both divs would have the same size and would be aligned exactly inline. 如果两个div都具有相同的大小并且精确地内联对齐,那就太棒了。

The CSS of the parent div: 父div的CSS:

#parent{
position:relative;
border:1px solid black;
width:303px;
}

The CSS of the first div: 第一个div的CSS:

#firstdiv{  
position:absolute; 
bottom:0; 
left:0; width:150px; 
border:1px solid black;
}

The CSS of the second div: 第二个div的CSS:

#seconddiv{  
position:absolute; 
bottom:0; 
right:0;
width:150px; 
border:1px solid black;
}

The result: 结果:

在此处输入图片说明

JSfiddle! JSfiddle!

You can try flex like this : 您可以这样尝试flex:

 * { box-sizing: border-box; } #parent { display: flex; flex-wrap: wrap; border: 1px solid black; width: 300px; text-align: center; } #parent span { width: 100%; padding: 20px; } .item { flex: 1; border: 1px solid black; } 
 <div id="parent"> <span>some content</span> <div class="item"> some content </div> <div class="item"> more more content and even more </div> </div> 

If this is how you want it to look according to your screenshot. 如果这是您希望它根据屏幕截图显示的样子。 Using position:absolute will be a disaster as you add more elements to it. 当您向位置添加更多元素时,使用position:absolute将是一场灾难。 Use flex instead. 请改用flex。

You can see that the child containers maintain there original height. 您会看到子容器保持原来的高度。 That is because of the class flex-item using flex-start to align them at top. 这是因为使用flex-start的flex-item类将它们顶部对齐。 but if you want them to occupy full height. 但是如果您希望它们占据全部高度。 Just remove flex-start; 只需删除flex-start即可;

 #parent { margin-top: 30px; position: relative; border: 1px solid black; width: 303px; text-align: center; } .sub-container { display: flex; } .flex-item { align-self: flex-start; //Use this only if you want uneven heights and want to align them from top } #firstdiv { width: 150px; border: 1px solid black; } #seconddiv { width: 150px; border: 1px solid black; } 
 <div id="parent"> Hello Stack overflow! <div class="sub-container"> <div id="firstdiv" class="flex-item"> First div with more content </div> <div id="seconddiv" class="flex-item"> second div </div> </div> </div> 

You can remove all position attributes and use display: table-cell in both first and second divs 您可以删除所有位置属性,并在第一和第二个div中使用display: table-cell

see below snippet : 请参阅以下代码段:

 #parent { border: 1px solid black; width: 400px; text-align: center; } #firstdiv { display: table-cell; width: 200px; border: 1px solid black; } #seconddiv { display: table-cell; width: 200px; border: 1px solid black; } 
 <div id="parent"> <span>Hello text<span> <div id="firstdiv">Stack </div> <div id="seconddiv">Overflow ing text and text adn text and other text </div> </div> 

Set the top of both absolute positioned children to the same value. 将两个绝对定位的子代的顶部设置为相同的值。

eg 20px or desired value 例如20px或期望值

https://www.w3schools.com/cssref/pr_pos_top.asp https://www.w3schools.com/cssref/pr_pos_top.asp

ie

#firstdiv {  
  position: absolute; 
  bottom: 0; 
  left: 0;
  width: 150px; 
  border: 1px solid black;
  top: 20px
}

#seconddiv {  
  position: absolute; 
  bottom: 0; 
  right: 0;
  width: 150px; 
  border: 1px solid black;
  top: 20px;
} 

You can use the table display attributes This will make sure all the cells are same size and will let you remove the positioning. 您可以使用table显示属性,这将确保所有单元格的大小均相同,并使您可以删除定位。

jsFiddle jsFiddle

 #parent { display: table; border: 1px solid black; width: 303px; } #content { display: table-row; } #firstdiv { width: 50%; display: table-cell; border: 1px solid black; } #seconddiv { display: table-cell ; width: 50%; border: 1px solid black; } 
 <div id="parent"> <div id="content"> Hello Stackoverflow! </div> <div id="firstdiv"> first div with more content </div> <div id="seconddiv"> second div </div> </div> 

Remove 去掉

bottom: 0;    

from both the first and the second div . 从第一和第二div

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

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