CSS and Its Selectors

The look and feel of a Web page depend upon the arrangement and appearance of HTML elements on it. HTML elements can be formatted to make it look appealing. When a website is created, the formatting and layout of all web pages should be consistent. To accomplish this task, Cascading Style Sheets (CSS) can be used.
CSS can be applied to specific HTML elements on a Web page, to all the elements on a Web page, or across all the pages of the website. Therefore, CSS can be used to enforce consistent display standards across all the Web pages of the Website
In this tutorial, we will show the audience the different CSS Selectors and their usage in designing your Web page.
What are Cascading Style Sheets (CSS)
Cascading Style Sheets fondly called CSS is a collection of styles that allows you to change the appearance of HTML elements on Web pages. It defines a set of standard rules that provide better control over the page layout and appearance. It is simply a designed language intended to simplify the process of making Web pages presentable.
Reasons for using CSS
The reasons for using CSS are:
• It can control the layout of multiple Web pages all at once which saves time.
• CSS describes how HTML elements are to be displayed on screen or in other media.
• CSS enables compatibility across Devices.
• CSS offers consistent platform independence.
• It reduces the file transfer size.
Identifying the syntax of CSS
As a Web developer, you need to ensure that your website is visually appealing and user-friendly. For example, you want to color text, background and hyperlinks on the website. To achieve consistency with multiple Web pages and graphics, you can implement CSS to stylize the Web pages and text on the website.
CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in your document. The syntax of CSS is made of the following parts:
• Selector: A Selector is an HTML tag to which a style will be applied. It specifies the HTML element that you want to style. For example <h1> or <table> etc.
• Property: A property is a type of attribute of an HTML tag. It is the attribute name of the element. They could be color or border.
• Value: Values are assigned to properties. For example, the color property can have a value of either red or #F1F1F1 It is the value of the attribute.
You can put CSS Style rule syntax as follows:
Selector
{
//Declaration Block
Property: value;
}
A table border can be defined as follows:
table {border :3px solid #C00;}
Here the table is a selector and the border is a property and given the value3px solid #C00 is the value of that property. Here the table is a selector and the border is a property and given the value3px solid #C00 is the value of that property
These selectors can be defined in simple forms. Let us go through them one after the other.
ID selectors
CSS styles can be applied to an element with a specific ID by using an ID selector. An ID selector is used to identify an element that you need to style differently from the rest of the page. An ID selector is defined by using the hash symbol (#).
#green {
color: #00000;
}
This rule renders the content in green for every element with the ID attribute set to green on our document.
You can make it more particular. For example:
h1#green {
color: #00000;
}
This rule renders the content in green for only <h1> elements with the ID attribute to be set to green.
ID selectors become effective especially when they are used as the foundation for descendant selectors.
For example:
#green h2{
color: #00000;
}
This example explains that all level 2 headings will be displayed in green color when those headings lie within tags with the ID attribute set to green.
DESCENDANT selectors
The descendant selector matches all elements that are descendants of a specified element. Suppose you want to style a rule to a particular element that lies inside a particular element.
<html>
<head>
<style>
div p{
background-color: green;
}
</style>
</head>
<body>
<p> The descendant selector matches all elements that are descendants of a specified element</p>
<div>
<p>Water is life. </p>
<p>Water is good. </p>
</div>
This is the result:

CLASS selectors
The class selector selects elements with a specific class attribute. All the elements having that class will be formatted according to the defined rule. To select elements with a specific class, write a period (.) character, followed by the name of the class.
<!DOCTYPE html>
<html>
<head>
<style>
.name {
background-color: green;
}
</style>
</head>
<body>
<h1>Demo of .class selector</h1>
<div class="name">
<p>My name is James. </p>
</div>
<p>I live in Nigeria. </p>
</body>
</html>
This is the result:

ATTRIBUTE selectors
The Attribute selector is used to select elements with a specified attribute. The advantage of this method is that the <input type= “text”/> is unaffected as the color is applied to desired fields in the document.
<!DOCTYPE html>
<html>
<head>
<style>
a[target] {
background-color: yellow
}
</style>
</head>
<body>
<h1>Attribute Selector</h1>
<p>The websites with [a target] attribute get the green background</p>
<a href="https://www.smart kids.com">smart kids.com</a>
<a href="https://www.codischool.com"target="top">www.codischool.com</a>
</body>
</html>
This is the result:

GROUPING selectors
You have used the different CSS selectors that only target either a class or an ID or a single element for applying styles. That notwithstanding, CSS makes it incredibly easy to target more than one element at a time to allow for specific CSS properties and rules. You can apply a style to many selectors if you like. To do this, you have to separate the selector names with commas.
For Example
h1, h2, h3 {
font-family: Arial black, Verdana, sans-serif
}
This defined style rule will apply to h1, h2, and h3 elements as well. The order of the list is irrelevant. All elements in the sector will have corresponding declarations applied to them.
Conclusion
A CSS selector is the first part of a CSS rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them. Now you can enjoy working with these important CSS selectors.
About the author
A professional technical writer with over two years of experience encompasses an ability and proficiency in producing technical documents such as manuals, guides, and handbooks for software systems and technical products. I have extensive professional experience in various industries, which include IT, finance, and telecommunication. As a technical writer, I can simplify technical content to a point where one can easily understand it, especially for the development groups and the users. I possess good knowledge and experience in the most typical tools and technologies for documentation and creating the best quality technical documents.
Comments
Join the discussionNo comments yet. Be the first to comment.


