解决inline-block元素的 bug
来源:互联网 发布:千岛湖 蓝色天使 知乎 编辑:程序博客网 时间:2024/06/05 15:32
在使用inline-block时,有时候出现的效果莫名奇妙,例如:
* 两个inline-block 元素之间如果有空格、回车、tab,那么在页面上就有一个空隙
* 两个不同高度的 inline-block 元素顶部无法对齐,或者使用inline-block下面无缘无故多出几像素
例子1,出现空隙
<!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.去掉空格
<!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 设置为 14px
<!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
<!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(该属性增加或减少字符间的空白(字符间距))
<!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 (该属性增加或减少单词间的空白(即字间隔))
<!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>
解决效果:
例子2,设置inline-block 后,莫名其妙出现一些空白
<!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-align
<!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>
解决效果
例子3,两个不同高度的 inline-block 元素顶部无法对齐
<!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-align
<!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>
解决效果
阅读全文
0 0
- 解决inline-block元素的 bug
- 解决inline和inline-block元素的默认间距问题
- Firefox Bug: inline/inline-block的间隙
- 如何解决inline-block元素的空白间距
- 如何解决input,inline-block元素间的空隙
- 如何解决inline-block元素的间距问题?
- 如何解决inline-block元素之间空格
- 解决inline-block元素默认间距
- block元素和inline元素的区别
- 去除inline-block元素间的空隙
- inline-block元素的空白间距解决方法
- inline-block元素的空白间距解决方法
- inline-block元素间的间隙问题
- inline-block元素间的间隙问题
- 去除inline-block元素间的空隙
- 去除inline-block元素间距的方法
- inline-block元素间隙的解决方法
- 如何去除inline和inline-block元素的空白间距
- eclispe配置dubbo时xml文件报错:cvc-complex-type.2.4.c: The matching...for element 'dubbo:application'.
- Flume --文件通道(file channel)
- 关于vs Code debug node.js
- 无名 2
- 火狐浏览器兼容
- 解决inline-block元素的 bug
- 视图
- HDU2685(gcd的定理)
- win8安装PCL1.8.1配置VS2017
- 将本地文档库提交到Github
- LintCode:M-单词切分
- JDBC中的四个最基本对象功能及其用法
- 23种设计模式-jvm-linux-nio-psb-集合-线程
- ZOJ3609 Modular Inverse (扩展欧几里得)