Techoceanhub logo

Selectors and styling rules in CSS

Selectors and styling rules are essential concepts in web development and cascading style sheets (CSS). They are used to target specific HTML elements and apply styles to them, such as changing their appearance, layout, or behavior. CSS is a stylesheet language used to control the presentation of web documents, including HTML and XML documents.

Let’s delve into each concept:

  1. Selectors: Selectors are patterns used to target HTML elements in a web page that you want to style. They define which elements should be affected by the styling rules. CSS selectors can target elements based on various criteria, such as element names, class names, IDs, attributes, and more.

Here are some common types of CSS selectors:

  • Element Selector: Targets elements by their HTML tag name. For example, p targets all <p> paragraphs in the document.
  • Class Selector: Targets elements with a specific class attribute. For example, .my-class targets all elements with class="my-class".
  • ID Selector: Targets an element with a specific ID attribute. For example, #my-id targets the element with id="my-id".
  • Attribute Selector: Targets elements with specific attribute values. For example, input[type="text"] targets all text input elements.
  1. Styling Rules: Once you’ve selected the elements you want to style using selectors, you define the styling rules that will be applied to those elements. Styling rules consist of one or more property-value pairs, where the property represents the CSS property you want to change, and the value is the new value you want to apply.

Here’s an example of a simple CSS rule:

p {
  color: blue;
  font-size: 16px;
}

In this example, the selector p targets all paragraphs, and the styling rules inside the curly braces change the text color to blue and set the font size to 16 pixels.

Multiple rules can be combined together, and styles can also be targeted using more complex selectors to achieve precise control over the appearance of web elements.

CSS can be included in an HTML document using the <style> tag or in an external CSS file, which is then linked to the HTML document using the <link> tag.

Here’s an example of an external CSS file:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <p class="my-class">This is a paragraph with a class.</p>
  <p id="my-id">This is a paragraph with an ID.</p>
</body>
</html>

And the corresponding styles.css file:

p.my-class {
  color: red;
}

p#my-id {
  font-weight: bold;
}

In this example, we’re targeting the paragraphs with the class my-class and the ID my-id and applying different styles to each of them.

These are the fundamental concepts of CSS selectors and styling rules, which allow you to control the visual presentation of your web pages and create appealing and consistent designs.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .