I have this HTML:
<div class = "block1">hi</div>
<div class = "block2">hi</div>
And in one case I have this CSS:
.block1 {
width:100px;
border:1px solid;
float: left;
}
.block2 {
width:100px;
border:1px solid;
}
Which makes this:
And the second case, I have this CSS:
.block1 {
width:100px;
border:1px solid;
float: left;
}
.block2 {
border:1px solid;
}
Which makes this:
Why setting a width makes the second div not being side by side with the first div? What if I want the second div to be side by side and have 100px? If I set float:left; it does that, but why?
Because if you specify a width to your second
divyou need to usefloat: left;for it, becausedivis a block level element, if you don’t specify a width, it will take up the rest of space available, if you define a width, it will float besides the other floated div if the space is available, inorder to float it right, you need to usefloat: rightDemo
Edit: Better understanding…
If you do not define width
As you said if you specify width it should float to left, it ont as you are giving same width to the
div2what you gave todiv1Reference, so if you change say150pxfor second div it will work… DemoBut if you clear your floats before floating the second div it wont make the div float besides the other 1, See this example