CSS 获取行内块元素的高度以填充父元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16404485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:46:45  来源:igfitidea点击:

Getting inline-block element's height to fill the parent

css

提问by Brian Genisio

I have a container with two items. One of those items is a selectelement, so I need to set the sizeattribute via HTML. I want the other item in the container to stretch its height to fit the container. I can't figure it out. I don't want to explicitly set the height of the container because I don't know the size of that select box.

我有一个装有两件​​物品的容器。其中一项是select元素,因此我需要size通过 HTML设置该属性。我希望容器中的另一个项目拉伸其高度以适合容器。我想不通。我不想明确设置容器的高度,因为我不知道该选择框的大小。

.container {
  padding: 5px;
  border: 1px solid black;
}

.container .column {
  display: inline-block;
  width: 40%;
  background-color: #AAA;
  padding: 5px;
  margin: 5px;
  vertical-align: top;
  height: 100%;
}

select {
  width: 100%;
}
<div class="container">
  <div class="column">Stretch to fill?</div>
  <div class="column">
    <select size="15">
            <option>Option 1</option>
            <option>Option 2</option>
        </select>
  </div>
  <div>

采纳答案by Antony

If table-cellis an option, here's a way to do it:

如果table-cell是一个选项,这是一种方法:

.container {
  display: table;
  width: 100%;
  padding: 5px;
  border: 1px solid black;
}

.container .column {
  display: table-cell;
  width: 40%;
  background-color: #AAA;
  padding: 5px;
  border: 5px solid white;
  vertical-align: top;
  height: 100%;
}

select {
  width: 100%;
}
<div class="container">
  <div class="column">Stretch to fill?</div>
  <div class="column">
    <select size="15">
            <option>Option 1</option>
            <option>Option 2</option>
        </select>
  </div>
  <div>

回答by Gaston Sanchez

If I understand what you are saying, you are facing the 100% height columnsproblem. I'm sorry to tell you there is no actual solution but "hacks".

如果我明白你在说什么,你就面临着100% 高度列的问题。我很遗憾地告诉你,除了“黑客”之外,没有实际的解决方案。

Hereyou can find several of those workarounds. I like to use the one true layout method.

在这里,您可以找到其中几种解决方法。我喜欢使用一种真正的布局方法。

By the way, this is thinking you don't want to use the experimental css3 columns properties.

顺便说一句,这是认为您不想使用实验性的 css3 列属性。

回答by Ederico Rocha

No answers here gave me comfort so I went and search for the truth. I added a bit more css to make a point on the spacing between two boxes.

这里没有答案给我安慰,所以我去寻找真相。我添加了更多的 css 来说明两个框之间的间距。

CSS:

CSS:

 .wrapper {
  background-color:gray;
}

.container {
    margin: 25px auto;
    display: inline-flex;
}

.leftbox {
    height: inherit;
    display: inline-block;
    border: 1px solid #D7D2CB;
    background-color: #FFFFFF;
    border-radius: 5px;
    max-width: 550px;
    margin-right: 18px;
    align-items: stretch;
    padding: 15px;
    width: 100%;
}

.rightbox {
    height: 100%;
    display: inline-block;
    border: 1px solid #D7D2CB;
    background-color: #FFFFFF;
    border-radius: 5px;
    align-items: stretch;
    max-width: 300px;
    width: 100%;
    padding: 20px 15px;
}

HTML:

HTML:

<div class="wrapper">
  <div class="container">
    <div class="leftbox">
      There is something here, I am not avoiding it.
    </div>
    <div class="rightbox">
      Probably something else here but much much much much much much much much much much much much much much much much much much much much much much much much much much much much much bigger.
    </div>
  </div>
</div>

Check the codepen: https://codepen.io/anon/pen/XRNMMp

检查代码笔:https://codepen.io/anon/pen/XRNMMp

回答by PK_info

An example where I needed a pseudo element also:

我还需要伪元素的示例:

.cf:before,
.cf:after {
 content: " "; /* 1 */
 display: table; /* 2 */
}

.cf:after {
 clear: both;
}


*, *:before, *:after {
 -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}

.faux-columns {
 width: 80%;
 margin:3em auto;
 background-image: -webkit-linear-gradient(left, #f2f7fa 25%, #fff 75%);
 background-image: -moz-linear-gradient(left, #f2f7fa 25%, #fff 75%);
 background-image: -o-linear-gradient(left, #f2f7fa 25%, #fff 75%);
 background-image: linear-gradient(left, #f2f7fa 25%, #fff 75%);
 background: -webkit-gradient(linear, left top, right top, color-stop(0%,#f2f7fa), color-stop(25%, #f2f7fa), color-stop(25%,#ffffff), color-stop(100%,#ffffff));
 background: -moz-linear-gradient(left, #f2f7fa 0%, #f2f7fa 25%, #ffffff 25%, #ffffff 100%);
 background: -webkit-gradient(linear, left top, right top, color-stop(0%,#f2f7fa), color-stop(25%,#f2f7fa), color-stop(25%,#ffffff), color-stop(100%,#ffffff));
 background: -o-linear-gradient(left, #f2f7fa 0%,#f2f7fa 25%,#ffffff 25%,#ffffff 100%);
 background: -ms-linear-gradient(left, #f2f7fa 0%,#f2f7fa 25%,#ffffff 25%,#ffffff 100%);
 background: linear-gradient(left, #f2f7fa 0%,#f2f7fa 25%,#ffffff 25%,#ffffff 100%);
 border:1px solid #c1c1c2; 
}

.col-1, .col-2 {
 float:left; 
 vertical-align:top; 
 padding:2em 3em;
}

.col-1 {
 position:relative;
 width:25%; 
 background:#F2F7FC;
 border-right:1px solid #c1c1c2;
}

.col-2 { 
 width:75%;
 border-left:1px solid #c1c1c2;
 margin-left: -1px;
}

.col-1:after,
.col-1:before {
   top:100%; 
   border:solid transparent;content:"";
   height:0;
   width:0;
   position:absolute;
   pointer-events:none; 
   display:block;
}
.col-1:after  {
  border-color: rgba(255, 255, 255, 0) rgba(255, 255, 255, 0) rgba(255, 255, 255, 0) #f2f7fa; 
  border-style: solid; 
  border-width: 21px 0 21px 22px; 
  left:100%; 
  top: 47px; 
}
.col-1:before {
  border-color: rgba(0, 0, 0, 0) rgba(0, 0, 0, 0) rgba(0, 0, 0, 0) #c1c1c2;
  border-style: solid; 
  border-width: 22px 0 22px 23px; 
  left: 100%; 
  top: 46px;
}
<div class="faux-columns cf">
    <div class="col-1">
        <h4>First column with bordered triangle pseudo element attached to it. The background needs to be the full height of .faux-columns</h4>
    </div>
    <div class="col-2">
        <h4> Second column which in some cases will be much taller than column</h4>
        There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
        
        There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
    </div>
</div>