CSS将背景图像对齐在*:before和*:after之前设置的背景颜色之上

时间:2022-11-21 08:49:40

I'm making a little Go board, in just CSS, and have got a problem when I put a counter on a point.

我正在制作一个Go Go板,只用CSS,当我把一个计数器放在一个点上时遇到了问题。

HTML:

<div class="GoBoard">
  <div class="aa black"></div>
  <div class="ab"></div>
  <div class="ba"></div>
  <div class="bb white"></div>
</div>

CSS:

.GoBoard{
    position:relative;
    background-color: #630;
    width: 100px;
    height: 100px;
}
.GoBoard>div{
    position: absolute;
    width: 50px;
    height: 50px;
}
.GoBoard>div:before, .GoBoard>div:after {
    content: "";
    position: absolute;
    z-index: 0;
    background-color: #000;
}

.GoBoard>div:before {
    left: 50%;
    width: 2%;
    margin-left: -1%;
    height: 100%;
}

.GoBoard>div:after {
    top: 50%;
    height: 2%;
    margin-top: -1%;
    width: 100%;
}

.white{
    border-radius: 50%;
    background-color:none!important;
    z-index: 1!important;
    background-image:    -moz-radial-gradient(15px 15px 45deg, circle cover, #ddd 0%, #555 100%)!important;
    background-image: -webkit-radial-gradient(15px 15px,       circle cover, #ddd,    #555)!important;
    background-image:         radial-gradient(15px 15px 45deg, circle cover, #ddd 0%, #555 100%)!important;
}

.black{
    border-radius: 50%;
    background-image:    -moz-radial-gradient(15px 15px 45deg, circle cover, #333 0%, #000 100%);
    background-image: -webkit-radial-gradient(15px 15px,       circle cover, #333,    #000);
    background-image:         radial-gradient(15px 15px 45deg, circle cover, #333 0%, #000 100%);
}
.aa{bottom:0px;left:0px}
.ab{bottom:50px;left:0px}
.ba{bottom:0px;left:50px}
.bb{bottom:50px;left:50px}

I used the .GoBoard>div as the #cross from this question. And the !important's were to show that this question doesn't work. Adding position: relative; to the .white, with !important takes it out of the board, and without makes no difference. I also tried moving the .white, up the CSS and changing it to .white:before, the former made no difference, and the latter changed the cross line to the color of the circle.

我使用.GoBoard> div作为此问题的#cross。并且!重要的是表明这个问题不起作用。添加位置:相对;对于.white来说,重要的是把它带出了棋盘,没有任何区别。我也尝试移动.white,然后将CSS更改为.white:之前,前者没有任何区别,后者将交叉线更改为圆的颜色。

I can think of changing the HTML and CSS to have another class for the cross, but I would prefer if the cross would go below the image. Or having a new div for the cross, which seems like a bit of a waist of a div if you can get this close without it.

我可以考虑改变HTML和CSS以便为十字架设置另一个类,但我更喜欢交叉将低于图像。或者为十字架设置一个新的div,如果你可以在没有它的情况下接近它,那就好像是一个div的腰部。

tl;dr: How do I get the counter (background-image) above the board cross (background-color)?

tl; dr:我如何得到电路板上方的背景图像(背景图像)?

1 个解决方案

#1


1  

I setup this JSFiddle.

我设置了这个JSFiddle。

Basically, you will only need 2 z-index's defined in your CSS:

基本上,您只需要在CSS中定义2个z-index:

.GoBoard{
    z-index: 0;
}

.GoBoard>div:before, .GoBoard>div:after {
    z-index: -1;
}

The one you have defined under .white selector is unnecessary. Also, this is a pretty awesome pure CSS setup!

您在.white选择器下定义的那个是不必要的。此外,这是一个非常棒的纯CSS设置!

#1


1  

I setup this JSFiddle.

我设置了这个JSFiddle。

Basically, you will only need 2 z-index's defined in your CSS:

基本上,您只需要在CSS中定义2个z-index:

.GoBoard{
    z-index: 0;
}

.GoBoard>div:before, .GoBoard>div:after {
    z-index: -1;
}

The one you have defined under .white selector is unnecessary. Also, this is a pretty awesome pure CSS setup!

您在.white选择器下定义的那个是不必要的。此外,这是一个非常棒的纯CSS设置!