How to avoid empty clear divs?

后端 未结 5 2154
半阙折子戏
半阙折子戏 2021-01-03 07:34

I have been using a lot of floats recently like this:

5条回答
  •  没有蜡笔的小新
    2021-01-03 08:13

    This is a big part of the CSS learning curve. When you float items, their containing element no longer takes the vertical height of the floated elements into account. There are a couple of solutions you could use to get around your dilemma.

    Simply specify a hight for your #button container to the hight of your buttons:

    #button { height: 30px; }
    

    A fancier solution would be the clearfix hack. It's pretty bullet proof and will also do the trick without the need to add the extra markup and inline CSS.

    #buttons:after {
        content: ".";
        display: block;
        clear: both;
        visibility: hidden;
        line-height: 0;
        height: 0;
    }
    
    #buttons {
        display: inline-block;
    }
    
    html[xmlns] #buttons {
        display: block;
    }
    
    * html #buttons {
        height: 1%;
    }
    

提交回复
热议问题