Extra margin appearing on elements with input-block display. How to solve?

2

I have a problem with my css. He was working out a style to format some buttons. Everything went well. However, my buttons have an extra margin appearing. That is, I did not set margin on my buttons, however they are appearing with margin.

Example:

.container .item{
    background-color: lightgreen;
    display:inline-block;
    margin:0;
    padding:10px;
}
<div class="container">

  <button class="item">Item</button>
  <button class="item">Item</button>
  <button class="item">Item</button>
</div>

Notice that I have defined that the button would have margin:0 , but it has margin.

I noticed that the problem happens because of inline-block , because when I change to block with float:left the problem is solved. See:

.container .item {
   float: left;
   background-color: lightgreen;
   margin:0;
   display:block;
}
<div class="container">

      <button class="item">Item</button>
      <button class="item">Item</button>
      <button class="item">Item</button>
    </div>

But float:left in this case is not desirable, for example, I usually use text-align:right to change button alignments, and this does not work with float:left .

Is there any way to remove those extra margins that are popping up, without taking inline-block from my buttons?

    
asked by anonymous 18.02.2016 / 18:08

2 answers

2

It places these spaces to separate the elements that "are on the same line" as the spaces we put between one word and another. This is "standard".

To resolve you can:

1) Negate the margin-left or margin-right of each element ( example ) ;

2) Leave the elements side by side in the code HTML , without line or space break ( example ).

There are still other ways to get around these situations, but I believe that with these two alternatives you will solve your problem.

    
18.02.2016 / 18:33
0

Leave your <div class="container"> with display: flex; or display: table; resolves, but I do not know if it will hinder you in your button alignment

    
18.02.2016 / 18:39