第五讲_css元素显示模式

css元素显示模式

  • [1. 元素的显示模式](#1. 元素的显示模式)
    • [1.1 块元素](#1.1 块元素)
    • [1.2 行内元素](#1.2 行内元素)
    • [1.3 行内块元素](#1.3 行内块元素)
  • [2. 元素根据显示模式分类](#2. 元素根据显示模式分类)
  • [3. 修改元素的显示模式](#3. 修改元素的显示模式)

1. 元素的显示模式

1.1 块元素

块元素的特性:

  • 在页面中独占一行,从上到下排列。
  • 默认宽度,撑满父元素。
  • 默认高度,由内容撑开。
  • 可以通过 css 设置宽高。
html 复制代码
<style>
	.first {
        width: 100px;
        height: 100px;
        background-color: blue;
	}

    .second {
        height: 100px;
        background-color: green;
    }
    .third {
        background-color: red;
    }
</style>

<div class="first">第一个块元素</div>
<div class="second">第二个块元素</div>
<div class="third">第三个块元素</div>

1.2 行内元素

行内元素的特性:

  • 在页面中不独占一行,从左到右排列。
  • 默认宽度,由内容撑开。
  • 默认高度,由内容撑开。
  • 无法通过 css 设置宽高。
html 复制代码
<style>
	.first {
        background-color: blue;
	}
    .second {
        background-color: green;
    }
    .third {
        background-color: red;
    }
</style>

<span class="first">第一个行内元素</span>
<span class="second">第二个行内元素</span>
<span class="third">第三个行内元素</span>

1.3 行内块元素

行内块元素的特性:

  • 在页面中不独占一行,从左到右排列。
  • 默认宽度,由内容撑开。
  • 默认高度,由内容撑开。
  • 可以通过 css 设置宽高。
html 复制代码
<style>
	.first {
        background-color: blue;
	}
    .second {
        height: 100px;
        width: 100px;
        background-color: green;
    }
    .third {
        background-color: red;
    }
</style>

<input class="first" placeholder="第一个行内块元素"/>
<input class="second" placeholder="第二个行内块元素"/>
<input class="third" placeholder="第三个行内块元素"/>

2. 元素根据显示模式分类

  • 块元素

<html>、<body>、<h1>-<h6>、<hr>、<p>、<pre>、<div>
<ul>、<ol>、<li>、<dl>、<dt>、<dd>
<table>、<tbody>、<thead>、<tfoot>、<tr>、<caption>
<form>、<option>

  • 行内元素

<br>、<em>、<strong>、<sup>、<sub>、<del>、<ins>、<a>、<label>

  • 行内块元素

<img>、<td> 、<th>、<input>、<textarea>、<select>、<button>、<iframe>

3. 修改元素的显示模式

css 复制代码
/* block:设置为块元素 */
/* inline:设置为行内元素 */
/* inline-block:设置为行内块元素 */
/* none:隐藏元素 */
选择器 {
  display: block;
}
相关推荐
程序员大阳5 小时前
HTML+CSS+JS对话式实战(5)-活动管理页之查看报名名单的实现
javascript·css·html·查看报名名单
曦樂~5 小时前
【Web Development】02 CSS
前端·css
风骏时光牛马5 小时前
后端服务接口开发与数据逻辑处理
前端
东鹏特饮5 小时前
经典打砖块(Breakout)游戏
前端·css·css3
IT_陈寒6 小时前
Vite的HMR把我整不会了:一个生产环境的热更新坑
前端·人工智能·后端
Mh6 小时前
如何使用双缓冲优化视频切换
前端
risenav6 小时前
低头蓄力,抬头起跳:《萤虫跳跳》的 Rokid AIUI 开发实战
前端
ZhengEnCi6 小时前
如何看待 Fable 5.2 用纯代码生成 1.4MB 视觉作品:光追、水面、倒影、火光的程序化生成原理
前端
再吃一根胡萝卜7 小时前
DSH 插件的 Host 与 Client:为什么「只加载了 Host」,以及要不要自己写 Client 加载器
前端
parade岁月7 小时前
AI 写的前端代码为什么总像 5 年前的?Chrome 官方出手了
前端