Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical/Height scrollbar appears when adding margin-top

Tags:

html

css

My HTML:

div.container

My CSS:

html, body {
  height: 100%;
  background-color: red;
  padding-left: 0px;
  padding-right: 0px;
}    
.container {
  height: 100%;
  background-color: blue;
  margin-top: 5px;
  *zoom: 1;
}

I want to use height=100% and DO NOT want to use overflow=hidden.

Which CSS properties I should use above so that I can have EFFECT OF margin-top for container div above which will not create vertical scroll-bar.

Just Imagine:

* Body with color red
* A div container in body and I want to see the margin of "x" px on top bewteen body and conatiner
* Color of div container blue
* No scrollbars and No overflow="hidden"

Please help

How can I accomplish this? Please help

like image 813
gaurav jain Avatar asked May 14 '26 18:05

gaurav jain


2 Answers

Try this CSS

*{
    margin:0;
    padding:0;
}
html, body {
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    box-sizing:border-box;
    height: 100%;
    background-color: red;
    padding-top:5px;
}    
.container {
    height: 100%;
    background-color: blue;
    *zoom: 1;
}

First reset all margin and padding.
box-sizing:border-box; means the size of an element will stay the same no matter how much padding you add to it.

You could also use an absolute positioned div for the red bar.

HTML

<div class="red-bar"></div>
<div class="content"></div>  

CSS

.red-bar{ position:absolute; width:100%; height:5px; top:0; background:red; }
.content{ height:100%; background:blue; }
like image 166
barro32 Avatar answered May 17 '26 09:05

barro32


Another solution is in use padding-top insted of margin-top

like image 39
Eugeny Avatar answered May 17 '26 09:05

Eugeny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!