Understanding Bootstrap's clearfix class

前端 未结 3 1455
余生分开走
余生分开走 2020-12-05 04:13
.clearfix {
  *zoom: 1;
  &:before,
  &:after {
    display: table;
    content: \"\";
    // Fixes Opera/contenteditable bug:
    // http://nicolasgallagher         


        
3条回答
  •  被撕碎了的回忆
    2020-12-05 04:32

    When a clearfix is used in a parent container, it automatically wraps around all the child elements.

    It is usually used after floating elements to clear the float layout.

    When float layout is used, it will horizontally align the child elements. Clearfix clears this behaviour.

    Example - Bootstrap Panels

    In bootstrap, when the class panel is used, there are 3 child types: panel-header, panel-body, panel-footer. All of which have display:block layout but panel-body has a clearfix pre-applied. panel-body is a main container type whereas panel-header & panel-footer isn't intended to be a container, it is just intended to hold some basic text.

    If floating elements are added, the parent container does not get wrapped around those elements because the height of floating elements is not inherited by the parent container.

    So for panel-header & panel-footer, clearfix is needed to clear the float layout of elements: Clearfix class gives a visual appearance that the height of the parent container has been increased to accommodate all of its child elements.

     

提交回复
热议问题