带有背景颜色和不透明度的前景CSS文本

huangapple go评论44阅读模式
英文:

CSS text in the foreground with background color with a opacity

问题

我有一个任务,需要在项目中实现以下屏幕。

项目框的灰色背景颜色是通过透明度设置为20%和背景颜色为#FDFFFF来实现的。

我已经添加了以下代码。

.card {
    background-color: #171B2D;
    border: 1px solid #1DFB9D;
}

.card h1 {
    color: whitesmoke;
}

.item {
    padding: 1rem;
    background-color: #FDFFFF;
    opacity: 0.2;
}

.item-title {
    color: #1DFB9D;
    z-index: 5;
}

.item-desc {
    color: #FDFFFF;
    z-index: 5;
}
<div class="card">
    <h1>Activities log</h1>
    <div class="item">
        <div class="item-title">01-01-2023 | 00:00:00</div>
        <div class="item-desc">Username: Order note 1</div>
    </div>
</div>

透明度应该添加到背景中,文本“01-01-2023 | 00:00:00”和“Username: Order note 1”应该显示在前景中。我已经添加了z-index,但似乎没有起作用。如何使用CSS实现这一点?

英文:

I have a task to implement below screen in a project.

The item box's gray background color is achieved by an opacity of 20% and a background color of #FDFFFF.

I have added the following code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  .card{
            background-color:#171B2D;
           
            border: 1px solid #1DFB9D;
        }
        .card h1 {
            color: whitesmoke;
        }
        .item{
            padding: 1rem;
            background-color:#FDFFFF;
           

            opacity: 20%;
        }
        .item-title{
            color: #1DFB9D;
            z-index: 5;
        }
        .item-desc{
            color:#FDFFFF;
            z-index: 5;

        }

<!-- language: lang-html -->

 &lt;div class=&quot;card&quot;&gt;
        &lt;h1&gt;
            Activities log

        &lt;/h1&gt;
        &lt;div class=&quot;item&quot;&gt;
            &lt;div class=&quot;item-title&quot;&gt;01-01-2023 | 00:00:00&lt;/div&gt;
            &lt;div class=&quot;item-desc&quot;&gt;Username: Order note 1&lt;/div&gt;

        &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

Opacity should be added to the background and the Texts "01-01-2023 | 00:00:00", "Username: Order note 1" should be on the foreground. I have added zIndex also but seems it is not working. How do I achieve this with CSS?

带有背景颜色和不透明度的前景CSS文本

答案1

得分: 1

要实现预期结果,请将opaccity添加到背景,但不要添加到文本,如下所示。

问题在于对item类的20%不透明度会导致标题描述的颜色淡出。

将背景颜色#fdffff更换为带有不透明度的rgba(255,255,255, 0.2)将解决此问题。

英文:

To achieve expected result, add opaccity to only background but not to the text as below

Issue is that opacity of 20% on item class is causing the color to title desc fade out

Replacing #fdffff background color to rgba(255,255,255, 0.2) with opacity will resolve the issue

codepen for reference- https://codepen.io/nagasai/pen/YzjgjVX

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.card {
  background-color: #171b2d;

  border: 1px solid #1dfb9d;
}
.card h1 {
  color: whitesmoke;
}
.item {
  padding: 1rem;
  background-color: rgba(255,255,255, 0.2);
}
.item-title {
  color: #1dfb9d;
  z-index: 5;
}
.item-desc {
  color: #fdffff;
  z-index: 5;
}

<!-- language: lang-html -->

&lt;div class=&quot;card&quot;&gt;
        &lt;h1&gt;
            Activities log

        &lt;/h1&gt;
        &lt;div class=&quot;item&quot;&gt;
            &lt;div class=&quot;item-title&quot;&gt;01-01-2023 | 00:00:00&lt;/div&gt;
            &lt;div class=&quot;item-desc&quot;&gt;Username: Order note 1&lt;/div&gt;

        &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 12:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定