考虑以下内容:
<div class="box">
...
</div>
.box{
width:500px;
border-bottom: 1px solid #ccc;
}
它将设置盒子的底部边框为全宽(500px)。 但是我想将底部边框设置为300px,位于盒子底部中央,应该如何操作?
您可以使用 ::after 或 ::before 伪选择器。例如:
<div> something here </div>
CSS:
div {
width: 500px;
height: 100px;
position: relative;
padding-top: 20px;
margin-top: 50px;
}
div::before {
content: '';
display: block;
position: absolute;
top: 0;
width: 50%;
left: 25%;
border-top: 1px solid red;
}
这里是jsfiddle
height: 1px;
然后在伪元素上设置 background: red;
。 - dev_masta<hr>
标签?<div class="box">
...
<hr>
</div>
.box{
width:500px;
}
.box hr{
width: 300px;
border-bottom: 1px solid #ccc;
}
.box {
padding-bottom: 10px;
background-image: linear-gradient(#ccc,#ccc);
background-size: 50% 2px;
background-position: bottom left;
background-repeat: no-repeat;
}
.box {
position: relative;
width: 500px;
}
.border {
position: aboslute;
background: #ccc;
left: 100px;
bottom: 0;
width: 300px;
height: 1px;
}
<div class="box">
<div class="border">
</div>
</div>
但是有无限的可能性。其中一些比其他的更符合语义学规范;这个解决方案只是一个快速修复。
您可以使用背景图片:
.box{
width:500px;
border-bottom: 1px solid #ccc;
background-image:(yourimage.png); /*make your image a solid line 1px tall by 250px wide (or so)*/
background-position: bottom left;
background-repeat:no-repeat;
}
background-image: linear-gradient(black, black); background-size: 50% 1px; background-position: 50% 100%
。 - Razvan Caliman<style type="text/css">
.box{
width: 500px;
height: 20px;
}
.boxHalfWidth{
width: 250px;
height: 1px;
border-bottom: 1px solid #CCC;
}
</style>
</head>
<body>
<div class="box">
some data
<div class="boxHalfWidth"></div>
</div>
</body>
css:
display: block;
margin: 0 auto;
width: 50%;
padding-top: 20px;
border-bottom: 1px solid #000;
<hr>
吗?然后你可以根据需要精确地设置样式。 - Chase Florellbox
中的内容。 - Chase Florell