First div should fill up remaining height that's left while second div should be positioned at the bottom with it's initial height.
DEMO:
.container {
width: 240px;
height: 400px;
background: #E0E0E0;
display: flex;
flex-direction: column;
}
.first {
border :1px solid black;
padding: 1em;
box-sizing: border-box;
}
.second {
border: 1px solid blue;
padding: 1em;
box-sizing: border-box;
}
<div class="container">
<div class="first">
I SHOULD FILL WHATS REMAINING AFTER SECOND ONE
</div>
<div class="second">
<div>
I SHOULD BE AT THE BOTTOM FILLING ONLY MY OWN HEIGHT
</div>
</div>
The answer to this would vary from markup to markup, but in your case you can just add this to your first element:
height: 100%;
This works because of your flex display property of the container. A different property on the container would likely require another solution.
Demo
Full code
.container {
width: 240px;
height: 400px;
background: #E0E0E0;
display: flex;
flex-direction: column;
}
.first {
height: 100%;
border :1px solid black;
padding: 1em;
box-sizing: border-box;
}
.second {
border: 1px solid blue;
padding: 1em;
box-sizing: border-box;
}
<div class="container">
<div class="first">
I SHOULD FILL WHATS REMAINING AFTER SECOND ONE
</div>
<div class="second">
<div>
I SHOULD BE AT THE BOTTOM FILLING ONLY MY OWN HEIGHT
</div>
</div>
You need to make height auto to container class so depend on length of string your height is increase.
<style>
.container {
width: 240px;
height: auto;
background: #E0E0E0;
display: flex;
flex-direction: column;
}
.first {
border :1px solid black;
padding: 1em;
box-sizing: border-box;
}
.second {
border: 1px solid blue;
padding: 1em;
box-sizing: border-box;
}
</style>
<div class="container">
<div class="first">
I SHOULD FILL WHATS REMAINING AFTER SECOND ONE
</div>
<div class="second">
<div>
I SHOULD BE AT THE BOTTOM FILLING ONLY MY OWN HEIGHT
</div>
</div>
Related
I want .board element to have a square aspect ratio. I want to show two of them side by side, together covering the width of their parent.
I don't want to use width: 50%, because I want to position .wrap element with display: flex.
.board {
position: relative;
background: red;
width: 100%;
height: 0;
padding-bottom: 100%;
border: 1px solid black;
}
.wrap {
display: flex;
}
<div class="wrap">
<div class="board"></div>
<div class="board"></div>
</div>
When I do it like this, I get two divs with squashed width.
Use aspect-ratio and flex:1
.board {
position: relative;
background: red;
flex:1;
aspect-ratio:1/1;
border: 1px solid black;
}
.wrap {
display: flex;
}
<div class="wrap">
<div class="board"></div>
<div class="board"></div>
</div>
Alternatively flex:1 and padding-bottom:50%;
.board {
position: relative;
background: red;
flex: 1;
padding-bottom: 50%;
border: 1px solid black;
}
.wrap {
display: flex;
}
<div class="wrap">
<div class="board"></div>
<div class="board"></div>
</div>
This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Fill remaining vertical space with CSS using display:flex
(6 answers)
Closed 3 years ago.
I have a <div> that takes up 60% of the window space, and it contains two things:
a narrow header line
an image that I want to take up the remainder of the div.
How can I do this with pure CSS (no Javascript)? I've been trying a bunch of things, no luck.
This is the closest I can get; the image sneaks outside of the green border of the div.container
html, body {
height: 100%;
overflow: hidden;
margin: 0px;
}
div.container {
height: 60%;
border: 2px solid green;
box-sizing: border-box;
}
div.rest {
height: 40%;
border: 2px solid red;
box-sizing: border-box;
}
div.img-container {
height: 100%; /* this is wrong, but what do I do? */
}
div.img-container img {
max-height: 100%;
max-width: 100%;
height: auto;
width: auto;
opacity: 0.5;
}
<html>
<body>
<div class="container">
<div class="header">hieronymus bosch last judgement</div>
<div class="img-container"><img src="https://i.imgur.com/TT6drhn.jpg"></div>
</div>
<div class="rest">
<h1>blah blah blah</h1>
</div>
</body>
</html>
Here's my attempt at using flex but that fails.
html, body {
height: 100%;
overflow: hidden;
margin: 0px;
}
div.container {
height: 60%;
border: 2px solid green;
box-sizing: border-box;
display: flex;
flex-direction: column
}
div.rest {
height: 40%;
border: 2px solid red;
box-sizing: border-box;
}
div.img-container {
flex: 1;
}
div.header {
flex: 0;
}
div.img-container img {
max-height: 100%;
max-width: 100%;
height: auto;
width: auto;
opacity: 0.5;
}
<html>
<body>
<div class="container">
<div class="header">hieronymus bosch last judgement</div>
<div class="img-container"><img src="https://i.imgur.com/TT6drhn.jpg"></div>
</div>
<div class="rest">
<h1>blah blah blah</h1>
</div>
</body>
</html>
#Christian's approach works if you know the height of the header element, however alternatively you could use flex.
This allows the element to grow to fill the remaining space dynamically, so your header can be any height.
html,
body {
height: 100%;
overflow: hidden;
margin: 0px;
}
div.container {
height: 60%;
border: 2px solid green;
box-sizing: border-box;
display: flex;
flex-direction: column;
}
div.rest {
height: 40%;
border: 2px solid red;
box-sizing: border-box;
}
div.img-container {
flex: 1;
position: relative;
}
div.img-container img {
opacity: 0.5;
height: 100%;
position: absolute;
}
<html>
<body>
<div class="container">
<div class="header">hieronymus bosch last judgement</div>
<div class="img-container"><img src="https://i.imgur.com/TT6drhn.jpg"></div>
</div>
<div class="rest">
<h1>blah blah blah</h1>
</div>
</body>
</html>
If you look at div.img-container in Chrome Inspector, you can see what the issue is - the img element is doing its job and filling its container, but the container itself is overflowing.
This is happening because it is set to height: 100% - what this says is "make my height 100% of my parent's height", but this does not mean "fill the remaining space." The browser just reads the computed height of the element's parent, and then multiplies it by your % value - basically, it's all in absolute terms. You can see that the blue box is 100% as tall as the box outlined in green, but because it sits below a line of text, it overflows by the height of that text.
flex could be used to solve this problem, but you can patch this pretty quickly by using calc to subtract out the height of that text. In your example, it's 19px, and I would recommend manually setting the height of that text element container just to be sure nothing will break in edge cases. Then, the .img-container gets height: calc(100% - 19px) and it works as expected.
html, body {
height: 100%;
overflow: hidden;
margin: 0px;
}
div.container {
height: 60%;
border: 2px solid green;
box-sizing: border-box;
}
div.rest {
height: 40%;
border: 2px solid red;
box-sizing: border-box;
}
div.img-container {
height: 100%; /* this is wrong, but what do I do? */
}
div.img-container img {
max-height: 100%;
max-width: 100%;
height: auto;
width: auto;
opacity: 0.5;
}
/*
ADDED CODE BELOW
*/
/* optional, just to be safe */
.header {
height: 19px;
}
/* overrides .img-container from above */
.img-container {
height: calc(100% - 19px) !important;
}
<html>
<body>
<div class="container">
<div class="header">hieronymus bosch last judgement</div>
<div class="img-container"><img src="https://i.imgur.com/TT6drhn.jpg"></div>
</div>
<div class="rest">
<h1>blah blah blah</h1>
</div>
</body>
</html>
With Flex, you may use the flex property and overflow (or min-height). Example:
html, body {
height: 100vh;
margin: 0px;
display:flex;
flex-direction:column;
}
div.container {
flex:6;/* instead height:xx% */
border: 2px solid green;
box-sizing: border-box;
display: flex;
flex-direction: column;
overflow:hidden; /* or min-height:0 if scroll is needed */
}
div.rest {
flex:4;/* instead height:xx% */
border: 2px solid red;
box-sizing: border-box;
}
div.img-container {
flex: 1;
min-height:0; /* or overflow:hidden; */
}
div.header {
min-height:1.6em; /* if you need something alike ?? */
}
div.img-container img {
max-height: 100%;
opacity:0.5;
}
<html>
<body>
<div class="container">
<div class="header">hieronymus bosch last judgement</div>
<div class="img-container"><img src="https://i.imgur.com/TT6drhn.jpg"></div>
</div>
<div class="rest">
<h1>blah blah blah</h1>
</div>
</body>
</html>
I have 3 divs which are horizontally aligned (aqua color). Inside each div, there are two divs (red and black one).
What I am trying to do is, align the black divs horizontally regardless of the red div. The css for the black div is
.black-div {
width: 100%;
height: 45px;
max-width: 235px;
display: inline-block;
color: #33244a;
font-size: 16px;
text-transform: uppercase;
font-weight: normal;
text-align: center;
line-height: 43px;
border: 2px dashed #d5d1d8;
border-radius: 6px;
box-sizing: border-box;
}
Output will something like this
I am not good at all in css. I have tried using position: fixed / absolute but no luck.
Try it.
Use div and min-height.
section{
display: inline-block;
border: 1px solid red;
width: 100px;
}
.textarea-wrap{
overflow: hidden;
min-height: 200px;
}
.textarea-wrap > textarea{
width: 100%;
resize: none;
}
.red{
background-color: red;
}
<div>
<section>
<div class="textarea-wrap">
<textarea rows="3">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
<section>
<div class="textarea-wrap">
<textarea rows="10">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
<section>
<div class="textarea-wrap">
<textarea rows="6">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
</div>
You should use table to make it more manageable, or use absolute positioning on the black div so you can position them measure from the bottom of the blue div.
There may be a solution without the spacer. Im looking for it :)
found solution without spacer justify-content: space-between;
.wrapper {
display: flex;
flex-direction: row; /* flex in a row inside (make columns .col) */
}
.col {
display: flex;
flex-direction: column; /* flex in a column inside */
justify-content: space-between; /* since the elements must not grow, fill the space between them */
flex: 1 1 100px; /* grow and shrink of col allowed to fill row evenly starting at 100px*/
margin: 5px;
border: 3px solid black;
background-color: aqua;
}
.red {
flex: 0 1 auto; /* no vertical (col) growing (so it does not expand vertically) */
border: 3px solid black;
border-radius: 10px;
background-color: red;
margin: 5px;
padding: 10px;
}
.black {
background-color: black;
color: white;
margin: 5px;
padding: 10px;
display: block;
flex: 0 1 auto; /* no growing allowed */
}
.resize {
overflow: hidden;
resize: vertical;
}
<div class='wrapper'>
<div class='col'>
<div class='red'>Some wide wide wide wide wide wide Text</div>
<div class='black'>Footer</div>
</div>
<div class='col'>
<div class='red'>Some<br/>much<br/>longer<br/>Text</div>
<div class='black'>Footer</div>
</div>
<div class='col'>
<div class='red resize'>Some Text<br><b><u>Resize me!</u></b></div>
<div class='black'>Footer</div>
</div>
</div>
Edit removed spacer div
Edit2 added css commenting for easier understanding
I have a stack of div boxes as follows:
My goal is: Box B and C should be of the same width, and box A should be the width of B and C. The content of each box should be shown without line break. They should be arranged as in the picture. The content of box A, B and C is created dynamically.
I almost achieved this (see code at bottom) by putting these 3 boxes in an display:inline-block and setting width:50% for box B and C. This works good in some cases, but when the content of box B is too large, I get this result:
I understand the outcome, since the total width is determined by box A and box B has width: 50%. However, I would like that box B gets stretched so that no line break is needed, like this:
How can I achieve this?
This is the code that produces the image (see this jsFiddle):
<style>
.container{
display: inline-block;
text-align:center;
padding: 5px;
border:dashed red;
}
.head{
border:solid;
background-color: yellow;
padding: 30px;
}
.item{
width: 50%;
display: inline-block;
border:solid;
box-sizing: border-box;
padding: 5px;
}
.red{
background-color: red;
}
.green{
background-color: green;
}
}
</style>
<div class="container">
<div class="head">
A
</div>
<div>
<div class="item red">
B
</div><!--
--><div class="item green">
C
</div>
</div>
</div>
A minor change on structure and flexbox can do that.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.container {
display: inline-flex;
text-align: center;
padding: 5px;
border: dashed red;
flex-wrap: wrap
}
.head {
border: solid;
background-color: yellow;
flex: 0 0 100%;
padding: 30px;
}
.item {
width: 50%;
display: inline-block;
border: solid;
box-sizing: border-box;
padding: 5px;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
<div class="container">
<div class="head">
Very very long text
</div>
<div class="item red">
Also very very long text
</div>
<div class="item green">
C
</div>
</div>
Or...if you want the bottom divs wrapped...still flexbox
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.container {
display: inline-flex;
text-align: center;
padding: 5px;
border: dashed red;
flex-wrap: wrap
}
.head {
border: solid;
background-color: yellow;
flex: 0 0 100%;
padding: 30px;
}
.wrap {
flex: 0 0 100%;
display: flex;
}
.item {
width: 50%;
display: inline-block;
border: solid;
box-sizing: border-box;
padding: 5px;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
}
<div class="container">
<div class="head">
Very very long text
</div>
<div class="wrap">
<div class="item red">
Also very very long text
</div>
<div class="item green">
C
</div>
</div>
</div>
I am trying to make a full height page using flexboxes, where the content also uses a flexbox. The page should look as follows example of what it should look like. The blue div is dynamic and could change in height and the red content should take up the remaining space of the content div. This works on both Firefox and IE, however on Chrome it overflows. Can somebody explain why it overflows on Chrome?
The HTML is as follows:
<body>
<div class="container">
<div class="navbar">Navbar</div>
<div class="content">
<div class="container">
<div class="fill"></div>
<div class="dynamic">Here is some dynamic content<br>Test</div>
</div>
</div>
</div>
</body>
And the CSS is:
body{
margin:0;
}
.container{
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.navbar{
background-color: #ccc;
flex: none;
}
.content{
background-color: #333;
flex: auto;
padding: 10px;
}
.dynamic{
background-color: #0066ff;
flex: none;
}
.fill{
flex: auto;
background-color: #ff0000;
}
Here is an updated snippet.
Use flex:1 for the container that needs to adjust the height automatically.
body {
margin: 0;
}
.container1 {
display: flex;
flex-direction: column;
width: 100%;
height: 100vh;
}
.navbar {
background-color: #ccc;
}
.content {
flex: 1;
background-color: #ff0000;
border: 10px solid #333;
border-bottom: none;
}
.dynamic {
background-color: #0066ff;
border: 10px solid #333;
border-top: none;
}
<body>
<div class="container1">
<div class="navbar">Navbar</div>
<div class="content">
</div>
<div class="dynamic">Here is some dynamic content
<br>Test</div>
</div>
</body>