CSS元素显示模式

什么是元素显示模式

元素以什么方式进行显示.

块元素

常见的块元素有 <h1> ~ <h6> , <p>, <div>, <ul>, <ol>, <li>

块元素的特点

  1. 自己独占一行
  2. 高度, 宽度, 外边距以及内边距都可以控制
  3. 宽度默认是容器的100%
  4. 是一个容器及盒子, 里面可以放行内或者块级元素

注意

文字类的元素内不能使用块级元素, <p><h1> ~ <h6>

行内元素

常见的行内元素有 <a>, <strong>, <b>, <em>, <i>, <del>, <s>, <ins>, <span>

行内元素的特点

  1. 相邻行内元素在一行上, 一行可以显示多个
  2. 高和宽直接设置是无效的
  3. 默认宽度就是它本身内容的宽度
  4. 行内元素只能容纳文本或者其他行内元素

行内块元素

行内元素中有几个特殊的标签 ----- <img /> <input /> <td> 它们同时具有块元素和行内元素的特点

元素显示模式转换

一个模式的元素需要另外一种模式的特性
比如想要增加链接 <a> 的触发范围

  • 把行内元素转换为块级元素使用 display: block;
  • 把块级元素转换为行内元素使用 display: inline;
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a {
            width: 150px;
            height: 50px;
            background-color: red;
            display: block;
        }
    </style>
</head>

<body>
    <a href="#">www.baidu.com</a>
</body>

</html>

单行文字垂直居中

让文字的行高等于盒子的高度

导航栏案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a {
            /* 元素转换 */
            display: block;
            /* 宽度 */
            width: 230px;
            /* 高度 */
            height: 40px;
            /* 背景颜色 */
            background-color: slategrey;
            /* 字体颜色 */
            color: #fff;
            /* 不要下划线 */
            text-decoration: none;
            /* 首行缩进两个字 */
            text-indent: 2em;
            line-height: 40px;
        }
        
        a:hover {
            background-color: red;
        }
    </style>
</head>

<body>
    <a href="#">手机    电话卡</a>
    <a href="#">电视    盒子</a>
    <a href="#">笔记本  平板</a>
    <a href="#">出行    穿戴</a>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值