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

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

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


当前回答

这使用了标准的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>

应用于小元素的大圆角可能会产生一些有趣的效果。

其他回答

这是我在最近的一个项目中使用的一个技巧,它几乎可以实现我想要的任何水平边框。每次需要水平边框时,我都使用<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属性。

在我的情况下,我需要弯曲的角和薄边界,所以我想出了这个解决方案:

/* 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模式放在元素后面,并仅显示该模式的细线作为元素边界。

你可以创建一个画布(通过javascript)并在里面画一条虚线。在画布内,您可以控制多长破折号和之间的空间应该。

我做了一个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>

<div style="width: 100%; height: 100vh; max-height: 20px; max-width: 100%; background: url('https://kajabi-storefronts-production.global.ssl.fastly.net/kajabi-storefronts-production/themes/853636/settings_images/Ei2yf3t7TvyRpFaLQZiX_dot.jpg') #000; background-repeat: repeat;">&nbsp;</div>

这就是我所做的——使用图像 在这里输入图像描述