[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".