web analytics

Class Selectors vs. ID Selector in CSS

Options

codeling 1602 - 6666
@2016-01-01 00:04:08

In the CSS, a class selector is a name preceded by a full stop (.) and an ID selector is a name preceded by a hash character (#).

So the CSS might look something like:

#top //id selector
{ 
    background-color: #ccc; 
    padding: 1em 
} 


.intro  //class selector

{
    color: red;
    font-weight: bold;
}

The HTML refers to the CSS by using the attributes id and class. It could look something like this:

<div id="top"> 

<h1>Chocolate curry</h1>

<p class="intro">This is my recipe for making curry purely with chocolate</p>

<p class="intro">Mmm mm mmmmm</p>

</div>

The difference between an ID and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one.

@2016-01-01 00:09:03

You can also apply a selector to a specific HTML element by simply stating the HTML selector first, so p.bold { font-weight: bold;} will only be applied to paragraph elements that have the class 'bold'.

<p>This is my recipe for making curry purely with chocolate</p>

<p class="bold">Mmm mm mmmmm</p>

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com