CSS: 将图像添加到带有文本的圆圈中
CSS: adding an image to a circle with text
我想将图像添加到由 CSS 创建并在其中包含文本的圆圈中。我知道如何用文本创建一个圆圈,例如, Whosebug 问答显示了如何做。这是 css 中的 circle
定义:
circle {
background: #f00;
width: 100px;
height: 100px;
border-radius: 50%;
display: inline-block;
text-align: center;
line-height: 100px;
margin-right:5px;
}
这是我在 html 中的内容:
<circle>THIS IS THE TEXT</circle>
现在我希望能够将背景图像添加到圆圈中,如果可能的话添加 0.5 的 opacity
。所以基本上我想要一个圆形的图像,上面有文字。这是一个例子:
"THIS IS THE TEXT"是可以在图片上方的html代码中写入的文字。
如何做到这一点?
不难发现如何用文字做一个圆圈。 <circle>
用于 SVG,所以这不是您想要的。改用普通的 <div>
。这里的解决方案是给背景图片加个不透明度。
body {
background-color: #121212;
}
.circle {
position: relative;
height: 300px;
width: 300px;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
}
.circle:hover:after {
opacity: 0.5;
}
.circle:after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-image: url(https://buyersguide.caranddriver.com/media/assets/submodel/280_8204.jpg);
background-size: cover;
background-repeat: no-repeat;
z-index: -1;
opacity: 1;
transition: opacity 300ms;
}
.circle__text {
padding: 10px;
background-color: yellow;
}
<div class="circle">
<span class="circle__text">random text</span>
</div>
如果你想要一个替代解决方案,这就是我使用的
.story_shape {
width: 15rem;
height: 15rem;
-webkit-shape-outside: circle(50% at 50% 50%);
shape-outside: circle(50% at 50% 50%);
clip-path: circle(50% at 50% 50%);
position: relative;
}
.story_img {
height: 100%;
transform: translateX(-4rem);
backface-visibility: hidden;
}
.story_caption {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
text-transform: uppercase;
font-size: 1.7rem;
text-align: center;
backface-visibility: hidden;
}
<figure class="story_shape">
<img src="https://orig00.deviantart.net/6afd/f/2015/182/4/f/croatia_nature_pack___sample__1___proref_org_by_proref-d8zgqmh.jpg" alt="person on a tour" class="story_img">
<figcaption class="story_caption">mary smith</figcaption>
</figure>
我想将图像添加到由 CSS 创建并在其中包含文本的圆圈中。我知道如何用文本创建一个圆圈,例如,circle
定义:
circle {
background: #f00;
width: 100px;
height: 100px;
border-radius: 50%;
display: inline-block;
text-align: center;
line-height: 100px;
margin-right:5px;
}
这是我在 html 中的内容:
<circle>THIS IS THE TEXT</circle>
现在我希望能够将背景图像添加到圆圈中,如果可能的话添加 0.5 的 opacity
。所以基本上我想要一个圆形的图像,上面有文字。这是一个例子:
"THIS IS THE TEXT"是可以在图片上方的html代码中写入的文字。
如何做到这一点?
不难发现如何用文字做一个圆圈。 <circle>
用于 SVG,所以这不是您想要的。改用普通的 <div>
。这里的解决方案是给背景图片加个不透明度。
body {
background-color: #121212;
}
.circle {
position: relative;
height: 300px;
width: 300px;
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
}
.circle:hover:after {
opacity: 0.5;
}
.circle:after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-image: url(https://buyersguide.caranddriver.com/media/assets/submodel/280_8204.jpg);
background-size: cover;
background-repeat: no-repeat;
z-index: -1;
opacity: 1;
transition: opacity 300ms;
}
.circle__text {
padding: 10px;
background-color: yellow;
}
<div class="circle">
<span class="circle__text">random text</span>
</div>
如果你想要一个替代解决方案,这就是我使用的
.story_shape {
width: 15rem;
height: 15rem;
-webkit-shape-outside: circle(50% at 50% 50%);
shape-outside: circle(50% at 50% 50%);
clip-path: circle(50% at 50% 50%);
position: relative;
}
.story_img {
height: 100%;
transform: translateX(-4rem);
backface-visibility: hidden;
}
.story_caption {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
text-transform: uppercase;
font-size: 1.7rem;
text-align: center;
backface-visibility: hidden;
}
<figure class="story_shape">
<img src="https://orig00.deviantart.net/6afd/f/2015/182/4/f/croatia_nature_pack___sample__1___proref_org_by_proref-d8zgqmh.jpg" alt="person on a tour" class="story_img">
<figcaption class="story_caption">mary smith</figcaption>
</figure>