Columns in percentage not aligned

Asked

Viewed 235 times

5

I am using on my site 3 columns(sections) each with 33.33% width and with margin-left and right from 10px to 3. Only the last column is falling down. Because they are width in %, they should not fit the size of my container instead of falling?

<main>
        <article>
            <section class="bg-col col-sm-4">
                <h2>Empresa</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                <div class="more"><a href="empresa.php">Saiba Mais</a></div>
            </section>

            <section class="bg-col col-sm-4">
                <h2>Serviços</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                <div class="more"><a href="servicos.php">Saiba Mais</a></div>
            </section>

            <section class="bg-col col-sm-4">
                <h2>Contatos</h2>
                <p>Tel: (xx) xxxx-xxxx</p>
                <p>E-mail: [email protected]</p>
                <div class="more"><a href="contatos.php">Saiba Mais</a></div>
            </section>
        </article>
    </main>
  • 1

    Please include HTML in the question. Anyway, it is the expected behavior: 99% width plus margins exceed available space.

  • @bfavaretto how could I solve? html was included.

  • @Lucas If my answer meets your need, please mark as accepted

2 answers

4

The problem is that the widths are relative to the container. This remaining 1% is not being enough to accommodate the 60px margins.

The simplest solution (but not working on IE9 and above) is to use the CSS3 column properties:

article {
    /* 3 colunas, 10px entre elas */
    -webkit-column-count: 3; 
    -webkit-column-gap: 10px;
       -moz-column-count: 3; 
       -moz-column-gap: 10px;
            column-count: 3; 
            column-gap: 10px;
}

http://jsfiddle.net/LguHE/

  • These properties work with any type of element, or only article?

  • With any element (maybe there is a restriction that it is a block).

3


Follows the solution in FIDDLE

I simply set a 30% width for the Section, and in the middle Section I gave a 5% margin to the left and right, thus adding: 30% + 5% + 30% + 5% + 30% = 100%.
Where, 30% corresponds to the space that Section occupies, and 5% corresponds to the space between section.

article section                  { width: 30%; float:left; }
article section:nth-child(2)     { margin: 0 5%; }

RESPONSIVENESS:
When using percentage, everything becomes relative, so if you want to align laterally 3 Divs, you should take into account EVERYTHING that can increase your side dimensions, such as:
- Border (right and left)
- Padding (right and left)
- Margin (right and left)
- Width (mainly the width of the div itself)

From there, one should think as follows.
If I want a div that occupies 100% of the available width, having 5% padding in both direct and left, the following CSS rules should apply:

.classe-da-div{
  width: 90%;
  padding-left: 5%;
  padding-right: 5%; 
/* 
    90% + 5% padding-left + 5% de padding-right = 100% 
*/
}

EDGE RESPONSIVENESS

If you want to apply responsiveness to blocks using edges, just apply the property box-sizing: border-box so the width of the block becomes automatically the sum of the content with the border and the padding, see the example:

.classe-da-div{
box-sizing: border-box;
width: 30%; /* Dentro desses 30% já estão calculadas as bordas e os paddings abaixo */
padding: 5%;
border: 1px solid #CCC;
}
.classe-da-div:nth-child(2){
margin: 0 5%; /* Margem pra direita e pra esquerda da div do meio */
}

Following this reasoning you can assemble a block with 3 Ivs aligned laterally, just add in percentage the width of the Ivs and divide what remains for the margins.

Look at this FIDDLE that I made, will help you.

  • It would have to demonstrate the fiddle with border?

  • In this case the property should be added box-sizing: border-box ... from that rule, the box shall count the widththe paddingand the borderas the total contents of the box ... see FIDDLE

  • Yeah, that’s not very stable if you want a smaller margin. So, thank you.

  • Isn’t it? Of course q is stable! Let’s say a tiny margin now. See here http://jsfiddle.net/LguHE/6/ If you prefer to move the partition of the result window, to see the behavior of the Divs

  • Or you can have an even smaller margin like this example, smaller than that? better not even have margin!

  • Smaller has already improved, what will always be work, is to hit the multiples to hit, and hope the browser round the calculation right. But in view of the limits of CSS, I find interesting the output.

  • There is no "twist the browser round the calculation right", if you give the exact values ... if you have 3 Divs with 30% width each (already including edges and paddings, no matter the value), 10% to complete the 100% space, just divide the 10% by 2(Number of spaces between the 3 Divs), obviously 5% between the first and second div, and 5% between the second and third. SIMPLE!

  • The problem is that it’s not about dividing 100% by several columns, but it’s about adding up several things to try to get 100% accurate. In this case you run the risk of accumulation of rounding error (which is unlikely but not impossible). It is a mathematical question, which as you said, is SIMPLE.

  • If for you this complicates, I will give you a much more practical solution, try to study Flexbox, will hit a little, but is a super help in building small templates

  • 1

    I did not criticise your reply, it was just a comment. I had started an answer to this question the day before yours, and I had already done some tests, which is why I noticed the question of rounding up. As to the Flexbox, You’re right, it’s a good way out too. By the way, it would be nice to add an option with Flexbox in your answer, it would be an option for those who access this question in the future.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.