Center parent div with float children

前端 未结 4 1906
逝去的感伤
逝去的感伤 2021-01-24 12:09

Parent hasnt got a defined width because there are unknown number of children inside.

Why do children fall into new line and how to prevent that? Children need to be on

4条回答
  •  没有蜡笔的小新
    2021-01-24 12:38

    You should consider using flex-box.

    And to answer your question: floated DIVs are not supposed and will not expand their parent's width, as they are 'floating' above the content flow.

    Absolute positioned block elements will have an initial width of zero, if not specified otherwise. Thus, they're behaving differently than usual.

    The floating elements indeed behave as usual: as the parent element provides not enough width (zero!) to display them all in one line, of course they break to the next line.

    .wrap {
      display: flex;
      justify-content: center;
      flex-wrap: nowrap;
      margin-top: 50px;
    }
    .box {
     width:40px;
     height:40px;
     float:left;
     background:red;
     margin-right:1px;
    }
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

提交回复
热议问题