How can you set the height of an outer div to alwa

2020-01-22 14:20发布

问题:

I have an outer div which contains three inner divs.

I want the left-most inner div to always determine the height of the outer div. If the other inner divs have less content [than the left-most div], they will have empty space. If they have more content [than the left-most div], they will get a scroll bar.

Some other questions on SO ask about setting the height of an outer div to be the height of the inner div. However, in my case I have multiple inner divs and I don't what the height of the outer div to ever be more or less than the height of the left-most div.

Is there any way to do this, especially using only CSS?

回答1:

An idea is to make some of the content out of the flow using absolute position so it won't have any impact on the height and then use flexbox for the main layout:

.container {
  display: flex;
  border: 1px solid;
  padding: 5px;
  box-sizing: border-box;
}

.container>div {
  flex: 1;
  margin: 5px;
  border: 1px solid;
  box-sizing: border-box;
}
div.inner {
  border-color:red;
}

.inner-alt {
  position: relative;
  overflow: auto;
}

.inner-alt>div {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
}
<div class="container">
  <div class="inner">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim ipsum orci, </div>
  <div class="inner-alt">
    <div>ut volutpat ligula finibus a. Maecenas ut pharetra ante. Nunc volutpat est eu odio vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>
  </div>
  <div class="inner-alt">
    <div>eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>
  </div>
</div>
<div class="container">
  <div class="inner">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim ipsum orci, io vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate</div>
  <div class="inner-alt">
    <div>ut volutpat ligula finibus a. Maecenas ut pharetra ante. Nunc volutpat est eu odio vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>
  </div>
  <div class="inner-alt">
    <div>eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>
  </div>
</div>
<div class="container">
  <div class="inner">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dignissim ipsum orci, io vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate , eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate</div>
  <div class="inner-alt">
    <div>ut volutpat ligula finibus a. Maecenas ut pharetra ante. Nunc volutpat est eu odio vestibulum, eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>
  </div>
  <div class="inner-alt">
    <div>eget bibendum risus commodo. Nullam tristique nibh sed iaculis vulputate. Vivamus ac tincidu</div>
  </div>
</div>



回答2:

different ways. two examples:

.outer_div {
  position: relative;
  overflow: hidden;
  max-width: 300px;
  margin: 0 auto;
}
.inner_div {
  width: 33.33%;
}
.inner_div_1 {
  float: left;
  background: khaki;
}
.inner_div_2,
.inner_div_3 {
  background: darkkhaki;
  position: absolute;
  top: 0;
  height: 100%;
  overflow-y: auto;
}
.inner_div_2 {
  left: 33.33%;
}
.inner_div_3 {
  left: 66.66%;
}
<div class="outer_div">
  <div class="inner_div inner_div_1">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </div>
  <div class="inner_div inner_div_2">
    Lorem ipsum.
  </div>
  <div class="inner_div inner_div_3">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
</div>

.outer_div {
  display: flex;
  flex-direction: row;
  max-width: 300px;
  margin: 0 auto;
  background : gray;
}
.inner_div {
  width: 33.33%;
}
.inner_div > div {
  position: relative;
  height: 100%;
}
.inner_div_1 span {
  background: khaki;
}
.inner_div_2 span,
.inner_div_3 span {
  background: darkkhaki;
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
}
<div class="outer_div">
  <div class="inner_div inner_div_1">
    <span>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </span>
  </div>
  <div class="inner_div inner_div_2">
    <div>
      <span>
        Lorem ipsum.
      </span>
    </div>
  </div>
  <div class="inner_div inner_div_3">
    <div>
      <span>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
      </span>
    </div>
  </div>
</div>