在使用inline-block时,有时候出现的效果莫名奇妙,例如:html
- 两个inline-block 元素之间若是有空格、回车、tab,那么在页面上就有一个空隙
- 两个不一样高度的 inline-block 元素顶部没法对齐,或者使用inline-block下面平白无故多出几像素
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> <style> div{ display: inline-block; width:100px; height: 100px; background-color: rgb(233, 148, 148); } </style> </head> <body> <div></div> <div></div> </body> </html>
1.去掉空格spa
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> <style> div{ display: inline-block; width:100px; height: 100px; background-color: rgb(233, 148, 148); } </style> </head> <body> <div></div><div></div> </body> </html>
2. 添加父元素,将父元素的 font-size 设置为0,而后在 inline-block 元素中将 font-size 设置为 14pxcode
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> <style> .parent{ font-size:0; } .child{ font-size:14px; display: inline-block; width:100px; height: 100px; background-color: rgb(233, 148, 148); } </style> </head> <body> <div class="parent"> <div class="child"></div> <div class="child"></div> </div> </body> </html>
3. 使用margin-right
htm
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> <style> .child{ display: inline-block; width:100px; height: 100px; background-color: rgb(233, 148, 148); margin-right:-5px; } </style> </head> <body> <div class="child"></div> <div class="child"></div> </body> </html>
4. 添加父元素,使用letter-spacing(该属性增长或减小字符间的空白(字符间距))utf-8
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> <style> .parent{ letter-spacing:-5px; } .child{ display: inline-block; width:100px; height: 100px; background-color: rgb(233, 148, 148); } </style> </head> <body> <div class="parent"> <div class="child"></div> <div class="child"></div> </div> </body> </html>
5. 使用word-spacing (该属性增长或减小单词间的空白(即字间隔))ci
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> <style> .parent{ word-spacing:-5px; } .child{ display: inline-block; width:100px; height: 100px; background-color: rgb(233, 148, 148); } </style> </head> <body> <div class="parent"> <div class="child"></div> <div class="child"></div> </div> </body> </html>
<!doctype html> <html> <head> <meta charset="utf-8"> <title>span设为inline-block以后下面的空白</title> <style> div{ border:solid 1px rgb(202, 43, 43); width:250px; } span{ display:inline-block; width:200px; height:200px; background-color:rgb(109, 195, 252); } </style> </head> <body> <div> <span></span> </div> </body> </html>
使用vertical-alignrem
<!doctype html> <html> <head> <meta charset="utf-8"> <title>span设为inline-block以后下面的空白</title> <style> div{ border:solid 1px rgb(202, 43, 43); width:250px; } span{ display:inline-block; width:200px; height:200px; background-color:rgb(109, 195, 252); vertical-align:top;//新增 } </style> </head> <body> <div> <span></span> </div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> <style> .child1{ display: inline-block; width:100px; height: 100px; background-color: rgb(109, 195, 252); } .child2{ display: inline-block; width:100px; height: 120px; background-color: rgb(233, 148, 148); } </style> </head> <body> <div class="child1"></div> <div class="child2"></div> </body> </html>
仍是使用vertical-alignit
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> <style> .child1{ display: inline-block; width:100px; height: 100px; vertical-align:top;//新增 background-color: rgb(109, 195, 252); } .child2{ display: inline-block; width:100px; height: 120px; background-color: rgb(233, 148, 148); } </style> </head> <body> <div class="child1"></div> <div class="child2"></div> </body> </html>