0

I have this html file

    <div class="con" style="width:100px;height:200px;">
        1
    </div>
    <div class="con" style="width:100px;height:200px;">
        2
    </div>
    <div class="con" style="width:100px;height:400px;">
        3
    </div>
    <div class="con" style="width:100px;height:400px;">
        4
    </div>
    <div class="con" style="width:100px;height:100px;">
        5
    </div>
    <div class="con" style="width:100px;height:100px;">
        6
    </div>
    <div class="con" style="width:100px;height:100px;">
        7
    </div>
    <div class="con" style="width:100px;height:100px;">
        8
    </div>

And I want with this html have this result : Html result

But I write this css :

<style>
body {
width:440px;
height:440px;
}
.con{
float:left;
background:#bebebe;
margin:1px;
}
</style>

And I got this result! :-( enter image description here

I repeat that I would have the result of the first image using only the html code that I wrote.

Community
  • 1
  • 1
zizzamia
  • 241
  • 3
  • 9
  • 3
    You are using the CSS rules wrong. Id's should be unique to a div, you should use a class if you want different div elements to have the same style. – gnur Feb 03 '11 at 17:14
  • I know, but now it is not important!!! – zizzamia Feb 03 '11 at 17:21
  • Hahaha you're right, but now I'd love to be able to solve this problem using only the left float. Why I am writing one particular thing that needs only float left. – zizzamia Feb 03 '11 at 17:38
  • 1
    @zizzamia - yes it is important; your code is invalid, which will cause you problems eventually even if it works now. And your problem will be much easier to solve if you use classes properly instead of ids. – Spudley Feb 03 '11 at 17:38
  • Instead of being so arrogant, with classes you would know it solve my problem. – zizzamia Feb 03 '11 at 17:46

3 Answers3

2

Firstly, you shouldn't have multiple items with the same id. This is what class is for. id should be unique within the page.

The solution is to your problem is to float the two tall blocks to the right and everything else to the left.

This will of course only work if the boxes are in a container (ie the body) that is just the right width for all four, otherwise you'll just end up with a gap in the middle of your layout.

The trouble is that because you've got the same ID for everything, you can't easily specify which ones to float right and which ones to float left.

There are ways to do it, but the correct solution would be to use classes.

<div class="con" style="width:100px;height:200px;">
    1
</div>
<div class="con" style="width:100px;height:200px;">
    2
</div>
<div class="con tall" style="width:100px;height:400px;">
    3
</div>
<div class="con tall" style="width:100px;height:400px;">
    4
</div>
<div class="con" style="width:100px;height:100px;">
    5
</div>
<div class="con" style="width:100px;height:100px;">
    6
</div>
<div class="con" style="width:100px;height:100px;">
    7
</div>
<div class="con" style="width:100px;height:100px;">
    8
</div>


<style>
body {
  width:408px;
  height:440px;
}
.con {
  float:left;
  background:#bebebe;
  margin:1px;
}
.tall {
  float:right;
}

</style>

If you absolutely cannot (or will not) change the HTML code, there is still one other solution I could suggest - the CSS [attr] selector will allow you to specify a different CSS style for elements that have particular attributes. In this case, you could use it to pick out the tall blocks and float them right.

#con[style~='height:400px;'] {float:right;}

This will only affect the elements that have id="con" and where the style attribute includes height:400px;.

However please note that the [attr] selector does not work on older version of IE, so you may need to excersise caution if you decide to use it.

This solution also involves floating certain elements to the right, which you seem to be dead set against, but it is still the only viable CSS-only solution.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Ok And if I wanted to use one and only "float: left" there is a trick to get it right? – zizzamia Feb 03 '11 at 17:13
  • @zizzamia - I've updated the answer with more detail and included working code. – Spudley Feb 03 '11 at 17:20
  • @zizzamia - I've added another possible way of doing it with CSS that doesn't involve changing you HTML at all. It would still need you to `float:right` the tall block though. – Spudley Feb 03 '11 at 18:08
2

change the width of the body to 408px; then float:right; the div 3 & 4.

demo: http://jsbin.com/imire5

Updated demo with float left only: http://jsbin.com/imire5/2

Sotiris
  • 38,986
  • 11
  • 53
  • 85
  • Sorry but if I wanted to use only "float: left". – zizzamia Feb 03 '11 at 17:26
  • @zizzamia Then I suppose you have to wrap each side (left and right). Div with number 1,2,5,6,7,8 in the left wrapper, and numbers 3 and 4 in the right wrapper. Check the updated link – Sotiris Feb 03 '11 at 17:37
  • Very Thanks, but the structure that I am dealing with is always changing, and the arrangement of the blocks are random, so I need to use only the left because then every time you place yourself. – zizzamia Feb 03 '11 at 17:47
  • I created a javascript engine that allows me to place several blocks, with varying size. So in order of appearance I want to place the various div correctly. The structure of the blocks I do not know, because it is always random, which is why I only use CSS to position it properly. – zizzamia Feb 03 '11 at 17:55
0

The basic issue you have is that CSS float works in a way that is different to how you want it to work.

Another answer to your problem might be to use Javascript to hack it.

There is a jQuery plug-in called Masonry which looks like it does what you're after. You can use float:left; for everything, and then use Masonry to close up the gaps.

It would mean relying on a solution that isn't entirely CSS-based though, which probably isn't ideal - especially since I've already given you a working CSS-only solution.

(also once you start using Javascript, you definitely need to fix your id vs class problem -- Javascript will disown you if you have all those elements with the same id)

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • So, I created a javascript engine that allows me to place several blocks, with varying size. So in order of appearance I want to place the various div correctly. The structure of the blocks I do not know, because it is always random, which is why I only use CSS to position it properly. – zizzamia Feb 03 '11 at 17:55
  • Did you look at the Masonry site I linked to? It effectively simulates a `float:downwards;` sort of layout. As far as I can tell, it's as close as you're going to get to your intended layout without using `float:right;` on the tall blocks as per the other answers. – Spudley Feb 03 '11 at 17:59