margin-top not working with clear: both

后端 未结 8 2119
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 00:53

相关标签:
8条回答
  • 2020-11-29 01:22

    Try setting a bottom margin on one of the floated elements. Alternatively, you can wrap the floats in a parent element, and use a css hack to clear it without additional markup.

    0 讨论(0)
  • 2020-11-29 01:24

    Alternative solution:

    You can actually put a margin-bottom on the floated elements to push DOWN the element underneath that has clear: both.

    http://jsfiddle.net/9EY4R/

    enter image description here

    Note: Having made this suggestion I have to immediately retract it as not generally a good idea, but in some limited situations may appropriate;


    <div class='order'>
    
        <div class='address'>
            <strong>Your order will be shipped to:</strong><br>
            Simon</br>
            123 Main St<br>
            Anytown, CA, US
        </div>
    
        <div class='order-details'>
            Item 1<br>
            Item 2<br>
            Item 3<br>
            Item 4<br>
            Item 5<br>
            Item 6<br>
            Item 7<br>
            Item 8<br>
            Item 9<br>
            Item 10<br>
        </div>
    
        <div class='options'>
            <button>Edit</button>
            <button>Save</button>
        </div>
    </div>
    

    The panel with items is called order-details with this css

    .order-details
    {
        padding: .5em;
        background: lightsteelblue;
    
        float: left;
        margin-left: 1em;
    
        /* this margin does take effect */
        margin-bottom: 1em;
    }
    

    In the above fiddle - the yellow panel has a margin-top, but unless it is greater than the tallest floated item then it won't do anything (of course that's the whole point of this question).

    If you set the margin-top of the yellow panel to 20em then it will be visible because the margin is calculated from the top of the outer blue box.

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