CSS Block float left

前端 未结 3 1453
既然无缘
既然无缘 2021-01-28 04:28

I have this html file

    
1
相关标签:
3条回答
  • 2021-01-28 04:46

    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

    0 讨论(0)
  • 2021-01-28 04:55

    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.

    0 讨论(0)
  • 2021-01-28 05:05

    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)

    0 讨论(0)
提交回复
热议问题