Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning Div placement

Tags:

html

css

Did a lot of research on all the separate components. However, I don't understand how the components work together. Several placement issues have plagued me on different occasions. I would like to understand why it behaves like it does.

Designing a site with a fixed header, containing some buttons. I want the buttons to be placed on a colored row (NAV). That's why I made a child of NAV. However I can't seem to place the buttons over the bar.

Html

<body>
<nav class="row">
    <ul class="menu">
        <li id="link1"><a href="#link1">Link 1</a></li>
        <li id="link2"><a href="#link2">Link 2</a></li>
        <li id="link3"><a href="#link3">Link 3</a></li>
        <li id="link4"><a href="#link4">Link 4</a></li>
        <li id="link5"><a href="#link5">Link 5</a></li>
    </ul>
</nav>
<div class="row main">
    @RenderBody()
</div>

CSS

nav, div, li {
        -moz-box-sizing: content-box;
        -webkit-box-sizing: content-box;
        box-sizing: content-box;
        border: 1px dashed black;
    }

    .row {
        width: 100%;
        padding-left: 20px;
        padding-right: 20px;
    }

    nav {
        position: fixed;
        top: 80px;
        height: 40px;
        z-index: 100;
        background-color: Green;
        border-bottom: solid greenyellow 2px;
    }

    .menu li {
        display: block;
        background-color: darkgreen;
        float: left;
        height: 40px;
        width: 60px;
    }

    .menu a {
        color: white;
    }

Result enter image description here

It can be fixed by several things, like button margin or placing the buttons relative with a negative Top offset. However, these solutions feel 'dirty', like it's not the right way to do it. Why are the LI's not on top of NAV?

like image 658
RB84 Avatar asked Dec 02 '25 01:12

RB84


2 Answers

because your broswer applies by default some margin to the ul tag

try adding

ul {
    margin: 0;
}

you could avoid these issues by using a css reset (Eric Meyer is the authority here: http://meyerweb.com/eric/tools/css/reset/) or Necolas' Normalize.css: http://necolas.github.io/normalize.css/

the first one zeroes all the values of all elements - you have to rebuild the style of some elements like lists.

The second one normalizes the values of elements to fix browsers inconsistencies

like image 133
Luca Avatar answered Dec 03 '25 18:12

Luca


When you use the "float" property on some elements (here the "LI"), the parent (here the "menu") ignore his floating children to calculate his height.

So you have to specify a valid height to your menu, or probably better, use "overflow:auto" on it to remember him his children.

So remove your

nav {
    height:40px;
}

and add in your CSS :

.menu {
    overflow:auto;
}

As in this fiddle : http://jsfiddle.net/bE3QH/

like image 20
Neekobus Avatar answered Dec 03 '25 19:12

Neekobus



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!