Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS CSS Height by percentage

  • This topic is empty.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #156503
    intodesign
    Participant

    Hey, I have body of 100% and 3 divs inside, let’s say i would like that div #1 will be 10%, div#2 30%, div#3 60%.
    I can’t figure how to control the div’s height ?

    #156504
    janet4now
    Participant

    the height will be determined by the content inside the div, or you can specify a height or min/max-height. (Not sure why you want to, though)

    What are you trying to do specifically? Do you have an image of the layout or a site in progress you can share?

    #156505
    intodesign
    Participant

    I’m trying to give it height by percentage, because if i’ll watch it on a TV it make no sense that the div will be in pixels

    code for example
    http://codepen.io/anon/pen/obyuI

    can you help please?

    #156507
    janet4now
    Participant
    body, html { width: 100%; height: 100%; }
    
    
    .div1 { background: blue; height:10vh; } .div2 { background: red; height:30vh; } .div3 { background: green; height:60vh; }
    

    Use “vh” which is “viewport height” rather than % or pixels.

    #156508
    Alen
    Participant

    I’m trying to give it height by percentage, because if i’ll watch it on a TV it make no sense that the div will be in pixels

    What do you think 720p and 1080p refer to?

    Pixels.

    #156536
    wolfcry911
    Participant

    @intodesign, you can use percentages. The only thing your missing is setting html to height: 100% as well as the body. And I’d remove all the width statements – the block level elements will take up all available width by default. You may want to set the body margin and padding to 0.

    #156538
    Merri
    Participant

    In short:

    html,body { height: 100%; margin: 0; }
    .div1 { background: blue; height: 10%; }
    .div2 { background: red; height: 30%; }
    .div3 { background: green; height: 60%; }
    

    http://codepen.io/Merri/pen/drkjc

    #156549
    paulob
    Participant

    Setting a height on elements is a bad move if they are going to contain fluid elements like text because the content will spill out if the text is resized or if there is simply not enough room (e.g. when the browsers window is closed smaller).

    I think you need to explain a little more about what you are trying to achieve as I can’t see much use for the elements to be spread across the viewport like that unless its just a simple page with a few headings or similar.

    You always need to try out your demos with “real content” so that you can see what will happen when there is too much (or too little) content in your elements.:)

    You could use Merri’s code above but use display:table (IE8+) so that the element can still grow if required.

    .div1,.div2,.div3 {display:table;width:100%}
    
    #156553
    Merri
    Participant

    Tables require a lot more CSS syntax: http://codepen.io/Merri/pen/vDius

    Instead I’d just replace div heights with min-height in my example earlier above to get similar results with less syntax. I updated the CodePen snippet to min-height (drkjc).

    Margins may give some additional headache when not using tables, but those issues are usually easily resolved (overflow: hidden; or giving some top and bottom padding to the divs).

    Of course we still don’t really know the context of this.

    #156560
    paulob
    Participant

    Tables require a lot more CSS syntax

    They only need the one line I posted in my last post but min-height would also work as you suggest as long as you don’t nest the min-height element or you will lose the height.

    However, both yours and my suggestions are flawed because should you add more content into one of the areas then the whole thing stretches past the viewport bottom edge when there is no need for it to do so.

    A better approach (assuming this was the OPs idea) is to use a complete CSS table structure and allow areas to grow while not forcing the content below the fold when its not required. It’s more mark up but ultimately more reliable.

    e.g.
    http://www.codepen.io/paulobrien/full/JjtnH

    Of course that assumes that I have understood the requirements properly – which may not be the case :)

    #156562
    Merri
    Participant

    You don’t need that much syntax to get that end result: http://codepen.io/Merri/pen/CcLlJ

    AFAIK table-cell element is automatically created if it’s omitted. Same for other missing table elements. Quite handy. Although I haven’t verified whether all browsers do follow this properly, because I don’t usually use table based layouts.

    #156565
    paulob
    Participant

    AFAIK table-cell element is automatically created

    Yes, that’s correct and that should work nicely in modern browsers and your latest example is now pretty concise. :)

    Firefox used to have an issue if you didn’t explicitly create a table-row and a table-cell but that’s long since been fixed but I keep forgetting to omit them.

    #156778
    intodesign
    Participant

    Thanks so much.. that was very helpful, Great code.

Viewing 13 posts - 1 through 13 (of 13 total)
  • The forum ‘CSS’ is closed to new topics and replies.