프로그래밍, 코딩, 컴퓨터/html, css, js

Comprehensive Guide on Types and Usage of CSS Selectors

마이데일리 2023. 7. 15. 23:01
반응형

CSS (Cascading Style Sheets) is a style sheet language used to specify the design and style of HTML documents. CSS applies styles to specific elements using selectors. In this article, we will take a comprehensive look at the types and usage of CSS selectors.

First of all, let's dive a little deeper into CSS. CSS is an essential aspect of web design, and it's necessary to understand how it works to create beautiful web pages. CSS selectors are a crucial tool for web designers to select and style specific HTML elements. Without selectors, it would be difficult to target specific elements and apply styles to them.

1. Basic Selectors

There are several types of basic selectors you can use in CSS. One type is name selectors, which select elements using the name of the element. Another type is class selectors, which select elements using the class name of the element. Finally, there are ID selectors, which select elements using the ID of the element. Basic selectors are an excellent way to target specific elements and apply styles to them.


/* Name Selectors */
p {
  color: red;
}

/* Class Selectors */
.intro {
  font-size: 20px;
}

/* ID Selectors */
#header {
  background-color: blue;
}

2. Combinator Selectors

Combinator selectors are used to combine two or more selectors. There are several types of combinator selectors, including descendant selectors, child selectors, and sibling selectors. These selectors can be used to target specific elements based on their relationship to other elements on the page.


/* Descendant Selectors */
div p {
  font-size: 16px;
}

/* Child Selectors */
ul > li {
  list-style-type: none;
}

/* Sibling Selectors */
h1 + p {
  font-weight: bold;
}

3. Attribute Selectors

Attribute selectors are another type of selector you can use in CSS. These selectors select HTML elements based on their attribute values. They can be used to target specific elements based on their attributes.


/* Attribute Selectors */
a[href="https://www.google.com"] {
  color: green;
}

In conclusion, CSS selectors are a powerful tool for web designers to create visually appealing web pages. By using basic selectors, combinator selectors, attribute selectors, and more, you can target specific elements and apply styles to them. By utilizing selectors, you can create a captivating web page that stands out from the rest.

반응형