Box-sizing doesn't work

Hello. There is a trace. markup:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Магазин</title>
</head>
<body>
    <div id="container">
        <div class="cols col-4">Один</div>
        <div class="cols col-4">Два</div>
        <div class="cols col-4">Три</div>
    </div>
</body>
</html>

There is a trace. css:

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    -o-box-sizing:border-box;
    -ms-box-sizing:border-box;
}
body{
    font:100%/1.5em Arial,Helvetica sans-serif;
    color:#000;
}
#container{
    width:90%;
    margin:0 auto;
    background:#d2d2d2;
}
#container .cols{
    display:inline-block;
    text-align: center;
    margin:0 0 1em;
    padding:0 1em;
    background:#fd4;
}
.col-1{width:8.333333333333333%;}
.col-2{width:16.66666666666667%;}
.col-3{width:25%;}
.col-4{width:33.33333333333333%;}
.col-5{width:41.66666666666667%;}
.col-6{width:50%;}
.col-7{width:58.33333333333333%;}
.col-8{width:66.66666666666666%;}
.col-9{width:75%;}
.col-10{width:83.33333333333333%;}
.col-11{width:91.66666666666666%;}
.col-12{width:100%;}

The problem in this code is that for some reason box-sizing does not work and the third column does not fit. As soon as I put the float:left rule in the #container .cols container display:inline-block, everything works as it should.

Code in jsbin.

Why doesn't box-sizing work in this case?

Author: Самат Жанбеков, 2014-06-04

2 answers

Because there are line feeds between div-s, they are perceived as spaces, spaces are wide, three blocks wide (100/3)% plus the width of spaces is more than 100% , and therefore it does not fit in one line.

This is a small dirty trick with inline blocks. You can get around it by removing line feeds and spaces between blocks, or by setting the parent element to a space width of 0 (you will have to restore it in the child elements).

 0
Author: MrClon, 2014-06-04 18:46:04

You have spaces between the blocks, try like this:

<div id="container">
    <div class="cols col-4">Один</div><div class="cols col-4">Два</div><div class="cols col-4">Три</div>
</div>

Http://jsbin.com/nahuhawi/9/edit

That is, so that there are no spaces between declaring the new one and closing the old one. It seems there is still a solution in pure css, you can Google in this direction.

Update: yes, float: left is a different solution

Well, yes, on the topic of a good article

 3
Author: RakkaTakka, 2014-06-04 18:47:57