简体   繁体   English

CSS箭头后面的半透明矩形

[英]semi-transparent rectangle behind css arrow

I'm quite new to coding so it's probably something really easy that I'm trying to do but can't get it to work. 我对编码非常陌生,因此我尝试做起来可能很简单,但却无法使其正常工作。 I've made some arrows with css borders. 我用css边框制作了一些箭头。 Now I want to do a rectangle that is semi transparent behind each arrow. 现在,我想做一个在每个箭头后半透明的矩形。

Something like this 这样

But with rectangles instead of the circle. 但是用矩形而不是圆形。 This is the code I've got so far : 这是到目前为止我得到的代码:

 <div id="arrow"></div>



#arrow {
 display: block;
 border-right: 2px solid; border-bottom: 2px solid;
 width: 30px; height: 30px;
 transform: rotate(-45deg);
 border-color:black;
 margin:auto;
display:block;
position:relative;
}

super easy way: 超级简单的方法:

HTML: HTML:

<div id="arrowBox">
<div id="arrow"></div>
</div>

CSS: CSS:

#arrow {
 display: block;
 border-right: 2px solid; border-bottom: 2px solid;
 width: 30px; height: 30px;
 transform: rotate(-45deg);
 border-color:black;
 margin:auto;
display:block;
position:relative;
}

#arrowBox{

  background:rgba(0,0,0,0.5);
  display:inline-block;
  padding:10px 15px 10px 0;

}

adjust padding to change the size of the box. 调整填充以更改框的大小。

Instead of using the div as your arrow, try using the div as your rectangle (or circle if desired). 不要将div用作箭头,而div用作矩形(如果需要,也可以使用圆形)。 You'll need background-color: rgba(0,0,0,0.4) or similar to get the "translucent black" effect. 您将需要使用background-color: rgba(0,0,0,0.4)或类似background-color: rgba(0,0,0,0.4)来获得“半透明黑色”效果。

Once that's done, put your arrow styles in the ::before pseudo-element. 完成后,将箭头样式放在::before伪元素中。 Use positioning to get it in the right place, but it should be pretty easy to get the arrow to appear. 使用定位将其放置在正确的位置,但箭头显示应该很容易。 Don't forget content:'' to make the pseudo-element appear. 不要忘记content:''以使伪元素出现。

将css属性设置为矩形div或任何形状,

{ opacity: 0.5;}

You can user pseudo-elements to add the box with no additional markup. 您可以使用伪元素添加没有附加标记的框。 As already suggested, use rgba to define the background color. 如已经建议的,使用rgba定义背景色。

I made a fiddle with an example showing the result, with 4 arrows in different directions on different background colors: https://jsfiddle.net/7f6tg9s3/4/ 我用显示结果的示例进行了摆弄,在不同背景颜色的不同方向上有4个箭头: https : //jsfiddle.net/7f6tg9s3/4/

Here is the arrows part: 这是箭头部分:

.arrow {
  display: inline-block;
  position:relative;
  width: 30px; 
  height: 30px;
}
.arrow::before {
  content: ' ';
  display: block;
  background-color: rgba(0, 0, 0, 0.3);
  position: relative;
  width: 30px; 
  height: 30px;
  margin-right: -30px;
  margin-bottom: -30px;
  z-index: 1;
}
.arrow::after {
  content: ' ';
  display: block;
  box-sizing: border-box;
  position: relative;
  border-right: 2px solid;
  border-bottom: 2px solid;
  width: calc(25px / 1.41421);
  height: calc(25px / 1.41421);
  border-color: #fff;
  z-index: 2;
}
.arrow.right::after {
  transform: rotate(-45deg);
  top: 6px;
  left: 2px;
}
.arrow.left::after {
  transform: rotate(135deg);
  top: 6px;
  left: 12px;
}
.arrow.up::after {
  transform: rotate(-135deg);
  top: 12px;
  left: 7px;
}
.arrow.down::after {
  transform: rotate(45deg);
  top: 2px;
  left: 7px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM