目录

前端开发display常用属性介绍

前端开发display常用属性介绍

在介绍display属性前我们要先了解CSS的元素显示模式

css元素显示模式总结

元素模式元素排列设置样式默认宽度包含
块级元素在页面上一行只能放一个块级元素比如
标签
其宽高可自定义设置容器的100%可以包含任何标签
行内元素在页面上一行可以放多个行内元素比如标签其宽高由内容本身决定不能自定义设置本身内容的宽度只能容纳文本或者其它行内元素
行内块元素在页面上一行可以放多个行内块元素比如标签其宽高可自定义设置本身内容的宽度

1、display:block

display:block将元素转换为块级元素,一般用于将行内元素转换为块级元素

<span style="width: 200px; height: 200px; background-color: aquamarine;">没有display属性的行内元素</span>
<span style="width: 200px; height: 200px; display:block; background-color: rgb(87, 87, 241);">display:block属性行内元素转换为块级元素</span>

https://i-blog.csdnimg.cn/blog_migrate/e1bfc25d8c3a430d2a7b8ec4b3a0aedc.png

2、display:inline

display:inline将元素转换为行内元素,一般用于将块级元素转换为行内元素

   <div style="width: 200px; height: 200px; background-color: aquamarine;">没有display属性的块级元素</div>
   <div style="width: 200px; height: 200px; display:inline; background-color: rgb(87, 87, 241);">display:inline属性块级元素转换为行内元素</div>
   <div style="width: 200px; height: 200px; display:inline; background-color: rgb(87, 87, 241);">display:inline属性块级元素转换为行内元素</div>

https://i-blog.csdnimg.cn/blog_migrate/7be971c13f2d50ec8bd29857af087e79.png

3、display:inline-block

display:inline-block将元素转换为行内块元素

    <div style="width: 200px; height: 200px; background-color: aquamarine;">没有display属性的块级元素</div>
    <div style="width: 200px; height: 200px; display:inline-block; background-color: rgb(87, 87, 241);">display:inline-block属性将元素转换为行内块元素</div>
    <div style="width: 200px; height: 200px; display:inline-block; background-color: rgb(87, 87, 241);">display:inline-block属性将元素转换为行内块元素</div>

https://i-blog.csdnimg.cn/blog_migrate/3d9a7340a688d8557330b0e8be66edbb.png

4、display:none

display:inline-block使得元素不显示,常用于对元素设置显示与隐藏

    <div style="width: 200px; height: 200px; background-color: aquamarine;">没有display属性的块级元素</div>
    <h3>下方有一个div使用了display:none属性元素不显示</h3>
    <div style="width: 200px; height: 200px; background-color: rgb(127, 142, 255); display: none;">使用了display:none属性元素不显示</div>
    

https://i-blog.csdnimg.cn/blog_migrate/40e7956efac9b32c4fad167b0f3840ed.png