小智 238
CSS生成的内容可以为您解决此问题:
div {
position: relative;
}
/* Main div for border to extend to 50% from bottom left corner */
div:after {
content: "";
background: black;
position: absolute;
bottom: 0;
left: 0;
height: 50%;
width: 1px;
}Run Code Online (Sandbox Code Playgroud)
(注意 - content: "";为了使伪元素呈现,声明是必要的)
小智 164
希望这可以帮助:
#mainDiv {
height: 100px;
width: 80px;
position: relative;
border-bottom: 2px solid #f51c40;
background: #3beadc;
}
#borderLeft {
border-left: 2px solid #f51c40;
position: absolute;
top: 50%;
bottom: 0;
}Run Code Online (Sandbox Code Playgroud)
<div id="mainDiv">
<div id="borderLeft"></div>
</div>Run Code Online (Sandbox Code Playgroud)
小智 33
该:after岩石:)
如果你玩了一下,你甚至可以将调整大小的边框元素设置为居中或仅在旁边有另一个元素时出现(如在菜单中).这是一个菜单示例:
#menu > ul > li {
position: relative;
float: left;
padding: 0 10px;
}
#menu > ul > li + li:after {
content:"";
background: #ccc;
position: absolute;
bottom: 25%;
left: 0;
height: 50%;
width: 1px;
}
Run Code Online (Sandbox Code Playgroud)
#menu > ul > li {
position: relative;
float: left;
padding: 0 10px;
list-style: none;
}
#menu > ul > li + li:after {
content: "";
background: #ccc;
position: absolute;
bottom: 25%;
left: 0;
height: 50%;
width: 1px;
}Run Code Online (Sandbox Code Playgroud)
<div id="menu">
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
</div>Run Code Online (Sandbox Code Playgroud)
Moh*_*man 16
使用CSS属性,我们只能控制边框的厚度; 不长.
但是,我们可以模仿边界效应并控制其width和height为我们想一些其他方式.
我们可以使用linear-gradient()创建背景图像并使用CSS控制其大小和位置,使其看起来像边框.由于我们可以将多个背景图像应用于元素,因此我们可以使用此功能创建多个边框状图像并应用于元素的不同侧面.我们还可以使用一些纯色,渐变或背景图像覆盖剩余的可用区域.
必填HTML:
我们所需要的只是一个元素(可能有一些类).
<div class="box"></div>
Run Code Online (Sandbox Code Playgroud)
脚步:
linear-gradient().background-size调整width/ height上面创建的图像(S),使得它看起来像一个边界.background-position调整位置(如left,right,left bottom等等)将上面创建的边界(或多个).必要的CSS:
.box {
background-image: linear-gradient(purple, purple),
// Above css will create background image that looks like a border.
linear-gradient(steelblue, steelblue);
// This will create background image for the container.
background-repeat: no-repeat;
/* First sizing pair (4px 50%) will define the size of the border i.e border
will be of having 4px width and 50% height. */
/* 2nd pair will define the size of stretched background image. */
background-size: 4px 50%, calc(100% - 4px) 100%;
/* Similar to size, first pair will define the position of the border
and 2nd one for the container background */
background-position: left bottom, 4px 0;
}
Run Code Online (Sandbox Code Playgroud)
例子:
随着linear-gradient()我们可以创建具有渐变的纯色边框以及.以下是使用此方法创建的边框的一些示例.
仅在一侧应用边框的示例:
.container {
display: flex;
}
.box {
background-image: linear-gradient(purple, purple),
linear-gradient(steelblue, steelblue);
background-repeat: no-repeat;
background-size: 4px 50%, calc(100% - 4px) 100%;
background-position: left bottom, 4px 0;
height: 160px;
width: 160px;
margin: 20px;
}
.gradient-border {
background-image: linear-gradient(red, purple),
linear-gradient(steelblue, steelblue);
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div class="box"></div>
<div class="box gradient-border"></div>
</div>Run Code Online (Sandbox Code Playgroud)
边框应用于两侧的示例:
.container {
display: flex;
}
.box {
background-image: linear-gradient(purple, purple),
linear-gradient(purple, purple),
linear-gradient(steelblue, steelblue);
background-repeat: no-repeat;
background-size: 4px 50%, 4px 50%, calc(100% - 8px) 100%;
background-position: left bottom, right top, 4px 0;
height: 160px;
width: 160px;
margin: 20px;
}
.gradient-border {
background-image: linear-gradient(red, purple),
linear-gradient(purple, red),
linear-gradient(steelblue, steelblue);
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div class="box"></div>
<div class="box gradient-border"></div>
</div>Run Code Online (Sandbox Code Playgroud)
边框应用于所有边的示例:
.container {
display: flex;
}
.box {
background-image: linear-gradient(purple, purple),
linear-gradient(purple, purple),
linear-gradient(purple, purple),
linear-gradient(purple, purple),
linear-gradient(steelblue, steelblue);
background-repeat: no-repeat;
background-size: 4px 50%, 50% 4px, 4px 50%, 50% 4px, calc(100% - 8px) calc(100% - 8px);
background-position: left bottom, left bottom, right top, right top, 4px 4px;
height: 160px;
width: 160px;
margin: 20px;
}
.gradient-border {
background-image: linear-gradient(red, purple),
linear-gradient(to right, purple, red),
linear-gradient(to bottom, purple, red),
linear-gradient(to left, purple, red),
linear-gradient(steelblue, steelblue);
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div class="box"></div>
<div class="box gradient-border"></div>
</div>Run Code Online (Sandbox Code Playgroud)
截图:
Edm*_*mhs 14
对于水平线,您可以使用hr标签:
hr { width: 90%; }
Run Code Online (Sandbox Code Playgroud)
但它不可能限制边界高度.只有元素高度.
这是一个 CSS 技巧,而不是正式的解决方案。我将代码保留为黑色,因为它可以帮助我定位元素。之后,为您的内容(颜色:白色)和(边距顶部:-5px 左右)着色,使其看起来好像没有句号。
div.yourdivname:after {
content: ".";
border-bottom:1px solid grey;
width:60%;
display:block;
margin:0 auto;
}
Run Code Online (Sandbox Code Playgroud)
做到这一点的另一种方法是结合使用边界图像和线性渐变。
div {
width: 100px;
height: 75px;
background-color: green;
background-clip: content-box; /* so that the background color is not below the border */
border-left: 5px solid black;
border-image: linear-gradient(to top, #000 50%, rgba(0,0,0,0) 50%); /* to top - at 50% transparent */
border-image-slice: 1;
}Run Code Online (Sandbox Code Playgroud)
<div></div>Run Code Online (Sandbox Code Playgroud)
jsfiddle:https ://jsfiddle.net/u7zq0amc/1/
浏览器支持:IE:11+
Chrome:全部
Firefox:15岁以上
为了获得更好的支持,还添加供应商前缀。
| 归档时间: |
|
| 查看次数: |
419909 次 |
| 最近记录: |