Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML text does not appear white

Tags:

html

css

I have html text that is colored white with css. I also have opacity set to 0.5, but that shouldn't change font color. It should stay white.

Is there any fix for this without removing opacity or background colors?

body {
  background: green;
}

.second-b {
  background-image: url(images/town.jpg);
  background-repeat: no-repeat;
  background-size: cover;
}

.color-box {
  width: 100%;
  height: 300px;
  background-color: red;
  opacity: 0.5;
  line-height: 300px;
  text-align: center;
}

.color-box-content {
  display: inline-block;
  vertical-align: middle;
  line-height: normal;
  color: white;
  font-size: 22px;
}
<div class="second-b">
  <div class="color-box">
    <div class="color-box-content">Lorem ipsum</div>
  </div>
</div>
like image 785
Pleinair Avatar asked Jan 26 '26 10:01

Pleinair


2 Answers

If you use opacity on an element it will affect all the items inside it, you can use the opacity on the background-color only

background-color: rgba(255, 0, 0, 0.5);

See code snippet:

body {
  background: green;
}

.second-b {
  background-image: url(images/town.jpg);
  background-repeat: no-repeat;
  background-size: cover;
}

.color-box {
  width: 100%;
  height: 300px;
  background-color: rgba(255, 0, 0, 0.5);
  line-height: 300px;
  text-align: center;
}

.color-box-content {
  display: inline-block;
  vertical-align: middle;
  line-height: normal;
  color: white;
  font-size: 22px;
}
<div class="second-b">
  <div class="color-box">
    <div class="color-box-content">Lorem ipsum</div>
  </div>
</div>
like image 50
Chiller Avatar answered Jan 29 '26 01:01

Chiller


opacity Property affect on all his children.

you can use The following attributes :

1)background-color:hsla(0,100%,50%,0.5);

2)background-color:rgba(255,0,0,0.5);

body{
    background: green;
}

.second-b{
    background-image: url(images/town.jpg); 
    background-repeat: no-repeat;
    background-size: cover; 
}

.color-box{
    width:100%;
    height:300px;
    background-color:hsla(0,100%,50%,0.5);
    line-height: 300px;
    text-align: center;
}

.color-box-content{
  display: inline-block;
  vertical-align: middle;
  line-height: normal;
  color:white;
  font-size:22px; 
}
<div class="second-b">
    <div class="color-box">
         <div class="color-box-content">Lorem ipsum</div>
    </div>
</div>
like image 36
Ehsan Avatar answered Jan 29 '26 03:01

Ehsan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!