I can’t get one div stuck on the other

Asked

Viewed 892 times

0

I want to put the 3 divs on the same line with width: 33.3333%, but a space is appearing between them. Note in the image that the padding, margin and borda are zero, and I have declared box-sizing:border-box in the reset.

Análise de código fonte no Chrome para desenvolvedor

html, body, div, span, applet, object, iframe,
            h1, h2, h3, h4, h5, h6, p, blockquote, pre,
            a, abbr, acronym, address, big, cite, code,
            del, dfn, em, img, ins, kbd, q, s, samp,
            small, strike, strong, sub, sup, tt, var,
            b, u, i, center,
            dl, dt, dd, ol, ul, li,
            fieldset, form, label, legend,
            table, caption, tbody, tfoot, thead, tr, th, td,
            article, aside, canvas, details, embed, 
            figure, figcaption, footer, header, hgroup, 
            menu, nav, output, ruby, section, summary,
            time, mark, audio, video {
                    margin: 0;
                    padding: 0;
                    border: 0;
                    font-size: 100%;
                    font: inherit;
                    vertical-align: baseline;
                    box-sizing: border-box;
            }

I don’t know where this came from if the browser itself says nothing by the element inspector. Does anyone know how I can remove this?

3 answers

1


Look, as already said display inline-block has a spacing that even zeroing all the margins still continues with spaces.

How can I solve this with the elements still being inline-block? Simple. just reset the font-size: 0 of the parent element. makes sense? I don’t know but so solves, follows example:

*{
  box-sizing:border-box
}

nav{
  font-size: 0;
}

nav a {
  display: inline-block;
  background: #ddd;
  padding: 10px;
  font-size: 15px;
  width: 33.333%;
  border: 1px solid #333;
}
<nav>
  <a href="#">One</a>
  <a href="#">Two</a>
  <a href="#">Three</a>
</nav>

In my example the parent element is the tag nav, but there is another way to solve this, look at the example:

*{
  box-sizing:border-box
}

nav a {
  display: inline-block;
  background: #ddd;
  padding: 10px;
  font-size: 15px;
  width: 33.333%;
  border: 1px solid #333;
}
<nav>
  <a href="#">One</a
  ><a href="#">Two</a
  ><a href="#">Three</a>
</nav>

Note that now I will not zero the font-size of the parent element, but the character > from the closing of each jump tag and soon started the next tag...

what happened was to leave almost everything in the same line, and observing this behavior it is clear that that space in the elements inline-block is caused by the line breaking in the code, makes no sense but beauty

  • I thought I’d seen it all, here you come with this one. Or rather "these". The two solutions have no sense! But if it works that’s what it’s worth. Thanks for the tip. Now, I think I’ll never suffer to put the elements in the same line. Great hug!

  • does not even kk just you jump the line in the code visually gets that demon space, fortunately has how to solve

1

Elements inline-block by default have spaces, this is some kind convention, do not know for sure, to remove these spaces just set a margin negative, example

nav a {
  display: inline-block;
  padding: 5px;
  background: red;
  margin-right: -4px;
}
<nav>
  <a href="#">One</a>
  <a href="#">Two</a>
  <a href="#">Three</a>
</nav>

By the way, your solution is the best possible, display flex, or better flexbox is for me the best way to behave the elements in the layout, follows the complete guide Flexbox css Tricks

  • I had forgotten this rule that the inline-blok added a margin of 4px to the right. 0/ I only remembered now after I read your reply. But anyway, I think I’ll stick with the display flex because it’s easier. Thanks!!!

0

I put display:flex; in father div and solved my problem. Thanks! But if still cuff can explain me this question will be good to know. This left me with "a flea behind my ear". I think the div should stand next to each other once I zeroed everything and asked display:inline-block; and width:33.3333%;.display:flex; CSS

  • This is the solution or a new doubt?

  • I believe that this is not the ideal solution. But if someone could tell me why a ledge was appearing, I would very much like to know.

  • I think it has to do with that question, but I’m not sure why you’re using Chrome, firefox makes it easier to identify this

Browser other questions tagged

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