CSS Selectors and Properties

CSS (Cascading Style Sheets) is a powerful language used to style and layout web pages. To apply styles, CSS uses selectors to target HTML elements, and properties to define the specific styles that should be applied to these elements. Understanding how CSS selectors and properties work is essential for controlling the look and feel of a website. In this article, we’ll explore CSS selectors and properties, and how they can be used to create visually appealing web pages.


What Are CSS Selectors?

CSS selectors are patterns used to select HTML elements on a webpage that you want to style. They tell the browser which HTML element or group of elements to apply specific styles to. Selectors can target elements by their tag name, class, ID, attribute, or other characteristics.

Types of CSS Selectors:

  1. Element Selector (Type Selector):

    • The element selector targets all elements of a specified type.

    • For example, the following CSS will apply the style to all <p> (paragraph) elements on a page.

    p {
      color: blue;
      font-size: 16px;
    }
  2. Class Selector:

    • The class selector targets elements with a specific class attribute. It is defined by a period (.) followed by the class name.

    • This allows you to apply styles to multiple elements that share the same class.

    .highlight {
      background-color: yellow;
      font-weight: bold;
    }

    HTML Example:

    <p class="highlight">This text will be highlighted.</p>
    <div class="highlight">This div will also be highlighted.</div>
  3. ID Selector:

    • The ID selector targets a single element with a specific ID attribute. It is defined by a hash (#) followed by the ID name.

    • Each ID must be unique within a webpage.

    #header {
      background-color: #333;
      color: white;
      padding: 20px;
    }

    HTML Example:

    <div id="header">This is the header</div>
  4. Universal Selector:

    • The universal selector (*) targets all elements on the page.

    • It's useful for applying a global style, like setting margin and padding to zero.

    * {
      margin: 0;
      padding: 0;
    }
  5. Attribute Selector:

    • The attribute selector targets elements based on the presence or value of their attributes.

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

    HTML Example:

    <input type="text" />
    <input type="password" />
  6. Descendant Selector:

    • The descendant selector targets an element that is a child, grandchild, or further descendant of another element.

    div p {
      color: red;
    }

    HTML Example:

    <div>
      <p>This paragraph will be red.</p>
    </div>
  7. Child Selector:

    • The child selector (>) targets direct children of a specified element.

    div > p {
      color: green;
    }

    HTML Example:

    <div>
      <p>This paragraph will be green.</p>
      <span><p>This paragraph will not be green.</p></span>
    </div>
  8. Pseudo-Classes:

    • Pseudo-classes allow you to style elements in specific states, such as when they are hovered over, focused, or active.

    a:hover {
      color: red;
    }

    HTML Example:

    <a href="#">Hover over me</a>
  9. Pseudo-Elements:

    • Pseudo-elements target parts of an element, such as the first letter or the content before an element.

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

    HTML Example:

    <p>This is a paragraph with the first letter styled.</p>

What Are CSS Properties?

CSS properties define the style applied to an element. They dictate how an element will look, from colors and fonts to positioning and layout. Each property has a corresponding value that specifies the desired style.

Common CSS Properties:

  1. Color and Background Properties:

    • color: Sets the text color.

    • background-color: Sets the background color of an element.

    • background-image: Sets a background image for an element.

    p {
      color: blue;
      background-color: lightgray;
    }
  2. Text Properties:

    • font-family: Specifies the font for text.

    • font-size: Sets the size of the font.

    • font-weight: Defines the thickness of the font (e.g., normal, bold).

    • line-height: Sets the amount of space between lines of text.

    h1 {
      font-family: Arial, sans-serif;
      font-size: 24px;
      font-weight: bold;
      line-height: 1.5;
    }
  3. Box Model Properties:

    • width and height: Define the dimensions of an element.

    • padding: Adds space inside an element, between the content and the border.

    • margin: Adds space outside an element, between it and other elements.

    • border: Defines the border style, width, and color of an element.

    div {
      width: 300px;
      height: 200px;
      margin: 20px;
      padding: 15px;
      border: 1px solid #ccc;
    }
  4. Layout Properties:

    • display: Specifies how an element is displayed on the page (e.g., block, inline, flex, grid).

    • position: Defines the positioning method for an element (e.g., relative, absolute, fixed).

    • top, right, bottom, left: Specifies the offset of an element in a positioned container.

    div {
      display: flex;
      justify-content: space-between;
    }
  5. Visibility and Opacity:

    • visibility: Controls whether an element is visible or hidden, while still taking up space.

    • opacity: Sets the transparency level of an element.

    .hidden {
      visibility: hidden;
    }
    
    .transparent {
      opacity: 0.5;
    }
  6. Border Radius and Box Shadow:

    • border-radius: Rounds the corners of an element.

    • box-shadow: Adds shadow effects to elements.

    div {
      border-radius: 10px;
      box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
    }
  7. Flexbox and Grid Properties:

    • flex: Defines how flex items are distributed in a flex container.

    • grid-template-columns: Specifies the number of columns in a CSS grid layout.

    .container {
      display: flex;
      justify-content: space-around;
    }
    
    .grid {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
    }

Last updated

Was this helpful?