Syntax and Selector in CSS

CSS syntax

CSS rules set includes selector (selector) and declarative block (declaration). The selector points to the part from the HTML that you want to style. The declaration block contains one or more declarations, separated by semicolons. Each declaration consists of CSS name and value, separated by commas.

CSS declaration always ends with a semicolon, the declaration block is enclosed in curly braces. In the example below, the elements

will be centered, red text.

 p { 
color: red;
text-align: center;
}

CSS Selector

The CSS selector is used to find (and select) HTML elements based on the element's name, id , class , attribute .

Element Selector

This is an element selection tool based on element names. You can select all elements

on the page as shown below (in this case, all elements

will be centered and red in color).

 p { 
text-align: center;
color: red;
}

id Selector

This tool uses the id attribute of the HTML element to select.id of an element in the page must be unique, so the Selector id is used to select a single element.

To select an element by id, use the # character in front, then the element id. The following style rule applies to HTML elements with id='para1'.

 #para1 { 
text-align: center;
color: red;
}

Note: The id name cannot start with a number.

Selector class

This tool selects the element with the class. attribute class. To select, write the character (.) Followed by the class name. In the example below, all HTML elements with class = "center" will be red and centered.

 .center { 
text-align: center;
color: red;
}

You can specify only certain HTML elements that are affected by the class. In the example below only the element

having class="center" is centered.

 p.center { 
text-align: center;
color: red;
}

HTML elements can refer to more than one class. In the example below, the element

styled according to class="center" and class="large" .

The paragraph uses two layers.

Note : class names cannot start with numbers.

Group Selector

If there are elements of the same type:

 h1 { 
text-align: center;
color: red;
}

h2 {
text-align: center;
color: red;
}

p {
text-align: center;
color: red;
}

You can group reselect tools to minimize code, comma separated tools. Here's how to group the selection tools above.

 h1, h2, p { 
text-align: center;
color: red;
}

Comment in CSS

Comments are used to explain the code, which may help to edit the source code later when needed. The browser will not display comments. The comment in CSS begins with / * and ends with * / and can span multiple lines.

 p { 
color: red;
/* Đây là bình luận một dòng */
text-align: center;
}

/* Đây là
bình luận
nhiều dòng */

Try the code below and see how the browser displays in the image below.








This title is preserved


This title is red and centered.


This title is red, centered and sized


to.


Syntax and Selector in CSS Picture 1
Use the selector to style HTML elements

Previous article: Introduction to CSS

Lesson: How to use CSS to style HTML pages

5 ★ | 2 Vote

May be interested

  • Read the File record in Node.jsRead the File record in Node.js
    in the previous chapters, you found that you used a lot of require (fs) syntax. so what is the syntax to do? this is the syntax to declare fs module to deploy file i / o operations in node.js.
  • Handling errors in JavaScriptHandling errors in JavaScript
    there are 3 types of errors in the program: (a) syntax error (syntax error), (b) error while running the program (runtime error), and (c) error of logic of program structure (logical error) .
  • PHP syntaxPHP syntax
    before starting programming in any language, the basic syntax and program structure is very important. this chapter introduces you to the basic php syntax, including: php tags, comments, print commands, echo commands, and two print and echo commands.
  • ID selector in CSSID selector in CSS
    in css, id is used to style the element with the specified id.
  • Loop Bug 6-year life is again discovered, affecting most PDF viewersLoop Bug 6-year life is again discovered, affecting most PDF viewers
    according to german software developer hanno böck, an error was discovered in the pdf syntax library from 2011 to appear on the popular pdf viewers today.
  • IF commands ... ELSE in SQL ServerIF commands ... ELSE in SQL Server
    like other programming languages, sql server also provides users with an if command el .... the article will detail how to use the syntax and clear examples to make it easier for you to imagine if ... else.
  • Basic syntax of C programmingBasic syntax of C programming
    you already know about the basic structure of the c program, now you will easily understand the basic blocks in the c language.
  • HLOOKUP function in Excel, syntax and detailed usageHLOOKUP function in Excel, syntax and detailed usage
    the hlookup function in excel helps to search for data horizontally and return results vertically. this article will guide you on how to use this function with simple syntax and examples.
  • Tinder integrates a new AI tool to automatically select the most suitable profile photo for usersTinder integrates a new AI tool to automatically select the most suitable profile photo for users
    online dating app tinder has just launched a new ai-powered tool called photo selector, helping users reduce the time it takes to choose photos for their tinder profile.
  • Combinator in CSSCombinator in CSS
    combinator represents the relationship between selectors, allowing combining selectors together as strings.