html - 如何制作边框覆盖子div?

标签 html css border

我必须制作一个边框覆盖它的内容以匹配此图像的外观:

Border overlay CSS

我得到了它的全景图:

offset border using CSS

我需要让边框覆盖绿色内容。我怎样才能完成它?正如我研究的那样,我不能为此使用 z-index 。 HTML 和 CSS 代码如下:

.box-border a {
  color: white;
  text-transform: uppercase;
  font-weight: bold;
  font-size: 16px;
}

.box-border a:hover {
  text-decoration: none;
  color: white;
  cursor: pointer;
}


.box-border-participe {
  position: relative;
  float: right;
  margin-right: 30%;
  border: 4px solid white;
}

.box-participe {
  background-color: #94C120;
  padding: 10px 40px;
  margin-left: 5px;
  margin-top: 5px;
  margin-bottom: -20px;
  margin-right: -20px;
}

body {
  background:grey;
}
<div class="box-border box-border-participe">
  <div class="box-participe">
    <a>Participe</a>
  </div>
</div>

最佳答案

您可以考虑使用伪元素来创建边框并避免额外的标记。您还可以轻松控制其位置/大小:

body {
  background: grey;
}

.button {
  background: #94c120;
  width: 200px;
  height: 50px;
  margin: 50px;
  position: relative;
}

.button:before {
  content: "";
  position: absolute;
  top: -15px;
  left: -15px;
  width: 100%;
  height: 100%;
  border: 5px solid #fff;
  box-sizing: border-box;
}
<div class="button">
  some text
</div>

这里还有另一个使用linear-gradient和多重背景的想法:

body {
 background:grey;
}
.button {
  width: 200px;
  height: 80px;
  margin: 50px;
  padding:20px 0 0 20px;
  box-sizing:border-box;
  background: 
    linear-gradient(to right,#fff 5px,transparent 5px calc(100% - 5px),#fff calc(100% - 5px)) 0 0/ calc(100% - 10px) calc(100% - 10px),
    linear-gradient(to bottom,#fff 5px,transparent 5px calc(100% - 5px),#fff calc(100% - 5px)) 0 0/ calc(100% - 10px) calc(100% - 10px),
    linear-gradient(#94c120,#94c120) 15px 15px;
  background-repeat:no-repeat;
}
<div class="button">
  some text
</div>

另一种带有渐变的语法:

body {
 background:grey;
}
.button {
  width: 200px;
  height: 80px;
  margin: 50px;
  padding:20px 0 0 20px;
  box-sizing:border-box;
  background: 
    linear-gradient(#fff,#fff) left -10px top     0  /100% 5px,
    linear-gradient(#fff,#fff) top  -10px left    0  /5px 100%,
    linear-gradient(#fff,#fff) left -10px bottom 10px/100% 5px,
    linear-gradient(#fff,#fff) top  -10px right  10px/5px 100%,
    linear-gradient(#94c120,#94c120) 20px 20px; 
  background-repeat:no-repeat;
}
<div class="button">
  some text
</div>

另一个使用背景和框阴影的想法:

body {
 background:grey;
}
.button {
  width: 200px;
  height: 80px;
  margin: 50px;
  padding:15px 0 0 15px;
  box-sizing:border-box;
  background: #94c120 content-box;
  border:5px solid #fff;
  box-shadow: 20px 20px 0px #94c120; /* value of padding + border*/
}
<div class="button">
  some text
</div>

关于html - 如何制作边框覆盖子div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48662493/

相关文章:

html - 水平显示无序列表

html - 忽略 iframe 中的指针事件

jquery - 有没有办法使用 CSS 向后工作

html - 删除边距底部的第 N 个子表达式在响应式中不起作用

javascript - 页脚高度根据网址?

html - 希望用位置稍有不同的不同大小的按钮替换按钮 onclick

html - 一些 tr 有边框,一些没有边框 css bootstrap

javascript - 如何让一个div不移动另一个

html - 使用css hover增加下划线和颜色,但没有效果

html - 无序列表中的边框不适合 div 的整个宽度