云切图

CSS

CSS属性复习,帮你快速掌握CSS,助推学生快速完成网页作业

首页 > 前端笔记 > CSS

html5中css边框和圆角如何设置?让网页添加更美的外观

作者:admin 发布时间:2022-09-18 点击数:

本章主要探讨 HTML5 中 CSS 边框和圆角,通过边框和圆角边框的样式设置,给元素增加更丰富的外观。

一.声明边框

边框的声明有三个属性设置,样式表如下:

属性说明版本
border-width长度值设置边框的宽度可选1
border-style样式名称设置边框的样式,必选1
border-color颜色值设置边框的颜色,可选1

这三个属性值,只有 border-style 是必须声明,才可以出现边框。而其他两个属性会出现默认值。

最简单的边框,边框长度默认 3px,边框颜色为黑色

div {border-style: solid;}

配置完整的边框

div {

border-style: solid;

border-width: 2px;

border-color: red;

}

如果元素长和高均为 200px 时,四个边框均为 2 时,元素的长高总尺寸均为 204px。

二.边框样式

边框的样式主要有三种,分别是边框长度取值、边框的颜色和边框的线条类型。颜色是通用的颜色代码,和所有其他颜色取值一下。而长度和线条类型,边框有自己独到的部分。边框宽度取值表如下:

说明
长度值CSS 长度值:比如 px、em 等
百分数直接设置百分数:1、2、3 等
thin使用长度名称的预设宽度。这三个值的具体意义由浏览器来定义,从小到大依次增大。
medium
thick

一般来说,边框为了更加精准,还要计算元素盒子的总尺寸,使用长度值的比较多。而定义边框线条的样式如下样式表:

说明
none没有边框
dashed破折线边框
dotted圆点线边框
double双线边框
groove槽线边框
inset使元素内容具有内嵌效果的边框
outset使元素内容具有外凸效果的边框
ridge脊线边框
solid实线边框

solid 实线使用频率最高

div {

border-style: solid;

border-width: 10px;

border-color: red;

}

如果想对四条边中某一条边单独进行设置,可以使用如下样式表:

属性说明版本

border-top-width

border-top-style

border-top-colo

定义顶端1

border-bottom-width

border-bottom-style

border-bottom-color

定义底部1

border-left-width

border-left-style

border-left-colo

定义左侧1

border-right-width

border-right-style

border-right-color

定义右侧1

只设置顶端

div {

border-top-style: solid;

border-top-width: 10px;

border-top-color: red;

}

如果四条变都一致,那么没必要分写成三句样式,直接通过简写即可:

属性说明版本
border<宽度><样式><颜色>设置四条边的边框1
border-top只设置上边框1
border-bottom只设置下边框1
border-left只设置左边框1
border-right只设置右边框1

简写形式四条边设置

div {border: 10px solid red;}

三.圆角边框

CSS3 提供了一个非常实用的圆角边框的设置。使用 border-radius 属性,就可以达到这种效果,样式表如下:

属性说明版本
border-radius长度值或百分数四条边角3
border-top-left-radius长度值或百分数左上边角3
border-top-right-radius长度值或百分数右上边角3
border-bottom-left-radius长度值或百分数左下边角3
border-bottom-right-radius长度值或百分数右下边角3

设置圆角矩形

div {border: 10px solid red; border-radius: 10px;}

四条边分别设置

div {border: 10px solid red; border-radius: 10px 20px 30px 40px;}

实操源码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS 边框和圆角样式</title>
<meta name="author" content="学生网页作业">
<link rel="stylesheet" href="css/base.css">
</head>
<body>
<dl>
    <dt>border</dt>
    <style>
        body{ background: green; }
        .dd_01 p,.dd_01 .u1 li{width:100px; height: 50px; background: #333; margin-bottom: 10px;}
        .dd_01 .p1{ border-style: solid; }
        .dd_01 .p2{ border-style: solid; border-width: 2px; border-color: red; }
        
        .p3{
            width:200px; height: 200px; 
            border-top-style: solid; border-top-width: 3px; border-top-color: red;
            border-right-style: solid; border-right-width: 3px; border-right-color: yellow;
            border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: #fff;
            border-left-style: solid; border-left-width: 3px; border-left-color: blue;
        }
    
        /*简写*/
        .p4{width:300px; height: 100px; border:5px solid #fff;}
    
        .dd_01 .u1 li:nth-child(1){border-style:none;} /*没有边框*/
        .dd_01 .u1 li:nth-child(2){border-style:dashed;} /*破折线边框*/
        .dd_01 .u1 li:nth-child(3){border-style:dotted;} /*圆点线边框*/
        .dd_01 .u1 li:nth-child(4){border-style:double;} /*双线边框*/
        .dd_01 .u1 li:nth-child(5){border-style:groove;} /*槽线边框*/
        .dd_01 .u1 li:nth-child(6){border-style:inset; background: #fff;} /*使元素内容具有内嵌效果的边框*/
        .dd_01 .u1 li:nth-child(7){border-style:outset; background: #fff;} /*使元素内容具有外凸效果的边框*/
        .dd_01 .u1 li:nth-child(8){border-style:ridge;} /*脊线边框*/
        .dd_01 .u1 li:nth-child(9){border-style:solid;} /*实线边框*/
    </style>
    <dd>
        <p></p>
        <p></p>
        <p></p>
        <p>简写</p>
        <ul>
            <li>none</li>
            <li>dashed</li>
            <li>dotted</li>
            <li>double</li>
            <li>groove</li>
            <li>inset</li>
            <li>outset</li>
            <li>ridge</li>
            <li>solid</li>
        </ul>
    </dd>
</dl>

<dl>
    <dt>border-radius</dt>
        <style>
            .dd_02 p{width:100px; height: 100px; background: red;}
            .dd_02 .p1{ 
                border-top-left-radius: 10px;
                border-top-right-radius: 20px;
                border-bottom-left-radius: 30px;
                border-bottom-right-radius: 50px;
            }
            .dd_02 .p2{ border-radius:50px; text-align: center; line-height: 100px;}
        </style>
    <dd>
        <p></p>
        <p>简写</p>
    </dd>
</dl>
</body>
</html


关注我们共同进步

  • 扣扣交流群

  • 微信公众号

  • 私技术顾问

嘿,我来帮您!