Ids vs. Classes
A fundamental concept in coding for the internet is the idea of ids and classes.
First, we will discuss ids and classes in HTML. You can assign an element (ie. <p>, <img>, <h1>, <div>, etc.) an id and a class as follows:
<img id="someImage" class="imageClass" src="hello.png"/>ID
- A unique identifier for an element
- Use: to identify a single element on the page
Class
- A way of characterizing an element that has the same attributes as other elements in that class
- Use: to identify multiple elements that share common attributes
Now we will extend our knowledge to cover how to add style to certain elements with a particular id/class in CSS.
In order to stylize a certain element in our HTML, the notation: #[name of id] allows us to access the element identified with the [name of id] while the notation: .[name of class] allows us to access the element(s) who belong to the class [name of class].
Get Ankita Chadha’s stories in your inbox
Join Medium for free to get updates from this writer.
Consider the following HTML code:
<p id="text1" class="editText>Hey</p>
<p id="text2" class="editText>Hello</p>
<p id="text3" class="editText>Goodbye</p>
<p id="text4" class="editText>Howdy</p>Say we are given the following specs:
- All the elements should have the same font
- “Hey” should be colored red
- “Hello” should be colored pink
- “Goodbye” should be colored blue
- “Howdy” should be colored green
This is how we could stylize it in CSS:
.editText{
font-family: "Times New Roman", Times, serif;
}#text1{
color: red;
}#text2{
color: pink;
}#text3{
color: blue;
}#text4{
color: green;
}
And there you have it!






