【问题标题】:Add border to this "shape"为这个“形状”添加边框
【发布时间】:2017-02-20 10:22:13
【问题描述】:

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

我需要达到的目标:

我目前的代码:

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>

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

感谢阅读。

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

【问题讨论】:

  • 此链接可能对您有所帮助stackoverflow.com/questions/34641588/…
  • 谢谢@geeky,我已经看到了,但看起来它不可扩展。我的意思是,它不能是 600x200 像素。至少在我尝试过的方式上。
  • 相关 - stackoverflow.com/questions/34644437/… 但 SVG 在这里是最佳选择。
  • 嗨,@paulie-d 很高兴在这里见到你! (我在 CSS-Tricks 附近见过你,你帮了我几次)。我有点害怕跨浏览器的支持。我没有尝试使用 SVG。问题是,这个形状是其他元素(文本、图像...)的容器,所以我不知道我是否可以信任 SVG。
  • 如果该解决方案不涉及 SVG,那么您将得到的答案将非常老套。喜欢this。你真的应该考虑使用 SVG,就像 @Paulie_D 已经建议的那样。

标签: html css styles border shape


【解决方案1】:

嗯,这是我能想到的用纯 CSS 处理它的唯一方法:

这里是 JSfiddle:https://jsfiddle.net/xfcjfz3d/7/

body {
    background:#fff;
}

#octagon {
  position:relative;
	width: 300px;
	height: 200px;
	background: green;
	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 green;
	border-left: 30px solid #fff;
	border-right: 30px solid #fff;
}

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

.tall {
  position:absolute;
  background:red;
  width:230px;
  height:190px;
  left:35px;
  top:5px;
  z-index:1;
}

.wide {
  position:absolute;
  background:red;
  width:290px;
  height:130px;
  left:5px;
  top:35px;
  z-index:1;
}

.corner {
  position:absolute;
  background:red;
  width:45px;
  height:43px;
  
  z-index:1;
  transform: rotate(45deg);
}

.topleft {
  left:14px;
  top:14px;
}

.topright {
  //background:black;
  left:241px;
  top:13px;
}

.bottomleft {
  background:red;
  left:13px;
  top:143px;
}

.bottomright {
  background:red;
  left:241px;
  top:143px;
}
<div id="octagon">
  <div class="tall"></div>
  <div class="wide"></div>
  <div class="corner topleft"></div>
  <div class="corner topright"></div>
  <div class="corner bottomleft"></div>
  <div class="corner bottomright"></div>
</div>

【讨论】:

  • 谢谢,@varin 不错的方法,但似乎不可扩展。此外,我需要在形状内添加元素(文本、图像等)。无论如何,非常感谢。
  • 嗯。我认为这可以通过使用不同的单元来扩展。如果你需要里面的元素,你总是可以在里面添加另一个具有内容和更高 z-index 的 div。
【解决方案2】:

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

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;
}
&lt;div id="octagon"&gt;Hello World!&lt;/div&gt;

【讨论】:

  • 哇@darrylyeo 感谢您的奉献!对此,我真的非常感激!如果我需要更细的边框(比如 2px),我该如何改变呢?
  • 很高兴能提供帮助,我很享受挑战!请参阅最后一个规则集#octagon:after。对角线由box-shadow 控制,其余边由linear-gradients 中的百分比控制。我有点看中它,但我确信有一种更数学的方法来计算这些值(calc() 会派上用场)。
  • 知道了!您现在只需替换​​ 10px 的所有实例。
  • 太棒了!再次感谢达里尔,你摇滚!哈哈,我尝试将固定值更改为百分比(n px 容器的 100%),但没有按预期工作。横向“边界”没有出现。此外,看起来它无法自动调整到子元素的高度。 jsfiddle.net/jimmyadaro/jrs7mxw1/4
猜你喜欢
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多