我在我的方框中使用虚线样式的边框

.box {
    width: 300px;
    height: 200px;
    border: dotted 1px #f00;
    float: left;
}

我想增加边界上每个点之间的空间。


当前回答

哎呀,没有办法做到这一点。你可以使用一个虚线边界或可能增加边界的宽度一点,但只是得到更多的间隔点是不可能的CSS。

其他回答

我做了一个javascript函数,用svg创建点。你可以在javascript代码中调整点间距和大小。

var make_dotted_borders = function() { // EDIT THESE SETTINGS: var spacing = 8; var dot_width = 2; var dot_height = 2; //--------------------- var dotteds = document.getElementsByClassName("dotted"); for (var i = 0; i < dotteds.length; i++) { var width = dotteds[i].clientWidth + 1.5; var height = dotteds[i].clientHeight; var horizontal_count = Math.floor(width / spacing); var h_spacing_percent = 100 / horizontal_count; var h_subtraction_percent = ((dot_width / 2) / width) * 100; var vertical_count = Math.floor(height / spacing); var v_spacing_percent = 100 / vertical_count; var v_subtraction_percent = ((dot_height / 2) / height) * 100; var dot_container = document.createElement("div"); dot_container.classList.add("dot_container"); dot_container.style.display = getComputedStyle(dotteds[i], null).display; var clone = dotteds[i].cloneNode(true); dotteds[i].parentElement.replaceChild(dot_container, dotteds[i]); dot_container.appendChild(clone); for (var x = 0; x < horizontal_count; x++) { // The Top Dots var dot = document.createElement("div"); dot.classList.add("dot"); dot.style.width = dot_width + "px"; dot.style.height = dot_height + "px"; var left_percent = (h_spacing_percent * x) - h_subtraction_percent; dot.style.left = left_percent + "%"; dot.style.top = (-dot_height / 2) + "px"; dot_container.appendChild(dot); // The Bottom Dots var dot = document.createElement("div"); dot.classList.add("dot"); dot.style.width = dot_width + "px"; dot.style.height = dot_height + "px"; dot.style.left = (h_spacing_percent * x) - h_subtraction_percent + "%"; dot.style.top = height - (dot_height / 2) + "px"; dot_container.appendChild(dot); } for (var y = 1; y < vertical_count; y++) { // The Left Dots: var dot = document.createElement("div"); dot.classList.add("dot"); dot.style.width = dot_width + "px"; dot.style.height = dot_height + "px"; dot.style.left = (-dot_width / 2) + "px"; dot.style.top = (v_spacing_percent * y) - v_subtraction_percent + "%"; dot_container.appendChild(dot); } for (var y = 0; y < vertical_count + 1; y++) { // The Right Dots: var dot = document.createElement("div"); dot.classList.add("dot"); dot.style.width = dot_width + "px"; dot.style.height = dot_height + "px"; dot.style.left = width - (dot_width / 2) + "px"; if (y < vertical_count) { dot.style.top = (v_spacing_percent * y) - v_subtraction_percent + "%"; } else { dot.style.top = height - (dot_height / 2) + "px"; } dot_container.appendChild(dot); } } } make_dotted_borders(); div.dotted { display: inline-block; padding: 0.5em; } div.dot_container { position: relative; margin-left: 0.25em; margin-right: 0.25em; } div.dot { position: absolute; content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" height="100" width="100"><circle cx="50" cy="50" r="50" fill="black" /></svg>'); } <div class="dotted">Lorem Ipsum</div>

哎呀,没有办法做到这一点。你可以使用一个虚线边界或可能增加边界的宽度一点,但只是得到更多的间隔点是不可能的CSS。

虽然迟到了,但我在网上找到了这个小巧玲珑的工具。

https://kovart.github.io/dashed-border-generator/

这是一个古老但仍然非常相关的话题。目前最上面的答案很好,但只适用于非常小的点。正如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。背景尺寸应该与这些相匹配。

这两个站点的定义是这样的点是漂亮的和光滑的:纯黑色的一半半径和比梯度到整个半径。

border-style的可用值请参阅MDN文档:

none:没有边框,设置宽度为0。 这是默认值。

hidden:与'none'相同,不同之处在于 边境冲突解决表 元素。 虚线:短的系列 破折号或线段。 点: 一系列的点。 双倍的:两连 加起来等于像素量的行 定义为border-width。 槽: 雕刻效果。 插图:制作盒子 嵌入的出现。 开始:相反的 “插图”。使盒子呈现3D (压花)。 脊:相对的 “槽”。边框显示为3D (出来)。 单体:单体, 直线,实线。

除了这些选择之外,没有任何方法可以影响标准边框的样式。

如果你不喜欢这种可能性,你可以使用CSS3的border-image,但请注意,浏览器对此的支持仍然非常不稳定(编辑:截至2020年,浏览器的支持很好)。