简体   繁体   English

如何在没有js的情况下设置div1 = container = div2的高度

[英]How to set height of div1 = container = div2 without js

html structure: html结构:

<div id="container">
  <div id="a">
  </div>

    <div id="b">
    </div>
</div>

css: CSS:

#container {
  background-color:blue;
  text-align:center;
  margin:0 auto;
  width:60%;
  padding:20px;
  position:relative;
}

#a {
  background-color:green;
  width:40%;
  display:inline-block;
  vertical-align:top;
  height:100%;
}

#b {
  background-color:red;
  width:40%;
  display:inline-block;
  vertical-align:top;
  height:100%;
}

Is there a way to set the height of "a" to the height of "b" by using the fact, that "container" equals the height of the longest div? 有没有办法通过使用“容器”等于最长div的高度这一事实将“a”的高度设置为“b”的高度? As you see I'm trying to achieve this by setting container's height to auto (=height equals the longest div) and then set the height of the divs "a"&"b" to 100%. 如你所见,我试图通过将容器的高度设置为auto(= height等于最长div)来实现此目的,然后将div“a”和“b”的高度设置为100%。 But that won't work. 但那不行。

I'd like to avoid to use js. 我想避免使用js。

Codepen here: http://codepen.io/danielpixelz/pen/VLLpjO Codepen在这里: http ://codepen.io/danielpixelz/pen/VLLpjO

Sure, just change the display of the child divs from inline-block to table-cell : 当然,只需将子div的显示从inline-block更改为table-cell

#a, #b {
    display:table-cell;
}

jsFiddle example jsFiddle例子

 #container { background-color:blue; text-align:center; margin:0 auto; width:60%; padding:20px; position:relative; } #a { background-color:green; width:40%; } #b { background-color:red; width:40%; } #a, #b { display:table-cell; } 
 <div id="container"> <div id="a">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</div> <div id="b">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata</div> </div> 

I have a feeling that you want to replicate the style of a table but without the syntax of a table. 我有一种感觉,你想复制一个表的样式,但没有表的语法。

Assign the two "a" and "b" div's css to display:table-cell; 分配两个“a”和“b”div的css来显示:table-cell;

  display:table-cell;

You may want to research its browser acceptance as it may not work for all browsers. 您可能希望研究其浏览器接受度,因为它可能不适用于所有浏览器。

http://codepen.io/anon/pen/WvvpOp http://codepen.io/anon/pen/WvvpOp

If you can use flex its very easy, Please have a look at the demo 如果您可以非常轻松地使用flex ,请查看演示

http://jsfiddle.net/Flocke/vxUb2/ http://jsfiddle.net/Flocke/vxUb2/

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

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