css divide width 100% to 3 column

前端 未结 11 616
余生分开走
余生分开走 2020-12-12 21:38

I have a layout where I have 3 columns.

Therefore, I divide 100% by 3.

The result is obviously 33.333....

My goal is perfect

11条回答
  •  攒了一身酷
    2020-12-12 22:19

    2018 Update This is the method I use width: 33%; width: calc(33.33% - 20px); The first 33% is for browsers that do not support calc() inside the width property, the second would need to be vendor prefixed with -webkit- and -moz- for the best possible cross-browser support.

    #c1, #c2, #c3 {
        margin: 10px; //not needed, but included to demonstrate the effect of having a margin with calc() widths/heights
        width: 33%; //fallback for browsers not supporting calc() in the width property
        width: -webkit-calc(33.33% - 20px); //We minus 20px from 100% if we're using the border-box box-sizing to account for our 10px margin on each side.
        width: -moz-calc(33.33% - 20px);
        width: calc(33.33% - 20px);
    }
    

    tl;dr account for your margin

提交回复
热议问题