Home >Web Front-end >Front-end Q&A >How to use css
CSS (Cascading Style Sheets) is a language used to define web page styles. It is used together with HTML to build web pages. CSS controls the appearance and position of HTML elements to beautify web pages and improve user experience.
In this article, we will introduce the basics of CSS and how to use CSS to change the style of HTML elements.
CSS consists of selectors and declarations. Selectors are used to select HTML elements to which styles should be applied, while declarations specify the styles to be applied to the selected elements.
The following are some common selectors:
Element selector: By specifying the HTML element name Select an element, for example:
p { color: red; }
This will select all e388a4556c0f65e1904146cc1a846bee elements in the document and set their color to red.
Class selector: Select elements by specifying the class name, for example:
.my-class { background-color: yellow; }
This will select all elements with class="my- class" elements and set their background color to yellow.
ID selector: Selects a single element by specifying the id, for example:
#my-id { font-size: 20px; }
This will select the element with id="my-id" element and set its font size to 20 pixels.
Attribute selector: Selects elements by specifying their attributes, for example:
a[href="https://www.google.com"] { color: blue; }
This will select all ## that point to Google sites # elements, set their color to blue.
Pseudo-classes and pseudo-elements: Select elements by specifying the element state or position, for example:
a:hover { text-decoration: underline; }This will select all
p { color: red; }In this rule, "color" is the attribute and "red" is the value. This will select all e388a4556c0f65e1904146cc1a846bee elements in the document and set their color to red. How to apply CSSCSS can be applied to HTML documents in three ways: internal style sheets, external style sheets, and inline styles. We will look at each of these three methods separately. Internal style sheetInternal style sheet means that CSS rules are included in the c9ccee2e6ea535a969eb3f532ad9fe89 tag and embedded in the 93f0f5c25f18dab9d176bd4f6de5d30e section of the HTML document. For example:
<!DOCTYPE html> <html> <head> <title>My Webpage</title> <style> p { color: red; } </style> </head> <body> <p>This text will be red.</p> </body> </html>In this example, the CSS rule is included in the c9ccee2e6ea535a969eb3f532ad9fe89 tag and sets the color of all e388a4556c0f65e1904146cc1a846bee elements to red. External style sheetAn external style sheet is an independent CSS file that is referenced in the HTML document through a link to this file. For example:
<!DOCTYPE html> <html> <head> <title>My Webpage</title> <link rel="stylesheet" href="style.css"> </head> <body> <p>This text will have the styles defined in style.css.</p> </body> </html>In this example, we link to a CSS file called "style.css" and define all the styles we want to apply in this file. This will affect every element in HTML, as long as the document is associated with a CSS file. Inline styleInline style refers to embedding CSS styles directly in HTML elements. For example:
<!DOCTYPE html> <html> <head> <title>My Webpage</title> </head> <body> <p style="color: red;">This text will be red.</p> </body> </html>In this example, the CSS style is written directly in the "style" attribute of the e388a4556c0f65e1904146cc1a846bee element. This will affect only elements with that "style" attribute. CSS Style PropertiesCSS has many style properties available. Here we list a few of the most commonly used ones:
The above is the detailed content of How to use css. For more information, please follow other related articles on the PHP Chinese website!