How to fade out text on top of semi-transparent div using CSS?

thdoan

I've seen a lot of questions on here about how to fade out text using CSS, but all of the answers so far assume the element under the text has a solid background. How would you fade out text if the text is on top of a div that itself has opacity less than 1 (i.e., semi-transparent)? Run the code below to see an example. What I'm trying to do is fade out the text in that box in a vertical gradient such that the font color is white at the top and fades into the semi-transparent background of the box when it reaches the bottom.

NOTE: The solution should work without modification even when the div's background opacity changes (e.g., 0.75 instead of 0.5).

body {
  background: #333;
  color: #fff;
}

.box {
  width: 320px;
  background: rgba(204, 153, 153, 0.5);
  border: 2px solid #000;
  padding: 1rem;
}

.box > p {
  margin: 0;
}
<div class="box">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>  
</div>

thdoan

I finally found a solution that meets all my requirements by adding just one line of CSS to .box > p: -webkit-mask-image (accepted answer by Adrian van Vliet). I've updated my codepen to show this solution: http://codepen.io/thdoan/pen/wKZBrN

Even though this is considered "non-standard" CSS, it's no big deal for my case since if a browser doesn't support it then the content will degrade gracefully to white text simply not having a gradient fade. In the meantime, I am experimenting with another solution that is more cross-browserish using SVG: Applying SVG effects to HTML content. I will update this answer with a codepen using an SVG mask if I ever get it to work.

Thanks to all who replied :-).

UPDATE: Here is a solution using SVG: http://codepen.io/thdoan/pen/rObVdJ

The full cross-browser solution is laid out in the nice tutorial, CSS Masks – How To Use Masking In CSS Now by Christian Schaefer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to fade out text on top of semi-transparent div using CSS?

From Dev

How can I fade in and then fade out text inside a <div>

From Dev

How can I fade in and then fade out text inside a <div>

From Dev

Fade bottom or top of text in a div

From Dev

Fade in and Fade Out Text in DIV - Remove Loop

From Dev

Fade out after div content has been shown using CSS

From Dev

How to get a semi transparent image on top of another image to hold text in android (screenshot provided)

From Dev

Using Animate.css to fade out then fade in

From Dev

how to fade-out/blur div's borders with css?

From Dev

How to place text on top of image within div with layout transparent layout?

From Dev

How to add semi-transparent highlight to text?

From Dev

How can I apply a fade-out-text effect with a transparent-to-white gradient?

From Dev

CSS how to make an element fade in and then fade out?

From Dev

Add a semi transparent square in an excisting square using CSS3 and no extra DIV

From Dev

How to make text fade out

From Dev

How to create a transparent region (div) over semi-transparent background?

From Dev

How to fade out SnackBar bottom to top

From Dev

How to fade color to transparent using hexbin and matplotlib?

From Dev

How to make Unity top panel semi-transparent?

From Dev

How do you fade in/fade out same div onclick of image using JQuery?

From Dev

Fade DIV out / in during CSS media query

From Dev

CSS Smooth fade and out transition (including text)

From Dev

How can I create a fade in/fade out effect using only CSS?

From Dev

Fade In/Out text and Loop using jQuery

From Dev

Fade only top part of div out as it reaches fixed navigation

From Dev

Fade out of div background affects other divs on top of background image

From Dev

fade in then fade out after some time by using css-transitions

From Dev

Fade out then Fade in Element using CSS3 Transition

From Dev

using css gradient to accomplish a fade out

Related Related

  1. 1

    How to fade out text on top of semi-transparent div using CSS?

  2. 2

    How can I fade in and then fade out text inside a <div>

  3. 3

    How can I fade in and then fade out text inside a <div>

  4. 4

    Fade bottom or top of text in a div

  5. 5

    Fade in and Fade Out Text in DIV - Remove Loop

  6. 6

    Fade out after div content has been shown using CSS

  7. 7

    How to get a semi transparent image on top of another image to hold text in android (screenshot provided)

  8. 8

    Using Animate.css to fade out then fade in

  9. 9

    how to fade-out/blur div's borders with css?

  10. 10

    How to place text on top of image within div with layout transparent layout?

  11. 11

    How to add semi-transparent highlight to text?

  12. 12

    How can I apply a fade-out-text effect with a transparent-to-white gradient?

  13. 13

    CSS how to make an element fade in and then fade out?

  14. 14

    Add a semi transparent square in an excisting square using CSS3 and no extra DIV

  15. 15

    How to make text fade out

  16. 16

    How to create a transparent region (div) over semi-transparent background?

  17. 17

    How to fade out SnackBar bottom to top

  18. 18

    How to fade color to transparent using hexbin and matplotlib?

  19. 19

    How to make Unity top panel semi-transparent?

  20. 20

    How do you fade in/fade out same div onclick of image using JQuery?

  21. 21

    Fade DIV out / in during CSS media query

  22. 22

    CSS Smooth fade and out transition (including text)

  23. 23

    How can I create a fade in/fade out effect using only CSS?

  24. 24

    Fade In/Out text and Loop using jQuery

  25. 25

    Fade only top part of div out as it reaches fixed navigation

  26. 26

    Fade out of div background affects other divs on top of background image

  27. 27

    fade in then fade out after some time by using css-transitions

  28. 28

    Fade out then Fade in Element using CSS3 Transition

  29. 29

    using css gradient to accomplish a fade out

HotTag

Archive