CSS3 对边框属性进行了显著的增强,引入了许多新的边框控制能力。这些新属性使得我们能够创建更加丰富和复杂的边框效果,包括圆角边框、边框图片、阴影效果等。
border-radius 属性用于创建圆角边框。
border-radius: length | percentage;
length:固定半径(如 10px)percentage:百分比半径(如 50%)/* 统一圆角 */
.rounded {
border-radius: 10px;
}
/* 不同圆角 */
.different-corners {
border-radius: 10px 20px 30px 40px;
}
/* 水平垂直半径 */
.elliptical {
border-radius: 10px / 20px;
}
/* 圆形 */
.circle {
border-radius: 50%;
}
/* 半圆 */
.semi-circle {
border-radius: 50% 50% 0 0;
}
/* 卡片圆角 */
.card {
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
/* 按钮圆角 */
.button {
border-radius: 4px;
padding: 12px 24px;
background: #007bff;
color: white;
border: none;
cursor: pointer;
}
/* 头像圆形 */
.avatar {
border-radius: 50%;
width: 100px;
height: 100px;
object-fit: cover;
}
/* 标签样式 */
.tag {
border-radius: 20px;
padding: 5px 15px;
background: #f8f9fa;
font-size: 14px;
}
border-image 属性用于使用图片作为边框。
border-image: source slice width outset repeat;
source:边框图片来源slice:切片偏移量width:边框宽度outset:边框外扩repeat:重复方式/* 基本边框图片 */
.border-image {
border: 10px solid transparent;
border-image: url('border.png') 30 round;
}
/* 渐变边框 */
.gradient-border {
border: 10px solid;
border-image: linear-gradient(45deg, #007bff, #28a745) 1;
}
/* 重复边框 */
.repeated-border {
border: 15px solid transparent;
border-image: url('pattern.png') 30 repeat;
}
/* 拉伸边框 */
.stretched-border {
border: 15px solid transparent;
border-image: url('pattern.png') 30 stretch;
}
/* 装饰边框 */
.decorative-border {
border: 20px solid transparent;
border-image: url('decorative.png') 30 round;
padding: 20px;
}
/* 渐变边框按钮 */
.gradient-border-button {
border: 3px solid;
border-image: linear-gradient(45deg, #007bff, #28a745) 1;
padding: 12px 24px;
background: white;
color: #333;
}
/* 图片边框 */
.image-border {
border: 15px solid transparent;
border-image: url('frame.png') 30 round;
padding: 10px;
}
box-shadow 属性用于为元素添加阴影效果。
box-shadow: h-shadow v-shadow blur-radius spread-radius color inset | none;
h-shadow:水平阴影位置v-shadow:垂直阴影位置blur-radius:模糊半径spread-radius:扩展半径color:阴影颜色inset:内阴影none:无阴影/* 基本阴影 */
.basic-shadow {
box-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}
/* 多重阴影 */
.multiple-shadow {
box-shadow:
0 2px 4px rgba(0,0,0,0.1),
0 4px 8px rgba(0,0,0,0.1),
0 8px 16px rgba(0,0,0,0.1);
}
/* 内阴影 */
.inset-shadow {
box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}
/* 扩展阴影 */
.spread-shadow {
box-shadow: 0 0 0 4px rgba(0,123,255,0.5);
}
/* 彩色阴影 */
.colored-shadow {
box-shadow: 2px 2px 4px rgba(0,123,255,0.5);
}
/* 卡片阴影 */
.card {
border-radius: 8px;
box-shadow:
0 2px 4px rgba(0,0,0,0.1),
0 4px 8px rgba(0,0,0,0.1);
transition: box-shadow 0.3s ease;
}
.card:hover {
box-shadow:
0 4px 8px rgba(0,0,0,0.15),
0 8px 16px rgba(0,0,0,0.15);
}
/* 按钮阴影 */
.button {
border-radius: 4px;
padding: 12px 24px;
background: #007bff;
color: white;
border: none;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
transition: all 0.3s ease;
}
.button:hover {
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
transform: translateY(-2px);
}
/* 输入框阴影 */
.input {
padding: 12px 16px;
border: 1px solid #ced4da;
border-radius: 4px;
transition: all 0.3s ease;
}
.input:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 0 3px rgba(0,123,255,0.25);
}
.card {
border-radius: 12px;
background: white;
box-shadow:
0 2px 4px rgba(0,0,0,0.1),
0 4px 8px rgba(0,0,0,0.1);
overflow: hidden;
transition: all 0.3s ease;
}
.card:hover {
box-shadow:
0 4px 8px rgba(0,0,0,0.15),
0 8px 16px rgba(0,0,0,0.15);
transform: translateY(-4px);
}
.card-image {
width: 100%;
height: 200px;
object-fit: cover;
}
.card-content {
padding: 20px;
}
.card-title {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.card-description {
color: #6c757d;
line-height: 1.6;
}
.button {
border-radius: 6px;
padding: 14px 28px;
border: none;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
}
.button-primary {
background: linear-gradient(45deg, #007bff, #28a745);
color: white;
box-shadow: 0 4px 6px rgba(0,123,255,0.3);
}
.button-primary:hover {
box-shadow: 0 6px 12px rgba(0,123,255,0.4);
transform: translateY(-2px);
}
.button-secondary {
background: white;
color: #333;
border: 2px solid #007bff;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.button-secondary:hover {
background: #007bff;
color: white;
box-shadow: 0 4px 8px rgba(0,123,255,0.3);
}
.navbar {
background: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
padding: 15px 30px;
border-radius: 0 0 12px 12px;
}
.navbar-brand {
font-size: 24px;
font-weight: bold;
color: #007bff;
}
.navbar-nav {
display: flex;
list-style: none;
margin: 0;
padding: 0;
}
.navbar-nav li {
margin-right: 20px;
}
.navbar-nav a {
color: #333;
text-decoration: none;
padding: 8px 16px;
border-radius: 4px;
transition: all 0.3s ease;
}
.navbar-nav a:hover {
background: #f8f9fa;
color: #007bff;
}
.form-group {
margin-bottom: 20px;
}
.form-label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #333;
}
.form-input {
width: 100%;
padding: 12px 16px;
border: 2px solid #e9ecef;
border-radius: 6px;
font-size: 16px;
transition: all 0.3s ease;
}
.form-input:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 0 3px rgba(0,123,255,0.25);
}
.form-input:disabled {
background: #f8f9fa;
border-color: #dee2e6;
cursor: not-allowed;
}
.form-input:invalid {
border-color: #dc3545;
box-shadow: 0 0 0 3px rgba(220,53,69,0.25);
}
.image-card {
position: relative;
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.image-card img {
width: 100%;
display: block;
transition: transform 0.3s ease;
}
.image-card:hover img {
transform: scale(1.05);
}
.image-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(to bottom, transparent, rgba(0,0,0,0.5));
opacity: 0;
transition: opacity 0.3s ease;
}
.image-card:hover::before {
opacity: 1;
}
.image-caption {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
padding: 20px;
color: white;
transform: translateY(100%);
transition: transform 0.3s ease;
}
.image-card:hover .image-caption {
transform: translateY(0);
}
CSS3 边框属性在所有现代浏览器中都有良好的支持:
border-radius:Chrome 4+, Firefox 4+, Safari 5+, IE 9+border-image:Chrome 16+, Firefox 15+, Safari 6+, IE 11+box-shadow:Chrome 10+, Firefox 4+, Safari 5.1+, IE 9+/* 推荐 - 适度的圆角 */
.card {
border-radius: 8px;
}
/* 不推荐 - 过度的圆角 */
.card {
border-radius: 50px;
}
/* 推荐 - 适度的阴影 */
.card {
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* 不推荐 - 过度的阴影 */
.card {
box-shadow:
0 2px 4px rgba(0,0,0,0.1),
0 4px 8px rgba(0,0,0,0.1),
0 8px 16px rgba(0,0,0,0.1),
0 16px 32px rgba(0,0,0,0.1);
}
/* 推荐 - 提供降级方案 */
.card {
border: 1px solid #e9ecef;
border-radius: 8px;
}
/* 不推荐 - 不提供降级方案 */
.card {
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* 推荐 - 优化性能 */
.card {
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* 不推荐 - 影响性能 */
.card {
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
transition: all 0.3s ease;
}
CSS3 边框属性提供了强大的边框控制能力:
主要属性:
border-radius:圆角边框border-image:边框图片box-shadow:阴影效果实际应用:
最佳实践:
优势:
注意事项:
掌握 CSS3 边框属性,能够让我们创建更加丰富和复杂的边框效果,大大提升网页的视觉效果和用户体验。