tags:

views:

91

answers:

3
.float1  {
float: left;
width: 50%;
height: 50%;
}
.float2  {
float: right;
width: 50%;
height: 50%;
}
.float3  {
float: left;
width: 50%;
height: 50%;
}
.float4  {
float: right;
width: 50%;
height: 50%;
}
.clear {
clear: both;
}

HTML:
<div class="float1">Float 1</div> <div class="float2">Float 2</div> <div class="clear"></div> <div class="float3">Float 3</div> <div class="float4">Float 4</div> <div class="clear"></div>


I want an output like this image:
alt text


Please Correct my css code. Thank you.

+3  A: 

height is spelled incorrectly. what does your display look like?

derek
+5  A: 

You only need to clear the third element, so there is no need for any clear elements. Float all elements to the left to put them beside each other instead of some to the left and some to the right:

.float1, .float2, .float3, .float4 { float: left; width: 50%; height: 50%; }
.float3 { clear: both; }

HTML:

<div class="float1">Float 1</div>
<div class="float2">Float 2</div>
<div class="float3">Float 3</div>
<div class="float4">Float 4</div>
Guffa
if float3 is clear on both, how will float4 be right next to it?
Matt Ellen
@Matt: `clear` impacts the preceding elements, not the ones that follow.
Samir Talwar
I like the refactored CSS, unlike the one asked above. but again I'm often skeptical of using % height's or widths.
phoenix24
@phoenix24: Yes, using percentages like this can cause rounding problems, e.g. 50% of 201 pixels has to be rounded to 100 or 101, so there will be one pixel too little or one too much when you have two elements beside each other.
Guffa
A `clear: left;` would make more sense in this context.
BalusC
A: 

If you're able to use CSS3, you can use a selector to accomplish this without tons of code:

CSS:

div.boxContainer>div{
    float: left;
    width: 50%;
    height: 50%;
}

div.boxContainer div:nth-of-type(3){
    clear:both;
}

HTML:

<div class="boxContainer">
<div class="box" style="width:50px;height:50px;border:solid 1px black;float:left;">1</div>
<div class="box" style="width:50px;height:50px;border:solid 1px black;float:left;">2</div>
<div class="box" style="width:50px;height:50px;border:solid 1px black;float:left;">3</div>
<div class="box" style="width:50px;height:50px;border:solid 1px black;float:left;">4</div>
</div>
BraedenP