div (높이)가 부모 나머지 높이를 차지하도록합니다.
div
두 아이 가있는 용기 가 있습니다. 첫 번째 아이는 주어진 키를 가지고 있습니다. 두 번째 아이가 div
특정 높이를주지 않고 컨테이너의 "여유 공간"을 차지하게하려면 어떻게 해야합니까?
이 예에서 분홍색 div
은 공백도 차지해야합니다.
이 질문과 비슷합니다. div가 나머지 높이를 차지하도록 만드는 방법은 무엇입니까?
하지만 절대 위치 를주고 싶지 않습니다 .
#down
남은 공간을 채우기 위해 자식을 확장하는 #container
것은 달성하려는 브라우저 지원 및 #up
정의 된 높이가 있는지 여부에 따라 다양한 방법으로 수행 될 수 있습니다 .
샘플
.container {
width: 100px;
height: 300px;
border: 1px solid red;
float: left;
}
.up {
background: green;
}
.down {
background: pink;
}
.grid.container {
display: grid;
grid-template-rows: 100px;
}
.flexbox.container {
display: flex;
flex-direction: column;
}
.flexbox.container .down {
flex-grow: 1;
}
.calc .up {
height: 100px;
}
.calc .down {
height: calc(100% - 100px);
}
.overflow.container {
overflow: hidden;
}
.overflow .down {
height: 100%;
}
<div class="grid container">
<div class="up">grid
<br />grid
<br />grid
<br />
</div>
<div class="down">grid
<br />grid
<br />grid
<br />
</div>
</div>
<div class="flexbox container">
<div class="up">flexbox
<br />flexbox
<br />flexbox
<br />
</div>
<div class="down">flexbox
<br />flexbox
<br />flexbox
<br />
</div>
</div>
<div class="calc container">
<div class="up">calc
<br />calc
<br />calc
<br />
</div>
<div class="down">calc
<br />calc
<br />calc
<br />
</div>
</div>
<div class="overflow container">
<div class="up">overflow
<br />overflow
<br />overflow
<br />
</div>
<div class="down">overflow
<br />overflow
<br />overflow
<br />
</div>
</div>
그리드
CSS's grid
layout offers yet another option, though it may not be as straightforward as the Flexbox model. However, it only requires styling the container element:
.container { display: grid; grid-template-rows: 100px }
The grid-template-rows
defines the first row as a fixed 100px height, and the remain rows will automatically stretch to fill the remaining space.
I'm pretty sure IE11 requires -ms-
prefixes, so make sure to validate the functionality in the browsers you wish to support.
Flexbox
CSS3's Flexible Box Layout Module (flexbox
) is now well-supported and can be very easy to implement. Because it is flexible, it even works when #up
does not have a defined height.
#container { display: flex; flex-direction: column; }
#down { flex-grow: 1; }
It's important to note that IE10 & IE11 support for some flexbox properties can be buggy, and IE9 or below has no support at all.
Calculated Height
Another easy solution is to use the CSS3 calc
functional unit, as Alvaro points out in his answer, but it requires the height of the first child to be a known value:
#up { height: 100px; }
#down { height: calc( 100% - 100px ); }
It is pretty widely supported, with the only notable exceptions being <= IE8 or Safari 5 (no support) and IE9 (partial support). Some other issues include using calc in conjunction with transform or box-shadow, so be sure to test in multiple browsers if that is of concern to you.
Other Alternatives
If older support is needed, you could add height:100%;
to #down
will make the pink div full height, with one caveat. It will cause overflow for the container, because #up
is pushing it down.
Therefore, you could add overflow: hidden;
to the container to fix that.
Alternatively, if the height of #up
is fixed, you could position it absolutely within the container, and add a padding-top to #down
.
And, yet another option would be to use a table display:
#container { width: 300px; height: 300px; border: 1px solid red; display: table;}
#up { background: green; display: table-row; height: 0; }
#down { background: pink; display: table-row;}
Its been almost two years since I asked this question. I just came up with css calc() that resolves this issue I had and thought it would be nice to add it in case someone has the same problem. (By the way I ended up using position absolute).
http://jsfiddle.net/S8g4E/955/
Here is the css
#up { height:80px;}
#down {
height: calc(100% - 80px);//The upper div needs to have a fixed height, 80px in this case.
}
And more information about it here: http://css-tricks.com/a-couple-of-use-cases-for-calc/
Browser support: http://caniuse.com/#feat=calc
My answer uses only CSS, and it does not use overflow:hidden or display:table-row. It requires that the first child really does have a given height, but in your question you state that only the second child need have its height not specified, so I believe you should find this acceptable.
html
<div id="container">
<div id="up">Text<br />Text<br />Text<br /></div>
<div id="down">Text<br />Text<br />Text<br /></div>
</div>
css
#container { width: 300px; height: 300px; border:1px solid red;}
#up { background: green; height: 63px; float:left; width: 100% }
#down { background:pink; padding-top: 63px; height: 100%; box-sizing: border-box; }
http://jsfiddle.net/S8g4E/288/
check the demo - http://jsfiddle.net/S8g4E/6/
use css -
#container { width: 300px; height: 300px; border:1px solid red; display: table;}
#up { background: green; display: table-row; }
#down { background:pink; display: table-row;}
Unless I am misunderstanding, you can just add height: 100%;
and overflow:hidden;
to #down
.
#down {
background:pink;
height:100%;
overflow:hidden;
}
Edit: Since you do not want to use overflow:hidden;
, you can use display: table;
for this scenario; however, it is not supported prior to IE 8. (display: table;
support)
#container {
width: 300px;
height: 300px;
border:1px solid red;
display:table;
}
#up {
background: green;
display:table-row;
height:0;
}
#down {
background:pink;
display:table-row;
}
Note: You have said that you want the #down
height to be #container
height minus #up
height. The display:table;
solution does exactly that and this jsfiddle will portray that pretty clearly.
You can use floats for pushing content down:
You have a fixed size container:
#container {
width: 300px; height: 300px;
}
Content is allowed to flow next to a float. Unless we set the float to full width:
#up {
float: left;
width: 100%;
}
While #up
and #down
share the top position, #down
's content can only start after the bottom of the floated #up
:
#down {
height:100%;
}
I'm not sure it can be done purely with CSS, unless you're comfortable in sort of faking it with illusions. Maybe use Josh Mein's answer, and set #container
to overflow:hidden
.
For what it's worth, here's a jQuery solution:
var contH = $('#container').height(),
upH = $('#up').height();
$('#down').css('height' , contH - upH);
참고URL : https://stackoverflow.com/questions/11225912/make-div-height-occupy-parent-remaining-height
'IT story' 카테고리의 다른 글
`에서 스크롤 비활성화 (0) | 2020.08.23 |
---|---|
PostgreSQL 데이터베이스 테이블에서 열의 위치를 어떻게 변경합니까? (0) | 2020.08.23 |
Emacs : 명령에 대한 키 바인딩 인쇄 또는 모든 키 바인딩 나열 (0) | 2020.08.23 |
테이블 대신 뷰를 사용하는 경우 (0) | 2020.08.23 |
동일한 SELECT 문에서 DISTINCT 및 ORDER BY를 사용하는 방법은 무엇입니까? (0) | 2020.08.23 |