Why can't floated elements set their left and right margins

后端 未结 3 1099
情话喂你
情话喂你 2021-01-02 06:58

In a wrapper div, the floated elements don\'t seem to respond to left and right margin settings. Example:

html:

<
3条回答
  •  走了就别回头了
    2021-01-02 07:35

    Margins do not move floated elements, they "push content away".

    If you want to move the floated element, you could give it the following CSS rules:

    #content {
        position: relative;
        left: 30px;
    }
    

    An alternative is giving the element a transparent border:

    #content {
        border-left: 30px transparent;
    }
    

    If you are just looking to position a div inside of another div, then use absolute positioning:

    #wrapper {
        position: relative; /* required for absolute positioning of children */
    }
    
    #content {
        position: absolute;
        left: 0;
    }
    

提交回复
热议问题