How HTML calls CSS
CSS (Cascading Style Sheet) is a language used to control the style of HTML pages. Through CSS, you can change the font, color, background, size, position, etc. of HTML components. There are many ways to call CSS in HTML, and this article will introduce two methods.
Method 1: Inline style sheet
Inline style sheet means defining the style in the HTML code. Style is specified by using the "style" attribute in the HTML tag. For example:
<p style="color:blue;">这是一段蓝色文字</p>
In the above example, we specified the text color in the paragraph component as blue, using an inline style sheet. For a small number of style changes, inline style sheets are a better choice, but if you need to modify the styles of multiple components, using inline style sheets will make the code difficult to maintain.
Method 2: External style sheet
External style sheet saves the CSS code in a separate CSS file and then calls it through HTML. The advantage of this method is that you can manage the style of the entire website in one CSS file. First, create a CSS file, save it on the server, and set the file name to "Example.css".
In HTML, we introduce CSS files by using the tag in the head. For example:
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="样式文件的路径"> </head> <body> <h1>这是一个标题</h1> <p>这是一段文本</p> </body> </html>
In the above example, we use the tag in the header to introduce the style file, and the parameter type should be "text/css". The rel parameter should be set to "stylesheet", indicating that this is a stylesheet. The parameter href should be the path to the style file we save on the server. This way, we can use the styles from the example .css file throughout the HTML page.
Summary
The above are two methods of calling CSS in HTML. Through inline style sheets and external style sheets, we can change the style of HTML pages and make the code more concise and readable. When using an external style sheet, attention should be paid to the setting of the file path and the definition of the file name so that it can be called normally in HTML.
The above is the detailed content of How to call css in html. For more information, please follow other related articles on the PHP Chinese website!