解决标签间的间隙问题(font-size:0)

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;
}
  •  

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

但是font-size有继承性,在父元素设置了font-size子元素的所有元素都受到其影响!不过对照其他方案,这种方案又是最好的! 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <style> body { background-image: url('/src/selsectIndex/'); background-size: cover; } .blue-line { width: 100%; height: 30px; background-color: blue; } .images { display: flex; flex-wrap: nowrap; justify-content: center; } .image { width: 350px; height: 220px; margin: 20px; } .button1{ width: 360px; height: 230px; margin: 20px; } .spacer { height: 0px; /* 设置间隙的高度 / } .container { display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; / 设置容器高度为视口高度 */ } .image-container { position: relative; } .image-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 50px; color: #ffffff; text-align: rgba(13, 13, 13, 0.5) 0 5px 6px, rgba(0, 0, 0, 0.2) 1px 3px 3px; margin: 0; padding: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; text-shadow: 2px 2px 0 #000, -2px -2px 0 #000, 2px -2px 0 #000, -2px 2px 0 #000; } .image-text2 { position: absolute; top: 70%; left: 50%; transform: translate(-50%, -50%); font-size: 40px; color: #ffffff; text-align: rgba(13, 13, 13, 0.5) 0 5px 6px, rgba(0, 0, 0, 0.2) 1px 3px 3px; margin: 0; padding: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; text-shadow: 2px 2px 0 #000, -2px -2px 0 #000, 2px -2px 0 #000, -2px 2px 0 #000; } </style> </head> <body> <div class="blue-line"></div> <div class="container"> <div class="images"> <div class="image-container"> <img class="image" src="/src/selsectIndex/public/hongganyao.png" onclick="imageClick()"> <p class="image-text">烘干窑系统</p> </div> </div> </div> <script> function imageClick() { // Add your code here for what should happen when an image is clicked } </script> </body> </html> 帮我在这个html页面中右上角添加一个用户头像的图像
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值