font-size: 0;解决inline元素间的空白间隙

本文介绍了一种使用 CSS 属性 font-size:0 来解决网页布局中因换行和缩进产生的空白问题的方法。适用于列表项和图片排布,保持页面整洁并提高用户体验。

在公司写页面的时候,会引用公司的一个公共css,其中给主体部分设置了font-size:0 这个属性。起初觉得有些不方便,因为有文字的部分都要去设置font-size。后来发现这个属性真的很好用!~

1. 比如我在写如下代码的时候:

<ul>
    <li>我是第一项</li>
    <li>我是第二项</li>
    <li>我是第三项</li>
    <li>我是第四项</li>
</ul>

设置css如下:

<style>
    ul {
        list-style: none;
    }
    li {
        width: 25%;
        display: inline-block;
        background: green;
        text-align: center;
        height: 40px;
        line-height: 40px;
    }
</style>

表面上看应该是出于同一行,没什么问题,但是效果其实是下面这样的:内容4掉了下来

我们为了页面代码的整洁可读性,往往会设置一些适当的缩进、换行,但当元素的display为inline或者inline-block的时候,这些缩进、换行就会产生空白,所以出现上述问题。虽然还有其他方法能解决我们因为缩进、换行而产生的问题,但此时,最合适的方法就是给li的父级ul设置: font-size: 0; 给li设置:font-size: 16px; 如此就达到了所需效果。

达到效果

2. 图片间的间隙问题。

其实图片的间隙问题也是因为我们的换行、缩进。

<div>
     <img src="pic1.jpg">
     <img src="pic2.jpg">
</div>

两张图片之间就会出现这样的间隙:
这里写图片描述

给这个div设置

div {
    font-size: 0;
}

图片之间的间隙就没有了:
木有间隙

<div class="container"> <div class="top"> <div class="top-left"> <div class="top-left-side"> </div> <div class="top-left-content"> <div class="logo"> <img src="/plugin_assets/polls/images/icon-company.png" alt="公司图标"> <div class="logo-text"> <div class="company-name">武汉光庭信息技术股份有限...</div> <div class="platform-name">数字化学习平台</div> </div> </div> <div class="nav"> <div class="nav-item"> <img src="/plugin_assets/polls/images/icon-home.png" alt="首页"> <span>首页</span> </div> <div class="nav-item"> <img src="/plugin_assets/polls/images/icon_study.png" alt="学习中心"> <span>学习中心</span> </div> <div class="nav-item"> <img src="/plugin_assets/polls/images/icon_shop.png" alt="知识商城"> <span>知识商城</span> </div> <div class="nav-item"> <img src="/plugin_assets/polls/images/icon_profile.png" alt="个人中心"> <span>个人中心</span> </div> </div> </div> </div> <div class="top-right"> <div class="search"> <label> <input type="text" placeholder="请输入想要的内容"> </label> <a class="search-icon">🔍︎</a> </div> </div> </div> <div class="bottom"> <div class="bottom-top-gap"></div> <div class="bottom-content"> <div class="bottom-left"> <div class="bottom-left-side"></div> <div class="bottom-left-content"> <div class="bottom-left-content-area"> <div class="carousel"> <!-- 轮播图内容 --> </div> </div> <div class="bottom-left-content-gap"></div> </div> </div> <div class="bottom-right"></div> </div> </div> </div> <style> * { box-sizing: border-box; margin: 0; padding: 0; } html, body { height: 100%; } .container { display: flex; flex-direction: column; height: 100vh; } .top { height: 10vh; background-color: #f0f0f0; border-bottom: 1px solid #ccc; display: flex; box-sizing: border-box; background-color: hsla(0, 0%, 100%, 0.6); } .top-left { flex: 0 0 66%; display: flex; align-items: center; } .top-left-side { flex: 0 0 30%; display: flex; align-items: center; } .top-left-content { flex: 0 0 70%; display: flex; align-items: center; gap: 20px; } .top-right { display: flex; align-items: center; justify-content: flex-start; } .logo { display: flex; align-items: center; } .logo img { width: 38px; height: 38px; margin-right: 16px; } .logo-text { display: flex; flex-direction: column; overflow: hidden; } .company-name { font-size: 18px; pointer-events: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; } .platform-name { font-size: 14px; color: #666; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; } .nav { display: flex; gap: 25px; justify-content: flex-start; } .nav-item { display: flex; flex-direction: row; align-items: center; white-space: nowrap; gap: 3px; font-size: 16px; color: #a9a9a9; text-decoration: none; transition: color 0.3s; } .nav-item img { width: 20px; height: 20px; flex-shrink: 0; display: block; } .nav-item:hover { color: #007bff; } .search { flex: 0 0 285px; position: relative; max-width: 400px; display: inline-block; } .search input { width: 100%; height: 30px; padding-left: 35px; font-size: 14px; border: 1px solid #759fcf; border-radius: 4px; box-sizing: border-box; } .search-icon { position: absolute; left: 0; top: 0; height: 100%; width: 30px; display: flex; align-items: center; justify-content: center; pointer-events: none; color: #888; font-size: 16px; background: transparent; border: none; } /*********************bottom****************************/ .bottom { flex: 1; display: flex; height: 90vh; flex-direction: column; background-color: rgba(242, 242, 242, 0.3); } .bottom-top-gap { flex: 0 0 5%; display: flex; align-items: center; justify-content: center; } .bottom-content { display: flex; width: 100%; box-sizing: border-box; gap: 0; } .bottom-right { flex: 1 1 auto; min-width: 0; box-sizing: border-box; border-left: 1px solid #ccc; } .bottom-left { flex: 0 0 67%; display: flex; flex-direction: row; box-sizing: border-box; gap: 0; } .bottom-left-side { flex: 0 0 25%; min-width: 25%; background-color: #f0f0f0; display: flex; align-items: flex-start; justify-content: flex-start; box-sizing: border-box; padding: 20px; } .bottom-left-content { flex: 1; width: 100%; display: flex; align-items: center; padding: 0 20px; box-sizing: border-box; } .bottom-left-content-area { flex: 1; display: flex; align-items: center; padding: 0; border-left: 1px solid #ccc; border-right: 1px solid #ccc; flex-direction: column; height: 90vh; } .bottom-left-content-gap { flex: 0 0 0.08%; display: flex; } .carousel { background-color: rgba(255, 255, 255, 1); border: 1px solid rgba(242, 242, 242, 0.5); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); width: 100%; height: 50vh; overflow: hidden; display: flex; align-items: flex-start; justify-content: flex-start; padding: 0; box-sizing: border-box; border-radius: 5px; margin: 0; } .bottom-left-content { flex: 0 0 72%; display: flex; align-items: center; padding: 0 20px; } </style> bottom-right 和 bottom-left 之存在一个真空区域
09-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值