C3的坑之inline-block

本文探讨了使用CSS的inline-block布局时遇到的间距问题,并提供了解决方案,包括使用负margin和调整父元素的font-size属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近开始复习css一直在踩坑,今天分享一个inline-block

关于inline-block可能很多人都不熟悉,布局这方面很多人用的都是flex或者浮动,flex很强大毋庸置疑的可是关于兼容性就不是很让人满意,而浮动虽然很兼容可是觉得清除浮动就很麻烦,于此我在一些大型网站,例如我们的segmentfault的首页导航展示用的布局就是inline-block,觉得inline-block可以撸一波,可是突然发现这里面也存在一些小问题,首先先po出代码
<div style="background-color:green">
      <div style="width:40px;height:30px;background-color:red;">

      </div>
      <div style="width:40px;height:30px;background-color:red;">

      </div>
      <div style="width:40px;height:30px;background-color:red;">

      </div>
</div>

clipboard.png
这时候我们采取inline-block进行布局,神奇的事情就发生了

<div style="background-color:green;">
      <div style="width:40px;height:30px;background-color:red;display:inline-block;">

        </div>
      <div style="width:40px;height:30px;background-color:red;display:inline-block;">

        </div>
      <div style="width:40px;height:30px;background-color:red;display:inline-block;">

        </div>
    </div>

clipboard.png

原本的3个div并排在一起,因为3个div变成了行内元素,所以也应该并排在一起,但是....
这里出现了两个问题:

  1. div之间出现间隔clipboard.png
  2. 子div与父div之间有一个4px的间距clipboard.png
这里会出现这种问题的原因是因为 inline-block水平呈现的元素间,换行显示或空格分隔的情况下会有间距

div之间的间距解决的方法

在这里我尝试过一个方法就是设置margin-right为负值,

<div style="background-color:green">
      <div style="width:40px;height:30px;background-color:red;display:inline-block;margin-bottom:-4px;margin-right:-4px">

      </div>
      <div style="width:40px;height:30px;background-color:red;display:inline-block;margin-bottom:-4px;margin-right:-4px">

      </div>
      <div style="width:40px;height:30px;background-color:red;display:inline-block;margin-bottom:-4px;margin-right:-4px">

      </div>
    </div>

clipboard.png

在这里我们能看到第一个div与第二个div仍然有一个1px的间距,而第二个和第三个没有间距,子div和父div没有间距,说明我们的问题不是在这里

我在参考张旭鑫大神的博客深感佩服觉得采用这种方法最好:

给父div加这样的css属性

font-size:0
-webkit-text-size-adjust:none;

po出代码

<div style="background-color:green;font-size:0;-webkit-text-size-adjust:none;">

        <div style="width:40px;height:30px;background-color:red;display:inline-block;">

        </div>

        <div style="width:40px;height:30px;background-color:red;display:inline-block;">

        </div>

        <div style="width:40px;height:30px;background-color:red;display:inline-block;">

        </div>

clipboard.png

那么又有一个问题了,-webkit-text-size-adjust是什么????

webkit内核的浏览器(chrome)中,当在css中定义的中文font-size小于12px的时候,浏览器仍然使用12px,这时就可以用-webkit-text-size-adjust:none;

哈哈,这样我们的问题就解决了


最后说下inline-block的兼容性:参考这篇文章
-->
IE6、IE7不识别inline-block但可以触发块元素。其它主流浏览器均支持inline-block。这就够了,ie6,7就不考虑了,原因嘛,你懂的...

.menu-preview .el-scrollbar[data-v-58274818]{height:100%}.menu-preview .el-scrollbar[data-v-58274818] .scrollbar-wrapper{overflow-x:hidden}.menu-preview .el-menu-horizontal-demo .el-menu-item[data-v-58274818]{cursor:pointer;padding:0;margin:0;color:#fff;white-space:nowrap;display:flex;font-size:16px;border-color:#ffc800 #ffc800 #ffc800 #ffc800;line-height:56px;border-radius:0;background:none;width:120px;justify-content:center;border-width:0;position:relative;border-style:solid;list-style:none;text-align:center;height:56px}.menu-preview .el-menu-horizontal-demo .el-menu-item.is-active[data-v-58274818],.menu-preview .el-menu-horizontal-demo .el-menu-item[data-v-58274818]:hover{cursor:pointer;padding:0;margin:0;color:#fff;white-space:nowrap;font-size:14px;border-color:#ffa100 #ffa100 #ffa100 #ffa100;line-height:56px;border-radius:0;background:#0b4887;width:120px;border-width:0;position:relative;border-style:solid;list-style:none;height:56px}.banner-preview .el-carousel[data-v-58274818] .el-carousel__indicator button{width:0;height:0;display:none}.banner-preview .el-carousel[data-v-58274818] .el-carousel__container .el-carousel__arrow--left{width:36px;font-size:12px;height:36px}.banner-preview .el-carousel[data-v-58274818] .el-carousel__container .el-carousel__arrow--left:hover{background:#2087c3}.banner-preview .el-carousel[data-v-58274818] .el-carousel__container .el-carousel__arrow--right{width:36px;font-size:12px;height:36px}.banner-preview .el-carousel[data-v-58274818] .el-carousel__container .el-carousel__arrow--right:hover{background:#2087c3}.banner-preview .el-carousel[data-v-58274818] .el-carousel__indicators{padding:0;margin:0 0 12px 0;z-index:2;position:absolute;list-style:none}.banner-preview .el-carousel[data-v-58274818] .el-carousel__indicators li{border-radius:50%;padding:0;margin:0 4px;background:#fff;display:inline-block;width:12px;opacity:.4;transition:.3s;height:12px}.banner-preview .el-carousel[data-v-58274818] .el-carousel__indicators li:hover{borde
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值