font-size: 0的作用和用途

解释:

font-size: 0 不是指字体大小是0px

代码自动格式化的时候,往往会设置一些适当的缩进、换行,但当元素的display为inline或者inline-block的时候,这些缩进、换行就会产生空白,导致前端页面展示变形。

1. 解决表格之间空隙

以ul和li为例:
最合适的方法就是给li的父级ul设置: font-size: 0;
给li设置:font-size: a px; 如此就达到了所需效果,li不设置font-size就是之间没有空格的效果。

<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
	<style>
    ul {
        list-style: none;
    }
    li {
        width: 50px;
	    height: 40px;
        display: inline-block;
        background: red;
    }
</style>
</head>

<body>
	<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
</ul>
</body>
</html>

效果:
在这里插入图片描述
因为li标签用到 display: inline-block; 行内级元素之间,换行会被解释成空格。在父级元素添加font-size:0可以除去间隙,记得给li也要给与font-size:多少 px;不然显示不出内容
在这里插入图片描述

2. 解决图片之间的空隙

在这里插入图片描述
一样因为行内级元素的原因,图片之间会出现间隙,直接在给div标签一个font-size:0; 即可。
在这里插入图片描述
这时候图片上下左右需要间隙可以用margin设置

3. 解决行内元素空隙多种方法

其实由display:inline-block元素之间空隙的解决办法不止设置父级font-size为0这一种。
我们还可以设置父元素,display:table和word-spacing
在这里插入图片描述
为inline-block元素添加样式float:left :
在这里插入图片描述
设置子元素margin值为适当的负数:
在这里插入图片描述
总结:

因为在父元素设置了font-size: 0,所以子元素继承了父元素的属性,记得需要添加子元素的font-size大小才能显示出来。其他解决空隙的方法都各有缺点,要选择一条适写该网页的方法

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请把一下代码每一行的用途注释上:<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>登录表单</title> <style> body { background-image: url("background.jpg"); background-size: cover; font-family: "宋体"; font-size: 30px; } form { margin: auto; width: 50%; background-color: rgba(255,255,255,0.8); padding: 20px; border-radius: 10px; } input[type=text], input[type=password] { width: 100%; padding: 12px 20px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; } input[type=submit], input[type=reset] { background-color: #4CAF50; color: white; padding: 14px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } input[type=submit]:hover, input[type=reset]:hover { background-color: #45a049; } .error { color: red; } </style> </head> <body> <form> <h2>登录</h2> <label for="username">用户名</label> <input type="text" id="username" name="username" ><br> <label for="password">密码</label> <input type="password" id="password" name="password" ><br> <label for="confirm_password">确认密码</label> <input type="password" id="confirm_password" name="confirm_password" ><br> <input type="submit" value="登录"> <input type="reset" value="重置"> <span class="error" id="password_error"></span><br> <a href="https://www.baidu.com/">忘记密码?</a> </form> <script> document.querySelector("form").addEventListener("submit", function(event) { var password = document.querySelector("#password").value; var confirm_password = document.querySelector("#confirm_password").value; if (password != confirm_password) { document.querySelector("#password_error").innerHTML = "密码不一致"; event.preventDefault(); } }); </script> </body> </html>
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值