Table of Contents
grammar
Example 1 (CSS class name selector)
Using the class selector in CSS ruleset
Example 2 (CSS ID Selector)
Using the id selector in CSS ruleset
Example 3 (CSS Multiple Selector)
Using the Multiple selector in CSS ruleset
Example 4 (CSS nested element selector)
Using the nested selectors in CSS ruleset
Example 5 (CSS pseudo-selector)
Using the pseudo selectors in CSS ruleset
Home Web Front-end CSS Tutorial What are CSS rulesets used for?

What are CSS rulesets used for?

Sep 09, 2023 pm 04:01 PM

What are CSS rulesets used for?

CSS stands for Cascading Style Sheets. It is used to style HTML elements. HTML is used to create or add content to web pages. After that, developers use CSS to render the HTML content in a specific style to make it look great.

The CSS rule set mainly contains two parts. One is a CSS selector and the other is a declaration block.

CSS selectors are used to select HTML elements, and the declaration block contains CSS properties in key-value format to be applied to HTML elements.

grammar

Users can use the CSS rule set to style HTML elements according to the following syntax.

selector {
   /* declaration block */
}
Copy after login

In the above syntax, 'selector' can be the class name, id, etc. of the HTML element, which is used to select HTML elements. A declaration block contains multiple CSS properties and their values ​​to apply to HTML elements.

Example 1 (CSS class name selector)

In the example below, we use the class name as the CSS selector when defining the CSS ruleset. The code below has three div elements with different class names. We selected each div element by its class name and applied a different CSS style, which the user can observe in the output.

<html>
<head>
   <style>
      .one {
         background-color: red;
         color: white;
         padding: 10px;
         margin: 10px;
         border: 1px solid green;
      }
      .two {
         background-color: green;
         color: white;
         padding: 10px;
         margin: 10px;
         border: 3px solid yellow;
      }
      .three {
         background-color: blue;
         color: white;
         padding: 10px;
         margin: 10px;
         border: 2px solid pink;
      }
   </style>
</head>
<body>
   <h2 id="Using-the-i-class-selector-i-in-CSS-ruleset"> Using the <i> class selector </i> in CSS ruleset </h2>
   <div class = "one"> One </div>
   <div class = "two"> Two </div>
   <div class = "three"> Three </div>
</body>
</html>
Copy after login

Example 2 (CSS ID Selector)

In the example below, we use the id of the HTML element as the CSS selector when defining the CSS rule set. In HTML, two elements can never contain the same id.

Here, we have a div element with an id of "card", which contains two other div elements with an id equal to "content1" and "content2". We style all HTML elements by accessing them by their ID, which the user can observe in the output.

<html>
<head>
   <style>
      #card {
         width: 140px;
         height: 300px;
         background-color: grey;
         border-radius: 12px;
         border: 2px solid red;
         display: flex;
         justify-content: space-between;
         flex-direction: column;
      }
      #content1 {
         width: 100px;
         height: 100px;
         background-color: blue;
         border-radius: 12px;
         color: white;
         border: 2px solid red;
         margin: 20px;
      }
      #content2 {
         width: 100px;
         height: 100px;
         color: white;
         background-color: blue;
         border-radius: 12px;
         border: 2px solid red;
         margin: 20px;
      }
   </style>
</head>
<body>
   <h2 id="Using-the-i-id-selector-i-in-CSS-ruleset"> Using the <i> id selector </i> in CSS ruleset </h2>
   <div id = "card">
      <div id = "content1"> HTML </div>
      <div id = "content2"> CSS </div>
   </div>
</body>
</html>
Copy after login

Example 3 (CSS Multiple Selector)

In the example below, we use multiple CSS selectors to apply the same CSS style to multiple HTML elements at once.

We have three

elements with different class names and IDs. In CSS, we use the “.text1, .text2, #para1” CSS selector to apply the same styles added in the declaration block to all HTML elements.

Additionally, we have selected all three HTML elements individually using class name and ID CSS selectors in order to apply different styles on different elements.

<html>
<head>
   <style>
      .text1,
      .text2,
      #para1 {
         margin: 10px;
         height: auto;
         padding: 10px;
         width: 200px;
      }
      .text1 {
         background-color: red;
         color: white;
         font-size: 2rem;
      }
      .text2 {
         background-color: green;
         color: red;
         font-size: 1.7rem;
      }
      #para1 {
         background-color: blue;
         color: white;
         font-size: 1rem;
      }
   </style>
</head>
<body>
   <h2 id="Using-the-i-Multiple-selector-i-in-CSS-ruleset"> Using the <i> Multiple selector </i> in CSS ruleset </h2>
   <p class = "text1"> This is the first paragraph </p>
   <p class = "text2"> This is a second paragraph. </p>
   <p id = "para1"> This is the third paragraph. </p>
</body>
</html>
Copy after login

Example 4 (CSS nested element selector)

In the following example, we introduce CSS’s nested selectors. In HTML, a div element contains multiple elements with the class name "link".

In CSS, we use the "div .link" CSS selector, which selects all HTML elements with the class name "link" and the descendants of the div element. If we use "div.link" as CSS selector, it will apply the style to all div elements with class name "link". Therefore, "div.link" and "div .link" are both different CSS selectors.

In the output, the user can observe that CSS styles are applied to all HTML elements that are descendants of the div element, but not to elements outside the div element.

<html>
<head>
   <style>
      div .link {
         color: red;
         text-decoration: none;
      }
   </style>
</head>
<body>
   <h2 id="Using-the-i-nested-selectors-i-in-CSS-ruleset"> Using the <i> nested selectors </i> in CSS ruleset </h2>
   <div>
      <a href = "#" class = "link"> Link1 </a>
      <a href = "#" class = "link"> Link2 </a>
      <a href = "#" class = "link"> Link3 </a>
   </div><br>
   <a href = "#" class = "link"> Link 5 </a>
</body>
</html>
Copy after login

Example 5 (CSS pseudo-selector)

In this example, we demonstrate the use of CSS pseudo-selectors. There are many kinds of CSS pseudo-selectors, and we use some of them here.

Here, we have a "container" div element that contains 4 child elements with the "element" class name. We use the ":hover" pseudo-selector to change the background color of the "container" div element when the user hovers over the div element.

After that, we use the ':first-child', ':last-child' and ':nth-child()' CSS selectors and the '.element' selector to select the first child element, the last child element elements, and the nth child, respectively.

In the output, the user can observe that different CSS styles are applied to the first child, the last child, and the second child.

<html>
<head>
   <style>
      .container {
         height: 100px;
         width: 500px;
         background-color: blue;
         padding: 10px;
         display: flex;
         justify-content: space-around;
         border-radius: 12px;
         font-size: 1.2rem;
      }
      /* hover pseudo class */
      .container:hover {
         background-color: red;
      }
      /* first child pseudo class */
      .element:first-child {
         background-color: green;
         color: white;
      }
      /* last child pseudo class */
      .element:last-child {
         background-color: grey;
         color: black;
      }
      /* nth child pseudo class */
      .element:nth-child(2) {
         background-color: pink;
         color: green;
      }
   </style>
</head>
<body>
   <h2 id="Using-the-i-pseudo-selectors-i-in-CSS-ruleset"> Using the <i> pseudo selectors </i> in CSS ruleset </h2>
   <div class = "container">
      <div class = "element"> First Child </div>
      <div class = "element"> Second Child </div>
      <div class = "element"> Third Child </div>
      <div class = "element"> Fourth Child </div>
   </div>
</body>
</html>
Copy after login

Users learned how to use different CSS rule sets in this tutorial. We use class name and id as selector. Additionally, we learned about using multiple CSS selectors and nested CSS selectors. In the previous example, we learned how to use pseudo-selectors from a CSS ruleset.

The above is the detailed content of What are CSS rulesets used for?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let&#039;s look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Create a JavaScript Contact Form With the Smart Forms Framework Create a JavaScript Contact Form With the Smart Forms Framework Mar 07, 2025 am 11:33 AM

This tutorial demonstrates creating professional-looking JavaScript forms using the Smart Forms framework (note: no longer available). While the framework itself is unavailable, the principles and techniques remain relevant for other form builders.

Demystifying Screen Readers: Accessible Forms & Best Practices Demystifying Screen Readers: Accessible Forms & Best Practices Mar 08, 2025 am 09:45 AM

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

Create an Inline Text Editor With the contentEditable Attribute Create an Inline Text Editor With the contentEditable Attribute Mar 02, 2025 am 09:03 AM

Building an inline text editor isn't trivial. The process starts by making the target element editable, handling potential SyntaxError exceptions along the way. Creating Your Editor To build this editor, you'll need to dynamically modify the content

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Comparing the 5 Best PHP Form Builders (And 3 Free Scripts) Mar 04, 2025 am 10:22 AM

This article explores the top PHP form builder scripts available on Envato Market, comparing their features, flexibility, and design. Before diving into specific options, let's understand what a PHP form builder is and why you'd use one. A PHP form

File Upload With Multer in Node.js and Express File Upload With Multer in Node.js and Express Mar 02, 2025 am 09:15 AM

This tutorial guides you through building a file upload system using Node.js, Express, and Multer. We'll cover single and multiple file uploads, and even demonstrate storing images in a MongoDB database for later retrieval. First, set up your projec

See all articles