2

I am not sure if I understood it correctly. When styling in CSS like this:

body {
}

I'm referring to an element that belongs to HTML. When I write something like this:

.body {
}

I'm referring to a class name given by me. Is this correct?

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
user3389442
  • 87
  • 1
  • 2
  • 7

2 Answers2

4

You may want to have a look at the article on CSS Selectors from MDN, this is also a pretty healthy introductory article on selectors of the type you list, as well as some other important ones.

A non symbol prefixed 'word' denotes an element (body), a 'word' prefixed with a period (.) denotes a 'class' (.body)

Element selectors (called 'type' selectors) only refer to a specific type of element (e.g. body), class selectors can be applied to elements of any type, whereas something like id selectors (a hash preceding a 'word') are unique and can only be applied to a single element (i.e. one id per element, which must not be used elsewhere)

There are many types and combinations of selectors, to get an idea you may also want to read up on CSS specificity

Specificity is the means by which a browser decides which property values are the most relevant to an element and gets to be applied. Specificity is only based on the matching rules which are composed of selectors of different sorts.

SW4
  • 69,876
  • 20
  • 132
  • 137
2

Yes.

.body{
}

Would be something like this:

<p class='body'>This is text with a class of body</p>

But can also affect other elements with the class .body.

body{
}

Will affect the body element eg:

<body></body>

You can also use ID's. For example:

#body{
}

And you use this similar to a class. However only 1 ID of the same name per page.

<p id='body'>This is a body tag, but there should only ever be 1 ID per page.</p>

Take a look here for more information.

Albzi
  • 15,431
  • 6
  • 46
  • 63