html中关于换行符占空间的解决办法

2025-09-07



如上图:parent的width:600px;
           child1和child2的width:300,display:inline-block;
我们希望它们并排显示,但为什么会换行呢?

    Document     

1

2

.parent{  width:600px;  border: solid;  /* font-size:0; */}.child1{  width:300px;  height: 300px;  display:inline-block;  /* font-size:20px; */  line-height: 300px;    text-align: center;    background:#ccc;}  .child2{    width:300px;    height: 300px;    display:inline-block;    /* font-size:20px; */  line-height: 300px;    text-align: center;    background: rgba(230, 32, 32, 0.51);  }

因为在html中,parent中间有换行空格等等,其实这些也是占据空间的;

如何解决?
① 在parent中添加font-size:0;这样parent内的空格换行就不占空间了;但有个问题,会发现child的文字也没有了,由于font-size默认是继承的;
② 所以第二部就是设置child的font-size;

标签: html换行符

本文地址:https://www.lifejia.cn/news/209693.html

免责声明:本站内容仅用于学习参考,信息和图片素材来源于互联网,如内容侵权与违规,请联系我们进行删除,我们将在三个工作日内处理。联系邮箱:cloudinto#qq.com(把#换成@)