0

I made this code that makes a responsive height, it adjusts according to the size of the viewport. (Run the snippet and resize the screen).

Whereas the html and body have a height: 100%, I set up a basic structure with 3 divs and I was handing this height: 100% between them (as you can see in the snippet). After that, I gave a position: absolute and top according to the size of each.

Well, as I assign attributes the top for each div in "hand", I got the feeling that this may be a quick fix/MacGyver on it, because as in the later, there are more divs, I have to do this calculation for top again. I think that have better ways to do this...

Thus, in what other ways I can do this? The code that I did can be considered a quick fix/MacGyver?

html, body{
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}

.div1{
    position: absolute;
    width: 100%;
    height: 10%;
    background: red;
}

.div2{
    position: absolute;
    top: 10%;
    width: 100%;
    height: 75%;
    background: green;
}

.div3{
   position: absolute;
   top: 85%;
   width: 100%;
   height: 15%;
   background: yellow;
}
<div class="principal">
    <div class="div1"></div>
    <div class="div2"></div>
    <div class="div3"></div>
</div>
Zkk
  • 741
  • 13
  • 29

2 Answers2

1

The answer to whether what you're doing is a good solution (which I will interpret as concise, not overly complicated, and as compatible as possibe) depends on what exactly you're trying to do. Because you don't provide much detail on that end, let me give you the rundown.

The generally best approach (by the definition above)

In most cases, you won't need any special properties and can simply set the height or min-height of your three containers to the appropriate value, since their parent (body) already has a height of 100%. Because everything is underneath each other, there is no need to use the position property in any way.

Because of the way html works by default, these containers will retain their size until their content will require more space; then they will expand to accomodate the content. This is a feature, not a bug.
If you want to prevent this, set the overflow to hidden or scroll, which will help retaining the original container size (though in case of scrolling, scrollbars might mess with your plans).

Alternative solutions

Sometimes layouters get weird ideas of what you need to put on a webpage, and weird ideas might require weird solutions. Let me try to come up with a list of options to choose from:

  • The approach you took works if you need to split the viewport into exact shares of fix values, disregarding the container's contents entirely. It's compatible with any relevant browser by a long shot, but it will (assuming you handle the overflow so it doesn't stretch the container) likely result in cut-off content on extreme screen sizes (if you have primarily text content) or aspect ratios (if you have primarily image content); to be honest, probably both - but if you're working on a game, for example, maintaining a relative container size can easily be more important than their contents
  • Flexboxes will "only" give you a benefit of stretching the content over the whole screen if you're desperately trying to avoid setting a height, but it shouldn't result in any unforeseen errors, aside from the compatibility issues. As an additional bonus, you can rearrange the containers with the order property, which none of the other methods will accomplish.
  • Using absolute-positioned elements, you can entirely disregard height attributes and just set both top:0 and bottom:0 (while having a relative-positioned parent) to stretch a container over the entire height, then position containers inside on the top and bottom the same way. Not many cases in which this is more useful than the above two come to mind, and you won't like fixing any problems you encounter on the way, but if you're developing for browsers thathave issues with overflow properties, you could look into it.
  • The vh unit, apart from suffering from compatibility issues about the same, can be used, but don't pose any actual benefit over using percentage values. They are used to size elements relative to the viewport dimensions, which your percentage solution does just the same for this specific use case.
  • You could use a table, though that's commonly considered bad practice for various reasons and will on top of that be the most complicated solution of all of these, so I won't go into it.

So, all in all, there are many ways to accomplish what you want (and I possibly even missed some), and without providing info about the exact nature of what you're trying to do, there can be no exact recommendations other than a quick summary of what I wrote above: If you plan on putting content in the top and bottom container and you can't use the topmost solution, flexbox will work the best for you; if you need the containers to take up precisely a certain percentage, go with your original solution; and only if both aren't suitable, expand your search to the other options.

TheThirdMan
  • 1,482
  • 1
  • 14
  • 27
0

Today flex can make this really easy:

examples to run in full page:

html, body {
  height:100%;
  /*
  }
  next can be declared only for body but won't hurt if both html/body
  body {
  */
  display:flex;
  flex-flow:column;
  }
main {flex:1;
}
/* makeup */
header, footer {
  background:tomato;
  padding:1em;
  }
main {
  background:turquoise;
  margin:1em;/* it can even stand away */
  }
<header> header no need to set an height </header>
<main> let's fill remaining space</main>
<footer> footer no need to set an height </footer>

or use many div

html,
body {
  height: 100%;
}
body {
  display: flex;
  flex-flow: column;
}
div {
  flex: 1;
}
/* makeup */

header,
footer {
  background: tomato;
  padding: 1em;
}
div {
  background: turquoise;
  margin: 1em;/* it can even stand away */
  
}
div.autoH {
  flex: none;
  margin: 0.25em 0em;
  background: brown
}
<header>header no need to set an height</header>
<div>let's fill remaining space</div>
<div class="autoH">let's fill only my needed space</div>
<header>or use header's / footer's properties</header>
<div>let's fill remaining space</div>
<footer>footer no need to set an height</footer>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129