웹아틀리에 - Web atelier
[CSS] inline-block 공백 지우기!! 본문
와 굉장하네요.. 이런 방법이 있었을 줄은...
이제 더이상 inline-block의 공백때문에 속이 터지지 맙시당.. ㅎㅎ
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.
Here's the deal: a series of inline-block
elements formatted like you normally format HTML will have spaces in between them.
In other words:
<nav>
<a href="#">One</a>
<a href="#">Two</a>
<a href="#">Three</a>
</nav>
nav a {
display: inline-block;
padding: 5px;
background: red;
}
Will result in:
We often want the elements to butt up against each other. In the case of navigation, that means it avoids the awkward little unclickable gaps.
This isn't a "bug" (I don't think). It's just the way setting elements on a line works. You want spaces between words that you type to be spaces right? The spaces between these blocks are just like spaces between words. That's not to say the spec couldn't be updated to say that spaces between inline-block elements should be nothing, but I'm fairly certain that is a huge can of worms that is unlikely to ever happen.
Here's some ways to fight the gap and get inline-block elements sitting directly next to each other.
#Remove the spaces
The reason you get the spaces is because, well, you have spaces between the elements (a line break and a few tabs counts as a space, just to be clear). Minimized HTML will solve this problem, or one of these tricks:
<ul>
<li>
one</li><li>
two</li><li>
three</li>
</ul>
or
<ul>
<li>one</li
><li>two</li
><li>three</li>
</ul>
or with comments...
<ul>
<li>one</li><!--
--><li>two</li><!--
--><li>three</li>
</ul>
They're all pretty funky, but it does the trick.
#Negative margin
You can scoot the elements back into place with negative 4px of margin (may need to be adjusted based on font size of parent). Apparently this is problematic in older IE (6 & 7), but if you don't care about those browsers at least you can keep the code formatting clean.
nav a {
display: inline-block;
margin-right: -4px;
}
#Skip the closing tag
HTML5 doesn't care anyway. Although you gotta admit, it feels weird.
<ul>
<li>one
<li>two
<li>three
</ul>
#Set the font size to zero
A space that has zero font-size
is... zero width.
nav {
font-size: 0;
}
nav a {
font-size: 16px;
}
Pre-Jellybean does not remove the space at all, and Jellybean has a bug whereby the last element randomly has a tiny bit of space.See research.
font-size
to bump it back up.#Just float them instead
Maybe they don't need to be inline-block at all, maybe they can just be floated one way or another. That allows you to set their width and height and padding and stuff. You just can't center them like you can by text-align: center;
the parent of inline-block
elements. Well... you kinda can but it's weird.
#Just use flexbox instead
If the browser support is acceptable to you and what you need out of inline-block is centering, you could use flexbox. They aren't exactly interchangeable layout models or anything, but you might get what you need out of it.
#See
Example on CodePen:
'IT-웹 Front end > CSS' 카테고리의 다른 글
[CSS]크롬 input 태그등에 자동생성 백그라운드 색 제거하는 방법 (0) | 2016.06.30 |
---|---|
[CSS3]하드웨어 가속에 대한 이해와 적용. 3D 속성! (0) | 2016.05.17 |
[CSS]중앙 정렬! (0) | 2016.05.12 |
[CSS]<input type="number"/>의 버튼을 지우는 방법! (2) | 2016.05.03 |
줄바꿈 규칙을 바꿔주는 CSS! word-break (0) | 2016.04.26 |