更改圈子的背景图片

时间:2018-02-05 07:58:22

标签: html css css3 flexbox background-image

我正在尝试绘制一些以图像为背景的CSS生成的圆圈。在我当前的代码中,背景图像在CSS代码中设置为固定图像。

.container {
  text-align: center;
}

.circle {
  position: relative;
  height: 180px;
  width: 180px;
  border-radius: 50%;
  display: inline-flex;
  vertical-align: top;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  text-decoration: none;
  border: 0;
  margin: 10px;
}

.circle:after {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background: url("http://deepchains.com/images/team.png") center / cover no-repeat;
  opacity: .25;
  transition: .25s;
}

.circle:hover:after {
  opacity: 1;
  color: transparent;
}

.circle:hover {
  color: transparent;
}

.ccont {
  display: inline-block;
  margin: 10px;
}

.ccont:hover {
  color: transparent;
}
<div class="container">
  <a class="circle" href="http://www.url1.html">
    <div class="ccont" style="font-weight: bold; text-decoration:none !important;">This is <br>Text1</div>
  </a>
  <a class="circle" href="http://www.url2.html">
    <div class="ccont" style="font-weight: bold; text-decoration:none !important;">This is <br>Text2</div>
  </a>
</div>

以下是我在Chrome浏览器中看到的示例结果:

enter image description here

我的问题是如何在HTML代码中单独更改每个圆圈的背景图像?我将有许多这些圈子,我喜欢它们在同一行中对齐,我想在HTML代码中设置它们的背景图像,并从CSS中删除背景图像。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

一个简单的解决方案是将peudo元素转换为元素,并使用背景图像作为内联样式,这样您就可以轻松更改每个元素的图像并应用与伪元素相同的所有属性:

\+1|1\+4|
.container {
  text-align: center;
}

.circle {
  position: relative;
  height: 180px;
  width: 180px;
  border-radius: 50%;
  display: inline-flex;
  vertical-align: top;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  text-decoration: none;
  border: 0;
  margin: 10px;
}

.circle .image {
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-position:center;
  background-size:cover;
  background-repeat:no-repeat;
  opacity: .25;
  transition: .25s;
}

.circle:hover .image {
  opacity: 1;
  color: transparent;
}

.circle:hover .ccont{
  color: transparent;
}

.ccont {
  display: inline-block;
  margin: 10px;
}

答案 1 :(得分:1)

只需在要从父级继承的伪设置背景图像,将父级的大小设置为0以将其隐藏在那里,最后在标记中设置style="background-image: url(...)"

更新

你甚至可以放弃内部div并仍然达到同样的效果

Stack snippet

.container {
  text-align: center;
}

.circle {
  position: relative;
  height: 180px;
  width: 180px;
  border-radius: 50%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  font-weight: bold;
  text-decoration: none;
  border: 0;
  margin: 10px;
  background-size: 0;              /*  hide image by set its size to 0  */
}

.circle:after {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-image: inherit;       /*  inherit from parent  */
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  opacity: .25;
  transition: .25s;
}

.circle:hover:after {
  opacity: 1;
}

.circle:hover {
  color: transparent;
}
<div class="container">
  <a class="circle" href="http://www.url1.html" style="background-image: url(https://picsum.photos/300/300?image=1070);">
    This is <br>Text1
  </a>
  <a class="circle" href="http://www.url2.html"  style="background-image: url(https://picsum.photos/300/300?image=1072);">
    This is <br>Text2
  </a>
</div>