Click here to Skip to main content
15,885,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some text in an h1 tag that I would like to put a shadow around using box-shadow. However, I only want the shadow to cover the width of the text, not the entire screen. I also want the text to remain centered. Here is my code so far (which is inside a style element).


h1 {

    text-align: center;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0,0,0,0.23);
        }


What I have tried:

I tried including width: 400px (which is about the size of my text) but then everything moves to the left - it is no longer aligned to the center which is what I want.
Posted
Updated 17-Aug-21 6:30am

1 solution

You'll need to wrap another element around the text. For example:
HTML
<h1><span>This is the text</span></h1>
CSS
h1 {
    text-align: center;
}

h1 > span {
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
}
Demo[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900