HTML/CSS 下的font、background

本文详细介绍了HTML/CSS中关于字体和背景的使用。内容包括字体的加载、图标字体、行高、简写属性、文本样式、背景颜色、背景图片、渐变效果等,展示了如何设置字体大小、颜色、行高、图标、背景定位和渐变等多种样式技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML/CSS 下的font、background

01-字体

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

        /* font-face 可以将服务器中的字体直接提供给用户去使用
            问题:
                1.加载速度
                2.版权
                3.字体格式
        
         */
        @font-face{
            /* 指定字体的名字 */
            font-family:'myfont' ;
            /* 服务器中的字体路径 */
            src: url();
        }
        p{
            /* 字体相关的样式 
                    color 用来设置字体的颜色
                    font-size 字体的大小
                    em 相当于当前元素的一个font-size
                    rem 相对于根元素的一个font-size
                font-family 字体族(字体的格式)
                    可选值:
                        serif  衬线字体
                        sans-serif 非衬线字体
                        monospace 等宽字体

                    - font-family 可以同时指定多个字体,多个字体间使用,隔开
                        字体生效时优先使用第一个,第一个无法使用则使用第二个,以此类推
            */
            color: red;
            font-size: 40px;

            font-family:cursive,monospace;

        }
    </style>
</head>
<body>
    <p>
        今天天气不错,Hello How are you!
    </p>
</body>
</html>

02-图标字体的简介

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./fa/css/all.css">
</head>
<body>
    <!-- 
        图标字体(iconfont)
            - 在网页中经常需要使用一些图标,可以通过图片来引入图标
                但是图片大小本身比较大,并且非常的不灵活
            - 所以在使用图标时,我们还可以将图标直接设置为字体
                然后通过font-face的形式对字体进行引入
            - 这样我们就可以通过使用字体的形式来使用图标

        font awesome 使用步骤
            1,下载 http://fontawesome.com/
            2. 解压
            3. css和webfonts移动到项目中
            4. 将all.css引入到网页中
            5. 使用图标字体
                - 直接通过类名来使用图标字体
                    class="fas fa-bell"
                    class="fab fa-accessoble-icon"


     -->
     <i class="fas fa-bell"></i>
     <i class="fab fa-accessoble-icon"></i>
     <i class="fas fa-otter" style="font-size: 160px; color: green;"></i>
</body>
</html>

03-图标字体

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./fa/css/all.css">
    <style>
       li{
           list-style: none;
       }
       li::before{
           /* 
                通过伪元素来设置图标字体
                    1,找到要设置图标的元素通过before或after选中
                    2、在content中设置字体的编码
                    3、设置字体的样式
                    fab
                        font-family:'Font Awesome 5 Brands' ;
                    fas
                         font-family:  'Font Awesome 5 Free';    
           */
           content: '\f368';
           font-family:'Font Awesome 5 Brands' ;
           /* font-family:  'Font Awesome 5 Free'; */
           /* font-weight: 900px; */
           color: blue;
           margin-right: 10px;
       }
    </style>
</head>
<body>
    
    <!-- <p class="fas fa-cat"></p> -->
    <ul>
        <li>
            锄禾日当午
        </li>
        <li>
            汗滴禾下土
        </li>
        <li>
            谁知盘中餐
        </li>
        <li>
            粒粒皆辛苦
        </li>
        <span class="fas">
            <!-- 
                通过实体来使用图标实体
                    &#x图标的编码
             -->
             &#xf0f3;
        </span>
    </ul>

</body>
</html>

04-行高

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            font-size: 50px;
            height: 200px;
            /* 可以将行高设置为和高度一样的值,使单行文字在一个元素中垂直居中*/
            line-height: 200px;
            /* 
                行高(line height)
                    - 行高指的是文字占有的实际高度
                    - 可以通过line-height来设置行高
                        行高可以直接指定一个大小(px em)
                        也可以直接为行高设置一个整数
                            如果一个整数的话,行高将会是字体的指定的倍数
                    - 行高经常还用来设置文字的行间距
                        行间距 = 行高 - 字体大小

                    字体框
                        - 字体框就是字体存在的格子,设置font-size实际上就是设置字体框的高度
             */
             border: 1px red solid;

             /* line-height: 10; */
        }
    </style>
</head>
<body>
    <div>今天天气不错 Hello hello</div>
</body>
</html>

05-字体的简写属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            border: 1px red solid;

            /* 
                font 可以设置字体的相关的所有属性
                    语法:
                        font:字体大小/行高 字体族
                        行高 可以省略不写 如果不写使用默认值
             */
             /* font-size: 50px;
             font-family: Georgia, 'Times New Roman', Times, serif; */
             font: 50px/2 Georgia, 'Times New Roman', Times, serif;
             font: italic bold 50px/2 Georgia, 'Times New Roman', Times, serif;
             font-size: 50px;

             /* 
                font-weight 字重 字体的加粗
                    可选值:
                        normal 默认值 不加粗
                        bold 加粗
                        100-900 九个级别(没什么用) 
                font-style 字体的风格
                    normal 正常的
                    italic 斜体
              */
              font-weight: 500;

        }
    </style>
</head>
<body>
    
    <div>
        今天天气不错 Hello hello
    </div>

</body>
</html>

06-文本样式1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            width: 800px;
            border: 1px red solid;

            /* 
                text-align 文本的水平对齐
                    可选值
                        left 左侧对齐
                        right 右对齐
                        center 居中对齐
                        justify 两端对齐
             */
            /* text-align: justify; */

            font-size: 50px;

        }
        span{
            font-size: 30px;
            border: 1px blue solid;

            /* 
                vertical-align 设置元素垂直对齐的方式
                    可选值:
                        baseline 默认值 基线对齐
                        top 顶部对齐
                        bottom 底部对齐
                        middle 居中对齐
            
             */
        vertical-align: bottom;

        }
        p{
            border: 1px red solid;
        }
        img{
            vertical-align: bottom;
        }
    </style>
</head>
<body>
    
<!-- <div>hagoihgohdsao s oahgo ahgahghad sahghaso gsa g hosahgosadhgohdsao </div> -->
<div>今天天 Hello hello
    <span>真不错</span></div>
<p>
    <img src="../exercise/img/01/01.jpg" alt="">
</p>
</body>
</html>

07文本样式2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            font-size: 50px;
            font-family: 'Times New Roman', Times, serif;
            /* text-decoration:underline; */
            /* 
            text-decoration 设置文本修饰
                可选值:
                    none 什么都没有
                    underline 下划线
                    line-through 删除线
                    overline 上划线
            */
            /* text-decoration:underline red dotted */
        }
        .box2{
            width: 200px;
            /* white-space 设置网页如何处理空白
                可选值:
                    normal 正常
                    nowrap 不换行
                    pre 保留空白
            
            */
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            }
    </style>
</head>
<body>
    <div class="box2">
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Blanditiis fugit provident dignissimos qui consequatur suscipit sequi culpa quia reprehenderit natus quisquam doloribus saepe, eos error beatae perspiciatis autem veniam repellendus.
    </div>
    <div class="box1">
        今天天气真不错
    </div>

</body>
</html>

08-背景1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            width: 500px;
            height: 500px;
            /*
                background-color 背景颜色 
             */
            background-color: rgb(165, 168, 164);
            /* background-image: 设置背景图片
                - 可以同时设置背景图片和背景颜色,这样背景颜色将会成为图片得背景色
                - 如果背景的图片小于元素,则背景图片会自动在元素中平铺将元素铺满
                - 如果背景的图片大于元素,将会一个部分背景无法完全显示
                - 如果背景图片和元素一样大,则会直接正常显示
             */
            background-image: url("../exercise/img/01/01.jpg");

            /* background-repeat 用来设置背景的重复方式
                    - 可选值:
                        repeat 默认值,背景会沿着x轴 y轴双方向重复
                        repeat-x 沿着x轴方向重复
                        repeat-y 沿着y轴方向重复
                        no-repeat 背景图片不重复
            
             */
            background-repeat: no-repeat;

            /* 
                background-position 用来设置背景图片的位置
                    设置方式:
                        通过top left right bottom center 几个表示方位的词来设置背景
                            方位词使用时必须要同时指定两个值,如果只写一个值则第二个默认的就是center
                        通过偏移量来指定背景图片的位置:
                            水平方向的偏移量 垂直方向变量
             */
            /* background-position: center; */
            background-position: 10px 10px;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
</body>
</html>

09-背景2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            width: 500px;
            height: 500px;
            background-color: rgb(165, 168, 164);
            background-image: url("../exercise/img/01/01.jpg");
            background-repeat: no-repeat;
            padding: 10px;
            /* background-position: 10px 10px; */

            /*
                    设置背景的范围
                        background-clip
                            可选值:
                                border-box 默认值,背景会出现在边框的下边
                                padding-box 背景不会出现在边框,只出现在内容去和内边距
                                content-box 背景只会出现在内容区
                        background-origin 背景图片的偏移量计算的原点
                            padding-box 默认值,background-position从内边距开始计算
                            content-box 背景图片的偏移量从内容区开始计算
                            border-box 背景图片的变量从边框处开始计算
            */
            /* background-origin: border-box; */
            /* background-clip: content-box; */
            /* border: 10px red double; */
            /* 
                background-size 设置背景图片的大小
                    第一个值表示宽度
                    第二个值表示高度
                    -如果只写一个,则第二个值默认值是auto

                    cover 图片的比例不变,将元素铺满
                    contain 图片的比例不变 ,将图片在元素中完整显示

            */
            background-size: contain;
        }
        /* 
            background-color
            background-image
            background-repeat
            background-position
            background-size
            background-origin
            background-clip
            background-attachment

            -background 背景相关的简写属性,所有背景相关的样式都可以通过该样式来设置
                    并且该样式没有顺序要求,也没有哪个属性是必须写的

                    注意:
                        background-size 必须写在background-position的后边,并且使用/隔开
                            background-position/background-size
                        
                        background-origin background-clip 两个样式,origin要在clip的前边
        */
        .box2{
            width: 300px;
            height: 1000px;
            background-image: url('../exercise/img/京东轮播图/002.jpg'); 
            background-repeat: no-repeat;
            
            /* 
                background-attachment
                    - 背景图片是否跟随元素移动
                    - 可选值:
                        scroll 默认值 背景图片会跟随元素移动
                        fixed 背景会固定在页面中,不会随元素移动
            */
            background-attachment: fixed;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2">
            Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolor, cumque? Neque, impedit perferendis voluptas cupiditate beatae reprehenderit exercitationem libero qui non et cum nulla distinctio numquam quos aperiam omnis voluptate.
            Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quia omnis ipsa sunt adipisci maxime ullam optio sequi. Illum ad, magni possimus consequuntur laboriosam recusandae, hic corporis architecto aliquam, blanditiis reprehenderit.
            Lorem ipsum dolor sit amet consectetur adipisicing elit. Perspiciatis quis sit accusamus eius. Magnam consectetur nihil laborum soluta voluptatum eaque at, quibusdam ipsa nemo unde qui laboriosam dignissimos cupiditate asperiores!
        </div>
    </div>
</body>
</html>

10-渐变

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            width: 200px;
            height: 200px;
            /* background-color: #bfa; */
            /* 
                通过渐变可以设置一些复杂的背景颜色,可以实现从一个颜色向其他颜色过渡的效果
                !!渐变是图片,需要background-image来设置

                线性渐变,颜色沿着一条直线发生变化
                    linear-gradient(red ,yellow)红色在开头,黄色在结尾,中间是过度区域
                    - 线性渐变的开头,我们可以指定一个渐变的方向
                    to left
                    to right
                    to bottom(默认值)
                    to top
                    xxxdeg deg 表示度数
                    turn 表示圈

                渐变可以同时指定多个颜色,多个颜色默认情况下平均分布
                    也可以手动指定渐变的分布情况
                
                background-image: repeating-linear-gradient() 可以平铺的线性渐变
             */
            /* background-image: linear-gradient( 45deg,red,yellow); */
            /* background-image: linear-gradient( red 50px,yellow 100px); */
            background-image: repeating-linear-gradient(red 50px,orange 100px);
        }
    </style>
</head>
<body>
    <div class="box1"></div>
</body>
</html>

11-径向渐变

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box1{
            width: 300px;
            height: 300px;

            /* 
                background-image: radial-gradient(red,yellow) 径向渐变(放射性的效果)
                |默认情况下径向渐变的形状根据元素的形状来计算的
                    正方形--> 圆形
                    长方形--> 椭圆形
                    - 我们也可以手动指定径向渐变的大小
                        circle 正圆
                        ellipse 椭圆

                    -也可以指定渐变的位置
                    - 语法:
                        radial-gradient1( 大小 at 位置,颜色 位置,颜色 位置) 
                        大小:
                            circle 圆形
                            ellipse 椭圆
                            closest-side 近边
                            closest-corner 近角
                            farthest-side   远边
                            farthest-corner 远角

                        位置:
                            top right left center bottom



             */
            background-image: radial-gradient(at center,red,yellow)
        }
    </style>
</head>
<body>
    <div class="box1"></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值