0

I have a div containing 3 div inside it. My problem is when am setting background color to first div, the div inside first div is not applying the background.

My code is,

<div class="how_next">
    <div class="how_next_single" align="center">
        <p><img src="images/livemonitoring.png" /></p>
        <p>Heading</p>
        <h3>Description</h3>
    </div>

    <div class="how_next_single" align="center">
        <p><img src="images/livemonitoring.png" /></p>
        <p>Heading</p>
        <h3>Description</h3>
    </div>

    <div class="how_next_last" align="center">
        <p><img src="images/livemonitoring.png" /></p>
        <p>Heading</p>
        <h3>Description</h3>
    </div>
</div>

My style is,

.how_next {
    height:auto;
    background-color:#E9E9E9;
    padding:15px;
    font-size:16px;
    font-weight:bold;
    margin-top:10px;
}
.how_next_single {
    width:32%;
    float:left;
}
.how_next_last {
    width:32%;
    float:right;
}

Now the output is

Output

What is the problem in my coding. Is there any solution.

Jithin Varghese
  • 2,018
  • 3
  • 29
  • 56

5 Answers5

3

When you use floating elements inside a wrapper you need to clear them. There are various ways to clear them using clearfix technique, using overflow:hidden, etc.

So, you need to do just:

.how_next {
   overflow: hidden;
}

You may have an interest in these q/a:

what is clearfix

which method of clearfix is best

clearfix with twitter bootstrap

all about floats

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
  • 1
    Note with this solution that at some point, if any contained element want's to exceed the bounds of the parent, it won't be able to - e.g. a dropdown – Dominic Nov 30 '15 at 09:56
0

See this is the perfect answer of your query.

Css:

.how_next {
  height:auto;
  background-color:#E9E9E9;
  padding:15px;
  font-size:16px;
  font-weight:bold;
 margin-top:10px;
}
 .how_next_single {
   width:32%;
  float:left
 }

Working Link

Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
0

What about create a new class style:

.clearfix{
   clear:both;
}

Then add it to the end of your HTML:

<div class="how_next">
   <div class="how_next_single" align="center">
       <p><img src="images/livemonitoring.png" /></p>
       <p>Heading</p>
       <h3>Description</h3>
   </div>

   <div class="how_next_single" align="center">
       <p><img src="images/livemonitoring.png" /></p>
       <p>Heading</p>
       <h3>Description</h3>
   </div>

   <div class="how_next_last" align="center">
       <p><img src="images/livemonitoring.png" /></p>
       <p>Heading</p>
      <h3>Description</h3>
   </div>

   <div class="clearfix"></div>

</div>

And don't use such a things like align="center" it's not a proper way of centering things due to HTML5 standards. All those things should be achieved by CSS, you shouln't write any styles in your HTML tags, that's a good practice.

Hmmm, I also think that you don't need height:auto in your how_next style.

Why clear:both?

You must do this in your case, because you're using floating elements, which won't make your parent expand by them.

Have a look! http://www.w3schools.com/cssref/pr_class_clear.asp

pavon147
  • 703
  • 1
  • 8
  • 15
0

Try to add:

 <div class="clear"></div>

and give him style:

<style>
   .clear{both;with:100%;}
</style>

after the .how_next_last div

Example:

<div class="how_next">
    <div class="how_next_single" align="center">
        <p><img src="images/livemonitoring.png" /></p>
        <p>Heading</p>
        <h3>Description</h3>
    </div>

    <div class="how_next_single" align="center">
        <p><img src="images/livemonitoring.png" /></p>
        <p>Heading</p>
        <h3>Description</h3>
    </div>

    <div class="how_next_last" align="center">
        <p><img src="images/livemonitoring.png" /></p>
        <p>Heading</p>
        <h3>Description</h3>
    </div>
    <div class="clear"></div>
</div>
0

This is your perfect answer for css.

.how_next {
    height:auto;
    background-color:#E9E9E9;
    padding:15px;
    font-size :16px;
    font-weight:bold;
    margin-top:10px;
    float:left;
    width:100%;
   }
.how_next_single {
    width:32%;
    float:left;
  }
.how_next_last {
    width:32%;
    float:right;
   }
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Jagdish Thakre
  • 158
  • 2
  • 11