-2

Here is my website: http://www.reshapefinancial.com/ and when I apply left border it creates a space on right site. When you inspect and remove the left border border-left: 1px solid white you'll see that the space on the right disappears.

enter image description here

Seema Mir
  • 41
  • 8
  • 6
    Please add meaningful code and a problem description here. Don't just link to the site that needs fixing - otherwise, this question will lose any value to future visitors once the problem is solved or if the site you're linking to is inaccessible. Posting a [Minimal, Complete, and Verifiable example (MCVE)](https://stackoverflow.com/help/mcve) that demonstrates your problem would help you get better answers. For more info, see [Something on my web site doesn't work. Can I just paste a link to it?](https://meta.stackexchange.com/questions/125997/) Thanks! – j08691 Aug 08 '17 at 13:58
  • 1
    Possible duplicate of [How to remove the space between inline-block elements?](https://stackoverflow.com/questions/5078239/how-to-remove-the-space-between-inline-block-elements) – Marvin Aug 08 '17 at 14:03
  • 1
    The reason is a whitespace between the list items. See the linked question. – Marvin Aug 08 '17 at 14:04
  • Actually, border just makes this space visible, not causes it. And the cause is whitespace characters between `inline-block` elements which act as whitespace text characters, as @Marvin pointed out. – Ilya Streltsyn Aug 08 '17 at 14:07
  • @j08691 there're couple of good people who'll help me sort this issue. I know it is off topic for future references but I need to solve it and I'm asking for help from the community. I'll update my answer when it gets solved. – Seema Mir Aug 08 '17 at 14:12
  • 1
    @SeemaMir Apparently you don't understand me. Your question needs the code that's causing the problem you have. Otherwise if you merely point to your site and the problem gets fixed, then in the future this question has no value to anyone. As it stands now your question is one vote away from being put on hold because you haven't added code. – j08691 Aug 08 '17 at 14:21

4 Answers4

1

Please add the related code to your question

The reason there's a gap there is because the lis are set as display: inline-block. This means the browser treats them like words, and like words when there's a space between them the browser will render it. In your case the space comes from the newline and spaces used for indenting in the html.

Solution 1 (kinda hacky)

One solution which addresses this problem directly would be to add a:

#top-menu{
    ...
    font-size: 0;
}

this essentially makes the space the browser is putting there have no font size which disappears it. Not perfect, but it addresses the problem directly. As @Kudla69 pointed out in his answer this does ruin font-size inheritance which can cause issues as well, which is what makes this particular hack not the recommended route.

Solution 2 Recommended

Another way to address this directly is to remove the space in the HTML. Restructuring the HTML to read more like.

<li> This is something
</li><li> This is something else
</li>

This removes the space between the elements and the browser will essentially treat them like words without a space between them and butts them right up against one another.

Solution 3

Finally, if you're fine with it, you can also float: left the li elements. This may cause sizing issues in some cases though. This removes them from the flow so the browser doesn't treat them like words anymore.

Or as others have said use display: flex which again can act a bit differently than inline-block at times but is a more modern approach.

Community
  • 1
  • 1
Don
  • 3,987
  • 15
  • 32
1

@Dons answer explains the underlying issue well. Here's an alternate solution by making the ul a flexbox. This method is better imo since you don't have to hackily set the font size to 0 which will mess up any font size inheritance.

#top-menu.nav {
 display: flex;
}
Jamie Kudla
  • 872
  • 3
  • 17
  • 37
  • 1
    yeah, it's not my favorite solution either. But it explains the problem well. I usually do it by reformatting the HTML. I'll label my answer better. – Don Aug 08 '17 at 14:20
0

Please Checkout Following URL to Get a Solution For White Spacing

display: inline-block leaves gap with respect to height after div element and Here is your Solution

Add float:left at li

#top-menu li {
display: inline-block;
float: left;
font-size: 14px;
padding-right: 22px;
}
Dhruvang Gajjar
  • 568
  • 1
  • 8
  • 20
  • You're giving a solution but not answering the question. – Don Aug 08 '17 at 14:07
  • 1
    Also, `float` overrides and effectively removes all the effects of `inline-block` (both wanted and unwanted), so keeping both properties in the rule doesn't make sense. And switching from inline-block to floats may cause the need to "clearfix" the container. I'd switch to Flexbox instead, leaving inline-blocks (even with gaps) as a fallback. – Ilya Streltsyn Aug 08 '17 at 14:10
0

In the comments to the OP, @Marvin pointed out the root cause of the problem (and linked to the topic where it's already answered): it's because of whitespace characters between inline-block items. Inline-block elements belong to inline formatting context, just like e.g. words of the text, and any whitespace characters between them act as a whitespace characters in the text, i.e. as inter-word spacing. It's not a bug. It's how display:inline-block is supposed to work by the standard.

Setting the border doesn't cause that spacing, it just makes it visible.

There are tons of ways to work around this. Just to mention some of them:

  • Negative margin-left. Downsides: you need to know the exact width of a whitespace character, and it depends on the font settings etc.
  • Negative word-spacing or letter-spacing (or even both, as in old YUI solution). Downsides: same as above + browsers treat negative spacings differently, some allow items to overlap, some don't.
  • Remove the space between the previous item's end tag and the next item's start tag. Downsides: needs control over markup, not always compatible with template engines, worsens the readability of the code.
  • Comment out the space between these tags with HTML comments (<!-- -->). Downsides: same as above, redundant DOM nodes for these comments.
  • font-size: 0; for the container. Downsides: you can't set the font size of the items in em units anymore, works buggy in old Android browsers.
  • Omit the </li> end tags. It's valid in HTML5 (and was always valid in HTML, but not in XHTML), and doing so makes the browser close them implicitly right before the next <li> start tag, leaving no whitespace between them. Downsides: works only for a limited set of tags, not always easy to read, may cause problems in code editors (e.g. broken syntax highlighting).
  • Use the custom font for the container in which the space character has a zero width. Downsides: extra HTTP request for that font (or extra bytes if it's embedded), doesn't work in old/proxy browsers like Opera Mini.
  • Use floats instead of inline-blocks. Downsides: effectively removes all the effects of inline-blocks (including their advantages like horizontal and vertical alignment options), needs "clearfixing" the container.
  • Use display:table for the container and display:table-cell for items instead of inline-blocks. Downsides: can't wrap to the new line, items can't have different height, needs different approach to styling (e.g. border-spacing instead of margin to control the spacing if it's needed).

This list is way not complete. And none of these ways is perfect, as most of them are basically hacks.

However, I deeply believe that the best solution to any problem is using the proper tools that don't cause that problem. In this case, there is such tool: Flexbox. It has all the same alignment capabilities as inline-blocks (and more!), it needs no "clearfixing", it can wrap to new line if necessary, and it's currently supported in almost all browsers. And it doesn't leave any space between items by default.

In practice, you can just add display:flex to the container, and in modern browsers the unwanted spaces automagically go away. And your existing inline-block solution will still work in very old browsers, providing decent fallback.

Ilya Streltsyn
  • 13,076
  • 2
  • 37
  • 57
  • You're not answering the question. Which is "Why" is the space there. – Don Aug 08 '17 at 14:16
  • The question is already answered by @Marvin in comments to the OP. It's whitespace characters between tags. I reacted to suggestions how to work around it. I believe that the best way to avoid unwanted side effects is using proper tool. Floats and inline blocks are great, but they are not layout tools. The best clearfix is not using floats for layout. The best way to avoid spaces between inline-blocks is not using them for layout. And it's possible now. – Ilya Streltsyn Aug 08 '17 at 14:27
  • Then add @Marvin's explanation to your answer, crediting him. Comments aren't the place to be answering the questions. – Don Aug 08 '17 at 14:29
  • @Don, see the updated answer:) – Ilya Streltsyn Aug 08 '17 at 15:08