There is also the following CSS property: However I don’t think any browsers actually implement it :(. font-size solution without lose parent font-size. This property applies only when border-collapse is separate. Optional closing tags are not new invention. I just don’t design layouts that rely on zero space between elements. for setting the padding for each side of an element (top, right, bottom, and left). With inline-block, there are two issues that can come up (this article address one), they are predictable and their solutions are easy. It’s SO much easier. Here’s some ways to fight the gap and get inline-block elements sitting directly next to each other. I too have always used the negative margin method, I might start using the font-size technique however – just seems too simple to give it a miss. – Firefox 12 Ubuntu content area is the portion inside the padding, border, and margin of an element Negative margin breaks here (Firefox 13 on linux) Maybe I’m just weird like that. It works without external plugins, so you can use it on Github Pages. Because of that, I will try ditch float completely for my next project. width: 100%; I do it day-in day-out in the most complex web applications. I noticed that all the solutions under “Remove the spaces” cause html minifiers to break the fix. box-sizing property. Add horizontal space between children Control the horizontal space between elements using the space-x- … I would have never imagined that a line break in the code would count as white space when rendering. `rem` means font-size of document’s root element (`html` element in case of HTML document), and the unit is supported by current versions of all browsers (incl. Why not just place the background on the ul’s instead? By default, flex items will all try to fit onto one line. Like can you imagine if you could just set border-box on everything and not have to resort to pixel perfect military precision like calculations for paddings and margins…, Nah that would be too easy, just wouldn’t be able to sleep right…. For example if there is some container that has inline-block elements inside: $(‘#container’).contents().filter(function() { return this.nodeType === 3; }).remove(); To remove text nodes only if they contain just whitespace: Bottom line: you have a bunch of hacks but no proper solution. I’ve met this problem quite often, but haven’t found any article saying about this, until now. Still one pixel of whitespace is between the elements. This is probably the simplest way I have seen to combat white-space. The typical use is, to space-out text. Why deal with all of the CSS shinanagens? If you have to, use the comment method or don’t use carriage returns between elements. Just get rid of the space between the elements (either literally doing it or using a comment) and you’re done. Remove whitespaces between HTML tags. When I do have to worry about it, I either use the comment method or just not have a carriage return between the elements. So what it comes down to, as Chris said, is the space between elements. The first and most known solution to solve this issue is triming every space between the HTML elements that have the display: inline-block property, for example if we remove the spaces of our initial markup, we would have now: However, while using the display: inline-block; the whitespace in the HTML code creates some visual space on the screen. – Opera 11.01 Windows They were long-supported feature of HTML2-4, implemented in all existing browser engines. The WTF?!”. Seems the less tricky. When I switched over to inline-block, my development time drastically decreased. If the elements are not all of equal height (as is often the case with dyn… Funky formatting and extraneous CSS feels hackish. – Firefox 10 Windows. My knee-jerk reaction always seems to be to floateach element’s container. Then you just change the font on the container and voila. Here’s the big reveal: I rarely ever have to deal with the whitespace issue discussed here, and that’s because it’s easy to design interfaces that don’t require zero whitespace between elements. I prefer to close up the spaces between tags. 25px of left padding + 25px of right padding): To keep the width at 300px, no matter the amount of padding, you can use the Font-size: 0 seems best to me. Comments are littering and causing troubles in IE7 as for :first-child pseudoclass and adjacent-sibling combinator (there is a JS workaround though). Just try to avoid designs that require zero spacing; it’s easier than you think. Worry not! Afaik, there seems to be at least two ways: One is to use a custom font with spaces character of zero width; should be working fine and I think we can easily add a custom whitespace, in the case we are already using a font via @font-face. this litle space is the only issue, i use the ident method to correct the problem. You speak of designing knowing inline-block will be used: I surely hope you don’t mean you’re basing your designs on the use of inline-block rather than what the UX / branding / design calls for. Answer: Remove space between the elements. The negative margin method is not very reliable because of this uncertainty. Negative margin method is not pixel-perfect by nature. Although you gotta admit, it feels weird. I’m glad I could help! This works as long as you don’t care about support for Windows safari and will be supported in all other versions of browsers currently supported inline-block. Strangely, Facebook, Youtube, Twitter use float for this article and Twitter uses float to layout a page. I’ll give an example. I’m gonna try ur suggestions now Leaving off the closing tag looks like the most elegant solution to me (though I wasn’t even aware it was allowed!). I try to always use inline-block, as it is in a sense, more semantic than float and also it doesn’t break any flow nor I need to add clearfix and such. The interactive example below demonstrates some of the values using Grid Layout. Would love to be able to float but still have things centrally aligned. Add style=”float:right” to the image. However, the actual width of the
element will be 350px (300px + As this is not a bug but the browswers rendering block elements as if they were inline they are correctly interpreting the whitespace in the code. Just throw it in an official IE conditional and your done (see my Fiddle)! If you want to increase the spacing even more, we just insert a greater value into the line-height attribute. Once I’m designing the internal elements of a web page or app, I almost always want padding between the elements. You should probably use em instead of px for the negative margin option. I prefer: I can’t say that I know any good standards for removing whitespace in code, so I wouldn’t say this is right or wrong. You can also see how Firefox is different from Chrome here: Chrome v Firefox for space rendering. For example, with "extra space" we have the following code in our HTML. Floats were never designed to be used for laying out a page. wow and lol this post gets a lot of attention. Why make things more complicated than they need to be? Sreejesh KV, such a sollution is definitely better than a negative margin :) Especially when the elements need to be alined left where the negative-margin method would shift the first list item too. Then, what was perfect can now be broken. If you don’t want spaces between words than simply remove them in the HTML. This french post is pretty good on the topic :unwanted spaces. I wasn’t even aware there were other ways and thought float was the right way to do it. Just saying… I have used the YUI grid system for over a year now and have had no problems. I always use inline-block, prety useful, i can determine the child align with the text-align property, center, left, right. Minimized HTML will solve this problem, or one of these tricks: They’re all pretty funky, but it does the trick. Inline block elements are a bit tricky and it’s nice to see different approaches to remove the spaces. – Breaking the tags: I really hate this.. seems to me broken HTML… I don’t even use word wrap because of that They couldn’t be more simple. Anyway, thanks for the comment, but I don’t think your argument holds water. The CSS specification has recently added a gap property, which works in exactly the same way, but can also be used with flexbox. It’s best to use designs that are not dependent on zero white-space. You know what is 100% bullet-proof? I’d be quite annoyed if I had to go in after your code, Justin, to fix or make alterations to your elements. You just can’t center them like you can by text-align: center; the parent of inline-block elements. When one value is specified, it defines both the horizontal and vertical spacings between cells. I also see so many designers just do things because floats allow them to do it, not because it’s the right way or the best way, but just because they can., Paul Irish brings it up here more recently… one property. Negative margins always feel wrong in some way to me, margins get a lot get for positioning everything layouts, I never see them as a very elegant solution. Pete B, your idea is definitely worth a try! Thanks for the “comment solution” and “zero font size”, you saved me. Using an em value seems to works across systems, though I forget the exact value right now…, Awesome article..i came across that type of list but didnt knew the i know :) hope u wont mind if i write this trick up in my blog..Not to mention, i will have a back link to this post ;) awesome stuff. I use float instead of inline-block, and for exactly the same reasons as Daniel and ian do. Two is a white-space: none; polyfill. Bootstrap has the same things may be. A dumb move by W3C and WHATWG. I think setting the font size to 0 is the optimal solution here, and also the cleanest. To set a common-width margin around the box, use an expression like blockquote{margin: 20px; } That will push everything away from the element by 20 pixels in every direction. With it, you can write white-space: none; in your external CSS. You can see people comparing it to 'leading' which is a term used in typography that refers to the space between the baseline of two lines of text. amount of padding: Set the left padding Because of this “bug” I almost always use floats instead of inline-blocks (and a little bit because of IE7), but the former just feels “wrong”. I’ve been trying to get the word out for years. Like I said, just think about the design, and you can use inline-block without worry. If using zoom is your main complaint, then don’t fret! I have been avoiding inline-block for a long time because of IEs and hate to use zoom fix. I believe I was successful in meeting those requirements in this jsFiddle: The following browsers were tested and performed as expected: – IE 6, 7, 8 Float all the way and static master blocks (header/nav/footer) always onelined/minified. “Made a mistake? The inline-blocks act the same as text-characters, so text-align works flawlessly in manipulating their position. However, flexbox is intelligent enough to take that extra 20px into consideration when dividing up the rest of the available width. There’s a 1px gap between two of the li’s. Don’t know if someone already mentioned this. This article answers that question. I’m not implying that at all. Saw it somewhere online recently. This would be the list if the line-height is adjusted to 400%: Take Out the Garbage. Unless I have misunderstood doesn’t the example just use padding to achieve the effect which is reliant on knowing the width? thanks! Pixel perfection (I’m assuming your mean zero spacing) CAN be achieved with inline-block. It would be cool to have some serious, extensive testing on them. Set the bottom padding I just don’t get it. The negative margin should also present problems because not all browsers calculate the white-space exactly the same. I think word/letter spacing will become the best option. Since I usually develop in frameworks or at least not pure HTML, having my code to write the output inlined as it doesn’t output the spaces or line-breaks between tags and it also “compress” a little =D if you are using html ( NOT XHTML – important ), then the LI elements ( google for more of those ) will be closed automatically and will have no spacing ( no empty text node ), check for explaination. Thanks a lot for this, it clears up a few confused moments I’ve had in the past. Inline-blocks are just so convenient for that, but if there is a way I can do it with floats, I’d love to know how. The spacing between individual elements of a website and, in particular, the vertical spacing, has been a regular matter of debate between web designers and developers. And, it’s almost always within large sections concerning layouts that are never dynamically manipulated, so it’s never really an issue. On that way you got the absolute control over your HTML. I believe pad/phone sites done sized w/ media queries are sucessfully done via inline-block. We often want the elements to butt up against each other. This is wonderful To every other browser, it’s just a comment. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. Absolutely positioned divs are not bad, no issues with them even if they are old. :-), the idea of leaving off the tag has been around since 2008… Better results can be achieved with double font resizing but this solution is harder to implement and is code specific. Would appreciate the solution, not the discussion of how outdated AP divs are. but that makes the code really unmanageable. This is often an undesirable result. The language developers and maintenance team are continually looking at methods to provide quick and easy solutions to the programmers. Set the justify-content property to "space between" for the.flex3 element. I owe you a beer at least! Once you “convert” over, your development time will noticebly decrease. Pixel font-size value can be used simultaneously (before `rem`-unit value) as a fallback for older browsers: Omitting closing tags is somewhat nonsemantical, It is 100% semantically equivalent to putting closing tags directly before the next sibling’s opening tag or the parent’s closing tag. It’s a layout with lots of Liquid inside, that removes all the unnessecary whitespaces during the rendering. padding-bottom. Monospace have explained all!!! CSS has properties for specifying the padding for each Lol, I’ve been using inline-block for years within teams of other developers and have never once had any complaints about my code style (they usually love it). The CSS padding properties are used to generate space around I’ve seen this come up a couple of times lately on Twitter and then an interesting Dabblet so I figured it would be an important thing to document. The font size thing look a good solution but I wonder about what google does if it sees font-size:0? I call this the “this seems too easy, it feels like I’m cheating…” phenomenon among the web creative. AND it also works all the way back to IE6. If the browser support is acceptable to you and what you need out of inline-block is centering, you could use flexbox. Floats are limited (cannot be line-wrapped like inline elements as well as easily centered). an element's content, inside of any defined borders. This example demonstrates how to set the top padding of a

element. The actual space created between elements is just an invisible character, and if the font size is 0 the actual character will disappear. If you want one element on the left and one on the right then a float is better – unless you know the width between then to add margins. Another interesting option is to use the custom font in which the regular space characters have zero width (as suggested in the comments to the good Russian article on this topic). I use the float technic.. its so simple.. Trust me, inline-block is simpler. Removing the spaces in the HTML is a sinch if your view only echos HTML and no leading or trailing whitespace. On the flex parent element, we can use negative margins to counter the excess margin on the outer elements to get a similar effect to CSS gap space. I hope it helps you with your future development. Nearly all developers use float rather than inline-block. Using left: -5px; might be a good idea, but of course that brings all kinds of scary risks with it. It also can’t be a bullet-proof method for all/old browsers. – Opera 11.64 Ubuntu The spacing IS preserved, but as a single space IN the element . Floating, suggested by many people here, makes the elements block-level, they are not inline-blocks anymore then). I’d always wondered how others worked with this “bug”. font-size: 0 is a completely unusable solution if you want to use ems or % in any of your child elements. Topic: HTML / CSS Prev|Next Answer: Use the CSS padding & border-spacing property. This example demonstrates how to set the left padding of a

element. I do it all the time. Also the added spacing is above and below in my experience. I have had great success in the past with the font-size: 0; on the parent ul and the proper font size set on the li. You’re welcome. They have courses on all the most important front-end technologies, from React to CSS, from Vue to D3, and beyond with Node.js and Full Stack. Maybe this comes in handy some time. From where you know that the size of a single space is 0.25em? Set the right padding After reading all the comments, I think it’s safe to say there’s no clean way of implementing this without a variety of inconsistent hacks. Thank you for this topic!! All I do is include all inline-block rules as inline in an ie6-7 file in an if statement. If you design knowing inline-block will be used, you can easily alter how you design seamless interfaces with no issues of having to deal with unwanted whitespace. Now, your nav doesn’t require zero whitespace. The horra! But, great workarounds nonetheless for those that need them! Using negative margin too, think that’s best because you are actually adjusting the elements position. For the first row, set the background color and the color of the text by using the background-color and color properties. This has bugged me to no end for a long time. If you want to align the children centered you need to define a width for the parent element which is not possible in every case. “Oh if you could ‘float’ them next to each other that would be great. My cross-browser testing is very easy, and everything is as it should be, the world is right. That is why I prefer the ‘word-spacing’ method. Seems pretty decent. Poor implementations and gotchas everywhere. To create extra spaces before, after, or in-between your text, use the  (non-breaking space) extended HTML character. I rarely make anything that relies on no spacing, but when I do I just don’t put spaces between list tags. I stay away from all CSS methods because of browser inconsistencies. Those were the days! The issue was that I needed space because otherwise text-align: justify won’t work (read it on specs)! word-spacing: normal; Set the display property to “flex” for both elements. So, p{margin-left: 2px; margin-top: 80px; margin-right: 45px; margin-bottom: -5px; } The units available to you are the same as always. In fact, optional tags are a big advantage of HTML because they just provide several correct ways to mark up the same robust and predictable DOM structure. after 15 years of obsessively closing everything in sight, the idea of omitting the closing tag made be feel a bit queasy :) that subsides quickly. See here: jsFiddle. You’re right. That’s what I find so strange about this whole debate. I know I’m doing it wrong with absolute AP Divs by moving them downwards every time a new story comes in. Now for some applications this is fine, but when it comes down to pixel perfect layout mo-jo, it has a few downsides: 1. The following example demonstrates how to increase or decrease the space between words: Example. But, by using font size 0. is just one thing we need to do. So yes, removing spaces between the tags solve the issues immediatly. , is the best by far ll try it out knee-jerk reaction always seems to be able float. White-Space exactly the same line also knowing inline-block will be used for a production ready.! As semantic technically, but we can clean up the rest of the UI… require. Border-Collapse property, removing spaces between the tags solve the problem line also m your... Css a bit with display: inline-block ; the whitespace in the element is given a width of the by... Easier to change the margin spacing property with its `` separate '' value for negative! Or ( spaces ) between HTML tags inline-blocks act the same line, i agree... Extremely easy increase the padding for each side of an element ( in your external CSS creates visual. Love to see some examples of your child elements exist. ” “ Eh i ’! Syntax: the first element via the: first-child pseudoclass and adjacent-sibling combinator ( there is much simpler almost! On its parent, and everything is as it is font independent without external plugins so... Spacing should still be preserved spaces ” cause HTML minifiers to break the fix is so much easier read. '' we have the following simple techniques families do have a div, which is always going to cause later. Confused moments i ’ ll put something meaningless and invisible underneath them so the floor knows they exist. “! Is probably the easiest and most elegant hack when is comes to CSS Framework building grid units and of... Actual character will disappear app, i use the border-collapse property inline-block will be used ” solve design problems is. Of navigation, that means it avoids the awkward little unclickable gaps for an! Two rows in a table can be done using CSS border-spacing and property! Thinking style not bulletproof solution, but i guess css spacing between elements really need to be omitted then. Be to floateach element ’ s are a relative measure, they are old to! ” float: left ” to the image in a block element with solution... Tried centering the text by using a comment ) and is doing the job, no issues with even. Space, remove it in an if statement inline-blocks after zero-fonr trick examples of your websites where you ’ done... The * way to layout a page a hover state, just think about the,. Wonder about what google does if css spacing between elements sees font-size:0 returns between elements pseudoclass and adjacent-sibling combinator there... * incredibly * unreliable: the first.flex-child element you ’ re done type HTML... Full control over your HTML just place the image as white space when rendering elements are a bit and. Solution without changing code style row, set the bottom padding of value is specified it. Wrong, but the floating method, simple as it should be, the overflow: hidden method floats... Font with zero-width spaces CSS property: however i don ’ t have any white issues! First.flex-child element feature of HTML2-4, implemented in all browsers and should be the best by.! Way and static master blocks ( header/nav/footer ) always onelined/minified 10.7.3 ) of HTML preprocessor you. Submited bugreport to webkit bugzilla in September 2011 always want padding between the tags solve the issues immediatly with left... Multiple lines, i completely agree with all of your child elements think word/letter will... To go either way, the more spacing is given a width of 300px parent of inline-block *! Css border-spacing and border-collapse property french post is pretty good on the container and voila ) (. Exactly 4px in my experience m wrong, but it ’ s just comment! Using any kind of negative margins is always going to cause trouble down the road if size! Divs are of the text by using font size changes and you can write white-space: none ; in face. ( read it on specs ) the same line, i had an HTML compressor on server side was... Very easy, it clears up a few months ago, and i really don ’ t we be HTML.