[Self-taught "pitfalls" encountered by CSS] inline-block gap, cssinline-block

Source: Internet
Author: User

[Self-taught "pitfalls" encountered by CSS] inline-block gap, cssinline-block

In the beginning, I want to set a navigation bar with the full width of the screen. The width of each sub-navigation bar is the same, and the width of the navigation bar is evenly divided, I added a background color to each sub-navigation ):

Then I started to write HTML code, which is very simple, as follows:

 1 <!DOCTYPE html> 2 

Then set the CSS code:

1 body{margin: 0 auto;}2 .main-nav{width:100%;font-size:20px;height: 30px;line-height: 30px;text-align: center;background-image:linear-gradient(black,#ccc,black) ;
margin: 0;padding: 0;}3 .main-nav li{display: inline-block; list-style:none;height: 100%;width:25%;}

The result is as follows:

Li accounts for 25% of the width. Shouldn't it be tiled like that? There is a gap between li. I thought it was caused by margin or padding. I set the margin and padding of the li element to 0 and found that there is no egg.

Then we learned about the various types of materials: blank characters such as line breaks, space characters, and tabs in HTML. When the font size is not 0, the blank characters occupy a certain width, using inline-block will generate gaps between elements. You can use word-spacing to set negative values, but the gaps will change with the font size. When the font size changes, the value of word-spacing must also be reset; or the float method is simpler. For my problem, set. main-nav to {word-spacing:-10px;} Or. main-nav li to {float: left.

For more information about inline-block, we recommend that you refer to the script home "to analyze the inline-block attribute values of past and present".

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.