如何制作边框叠加子div? [英] How to make a border overlay child div?

查看:93
本文介绍了如何制作边框叠加子div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在其内容上加上边框以匹配此图像的外观:

I have to make a border overlay it's content to match the looking of this image:

我得到了它的大印象:

我需要使边框覆盖绿色内容.我该怎么做?正如我研究的那样,我不能为此使用z-index. HTML和CSS代码如下:

And I need to make that border overlay the green content. How can I accomplish it? I can't use z-index for this, as I researched. The HTML and CSS code follows:

.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>

推荐答案

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

You can consider the use of pseudo element to create the border and avoid extra markup. You can also easily control its position/size:

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和多个背景的另一个想法:

Here is also another idea using linear-gradient and multiple background:

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>

另一种带有渐变的语法:

Another syntax with gradient:

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>

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

Another idea using background and box-shadow:

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>

这篇关于如何制作边框叠加子div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆