Multiple methods to remove inline-block elements from css

Source: Internet
Author: User
In the true sense, the horizontal inline-block elements are separated by line breaks or spaces. A very simple example is as follows:
<Input/> <input type = "submit"/>
The spacing is coming ~~
This problem also occurs when we use CSS to change the horizontal element of a non-inline-block to the inline-block level:
. Space a {display: inline-block; padding:. 5em 1em; background-color: # cad5eb ;}
<Div class = "space"> <a href = "#"> melancholy </a> <a href = "#"> calm </a> <a href = "##"> Hot Blood </a> </div>
Click here: inline-block spacing demo
This is the expected performance that complies with the specifications (if someone thinks it is a bug, it is too much () ay () oy ).
However, such spacing sometimes affects our layout or compatibility. What should we do if we need to remove it? The following describes N methods (please add one )!
2. Remove spaces in the method
The reason for the white margin between elements is the space between the tag segments. Therefore, remove the space in HTML and the natural margin will be available. Considering the readability of the code, it is obvious that writing a line is not advisable. We can:
<Div class = "space"> <a href = "#"> melancholy </a> <a href = "#"> calm </a> <a href = "##"> Hot Blood </a> </div>
Or:
<Div class = "space"> <a href = "#"> melancholy </a> <a href = "#"> calm </a> <a href = "##"> Hot Blood </a> </div>
Or use HTML annotations:
<Div class = "space"> <a href = "##"> melancholy </a> <! --> <A href = "#"> calm </a> <! --> <A href = "#"> Hot Blood </a> </div>
.
III. Use a negative value of margin
. Space a {display: inline-block; margin-right:-3px ;}
The size of the negative value of margin is related to the font and text size of the context. For details about the size value of the spacing, refer to "based on display: inline-block list layout "part 6 Statistical table:
For example, for a 12-pixel context MarginNegative value -3Pixel. Tahoma and Verdana are -4Pixel while Geneva is -6Pixel.
Due to the uncertainty of the external environment and the parent margin value of the last element, this method is not suitable for large-scale use.
4. Allow closed labels to take capsules
Handle the following:
<Div class = "space"> <a href = "#"> melancholy <a href = "#"> calm <a href = "#"> Hot Blood </ a> </div>
Note: to be backward compatible with browsers such as Internet Explorer 6 and Internet Explorer 7, the end (closed) label of the last list cannot be lost.
In HTML5, we directly:
<Div class = "space"> <a href = "#"> melancholy <a href = "#"> calm <a href = "#"> Hot Blood </ div>
Okay, although it seems a little strange, but this is OK.
You can click here: Remove inline-block element spacing without closed tags demo
5. Use font-size: 0
Similar to the following code:
. Space {font-size: 0;}. space a {font-size: 12px ;}
This method basically solves the gap between inline-block elements in most browsers (IE 7 and other browsers sometimes have a 1 pixel gap ). However, there is a browser, Chrome, which has the minimum font size limit by default, because, considering compatibility, we also need to add:
Similar to the following code:
. Space {font-size: 0;-webkit-text-size-adjust: none ;}
Click here (a simple demo created last year): font-size: 0 clear line break demo
Supplement:According to Mr. Du's remarks, Chrome has now removed the minimum font limit. Therefore -Webkit-text-size-adjust: none;The code may not be long enough.
6. Use letter-spacing
Similar to the following code:
. Space {letter-spacing:-3px;}. space a {letter-spacing: 0 ;}
According to my tests last year, this method can basically deal with all browsers, including eating "East shoes", "Xidu (capsule)", and "Southland (Gully oil) "," North calcium (Sanlu) "IE6/IE7 browser, but Opera browser has the problem of egg pain: the minimum spacing is 1 pixel, then, Letter-spacingIt will be restored soon.
7. Use word-spacing
Code similar to the following:
. Space {word-spacing:-6px;}. space a {word-spacing: 0 ;}
One is the character spacing ( Letter-spacing) One is the word spacing ( Word-spacing. According to my tests, Word-spacingAs long as the negative value of is large to a certain extent, the difference in compatibility can be ignored. Because, looks like, Word-spacingEven if the negative value is large, it does not overlap.
Click here: word-spacing and element spacing removal demo
The effect is the same as that of the demo above. The screenshot is not displayed here. If you use Chrome, you may see that the spacing still exists. This problem exists because I am not clear, but I know that you can add Display: table;Or Display: inline-table;It also makes Chrome better.
. Space {display: inline-table; word-spacing:-6px ;}
VIII. Other finished product methods
The following shows how to use YUI 3 CSS Grids Letter-spacingAnd Word-spacingTo remove the grid unit, see The interval method (note that it is aimed at the horizontal element of the block, so the IE8-browser is hack processed ):
. The yui3-g {letter-spacing:-0.31em;/* webkit */* letter-spacing: normal;/* IE <8 reset */word-spacing:-0.43em; /* IE <8 & gecko */}. the yui3-u {display: inline-block; zoom: 1; * display: inline;/* IE <8: forged inline-block */letter-spacing: normal; word-spacing: normal; vertical-align: top ;}
The following is a method provided by RayM:
Li {display: inline-block; background: orange; padding: 10px; word-spacing: 0;} ul {width: 100%; display: table; /* call webkit */word-spacing:-1em ;}. nav li {* display: inline ;}
That is, the combination of the above series of CSS methods.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.