13

If I have something like this in my HTML

    <div id="top">
        <div class="txt">
            <span class="welcome">Welcome on my website</span>
            <span class="links"><a href="Home">Home</a></span>
        </div>
    </div>

How I can select the welcome class in my CSS.

I've tried #top.txt.welcome but doesn't work. I've also tried #top.txt span.welcome.

Alex
  • 131
  • 1
  • 1
  • 3

6 Answers6

15

#top .txt is not #top.txt the latter means that the matched element has the id AND the class, while the former means that the matched element has the class, and one of its ancestors element has the id

greg0ire
  • 22,714
  • 16
  • 72
  • 101
4

You can use


span.welcome
#top .welcome
#top div.txt span.welcome
.welcome
Kubain
  • 56
  • 4
2
.welcome

#top div.txt .welcome

div#top div.txt span.welcome

it depends on how specific you want to be

Rony
  • 9,331
  • 2
  • 22
  • 22
2
#top .txt .welcome{}
theorise
  • 7,245
  • 14
  • 46
  • 65
1
div#top div.txt span.welcome

or

#top div.txt .welcome

or some other variation thereof...

Jim Lamb
  • 25,355
  • 6
  • 42
  • 48
1

If you want to use that 'welcome' class for perticular span tag than you can use two ways like...

.txt span.welcome

or

span.welcome

It'll works for your CSS class in your code perfectly.

This is the concept of nesting class you can directly refer from references below.

References: