边框属性用于控制元素边框的外观,包括边框的宽度、样式和颜色。掌握边框属性对于创建精美的网页界面至关重要。
border-width 属性用于设置边框的宽度。
/* 使用像素 */
.box {
border-width: 1px;
}
.thick-border {
border-width: 3px;
}
/* 使用关键字 */
.thin {
border-width: thin;
}
.medium {
border-width: medium;
}
.thick {
border-width: thick;
}
/* 使用百分比 */
.percent {
border-width: 1%;
}
/* 上边框 */
.top-border {
border-top-width: 2px;
}
/* 右边框 */
.right-border {
border-right-width: 2px;
}
/* 下边框 */
.bottom-border {
border-bottom-width: 2px;
}
/* 左边框 */
.left-border {
border-left-width: 2px;
}
/* 卡片边框 */
.card {
border-width: 1px;
}
/* 强调边框 */
.highlight {
border-width: 3px;
}
/* 分隔线 */
.divider {
border-top-width: 2px;
border-bottom-width: 2px;
border-left-width: 0;
border-right-width: 0;
}
border-style 属性用于设置边框的样式。
/* 实线 */
.solid {
border-style: solid;
}
/* 虚线 */
.dashed {
border-style: dashed;
}
/* 点线 */
.dotted {
border-style: dotted;
}
/* 双线 */
.double {
border-style: double;
}
/* 凹槽 */
.groove {
border-style: groove;
}
/* 脊状 */
.ridge {
border-style: ridge;
}
/* 内嵌 */
.inset {
border-style: inset;
}
/* 外凸 */
.outset {
border-style: outset;
}
/* 无边框 */
.none {
border-style: none;
}
/* 隐藏边框 */
.hidden {
border-style: hidden;
}
/* 上边框样式 */
.top-style {
border-top-style: solid;
}
/* 右边框样式 */
.right-style {
border-right-style: dashed;
}
/* 下边框样式 */
.bottom-style {
border-bottom-style: dotted;
}
/* 左边框样式 */
.left-style {
border-left-style: double;
}
/* 卡片边框 */
.card {
border-style: solid;
}
/* 强调边框 */
.highlight {
border-style: double;
}
/* 分隔线 */
.divider {
border-top-style: solid;
border-bottom-style: solid;
}
/* 输入框边框 */
input {
border-style: solid;
}
input:focus {
border-style: solid;
}
border-color 属性用于设置边框的颜色。
/* 使用颜色名称 */
.red-border {
border-color: red;
}
/* 使用十六进制 */
.blue-border {
border-color: #3498db;
}
/* 使用 RGB */
.green-border {
border-color: rgb(46, 204, 113);
}
/* 使用 RGBA(带透明度) */
.transparent-border {
border-color: rgba(52, 152, 219, 0.5);
}
/* 使用 HSL */
.purple-border {
border-color: hsl(270, 50%, 50%);
}
/* 使用 HSLA(带透明度) */
.hsla-border {
border-color: hsla(270, 50%, 50%, 0.8);
}
/* 透明边框 */
.clear-border {
border-color: transparent;
}
/* 上边框颜色 */
.top-color {
border-top-color: red;
}
/* 右边框颜色 */
.right-color {
border-right-color: blue;
}
/* 下边框颜色 */
.bottom-color {
border-bottom-color: green;
}
/* 左边框颜色 */
.left-color {
border-left-color: yellow;
}
/* 卡片边框 */
.card {
border-color: #dee2e6;
}
/* 强调边框 */
.highlight {
border-color: #007bff;
}
/* 错误边框 */
.error {
border-color: #dc3545;
}
/* 成功边框 */
.success {
border-color: #28a745;
}
border 简写属性可以同时设置边框的宽度、样式和颜色。
/* 完整语法 */
border: border-width border-style border-color;
/* 示例 */
.box {
border: 1px solid #333;
}
.thick-blue {
border: 3px solid #3498db;
}
.dashed-red {
border: 2px dashed #e74c3c;
}
/* 上边框 */
.top {
border-top: 2px solid #333;
}
/* 右边框 */
.right {
border-right: 2px dashed #666;
}
/* 下边框 */
.bottom {
border-bottom: 2px dotted #999;
}
/* 左边框 */
.left {
border-left: 2px double #aaa;
}
/* 卡片 */
.card {
border: 1px solid #dee2e6;
border-radius: 8px;
}
/* 按钮 */
.button {
border: none;
border-radius: 4px;
}
/* 输入框 */
input {
border: 1px solid #ced4da;
border-radius: 4px;
}
input:focus {
border-color: #007bff;
}
border-radius 属性用于设置边框的圆角。
/* 所有角相同圆角 */
.rounded {
border-radius: 4px;
}
.large-radius {
border-radius: 8px;
}
/* 圆形 */
.circle {
border-radius: 50%;
}
/* 椭圆 */
.ellipse {
border-radius: 50% / 25%;
}
/* 左上角 */
.top-left {
border-top-left-radius: 4px;
}
/* 右上角 */
.top-right {
border-top-right-radius: 8px;
}
/* 右下角 */
.bottom-right {
border-bottom-right-radius: 12px;
}
/* 左下角 */
.bottom-left {
border-bottom-left-radius: 16px;
}
/* 对角圆角相同 */
.diagonal {
border-radius: 4px 8px;
}
/* 四个角不同 */
.different {
border-radius: 4px 8px 12px 16px;
}
/* 水平和垂直圆角 */
.separate {
border-radius: 4px / 8px;
}
/* 卡片圆角 */
.card {
border: 1px solid #dee2e6;
border-radius: 8px;
}
/* 按钮圆角 */
.button {
border: none;
border-radius: 4px;
}
/* 圆形头像 */
.avatar {
border: 2px solid #fff;
border-radius: 50%;
}
/* 标签圆角 */
.badge {
border-radius: 12px;
}
outline 属性用于设置元素的轮廓,轮廓不占用空间。
/* 轮廓样式 */
.outline {
outline: 2px solid #007bff;
}
/* 轮廓偏移 */
.outline-offset {
outline: 2px solid #007bff;
outline-offset: 2px;
}
/* 无轮廓 */
.no-outline {
outline: none;
}
/* 输入框聚焦 */
input:focus {
outline: 2px solid #007bff;
outline-offset: 2px;
}
/* 按钮聚焦 */
button:focus {
outline: 2px dashed #007bff;
outline-offset: 2px;
}
/* 移除默认轮廓 */
button:focus {
outline: none;
}
border-image 属性用于使用图片作为边框。
/* 基本语法 */
.border-image {
border-image: source slice width outset repeat;
}
/* 示例 */
.image-border {
border: 10px solid transparent;
border-image: url('border.png') 30 round;
}
/* 图片边框 */
.decorative {
border: 15px solid transparent;
border-image: url('frame.png') 30 stretch;
}
.card {
border: 1px solid #dee2e6;
border-radius: 8px;
background: white;
padding: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.card:hover {
border-color: #007bff;
box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}
.button {
border: none;
border-radius: 4px;
padding: 12px 24px;
cursor: pointer;
transition: all 0.3s ease;
}
.button-primary {
background: #007bff;
color: white;
border: 2px solid #007bff;
}
.button-primary:hover {
background: transparent;
color: #007bff;
}
.button-outline {
background: transparent;
color: #007bff;
border: 2px solid #007bff;
}
.button-outline:hover {
background: #007bff;
color: white;
}
.input {
border: 1px solid #ced4da;
border-radius: 4px;
padding: 10px 15px;
width: 100%;
font-size: 16px;
transition: border-color 0.3s ease;
}
.input:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 0 0 3px rgba(0,123,255,0.25);
}
.input-error {
border-color: #dc3545;
}
.input-error:focus {
border-color: #dc3545;
box-shadow: 0 0 0 3px rgba(220,53,69,0.25);
}
.table {
border-collapse: collapse;
width: 100%;
}
.table th,
.table td {
border: 1px solid #dee2e6;
padding: 12px 15px;
text-align: left;
}
.table th {
background: #f8f9fa;
font-weight: bold;
border-bottom: 2px solid #dee2e6;
}
.table tr:hover td {
background: #f8f9fa;
}
/* 水平分隔线 */
.divider {
border-top: 1px solid #dee2e6;
margin: 20px 0;
}
/* 垂直分隔线 */
.vertical-divider {
border-left: 1px solid #dee2e6;
height: 100%;
margin: 0 15px;
}
/* 双线分隔线 */
.double-divider {
border-top: 3px double #dee2e6;
margin: 20px 0;
}
/* 边框颜色动画 */
.animated-border {
border: 2px solid #007bff;
transition: border-color 0.3s ease;
}
.animated-border:hover {
border-color: #0056b3;
}
/* 边框宽度动画 */
.animated-width {
border: 2px solid #007bff;
transition: border-width 0.3s ease;
}
.animated-width:hover {
border-width: 4px;
}
/* 圆角动画 */
.animated-radius {
border-radius: 4px;
transition: border-radius 0.3s ease;
}
.animated-radius:hover {
border-radius: 12px;
}
边框属性是 CSS 中重要的属性类别,它们控制着元素边框的外观:
记住以下几点: