Upper and lower fixed heights with medium liquid heights

What I'm going to ask is what I used with javascript, but I want to achieve it only with css (if possible)

Here is the scenario:

I have a div element whose height is h px. This DIV element has 3 children, which are also DIV elements. Their goal is the following:

The first DIV element is k px high and is attached to the top of the parent container. Its height is constant.

The second div element is n px high and is attached to the bottom of the parent container. Its height is constant.

The third element of the DIV h is (n + k) px.

I want when resizing the parent div (which is a floating field) to automatically save the third div element h - (n + k) px.

Is this possible with just css ?

+4
source share
6 answers

Yes use the function calc():

for your 3rd set of div heightCSS Property:

height: calc(h - n - k);
+6
source

If you plan to support browsers that do not supportcalc() , this is an approach without it.

, , ( , ) box-sizing:border-box;:

DEMO

html,body,.wrap {
  height: 100%;
  margin: 0;
  padding: 0;
}
div {
  box-sizing: border-box;
}
.wrap {
  position: relative;
  width: 50%;
  padding: 50px 0 80px;
  border: 5px solid tomato;
}
.one,.two {
  position: absolute;
  left: 0;
  width: 100%;
  border: 5px solid teal;
}
.one {
  height: 50px;
  top: 0;
}
.two {
  height: 80px;
  bottom: 0;
}
.three {
  height: 100%;
  background: gold;
}
<div class="wrap">
  <div class="one"></div>
  <div class="two"></div>
  <div class="three"></div>
</div>
Hide result

:

box-sizing :

DEMO

html, body, .wrap {
    height: 100%;
    margin: 0;
    padding: 0;
}
.wrap {
    position: relative;
    width: 50%;
}
.one, .two, .three {
    position: absolute;
    left: 0;
    width: 100%;
    background: teal;
}
.one {
    height: 50px;
    top: 0;
}
.two {
    height: 80px;
    bottom: 0;
}
.three {
    top: 50px;
    bottom: 80px;
    background: gold;
}
<div class="wrap">
    <div class="one">... content 1 ...</div>
    <div class="two">... content 2 ...</div>
    <div class="three">...content 3 ...</div>
</div>
Hide result
+6

calc() flexboxes. , , - , CSS .

:

$('button').on('click', function() {
    $('.container').height(function(i, v) {
        return v < 200 ? 250 : 150
    });
})
.container {
    float:left;
    height:150px;
    width:300px;
    background-color:#000;
    /* set as a flex container laid out in a column (top to bottom) */
    display:flex;
    flex-direction:column;
}

/* content div, flex auto fills the remaining height */
.container div {
    background-color:#0f0;
    flex:1 1 auto;
}

/* top red div, fixed at 50px */
.container div:first-child {
    flex:0 0 50px;
    background-color:#f00;
}

/* bottom blue div, fixed at 50px */
.container div:last-child {
    flex:0 0 50px;
    background-color:#00f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div class="container">
    <div>Header</div>
    <div>Content</div>
    <div>Footer</div>
</div>
<div><button>Resize Container</button></div>
Hide result
+2
+1

jsFiddle

HTML:

<div id="main">
    <div id="a"></div>
    <div id="b">Hello</div>
    <div id="c"></div>
</div>

CSS

#main{height: 100%;background:red;}
#a{height: 100px;background:blue;}
#c{height: 50px;background:green;}
#b{height: calc(100% - 150px});
+1
source

You can use calc(). Below is a working demo

Remember to copy the parenthesis to the desired location (e.g. calc(200px - (50px + 50px)))

#parent {
    width: 100px;
    height: 200px;
}

#div1 {
    height: 50px;
    background: red;
}

#div2 {
    height: 50px;
    background: blue;
}

#div3 {
    height: calc(200px - (50px + 50px));
    background: green;
}

Also make sure to check browser support

+1
source

All Articles