本文摘自PHP中文网,作者V,侵删。

以前需要边框长度比容器小一些时,我用div嵌套。后来发现伪类在实现这个效果时很方便,只需要一个div就够了,另外调整padding和margin都不会很麻烦。
(推荐教程:CSS入门教程)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <div class= "content-block" >
<div class= "box-container" >
<div class= "border-top" >border top </div>
</div>
<div class= "box-container" >
<div class= "border-left" >border left </div>
</div>
<div class= "box-container" >
<div class= "border-right" >border right </div>
</div>
<div class= "box-container" >
<div class= "border-bottom" >border bottom </div>
</div>
</div>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | .box-container {
position : relative ;
width : 90% ;
color : #777 ;
}
.border- top {
background : #b4bcbf ;
padding : 15px ;
}
. border-top :before {
content : '' ;
position : absolute ;
left : 42% ;
top : 0 ;
bottom : auto ;
right : auto ;
height : 7px ;
width : 50% ;
background-color : #8796a9 ;
}
.border- left {
background : #dfdad6 ;
padding : 15px ;
}
. border-left :before {
content : '' ;
position : absolute ;
left : 0 ;
top : 6% ;
bottom : auto ;
right : auto ;
height : 52% ;
width : 5px ;
background-color : #a89d9e ;
}
.border- right {
background : #eee9c4 ;
padding : 15px ;
}
. border-right :after {
content : '' ;
position : absolute ;
left : auto ;
top : auto ;
bottom : 5px ;
right : 0 ;
height : 52% ;
width : 5px ;
background-color : #f39c81 ;
}
.border- bottom {
background : #bcdc9d ;
padding : 15px ;
}
. border-bottom :after {
content : '' ;
position : absolute ;
left : 18px ;
top : auto ;
bottom : 0 ;
right : auto ;
height : 6px ;
width : 105px ;
background-color : #32b66b ;
}
|
效果如下图:

相关视频教程推荐:css视频教程
以上就是css如何实现边框长度控制功能的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
css grid-rows属性怎么用
谈谈css中的3种预处理器
css怎么去掉边框
css怎么在图片上显示遮罩层
css怎么设置显示隐藏动画
css字体颜色的设置方法
css实现元素横向滚动的方法
css border-top-style属性怎么用
左浮动 css怎么写
css class和id选择器的区别是什么
更多相关阅读请进入《css》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » css如何实现边框长度控制功能