我在我的方框中使用虚线样式的边框
.box {
width: 300px;
height: 200px;
border: dotted 1px #f00;
float: left;
}
我想增加边界上每个点之间的空间。
我在我的方框中使用虚线样式的边框
.box {
width: 300px;
height: 200px;
border: dotted 1px #f00;
float: left;
}
我想增加边界上每个点之间的空间。
当前回答
在我的情况下,我需要弯曲的角和薄边界,所以我想出了这个解决方案:
/* For showing dependencies between attributes */ :root { --border-width: 1px; --border-radius: 4px; --bg-color: #fff; } /* Required: */ .dropzone { position: relative; border: var(--border-width) solid transparent; border-radius: var(--border-radius); background-clip: padding-box; background-color: var(--bg-color); } .dropzone::before { content: ''; position: absolute; top: calc(var(--border-width) * -1); /* or without variables: 'top: -1px;' */ right: calc(var(--border-width) * -1); bottom: calc(var(--border-width) * -1); left: calc(var(--border-width) * -1); z-index: -1; background-image: repeating-linear-gradient(135deg, transparent 0 8px, var(--bg-color) 8px 16px); border-radius: var(--border-radius); background-color: rgba(0, 0, 0, 0.38); } /* Optional: */ html { background-color: #fafafb; display: flex; justify-content: center; } .dropzone { box-sizing: border-box; height: 168px; padding: 16px; display: flex; align-items: center; justify-content: center; cursor: pointer; } .dropzone::before { transition: background-color 0.2s ease-in-out; } .dropzone:hover::before { background-color: rgba(0, 0, 0, 0.87); } <div class='dropzone'> Drag 'n' drop some files here, or click to select files </div>
其思想是将svg模式放在元素后面,并仅显示该模式的细线作为元素边界。
其他回答
border-style的可用值请参阅MDN文档:
none:没有边框,设置宽度为0。 这是默认值。
hidden:与'none'相同,不同之处在于 边境冲突解决表 元素。 虚线:短的系列 破折号或线段。 点: 一系列的点。 双倍的:两连 加起来等于像素量的行 定义为border-width。 槽: 雕刻效果。 插图:制作盒子 嵌入的出现。 开始:相反的 “插图”。使盒子呈现3D (压花)。 脊:相对的 “槽”。边框显示为3D (出来)。 单体:单体, 直线,实线。
除了这些选择之外,没有任何方法可以影响标准边框的样式。
如果你不喜欢这种可能性,你可以使用CSS3的border-image,但请注意,浏览器对此的支持仍然非常不稳定(编辑:截至2020年,浏览器的支持很好)。
这是我在最近的一个项目中使用的一个技巧,它几乎可以实现我想要的任何水平边框。每次需要水平边框时,我都使用<hr/>。给这个hr添加边框的基本方法是
hr {border-bottom: 1px dotted #000;}
但如果你想控制边界,例如增加点之间的间距,你可以尝试这样做:
hr {
height:14px; /* specify a height for this hr */
overflow:hidden;
}
在下面,你创建你的边界(这里是一个点的例子)
hr:after {
content:".......................................................................";
letter-spacing: 4px; /* Use letter-spacing to increase space between dots*/
}
这也意味着你可以添加文本阴影的点,梯度等。你想要什么都行……
它对水平边界非常有效。如果你需要垂直的,你可以为另一个hr指定一个类,并使用CSS3的rotation属性。
这使用了标准的CSS边框和一个伪元素+overflow:hidden。 在这个例子中,你得到了三个不同的2px虚线边界:normal,间隔为5px,间隔为10px。实际上是10px,只有10-8=2px可见。
div.two{border:2px dashed #FF0000} div.five:before { content: ""; position: absolute; border: 5px dashed #FF0000; top: -3px; bottom: -3px; left: -3px; right: -3px; } div.ten:before { content: ""; position: absolute; border: 10px dashed #FF0000; top: -8px; bottom: -8px; left: -8px; right: -8px; } div.odd:before {left:0;right:0;border-radius:60px} div { overflow: hidden; position: relative; text-align:center; padding:10px; margin-bottom:20px; } <div class="two">Kupo nuts here</div> <div class="five">Kupo nuts<br/>here</div> <div class="ten">Kupo<br/>nuts<br/>here</div> <div class="ten odd">Kupo<br/>nuts<br/>here</div>
应用于小元素的大圆角可能会产生一些有趣的效果。
这是一个非常老的问题,但它在谷歌中有很高的排名,所以我将抛出我的方法,它可以根据你的需要工作。
在我的例子中,我想要一个粗虚线边界,在虚线之间有一个最小的中断。我用一个CSS图案生成器(比如:http://www.patternify.com/)创建了一个10px宽1px高的图案。其中9px为纯色,1px为白色。
在我的CSS中,我将该图案作为背景图像,然后使用background-size属性将其放大。我最终得到了一个20px * 2px的重复破折号,18px的实线和2px的白色。你可以把虚线放大一点。
好的方面是,由于图像被编码为数据,因此不需要额外的外部HTTP请求,因此没有性能负担。我将图像存储为SASS变量,以便在我的站点中重用它。
这是一个古老但仍然非常相关的话题。目前最上面的答案很好,但只适用于非常小的点。正如Bhojendra Rauniyar已经在评论中指出的,对于较大的点(>2px),点看起来是方形的,而不是圆形的。我发现这个页面搜索的是间隔点,而不是间隔方块(甚至破折号,就像这里的一些答案使用的那样)。
在此基础上,我使用了径向梯度。同样,使用Ukuser32的答案,可以很容易地为所有四个边框重复点属性。只有角落不完美。
div { padding: 1em; background-image: radial-gradient(circle at 2.5px, #000 1.25px, rgba(255,255,255,0) 2.5px), radial-gradient(circle, #000 1.25px, rgba(255,255,255,0) 2.5px), radial-gradient(circle at 2.5px, #000 1.25px, rgba(255,255,255,0) 2.5px), radial-gradient(circle, #000 1.25px, rgba(255,255,255,0) 2.5px); background-position: top, right, bottom, left; background-size: 15px 5px, 5px 15px; background-repeat: repeat-x, repeat-y; } <div>Some content with round, spaced dots as border</div>
径向梯度期望:
形状和可选位置 两个或更多的停止:一个颜色和半径
在这里,我想要一个直径为5像素(半径为2.5px)的点,点之间的直径为2倍(10px),加起来为15px。背景尺寸应该与这些相匹配。
这两个站点的定义是这样的点是漂亮的和光滑的:纯黑色的一半半径和比梯度到整个半径。