如何使用具有圆角背景的文本覆盖图像
P粉328911308
P粉328911308 2023-08-30 15:20:14
0
1
304
<p>我需要在 HTML 中复制您在此图像中看到的内容:</p> <p>问题是文本覆盖了 div 和背景图像。如果外部 div 中没有图像并且没有纯色,我可以想象我可以相当轻松地使用一些带有圆角的小 html 元素放置在正确的位置来完成此操作,但是背景图像是是什么增加了复杂性。</p> <p>到目前为止,我有这个......如你所见,我被困在两个圆角上。谁能提出解决方案?请注意,它必须在<strong>所有现代浏览器</strong>中工作:</p> <p> <pre class="brush:css;toolbar:false;">#outer { width:100%; height:400px; border-radius:20px; background-image:url(https://media.istockphoto.com/id/1323032473/es/vector/panal-abstracto-de-vector-azul-moderno-con-fondo-de-monitor-de-coraz%C3%B3n-con-para-la.jpg?s=2048x2048&amp;w=is&amp;k=20&amp;c=mXe4wSHc8kAcOXastbN9jhinrWGQX3vvJQUhDgvOcqA=); position:relative; } #innertext { display:inline; border-top-right-radius:20px; background-color:#fff; padding:5px 25px 0px 5px; font-size:40px; color:#000; position:absolute; bottom:0px; }</pre> <pre class="brush:html;toolbar:false;">&lt;div id="outer"&gt; &lt;div id="innertext"&gt;A test title&lt;br&gt;that is on two lines&lt;/div&gt; &lt;/div&gt;</pre> </p>
P粉328911308
P粉328911308

全部回复(1)
P粉391677921

您需要将 :before:after 添加到 .innertext 元素

更新:

要有多行,只需添加一个 flex-direction: column 容器,每行都会有 :after (右)角,并且只有第一个子元素将有 :before (顶部)角

#outer {
  width: 100%;
  height: 400px;
  border-radius: 20px;
  background-image: url(https://media.istockphoto.com/id/1323032473/es/vector/panal-abstracto-de-vector-azul-moderno-con-fondo-de-monitor-de-coraz%C3%B3n-con-para-la.jpg?s=2048x2048&w=is&k=20&c=mXe4wSHc8kAcOXastbN9jhinrWGQX3vvJQUhDgvOcqA=);
  position: relative;
}
#inner-container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  position: absolute;
  bottom: 0px;
}
.innertext {
  display: inline;
  position: relative;
  border-top-right-radius: 20px;
  background-color: #fff;
  padding: 5px 25px 0px 5px;
  font-size: 40px;
  color: #000;
}
.innertext:first-child::before,
.innertext::after {
  content: "";
  display: block;
  width: 40px;    /* double the radius */
  height: 40px;   /* double the radius */
  background-color: transparent;
  position: absolute;
  border-bottom-left-radius: 20px;
  box-shadow: 0 20px 0 0 #fff;  /* this to create the inverted corner border radius area, if you desire to change the positon you can control it via the first two values 0 20px which represents the x & y */
}
.innertext::before {
  top: -40px;
  left: 0;
}
.innertext::after {
  right: -40px;
  bottom: 0;
}
.innertext span {
  position: relative;
  z-index: 1;   /* to overcome the overlapping with the text */
}
<div id="outer">
  <div id="inner-container">
    <div class="innertext"><span>A test</span></div>
    <div class="innertext"><span>A test with a second line</span></div>
  </div>
</div>
热门教程
더>
最新下载
더>
网站特效
网站源码
网站素材
프론트엔드 템플릿
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!