Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div to float left/right inside a centered div

Tags:

html

css

center


when i want to float a child div to left or right inside the centered parent div, the whole design goes left or right, depending on the float. So, how to float a child div and make the centered parent div in the center.

HTML:

<div id="parent">
<div id="child-left"></div>
<div id="child-right"></div>
</div>

CSS:

#parent{
    padding: 0 auto;
    width: 600px;
}
#child-left{
    float: left;
    width: 300px;
}
#child-right{
    float: right;
    width: 300px;
}



Why does parent div go left/right, and doesn't stay in center? And how to make it to stay in center?

like image 689
123dcck Avatar asked Dec 26 '22 16:12

123dcck


2 Answers

For parent div you use this css code

margin:0 auto;
width:980px;

and for child u use this code for float

float:right or left;
width:anypx;

best regards

like image 33
Saeed Avatar answered Dec 29 '22 09:12

Saeed


See the demo

#parent{
    padding: 0px, auto;
    width: 605px;
  height:200px;
  border:solid 1px #f00;
}
#child-left{
  float: left;
  width: 300px;
  height:200px;
  border:solid 1px #0F0;
}
#child-right{
    float: right;
    width: 300px;
   height:200px;
  border:solid 1px #00F;
}
like image 65
GajendraSinghParihar Avatar answered Dec 29 '22 08:12

GajendraSinghParihar