css 轮廓线

本文介绍了CSS中的轮廓线(outline)与边框(border)的区别,轮廓线不占用元素的宽高,可用于美化而不影响布局。示例展示了如何设置轮廓线的宽度、样式、颜色及偏移量,并通过对比边框强调其特点。同时,文章还讨论了在输入框和按钮上的轮廓线应用,以及如何在聚焦时调整轮廓样式。

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

轮廓线

1.轮廓线:显示在边框外部的线段,不占用宽高,使用的方法和
border一样
outline-width: 20px;
outline-style: dashed;
outline-color: blue;
2. 轮廓线的偏移量(离边框的偏移量)
outline-offset: -10px;
3.如果使用边框那么会占用宽高,影响到布局,
使用轮廓线不会占用宽高
outline: 10px solid black;

<!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>
        body{
            margin: 0;
        }
        #div1{
            width: 200px;
            height: 200px;
            border: 20px solid red;
            padding: 20px;
            margin: 30px;
            /* 轮廓线:显示在边框外部的线段,不占用宽高,使用的方法和
            border一样 */
            outline-width: 20px;
            outline-style: dashed;
            outline-color: blue;
            /* 轮廓线的偏移量(离边框的偏移量) */
            outline-offset: -10px;
        }
        .box{
            width: 1000px;
            height: 300px;
            background-color: red;
            font-size: 0;
        }
        .item{
            font-size: 16px;
            width: 250px;
            height: 250px;
            background-color: blue;
            display: inline-block;
            /* 如果使用边框那么会占用宽高,影响到布局,
            使用轮廓线不会占用宽高 */
            outline: 10px solid black;
        }
        .item:nth-child(2){
            background-color: green;
        }
        .item:nth-child(3){
            background-color: gold;
        }
        .item:nth-child(4){
            background-color: grey;
        }
        /* input自带有轮廓线 */
        input{
            border: 0;
            /* 获取焦点时有轮廓 取消轮廓线*/
            outline: 0;
        }
        button{
            border: solid 1px red;
        }
        button:focus{
            outline: 10px solid blueviolet;
        }
    </style>
</head>
<body>
    <div id="div1">
       内容区内容区内容区内容区内容区内容区内容区内容区内容区内容区
    </div>
    <div class="box">
       <div class="item"></div>
       <div class="item"></div>
       <div class="item"></div>
       <div class="item"></div>
    </div>
    <input type="text"> 
    <button>按钮</button>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值