控制虚线边框笔划长度和笔划之间的距离

发布于 2024-08-31 12:02:42 字数 631 浏览 5 评论 0原文

是否可以在CSS中控制虚线边框笔画之间的长度和距离?

下面的示例在浏览器之间显示有所不同:

div {
  border: dashed 4px #000;
  padding: 20px;
  display: inline-block;
}
<div>I have a dashed border!</div>

大差异:IE 11 / Firefox / Chrome

IE 11 borderFirefox BorderChrome border

是否有任何方法可以更好地控制虚线边框外观?

Is it possible to control the length and distance between dashed border strokes in CSS?

This example below displays differently between browsers:

div {
  border: dashed 4px #000;
  padding: 20px;
  display: inline-block;
}
<div>I have a dashed border!</div>

Big differences: IE 11 / Firefox / Chrome

IE 11 borderFirefox BorderChrome border

Are there any methods that can provide greater control of the dashed borders appearance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

最佳男配角 2024-09-07 12:02:43

本机虚线边框属性值不提供对虚线本身的控制...因此请使用 border-image 属性!

使用 border-image 创建您自己的边框

兼容性:它提供出色的浏览器支持(IE 11 和所有现代浏览器)。可以将正常边框设置为旧版浏览器的后备。

让我们创建这些

边框,这些边框将在跨浏览器上显示完全相同!

目标示例 目标示例间隙更宽

步骤 1 - 创建合适的图像

此示例为 15 像素宽 x 15 像素高,间隙当前为 5 像素宽。它是具有透明度的 .png。

这是在 Photoshop 中放大时的样子:

示例边框图像背景放大

这就是缩放后的样子:

示例边框图像背景实际大小

控制间隙和描边长度

要创建更宽/更短的间隙或描边,请加宽/缩短间隙或图像中的笔画。

这是一个具有更宽 10 像素间隙的图像:

Larger gaps 正确缩放 = 更大的缩放间隙

步骤 2 - 创建 CSS - 此示例需要 4 个基本步骤

  1. 定义 边框图像源

     border-image-source:url("https://i.sstatic.net/wLdVc.png");  
    
  2. < p>可选 - 定义边框图像宽度

     边框图像宽度:1;
    

    默认值为 1。也可以设置为像素值、百分比值或其他倍数(1x、2x、3x 等)。这会覆盖任何 border-width 设置。

  3. 定义border-image-slice

:在这个例子中,图像的上、右、下、左边框的厚度是 2px,并且它们之外没有间隙,所以我们的切片值为 2:

    border-image-slice: 2; 

切片看起来像这样,距离上、右、下各 2 个像素左:

Slices example

  1. 定义 border-image-repeat

在此示例中,我们希望图案在 div 周围均匀地重复。所以我们选择:

    border-image-repeat: round;

书写简写

上面的属性可以单独设置,也可以使用 border-image

border-image: url("https://i.sstatic.net/wLdVc.png") 2 round;

完整示例

注意:border-style 必须设置为一个值,例如 solid 或 < code>dashed 因为初始边框样式是none。或者,您可以添加后备边框,例如本例中的 border: dashed 4px #000,不支持的浏览器将收到此边框。

.bordered {
  display: inline-block;
  padding: 20px;
  /* Optional: 
     A normal dashed border can be set as a fallback, particularly if older browsers need to be supported. Notes:
     - the 4px width here is overwritten with any border-image-width value
     - a border-image-width can be omitted if it is the same value as the dashed border width
  */
  border: dashed 4px #000;/*optional*/

  /* the initial value of border-style is "none", so it must be set to a different value for the border-image to show*/
  border-style: dashed;

  /* Individual border image properties */
  border-image-source: url("https://i.sstatic.net/wLdVc.png");
  border-image-slice: 2;
  border-image-repeat: round;

  /* or use the shorthand border-image */
  border-image: url("https://i.sstatic.net/wLdVc.png") 2 round;
}


/*The border image of this one creates wider gaps*/

.largeGaps {
  border-image-source: url("https://i.sstatic.net/LKclP.png");
  margin: 0 20px;
}
<div class="bordered">This is bordered!</div>

<div class="bordered largeGaps">This is bordered and has larger gaps!</div>

The native dashed border property value does not offer control over the dashes themselves... so bring on the border-image property!

Brew your own border with border-image

Compatibility: It offers great browser support (IE 11 and all modern browsers). A normal border can be set as a fallback for older browsers.

Let's create these

These borders will display exactly the same cross-browser!

Goal example Goal example with wider gaps

Step 1 - Create a suitable image

This example is 15 pixels wide by 15 pixels high and the gaps are currently 5px wide. It is a .png with transparency.

This is what it looks like in photoshop when zoomed in:

Example Border Image Background Blown Up

This is what it looks like to scale:

Example Border Image Background Actual Size

Controlling gap and stroke length

To create wider / shorter gaps or strokes, widen / shorten the gaps or strokes in the image.

Here is an image with wider 10px gaps:

Larger gaps correctly scaled = Larger gaps to scale

Step 2 - Create the CSS — this example requires 4 basic steps

  1. Define the border-image-source:

     border-image-source:url("https://i.sstatic.net/wLdVc.png");  
    
  2. Optional - Define the border-image-width:

     border-image-width: 1;
    

    The default value is 1. It can also be set with a pixel value, percentage value, or as another multiple (1x, 2x, 3x etc). This overrides any border-width set.

  3. Define the border-image-slice:

In this example, the thickness of the images top, right, bottom and left borders is 2px, and there is no gap outside of them, so our slice value is 2:

    border-image-slice: 2; 

The slices look like this, 2 pixels from the top, right, bottom and left:

Slices example

  1. Define the border-image-repeat:

In this example, we want the pattern to repeat itself evenly around our div. So we choose:

    border-image-repeat: round;

Writing shorthand

The properties above can be set individually, or in shorthand using border-image:

border-image: url("https://i.sstatic.net/wLdVc.png") 2 round;

Complete example

Note: border-style must be set to a value such as solid or dashed as the initial border style is none. Alternatively, you can add a fallback border, such as border: dashed 4px #000 in this example, non-supporting browsers will receive this border.

.bordered {
  display: inline-block;
  padding: 20px;
  /* Optional: 
     A normal dashed border can be set as a fallback, particularly if older browsers need to be supported. Notes:
     - the 4px width here is overwritten with any border-image-width value
     - a border-image-width can be omitted if it is the same value as the dashed border width
  */
  border: dashed 4px #000;/*optional*/

  /* the initial value of border-style is "none", so it must be set to a different value for the border-image to show*/
  border-style: dashed;

  /* Individual border image properties */
  border-image-source: url("https://i.sstatic.net/wLdVc.png");
  border-image-slice: 2;
  border-image-repeat: round;

  /* or use the shorthand border-image */
  border-image: url("https://i.sstatic.net/wLdVc.png") 2 round;
}


/*The border image of this one creates wider gaps*/

.largeGaps {
  border-image-source: url("https://i.sstatic.net/LKclP.png");
  margin: 0 20px;
}
<div class="bordered">This is bordered!</div>

<div class="bordered largeGaps">This is bordered and has larger gaps!</div>

甜柠檬 2024-09-07 12:02:43

除了 border-image 属性之外,还有一些其他方法可以创建虚线边框,并控制笔画的长度和笔画之间的距离。它们的描述如下:

方法 1:使用 SVG

我们可以通过使用 pathpolygon 元素并设置 Strong-dasharray 属性。该属性采用两个参数,其中一个参数定义破折号的大小,另一个确定它们之间的间距。

优点

  1. SVG 本质上是可缩放的图形,可以适应任何容器尺寸。
  2. 即使涉及到 border-radius 也可以很好地工作。我们只需将 path 替换为 circle,如 这个答案(或)将路径转换为圆形。
  3. 浏览器对 SVG 的支持非常好,并且可以使用 IE8 的 VML 提供后备。

缺点:

  1. 当容器的尺寸不按比例变化时,路径往往会缩放,导致破折号的大小以及它们之间的空间发生变化(尝试将鼠标悬停在代码片段中的第一个框上) 。这可以通过添加vector-effect='non-scaling-lines'(如第二个框中所示)来控制,但 IE 中对此属性的浏览器支持为零。
.dashed-vector {
  position: relative;
  height: 100px;
  width: 300px;
}
svg {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
}
path{
  fill: none;
  stroke: blue;
  stroke-width: 5;
  stroke-dasharray: 10, 10;
}
span {
  position: absolute;
  top: 0px;
  left: 0px;
  padding: 10px;
}


/* just for demo */

div{
  margin-bottom: 10px;
  transition: all 1s;
}
div:hover{
  height: 100px;
  width: 400px;
}
<div class='dashed-vector'>
  <svg viewBox='0 0 300 100' preserveAspectRatio='none'>
    <path d='M0,0 300,0 300,100 0,100z' />
  </svg>
  <span>Some content</span>
</div>

<div class='dashed-vector'>
  <svg viewBox='0 0 300 100' preserveAspectRatio='none'>
    <path d='M0,0 300,0 300,100 0,100z' vector-effect='non-scaling-stroke'/>
  </svg>
  <span>Some content</span>
</div>


方法2:使用渐变

我们可以使用多个线性渐变背景图像并将它们适当放置以创建虚线边框效果。这也可以通过重复线性梯度来完成,但由于使用重复梯度,并没有太大的改进,因为我们需要每个梯度仅在一个方向上重复。

.dashed-gradient{
  height: 100px;
  width: 200px;
  padding: 10px;
  background-image: linear-gradient(to right, blue 50%, transparent 50%), linear-gradient(to right, blue 50%, transparent 50%), linear-gradient(to bottom, blue 50%, transparent 50%), linear-gradient(to bottom, blue 50%, transparent 50%);
  background-position: left top, left bottom, left top, right top;
  background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
  background-size: 20px 3px, 20px 3px, 3px 20px, 3px 20px;
}

.dashed-repeating-gradient {
  height: 100px;
  width: 200px;
  padding: 10px;
  background-image: repeating-linear-gradient(to right, blue 0%, blue 50%, transparent 50%, transparent 100%), repeating-linear-gradient(to right, blue 0%, blue 50%, transparent 50%, transparent 100%), repeating-linear-gradient(to bottom, blue 0%, blue 50%, transparent 50%, transparent 100%), repeating-linear-gradient(to bottom, blue 0%, blue 50%, transparent 50%, transparent 100%);
  background-position: left top, left bottom, left top, right top;
  background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
  background-size: 20px 3px, 20px 3px, 3px 20px, 3px 20px;
}

/* just for demo */

div {
  margin: 10px;
  transition: all 1s;
}
div:hover {
  height: 150px;
  width: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='dashed-gradient'>Some content</div>
<div class='dashed-repeating-gradient'>Some content</div>

优点

  1. 可扩展,即使容器的尺寸是动态的,也能适应。
  2. 不使用任何额外的伪元素,这意味着它们可以保留用于任何其他潜在用途。

缺点:

  1. 浏览器对线性渐变的支持相对较低,这是如果您想支持 IE 9-,则不行。即使像 CSS3 PIE 这样的库也不支持在 IE8 中创建渐变图案。
  2. 当涉及 border-radius 时无法使用,因为背景不会根据 border-radius 弯曲。相反,它们被剪掉了。

方法 3:框阴影

我们可以使用伪元素创建一个小条(以破折号的形状),然后创建它的多个 box-shadow 版本来创建边框,如下面的代码片段所示。

如果破折号是正方形,那么单个伪元素就足够了,但如果它是矩形,我们需要一个伪元素用于顶部 + 底部边框,另一个伪元素用于左 + 右边框。这是因为顶部边框上的破折号的高度和宽度与左侧的破折号不同。

优点

  1. 可以通过更改伪元素的尺寸来控制破折号的尺寸。通过修改每个阴影之间的间距可以控制间距。
  2. 通过为每个盒子阴影添加不同的颜色可以产生非常独特的效果。

缺点:

  1. 由于我们必须手动设置破折号的尺寸和间距,因此当父框的尺寸是动态的时,这种方法并不好。
  2. IE8 及更低版本不支持框阴影。但是,可以通过使用 CSS3 PIE 等库来克服这个问题。
  3. 可以与 border-radius 一起使用,但定位它们会非常棘手,因为必须在圆上找到点(甚至可能是变换)。
.dashed-box-shadow{
  position: relative;
  height: 120px;
  width: 120px;
  padding: 10px;
}
.dashed-box-shadow:before{ /* for border top and bottom */
  position: absolute;
  content: '';
  top: 0px;
  left: 0px;
  height: 3px; /* height of the border top and bottom */
  width: 10px; /* width of the border top and bottom */
  background: blue; /* border color */
  box-shadow: 20px 0px 0px blue, 40px 0px 0px blue, 60px 0px 0px blue, 80px 0px 0px blue, 100px 0px 0px blue, /* top border */
    0px 110px 0px blue, 20px 110px 0px blue, 40px 110px 0px blue, 60px 110px 0px blue, 80px 110px 0px blue, 100px 110px 0px blue; /* bottom border */
}
.dashed-box-shadow:after{ /* for border left and right */
  position: absolute;
  content: '';
  top: 0px;
  left: 0px;
  height: 10px; /* height of the border left and right */
  width: 3px; /* width of the border left and right */
  background: blue; /* border color */
  box-shadow: 0px 20px 0px blue, 0px 40px 0px blue, 0px 60px 0px blue, 0px 80px 0px blue, 0px 100px 0px blue, /* left border */
    110px 0px 0px blue, 110px 20px 0px blue, 110px 40px 0px blue, 110px 60px 0px blue, 110px 80px 0px blue, 110px 100px 0px blue; /* right border */
}
<div class='dashed-box-shadow'>Some content</div>

In addition to the border-image property, there are a few other ways to create a dashed border with control over the length of the stroke and the distance between them. They are described below:

Method 1: Using SVG

We can create the dashed border by using a path or a polygon element and setting the stroke-dasharray property. The property takes two parameters where one defines the size of the dash and the other determines the space between them.

Pros:

  1. SVGs by nature are scalable graphics and can adapt to any container dimensions.
  2. Can work very well even if there is a border-radius involved. We would just have replace the path with a circle like in this answer (or) convert the path into a circle.
  3. Browser support for SVG is pretty good and fallback can be provided using VML for IE8-.

Cons:

  1. When the dimensions of the container do not change proportionately, the paths tend to scale resulting in a change in size of the dash and the space between them (try hovering on the first box in the snippet). This can be controlled by adding vector-effect='non-scaling-stroke' (as in the second box) but the browser support for this property is nil in IE.

.dashed-vector {
  position: relative;
  height: 100px;
  width: 300px;
}
svg {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
}
path{
  fill: none;
  stroke: blue;
  stroke-width: 5;
  stroke-dasharray: 10, 10;
}
span {
  position: absolute;
  top: 0px;
  left: 0px;
  padding: 10px;
}


/* just for demo */

div{
  margin-bottom: 10px;
  transition: all 1s;
}
div:hover{
  height: 100px;
  width: 400px;
}
<div class='dashed-vector'>
  <svg viewBox='0 0 300 100' preserveAspectRatio='none'>
    <path d='M0,0 300,0 300,100 0,100z' />
  </svg>
  <span>Some content</span>
</div>

<div class='dashed-vector'>
  <svg viewBox='0 0 300 100' preserveAspectRatio='none'>
    <path d='M0,0 300,0 300,100 0,100z' vector-effect='non-scaling-stroke'/>
  </svg>
  <span>Some content</span>
</div>


Method 2: Using Gradients

We can use multiple linear-gradient background images and position them appropriately to create a dashed border effect. This can also be done with a repeating-linear-gradient but there is not much improvement because of using a repeating gradient as we need each gradient to repeat in only one direction.

.dashed-gradient{
  height: 100px;
  width: 200px;
  padding: 10px;
  background-image: linear-gradient(to right, blue 50%, transparent 50%), linear-gradient(to right, blue 50%, transparent 50%), linear-gradient(to bottom, blue 50%, transparent 50%), linear-gradient(to bottom, blue 50%, transparent 50%);
  background-position: left top, left bottom, left top, right top;
  background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
  background-size: 20px 3px, 20px 3px, 3px 20px, 3px 20px;
}

.dashed-repeating-gradient {
  height: 100px;
  width: 200px;
  padding: 10px;
  background-image: repeating-linear-gradient(to right, blue 0%, blue 50%, transparent 50%, transparent 100%), repeating-linear-gradient(to right, blue 0%, blue 50%, transparent 50%, transparent 100%), repeating-linear-gradient(to bottom, blue 0%, blue 50%, transparent 50%, transparent 100%), repeating-linear-gradient(to bottom, blue 0%, blue 50%, transparent 50%, transparent 100%);
  background-position: left top, left bottom, left top, right top;
  background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
  background-size: 20px 3px, 20px 3px, 3px 20px, 3px 20px;
}

/* just for demo */

div {
  margin: 10px;
  transition: all 1s;
}
div:hover {
  height: 150px;
  width: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='dashed-gradient'>Some content</div>
<div class='dashed-repeating-gradient'>Some content</div>

Pros:

  1. Scalable and can adapt even if the container's dimensions are dynamic.
  2. Does not make use of any extra pseudo-elements which means they can be kept aside for any other potential usage.

Cons:

  1. Browser support for linear gradients is comparatively lower and this is a no-go if you want to support IE 9-. Even libraries like CSS3 PIE do not support creation of gradient patterns in IE8-.
  2. Cannot be used when border-radius is involved because backgrounds don't curve based on border-radius. They get clipped instead.

Method 3: Box Shadows

We can create a small bar (in the shape of the dash) using pseudo-elements and then create multiple box-shadow versions of it to create a border like in the below snippet.

If the dash is a square shape then a single pseudo-element would be enough but if it is a rectangle, we would need one pseudo-element for the top + bottom borders and another for left + right borders. This is because the height and width for the dash on the top border will be different from that on the left.

Pros:

  1. The dimensions of the dash is controllable by changing the dimensions of the pseudo-element. The spacing is controllable by modifying the space between each shadow.
  2. A very unique effect can be produced by adding a different color for each box shadow.

Cons:

  1. Since we have to manually set the dimensions of the dash and the spacing, this approach is no good when the dimensions of the parent box is dynamic.
  2. IE8 and lower do not support box shadow. However, this can be overcome by using libraries like CSS3 PIE.
  3. Can be used with border-radius but positioning them would be very tricky with having to find points on a circle (and possibly even transform).

.dashed-box-shadow{
  position: relative;
  height: 120px;
  width: 120px;
  padding: 10px;
}
.dashed-box-shadow:before{ /* for border top and bottom */
  position: absolute;
  content: '';
  top: 0px;
  left: 0px;
  height: 3px; /* height of the border top and bottom */
  width: 10px; /* width of the border top and bottom */
  background: blue; /* border color */
  box-shadow: 20px 0px 0px blue, 40px 0px 0px blue, 60px 0px 0px blue, 80px 0px 0px blue, 100px 0px 0px blue, /* top border */
    0px 110px 0px blue, 20px 110px 0px blue, 40px 110px 0px blue, 60px 110px 0px blue, 80px 110px 0px blue, 100px 110px 0px blue; /* bottom border */
}
.dashed-box-shadow:after{ /* for border left and right */
  position: absolute;
  content: '';
  top: 0px;
  left: 0px;
  height: 10px; /* height of the border left and right */
  width: 3px; /* width of the border left and right */
  background: blue; /* border color */
  box-shadow: 0px 20px 0px blue, 0px 40px 0px blue, 0px 60px 0px blue, 0px 80px 0px blue, 0px 100px 0px blue, /* left border */
    110px 0px 0px blue, 110px 20px 0px blue, 110px 40px 0px blue, 110px 60px 0px blue, 110px 80px 0px blue, 110px 100px 0px blue; /* right border */
}
<div class='dashed-box-shadow'>Some content</div>

蓝咒 2024-09-07 12:02:43

输入图片此处描述

有一个由 @kovart 制作的很酷的工具,称为 虚线边框生成器

它使用 svg 作为背景图像来允许设置您想要的笔划破折号数组,并且非常方便。

然后,您只需将其用作元素上的背景属性来代替边框:

div {
  background-image: url("data:image/svg+xml,%3csvg width='100%25' height='100%25' xmlns='http://www.w3.org/2000/svg'%3e%3crect width='100%25' height='100%25' fill='none' stroke='black' stroke-width='4' stroke-dasharray='6%2c 14' stroke-dashoffset='0' stroke-linecap='square'/%3e%3c/svg%3e");
  padding: 20px;
  display: inline-block;
}

enter image description here

There's a cool tool made by @kovart called the dashed border generator.

It uses an svg as a background image to allow setting the stroke dash array you desire, and is pretty convenient.

You would then simply use it as the background property on your element in place of the border:

div {
  background-image: url("data:image/svg+xml,%3csvg width='100%25' height='100%25' xmlns='http://www.w3.org/2000/svg'%3e%3crect width='100%25' height='100%25' fill='none' stroke='black' stroke-width='4' stroke-dasharray='6%2c 14' stroke-dashoffset='0' stroke-linecap='square'/%3e%3c/svg%3e");
  padding: 20px;
  display: inline-block;
}
弱骨蛰伏 2024-09-07 12:02:43

更新
感谢 kovart 提供了这个很棒的工具,尝试一下
https://kovart.github.io/dashed-border-generator/

 生成虚线的工具自定义边框

我的回答是:

我最近也遇到了同样的问题。
我已经解决了这个问题,希望对某人有所帮助。

HTML + 顺风

<div class="dashed-border h-14 w-full relative rounded-lg">
    <div class="w-full h-full rounded-lg bg-page z-10 relative">
        Content goes here...
    <div>
</div>

CSS

.dashed-border::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: calc(100% + 4px);
  transform: translateY(-50%);
  background-image: linear-gradient(to right, #333 50%, transparent 50%);
  background-size: 16px;
  z-index: 0;
  border-radius: 0.5rem;
}
.dashed-border::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 0;
  height: 100%;
  width: calc(100% + 4px);
  transform: translateX(-50%);
  background-image: linear-gradient(to bottom, #333 50%, transparent 50%);
  background-size: 4px 16px;
  z-index: 1;
  border-radius: 0.5rem;
}

Update
Thanks to kovart for this great tool try it
https://kovart.github.io/dashed-border-generator/

 a tool to generate dashed custom borders

my answer was:

I just recently had the same problem.
I have made this work around, hope it will help someone.

HTML + tailwind

<div class="dashed-border h-14 w-full relative rounded-lg">
    <div class="w-full h-full rounded-lg bg-page z-10 relative">
        Content goes here...
    <div>
</div>

CSS

.dashed-border::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: calc(100% + 4px);
  transform: translateY(-50%);
  background-image: linear-gradient(to right, #333 50%, transparent 50%);
  background-size: 16px;
  z-index: 0;
  border-radius: 0.5rem;
}
.dashed-border::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 0;
  height: 100%;
  width: calc(100% + 4px);
  transform: translateX(-50%);
  background-image: linear-gradient(to bottom, #333 50%, transparent 50%);
  background-size: 4px 16px;
  z-index: 1;
  border-radius: 0.5rem;
}
奈何桥上唱咆哮 2024-09-07 12:02:43

简而言之:不,不是。您将不得不使用图像。

Short one: No, it's not. You will have to work with images instead.

温馨耳语 2024-09-07 12:02:43

CSS 渲染是特定于浏览器的,我不知道对其进行任何微调,您应该按照 Ham 的建议使用图像。
参考:http://www.w3.org/TR/CSS2 /box.html#border-style-properties

Css render is browser specific and I don't know any fine tuning on it, you should work with images as recommended by Ham.
Reference: http://www.w3.org/TR/CSS2/box.html#border-style-properties

北城孤痞 2024-09-07 12:02:43

笔划长度取决于笔划宽度。您可以通过增加宽度来增加长度,并通过内部元素隐藏部分边框。

编辑: 添加了 pointer-events: none; 感谢 benJ。

.thin {
    background: #F4FFF3;
    border: 2px dashed #3FA535;  
    position: relative;
}

.thin:after {
    content: '';
    position: absolute;
    left: -1px;
    top: -1px;
    right: -1px;
    bottom: -1px;
    border: 1px solid #F4FFF3;
    pointer-events: none;
}

https://jsfiddle.net/ksf9zoLh/

Stroke length depends on stroke width. You can increase length by increasing width and hide part of border by inner element.

EDIT: added pointer-events: none; thanks to benJ.

.thin {
    background: #F4FFF3;
    border: 2px dashed #3FA535;  
    position: relative;
}

.thin:after {
    content: '';
    position: absolute;
    left: -1px;
    top: -1px;
    right: -1px;
    bottom: -1px;
    border: 1px solid #F4FFF3;
    pointer-events: none;
}

https://jsfiddle.net/ksf9zoLh/

情域 2024-09-07 12:02:43
.outline {
    outline: 48px dashed #d5fb62;
    outline-offset: -4px;
    overflow:hidden;
}

如果溢出隐藏不是问题,则概述 4 而不是 48。

<div class="outline"></div>
.outline {
    outline: 48px dashed #d5fb62;
    outline-offset: -4px;
    overflow:hidden;
}

if overflow hidden not problem else outline 4 instead 48.

<div class="outline"></div>
梦萦几度 2024-09-07 12:02:43

我想我刚刚通过使用 clip-path 属性找到了解决此问题的最终解决方案。基本上只需添加虚线边框然后掩盖多余的部分即可。

clip-path 属性还支持圆角,因此您可以将其与 border-radius 匹配,并具有自定义虚线边框和圆角!

.demo {
  display: inline-flex;
  width: 200px;
  height: 100px;
  position: relative;
  clip-path: inset(0 round 30px 0 30px 0);
}

.demo::before {
  content: '';
  position: absolute;
  left: -7px;
  top: -7px;
  right: -7px;
  bottom: -7px;
  border: 8px dashed rgba(0, 0, 255, 0.3);
  border-radius: 37px 0 37px 0;
  box-sizing: border-box;
}
<div class="demo"></div>

当然,您可以直接在 div 本身上执行此操作,而无需使用 ::after 伪元素。但这意味着您必须剪辑到 div 中,并且它最终会小于其初始大小。

I think I've just found the definitive solution to this problem with the use of clip-path property. Basically all there is to add a dashed border then mask the excess.

The clip-path property also supports rounded corners so you can match it up with the border-radius and have custom dashed borders and rounded corners!

.demo {
  display: inline-flex;
  width: 200px;
  height: 100px;
  position: relative;
  clip-path: inset(0 round 30px 0 30px 0);
}

.demo::before {
  content: '';
  position: absolute;
  left: -7px;
  top: -7px;
  right: -7px;
  bottom: -7px;
  border: 8px dashed rgba(0, 0, 255, 0.3);
  border-radius: 37px 0 37px 0;
  box-sizing: border-box;
}
<div class="demo"></div>

You could do this directly on the div itself of course without using the ::after pseudo element. But this would mean you have to clip into the div and it would end up smaller than it's initial size.

仙气飘飘 2024-09-07 12:02:43

我最近也遇到了同样的问题。

我设法用两个带有边框的绝对定位 div(一个用于水平,一个用于垂直),然后转换它们来解决这个问题。
外盒只需相对定位即可。

<div class="relative">
    <div class="absolute absolute--fill overflow-hidden">
        <div class="absolute absolute--fill b--dashed b--red"
            style="
                border-width: 4px 0px 4px 0px;
                transform: scaleX(2);
        "></div>
        <div class="absolute absolute--fill b--dashed b--red"
            style="
                border-width: 0px 4px 0px 4px;
                transform: scaleY(2);
        "></div>
    </div>

    <div> {{Box content goes here}} </div>
</div>

注意:我在这个例子中使用了快子,但我想这些类是不言自明的。

I just recently had the same problem.

I managed to solve it with two absolutely positioned divs carrying the border (one for horizontal and one for vertical), and then transforming them.
The outer box just needs to be relatively positioned.

<div class="relative">
    <div class="absolute absolute--fill overflow-hidden">
        <div class="absolute absolute--fill b--dashed b--red"
            style="
                border-width: 4px 0px 4px 0px;
                transform: scaleX(2);
        "></div>
        <div class="absolute absolute--fill b--dashed b--red"
            style="
                border-width: 0px 4px 0px 4px;
                transform: scaleY(2);
        "></div>
    </div>

    <div> {{Box content goes here}} </div>
</div>

Note: i used tachyons in this example, but i guess the classes are kind of self-explanatory.

陌生 2024-09-07 12:02:43

这将使用 div 上的 class="myclass" 创建橙色和灰色边框。

.myclass {
    outline:dashed darkorange  12px;
    border:solid slategray  14px;
    outline-offset:-14px;
}

This will make an orange and gray border using the class="myclass" on the div.

.myclass {
    outline:dashed darkorange  12px;
    border:solid slategray  14px;
    outline-offset:-14px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文