html - 给这个 "shape"添加边框

标签 html css styles border shapes

我需要为这个“形状”添加边框。这有点困难,因为形状是用 afterbefore 伪元素制作的。我找不到正确的方法。

我需要达到的目标:

enter image description here

我目前的代码:

https://jsfiddle.net/jimmyadaro/xfcjfz3d/

#octagon {
    width: 300px;
    height: 200px;
    background: red;
    position: relative;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    display: block;
}

#octagon:before,
#octagon:after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
}

#octagon:before {
    top: 0;
    border-bottom: 30px solid red;
    border-left: 30px solid #fff;
    border-right: 30px solid #fff;
}

#octagon:after {
    bottom: 0;
    border-top: 30px solid red;
    border-left: 30px solid #fff;
    border-right: 30px solid #fff;
}

<div id="octagon"></div>

我尝试了阴影和轮廓但没有成功。

感谢阅读。

注意:如果重要的话,我将使用纯色背景。

最佳答案

这是我的解决方案。不需要纯色背景。这可能适合也可能不适合您的实际用例。

JSFiddle

#octagon {
    display: flex;
    justify-content: center;
    align-items: center;
    
    width: 300px;
    height: 200px;
    overflow: hidden;
    position: relative;
}

#octagon:before,
#octagon:after {
    content: "";
    display: block;
    width: 300px;
    padding-top: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(45deg);
    z-index: -1;
}
#octagon:before {
    background: red;
}
#octagon:after {
    background:
        linear-gradient(
    		45deg,
	    	#0e0 calc(50% - 150px + 10px), transparent 0,
    		transparent calc(50% + 150px - 10px), #0e0 0%),
    	linear-gradient(
    		-45deg,
    		#0e0 calc(50% - 100px + 10px), transparent 0,
    		transparent calc(50% + 100px - 10px), #0e0 0);
    box-shadow: 0 0 0 10px #0e0 inset;
}
<div id="octagon">Hello World!</div>

关于html - 给这个 "shape"添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39984652/

相关文章:

javascript - 使用 JavaScript 在 IE 10 中导航嵌套父框架

html - 如何仅更改外部 div 中所有特定元素标签的 CSS 样式?

javascript - onclick 和 <a> 无法正常工作

wpf - 在 WPF 中显示 GIF 动画图像的最佳方式是什么?

javascript - 我如何重用不同颜色的 react 组件?

html - 用户代理如何区分域和相对 URL 中的文件扩展名?

html - 像 CSS 布局一样的多列

css - 对齐在CSS中旋转的元素

css - LESS 不输出源映射

css - 如何创建两个边框 - 纯 CSS