0

I am working on a header that contain a brand logo and a navigation. The header itself is contained in a div named container. The markup is like -

<div class="container">
    <div class="head">
        <div class="brand>Brand Name</div>
        <div class="nav'>Nav content Here</div>
    </div>
</div>

The CSS I am using is like

.container{
    width:100%;
    margin:10% auto;
}
.head{
    float:left;
    width:100%
}
.brand{
    float:left;
    margin:0 3%;
    width:auto;
}
.nav{
    float:right;
    margin:0 3%;
    width:auto
}

The Problem is that the all the content flows out of the Main container <div>and the container takes a height of 0px. The content is displaying as i want it to be but the main container is taking a height of 0px.

I want to display the brand logo and the navigation side by side. It would be great if you could make a fiddle with the code. Thanks in advance.

EDIT: Sorry for not researching before posting the question. The Clearfix hack is a life saver but there were several different ways also. Listed here - https://stackoverflow.com/questions/218760/how-do-you-keep-parents-of-floated-elements-from-collapsing

Community
  • 1
  • 1
Nishant Dixit
  • 273
  • 1
  • 2
  • 12

4 Answers4

2

You can use clearfix hack explained here

html:

<div class="container clearfix">
<div class="head">
    <div class="brand">Brand Name</div>
    <div class="nav">Nav content Here</div>
</div>

css:

.container{
    width:100%;
    margin:10% auto;
}
.head{
    float:left;
    width:100%
}
.brand{
    float:left;
    margin:0 3%;
    width:auto;
}
.nav{
    float:right;
    margin:0 3%;
    width:auto
}
.clearfix:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
}
.clearfix { display: inline-block; }
/* start commented backslash hack \*/
* html .clearfix { height: 1%; }
.clearfix { display: block; }
/* close commented backslash hack */

Fiddle here

Siffer
  • 353
  • 1
  • 10
0

You have to use clearfix for .container div like this: Demo to automatically clear its child elements, which have float.

<div class="container clearfix">
  <div class="head">
    <div class="brand">Brand Name</div>
    <div class="nav">Nav content Here</div>
  </div>
</div>

and here corrected HTML syntax errors

G.L.P
  • 6,913
  • 4
  • 20
  • 39
0

You got html issue:

<div class="brand>Brand Name</div>

and:

<div class="nav'>Nav content Here</div>

I fixed these and it wokring fine! without changing your `css

<div class="container">
    <div class="head">
        <div class="brand">Brand Name</div>
        <div class="nav">Nav content Here</div>
    </div>
</div>

jsFiddle

Pedram
  • 12,941
  • 7
  • 35
  • 64
0
    <div class="brand>Brand Name</div>
    <div class="nav'>Nav content Here</div>

change to

<div class="brand">Brand Name</div>
<div class="nav">Nav content Here</div>
VishnuKumar Pv
  • 167
  • 1
  • 13