Inline-block make whitespace between labels

Source: Internet
Author: User

HTML Markup

<ul>  <li>item1</li>  <li>item2</li>  <li>item3</li>  <li>item4</li>  <li>item5</li></ul>

CSS Code

*{  margin:0;  padding:0;} UL {  list-style:none outside none;  padding:10px;  Background:green;  Text-align:center;} UL li {  display:inline-block;  *display:inline;  zoom:1;  Background:orange;  padding:5px;}

Above the demo effect, it is obvious that there is a "4px" gap between the elements of Inline-block:

The above is: ie8-9, Firefox, Safari and other browser effects, in other words, this is only in these browsers will appear. Let's talk about several ways to solve this "4px" (8px under Chrome):

Method One: Change the HTML structure

The simple way is to change the structure of the HTML, you can use any of the following methods can achieve the effect:

Structure One:

<ul>  <li>  item1</li><li>  item2</li><li>  item3</li> <li>  item4</li><li>  item5</li>  </ul>

This approach is close to the way the label wraps and is closer to reading.

Structure two:

<ul>  <li>item1</li  ><li>item2</li  ><li>item3</li  ><li>item4</li  ><li>item5</li>  </ul>

Structure Two and structure one is almost the same, the ">" of the end tag becomes the starting label of the other line.

Structure Three:

<ul>  <li>item1</li><!--  --><li>item2</li><!--  --><li >item3</li><!--  --><li>item4</li><!--  --><li>item5</li>  </ul>

The method of structure three uses the method of HTML annotation, which I think people are not very common, but also can solve the problem that we need to solve.

Structure four:

<ul><li>item1</li><li>item2</li><li>item3</li><li>item4</li ><li>item5</li></ul>

Structure Four, I think is commonly used to solve such problems, let's look at the following methods to write the effect:

One way to do this is by labeling the solution, although the problem is solved, but can be said not to be a skill. And the above method is only suitable for writing static pages, once your HTML is not your own writing, but background generation, such as CMS, tag background generation, at this time, I think we have to squalling again, this can do? In fact, in addition to the above method, we can also use CSS to solve.

Method Two: Negative margin

Many places discuss the use of negative margin to solve, for example:

    UL {        font-size:12px;    }    UL li {        margin-right: -4px;        *margin-right:0;    }

This workaround is not perfect, and if your parent element is set to a different font size, your " -4px" may not solve the problem. Besides, you need to set a negative margin value in chrome to achieve the same effect.

Of course, some articles are described using " -0.25em" to solve, which is also with the element's font size has a great relationship. so I personally recommend not using negative margin to solve this problem.

Method Three: Set the parent element font to 0

The third method sets the parent element's font to "0", and then resets the font size required on the "Inline-block" element.

UL {  list-style:none outside none;  padding:10px;  Background:green;  Text-align:center;    font-size:0px;    } UL li {  display:inline-block;  *display:inline;  zoom:1;  Background:orange;  padding:5px;  font-size:12px;}

This is done in browsers such as Firexfox,chrome, but there are still problems with Safari:

In this case, the method of three is not a good way to use, do not have to consider the people themselves.

Method four: Missing end tag

To be honest, this approach goes back to method one and hands on the HTML tag. is to let the "inline-block" element lose the Close tag

<ul>  <li>item1  <li>item2  <li>item3  <li>item4  <li> Item5</ul>

The style is basically the same, let's look at the effect:

Although this method can achieve the compatibility of each browser, but there is a premise, that is, "DOCTYPE" to choose the right, in the "XHTML" can be the problem again.

Method Five: JQuery method

Finally, we offer you a way to jquery:

HTML Markup

<ul class= "Removetextnodes" >  <li>item1</li>  <li>item2</li>  <li> item3</li>  <li>item4</li>  <li>item5</li></ul>

CSS Code

UL {  list-style:none outside none;  padding:10px;  Background:green;  Text-align:center;  font-size:12px;} UL li {  display:inline-block;  *display:inline;  zoom:1;  Background:orange;  padding:5px;}

JQuery Code

    $ ('. Removetextnodes '). Contents (). filter (function () {    return this.nodetype = = = 3;    }). Remove ();

The last method is to change the value of "NodeType" through jquery to achieve the effect we need.

There are a number of ways to do this, but to be compatible with multiple browser versions, not every method is practical, and the previously used method, this test is not compatible with all browsers. In order to be compatible with all browsers, the individual believes that the HTML tag to do some processing, or the last method, through "jQuery" to change the "nodeType" value to achieve the effect. For this "inline-block" between the spacing there are several articles have been introduced, but the method, and the above described in the basic test, the specific how to use, we still according to their own needs to choose or deal with.

So about the "inline-block" How to remove the spacing, today this is the case, I hope this article to like to use inline-block children's shoes to help. If you have a better idea, remember to share it with us, and feel free to leave your experience in the comments below, or correct the mistake above.

Update: fully compatible style resolution

After the expert pointing, using pure CSS or found a compatible method, is to set font-size:0 in the parent element, to be compatible with chrome, and use letter-space:-n px to compatible with Safari:

. finally-solve {  letter-spacing: -4px;/* Depending on the font size may need to make some adjustments  */word-spacing: -4px;  font-size:0;}. Finally-solve li {  font-size:16px;  Letter-spacing:normal;  Word-spacing:normal;  Display:inline-block;  *display:inline;  Zoom:1;}

Inline-block make whitespace between labels

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.