Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css float messes up layout

Tags:

css

I want this layout where I have a rectangular box. And inside the box on the left there is a text and on the right there is an image. If I do it without a table, the float causes the elements inside the box appear outside. How do i make this work?

<div style="width: 100%; border-style:solid;">
  <span style="float: left;">  
     <h3> Your appointment Details</h3>
  </span>
  <span style="float: right;">
     <img src="someImage"/>
  </span>
</div>
like image 746
Foo Avatar asked Dec 27 '22 07:12

Foo


2 Answers

Add this to the parent container:

.parent {
    overflow: hidden;
}

<div class="parent" style="width: 100%;border-style:solid;">

    <span style="float: left;">  
         <h3> Your appointment Details</h3>
    </span> 
    <span style="float: right;">
        <img src="someImage"/>
    </span>

</div>

jsfiddle with example of solution

like image 90
Billy Moat Avatar answered Dec 28 '22 19:12

Billy Moat


The answer by Billy Moat is the right way to do it. To add on there is another way also. You could add an empty element to the end of the parent element with its float cleared. i.e

.dummyClear{
   clear:both
}


<div style="width: 100%;border-style:solid;">    
    <span style="float: left;">  
         <h3> Your appointment Details</h3>
    </span> 
    <span style="float: right;">
        <img src="someImage"/>
    </span>
    <div class="dummyClear"></div> <!-- Any tag is fine -->

</div>
like image 44
Rakesh Avatar answered Dec 28 '22 19:12

Rakesh