search
HomeWeb Front-endCSS TutorialWhat 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 */
}

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>

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>

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>

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>

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>

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
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Anchor Positioning Just Don't Care About Source OrderAnchor Positioning Just Don't Care About Source OrderApr 29, 2025 am 09:37 AM

The fact that anchor positioning eschews HTML source order is so CSS-y because it's another separation of concerns between content and presentation.

What does margin: 40px 100px 120px 80px signify?What does margin: 40px 100px 120px 80px signify?Apr 28, 2025 pm 05:31 PM

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

What are the different CSS border properties?What are the different CSS border properties?Apr 28, 2025 pm 05:30 PM

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

What are CSS backgrounds, list the properties?What are CSS backgrounds, list the properties?Apr 28, 2025 pm 05:29 PM

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

What are CSS HSL Colors?What are CSS HSL Colors?Apr 28, 2025 pm 05:28 PM

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

How can we add comments in CSS?How can we add comments in CSS?Apr 28, 2025 pm 05:27 PM

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

What are CSS Selectors?What are CSS Selectors?Apr 28, 2025 pm 05:26 PM

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

Which type of CSS holds the highest priority?Which type of CSS holds the highest priority?Apr 28, 2025 pm 05:25 PM

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.