Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css height 80% not working

Tags:

css

I want to make my table take up 80% of the screen, but right now its only the size of the content in the table.

#ecom-mainarea .center
{
margin-left: 10%;
position: relative;
width: 80%;
height: 80%;   /* when this is 500px it works fine, but % doesn't work */ 
border: 1px solid;
border-bottom-color: teal;
border-top-color: gray;
border-left-color: gray;
border-right-color: teal;
background-color: white;
voice-family: "\"}\"";
voice-family: inherit;
vertical-align: text-top;
}
like image 695
patrick Avatar asked Jul 16 '11 17:07

patrick


People also ask

Why is height percent not working CSS?

Height % is based on it's parent (so you have to set every element above the target element to 100%) , there are a few workarounds to this though. For instance you can set it to height: 100vh; This will create the element to be 100% of your window height. Or you can use px instead.

Why 100vh is not working?

This is caused by the default margin being 8px so redefining it using CSS will correct it.

Can you use percentage for height in CSS?

The CSS height property applies to block level and replaced elements. When the value is provided as a percentage, it is relative to the height of the containing block.

How do I fix max-height in CSS?

CSS Demo: max-heightThis is a box where you can change the maximum height. This will limit how tall the box can be, potentially causing an overflow. max-height overrides height , but min-height overrides max-height .


2 Answers

You need to make sure that the htmland body elements have 100% height. They need to stretch from top to bottom. If not the html and body element will just be as high as your table.

Here you have a working sample:

<!doctype html>
<html>
<head>
    <title>Table height</title>
    <style>
        html, body
        {
            padding: 0;
            margin: 0;
            height: 100%;
        }
    </style>
</head>
<body>
    <table style="background: cyan; height: 80%;">
        <tr>
            <td>
                Table has 80% of the height of the body
            </td>
        </tr>
    </table>
</body>
</html>
like image 105
knut Avatar answered Sep 22 '22 22:09

knut


Works fine so long as you specify a height of the parent element (in absolute units)

like image 29
Stuart Burrows Avatar answered Sep 26 '22 22:09

Stuart Burrows