有什么方法可以限制边框长度?

mcb*_*eav 197 css css3

有没有办法限制边框的长度.我有一个<div>底部边框,但我想在左边添加一个边框<div>,只延伸一半.

如果没有在页面上添加额外的元素,有没有办法这样做?

小智 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: "";为了使伪元素呈现,声明是必要的)

  • 我认为这在语义上是一个更好的选择,因为它不会引入额外的div. (18认同)
  • 这应该是公认的答案,因为它有利于CSS作为标记样式的唯一手段. (18认同)
  • 完善。而且它也可以按预期与padding和margin一起使用。 (3认同)
  • 请记住:你**不能**管理来自JS**的伪元素属性**(实际上你可以,但它会非常hacky).因此,如果您计划使用此解决方案制定一种进度条 - 请忘记.选择已经接受的答案.在所有其他情况下,这个答案更好,这是真的. (3认同)
  • 这个跨浏览器和/或 JS 兼容吗?由于@SergeyPanfilov 的评论,我更喜欢接受的答案。 (2认同)

小智 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)

  • 这仍然是最好的方法.它具有跨浏览器兼容性,易于维护. (5认同)
  • 与古老的浏览器相比,这可能具有少量的跨浏览器兼容性优势,并且更容易通过JS进行控制...但是这些都不是原始问题的关注点。但是,这确实在标记中添加了一个额外的元素,原始问题专门要求避免这种标记。所以我不明白为什么这是这个问题的可接受答案。 (2认同)

小智 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)

  • 您可以添加 HTML 示例吗?您认为谁可以将它与表格单元格一起使用,有一些线索吗? (2认同)

Moh*_*man 16

使用CSS属性,我们只能控制边框的厚度; 不长.

但是,我们可以模仿边界效应并控制其widthheight为我们想一些其他方式.

使用CSS(线性渐变):

我们可以使用linear-gradient()创建背景图像并使用CSS控制其大小和位置,使其看起来像边框.由于我们可以将多个背景图像应用于元素,因此我们可以使用此功能创建多个边框状图像并应用于元素的不同侧面.我们还可以使用一些纯色,渐变或背景图像覆盖剩余的可用区域.

必填HTML:

我们所需要的只是一个元素(可能有一些类).

<div class="box"></div>
Run Code Online (Sandbox Code Playgroud)

脚步:

  1. 使用创建背景图像linear-gradient().
  2. 使用background-size调整width/ height上面创建的图像(S),使得它看起来像一个边界.
  3. 使用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)

但它不可能限制边界高度.只有元素高度.


Ben*_*Ben 8

边界仅在每一侧定义,而不是在一侧的分数中定义.所以,不,你不能这样做.

此外,一个新元素也不会是边界,它只会模仿你想要的行为 - 但它仍然是一个元素.


Jus*_*nce 6

这是一个 CSS 技巧,而不是正式的解决方案。我将代码保留为黑色,因为它可以帮助我定位元素。之后,为您的内容(颜色:白色)和(边距顶部:-5px 左右)着色,使其看起来好像没有句号。

div.yourdivname:after {
content: ".";
  border-bottom:1px solid grey;
  width:60%;
  display:block;
  margin:0 auto;
}
Run Code Online (Sandbox Code Playgroud)


Dan*_* Z. 5

做到这一点的另一种方法是结合使用边界图像和线性渐变。

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岁以上

为了获得更好的支持,还添加供应商前缀。

犬的边框图像