Sunday, April 20, 2025

CSS Class Explained

In CSS, classes are used to apply styles to one or more HTML elements. They are a way of grouping multiple HTML elements together and applying the same styles to all of them.

Classes are useful in situations where we want to apply a specific set of styles to multiple elements on a page. For example, if we have several buttons on a page that should all look the same, we can give them all the same class name and apply the desired styles to that class in our CSS file. This way, we don't have to repeat the same styles for each individual button.

IDs, on the other hand, are used to apply styles to a single HTML element. Each ID should be unique to a single element on the page. IDs are useful when we want to apply a specific set of styles to a single element on the page that should not be shared with any other element.

There may be situations where using IDs is appropriate, such as when we want to target a specific element with JavaScript. Ultimately, the choice between using classes and IDs depends on the specific requirements of the project and how the styles need to be applied. 

Classes are created in CSS by using the period (.) symbol followed by a class name. For example: 

.my-class {
   /* CSS properties and values go here */
}

In this example, .my-class is the class selector, and any HTML element with the class attribute set to "my-class" will be targeted by these styles.

To apply this class to an HTML element, we simply add the class attribute to the element and set it equal to the class name: 

<div class="my-class">This element will be styled with the .my-class styles</div>

We can also apply multiple classes to a single element by separating each class name with a space: 

<div class="my-class another-class">This element will be styled with both the .my-class and .another-class styles</div>

In CSS, classes are a powerful way to apply styles to multiple HTML elements at once, and can greatly reduce the amount of redundant code needed in a stylesheet.


<!DOCTYPE html>

<html>
<head><title>Title</title></head>

<link rel="stylesheet" type="text/css" href="mystyle.css">

<body>

<p class="first_group">Default Sentence - First Group</p>

<p class="first_group">Default Sentence - First Group</p>

<p class="second_group">Default Sentence - Second Group</p>

<p class="second_group">Default Sentence - Second Group</p>

</body>
</html>

Classes are used here to apply styles to specific groups of paragraphs.

The CSS class selector is defined in the "mystyle.css" file, and the HTML paragraphs are assigned to the classes by using the "class" attribute.

For example, the first two paragraphs have the "first_group" class, while the next two paragraphs have the "second_group" class.

By using these classes in CSS, we can apply different styles to each group of paragraphs. This allows us to easily manage the styles of multiple elements that share a common attribute. 


.first_group {
	background: red;
	color: white;
	height: 20px;
}

.second_group {
	background: yellow;
	color: blue;
	height: 30px;
}

The .first_group class is applied to any HTML element with class="first_group", and the following CSS properties are applied to those elements:

  • background: red sets the background color to red
  • color: white sets the font color to white
  • height: 20px sets the height of the element to 20 pixels

The .second_group class is applied to any HTML element with class="second_group", and the following CSS properties are applied to those elements:

  • background: yellow sets the background color to yellow
  • color: blue sets the font color to blue
  • height: 30px sets the height of the element to 30 pixels

By using classes to apply styles, multiple HTML elements can be styled the same way by applying the same class to each element.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...