top of page

Understanding CSS Selectors and Properties

Writer's picture: IOTA ACADEMYIOTA ACADEMY

A fundamental component of web development, CSS (Cascading Style Sheets) gives programmers complete control over a website's appearance and feel. Selectors and properties, which specify what is styled and how, are two of CSS's essential components. This blog offers a thorough explanation of these ideas together with real-world examples and implementations.


css

CSS Selectors: What Are They?


Patterns called CSS selectors are used to choose the HTML components you wish to decorate. They serve as a link between your intended stylistic rules and your HTML structure.


Why do selectors matter? 


Selectors enable exact style without requiring changes to the HTML structure by allowing you to target particular elements or groups of elements.

CSS Selector TypesThere are several kinds of CSS selectors, and each is appropriate for a certain scenario. Here is a thorough analysis:


Selector Type

Syntax

Description

Example

Universal Selector

*

Selects all elements in the document.

* { margin: 0; }

Type Selector

element_name

Selects all elements of the specified type (e.g., <p> or <h1>).

p { color: blue; }

Class Selector

.class_name

Targets elements with a specific class attribute.

.card { padding: 10px; }

ID Selector

Targets a specific element with an ID attribute.

#header { font-size: 20px; }

Grouping Selector

selector1, selector2

Allows you to style multiple selectors simultaneously.

h1, h2 { font-family: Arial; }

Descendant Selector

element element

Selects elements nested within another element.

div p { color: green; }

Child Selector

element > element

Targets direct child elements of a parent.

ul > li { list-style: none; }

Adjacent Sibling Selector

element + element

Targets an element that is immediately preceded by a sibling element.

h1 + p { margin-top: 10px; }

General Sibling Selector

element ~ element

Targets all sibling elements that share the same parent.

h1 ~ p { color: gray; }

Pseudo-Class Selector

:pseudo-class

Targets elements in a specific state (e.g., hover, focus, first-child).

a:hover { color: red; }

Pseudo-Element Selector

::pseudo-element

Targets specific parts of elements, such as the first letter or line.

p::first-letter { font-size: 2em; }

Attribute Selector

[attribute=value]

Selects elements with specific attribute values.

input[type="text"] { border: 1px solid black; }


Practical Implementation: Selectors


Example Code: CSS Selectors in Action


Here’s a concise example demonstrating how the selectors are applied:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>CSS Selectors Example</title>

    <style>

        * { margin: 0; padding: 0; }

        h1 { color: darkblue; }

        .highlight { background-color: yellow; }

        #intro { font-weight: bold; }

        h1, h2 { font-family: Arial, sans-serif; }

        div p { color: green; }

        ul > li { color: red; }

        a:hover { text-decoration: underline; }

        input[type="text"] { border: 2px solid gray; }

    </style>

</head>

<body>

    <h1>CSS Selectors Example</h1>

    <h2>Understanding the Basics</h2>

    <div id="intro">

        <p>This paragraph is part of a div with an ID selector.</p>

        <p class="highlight">This paragraph uses a class selector for highlighting.</p>

    </div>

    <ul>

        <li>First Item</li>

        <li>Second Item</li>

    </ul>

    <a href="#">Hover over this link</a>

    <br><br>

    <input type="text" placeholder="Type something...">

</body>

</html>

Code Explanation

  • The * selector resets margin and padding for all elements.

  • The h1 selector applies dark blue color to headers.

  • The .highlight class applies a yellow background to targeted elements.

  • The #intro ID selector makes the text bold within the <div>.

  • The div p descendant selector colors paragraph text inside a div green.

  • The ul > li child selector colors list items in red.

  • The a:hover pseudo-class adds an underline effect when hovering over the link.

  • The input[type="text"] attribute selector applies a border to text input elements.


Example Output:

This code will give output as shown in the screen below:


css selectors

What Are Properties in CSS?


The way HTML components look is controlled by CSS attributes. Every property has a corresponding value that determines the appearance of the elements. The layout, colour, size, spacing, and other characteristics of web elements are all defined by properties.


Common CSS Properties

Property

Description

Example

color

Sets the text color.

color: blue;

background-color

Sets the background color of an element.

background-color: lightgray;

font-size

Adjusts the text size.

font-size: 16px;

font-family

Defines the font style.

font-family: Verdana;

margin

Creates space outside the element.

margin: 20px;

padding

Creates space inside the element.

padding: 10px;

border

Adds a border around the element.

border: 1px solid black;

width

Sets the element’s width.

width: 200px;

height

Sets the element’s height.

height: 100px;

Practical Implementation of Properties


A concise example to show CSS properties in action:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>CSS Properties Example</title>

    <style>

        body { font-family: Arial, sans-serif; }

        h1 { color: darkred; }

        div { background-color: lightblue; padding: 10px; margin: 10px; }

        p { font-size: 18px; border: 2px dashed green; }

        .box { width: 200px; height: 100px; background-color: lightcoral; }

    </style>

</head>

<body>

    <h1>CSS Properties Example</h1>

    <div>

        <p>This paragraph has a border and larger font size.</p>

        <p class="box">This is a box with fixed dimensions.</p>

    </div>

</body>

</html>

Explaination

  • The <h1> element appears in dark red.

  • The <div> has a light blue background with padding and margins.

  • Paragraphs have a green dashed border and a larger font size.

  • The .box class creates a rectangular box with a coral background.


Example Output:

This code will give output as shown in the screen below:

css properties

Conclusion


In order to style web pages, CSS selectors and attributes are essential. While attributes determine an element's appearance, selectors aid in focusing on particular elements or groups. You may make visually appealing and useful websites by becoming proficient with these tools. Develop your ability to use these ideas in practical projects to create aesthetically pleasing and user-friendly web experiences.


Call to Action


Do you want to improve the look and feel of your websites? Enroll in IOTA Academy's Web Development Course to begin learning CSS right now! Our professionals will walk you through every stage, regardless of your level of experience or desire to improve your abilities. Enroll now to get started on your path to becoming an expert in CSS and take the first step toward building beautiful, dynamic webpages!

 

6 views0 comments

Comments


bottom of page