How to use CSS to set HTML

PHPz
Release: 2023-04-26 16:08:57
Original
1157 people have browsed it

HTML is the basic structure of web pages, while CSS provides support for the style of web pages. Through CSS settings, we can beautify and customize web pages. Therefore, it is a very important step for web developers to be familiar with the setting method of CSS. In this article, we will explore how to use CSS to set HTML.

1. CSS style basics

Before using CSS to set HTML styles, there are several basic concepts that need to be understood:

  1. CSS style selector

CSS style selector refers to the CSS code used to select specific HTML tags. Common CSS style selectors include tag selectors, class selectors, ID selectors, attribute selectors, etc.

  1. CSS properties and property values

CSS properties refer to the style properties to be set. The CSS attribute value is the specific value of the attribute.

For example, to set the color of HTML text, we can use the "color" attribute to set it, and the attribute value is the specific color code, such as: "color: #ff0000;" means setting the text color to red.

  1. CSS style priority

When the same HTML tag has multiple CSS style codes setting it, there will be a problem with CSS style priority.

In CSS, priority can be divided into four levels. The priority from high to low is: ID selector, class selector, label selector and universal selector. That is, the style set by the ID selector has the highest priority, and the style set by the universal selector has the lowest priority.

If there are multiple label styles of the same level, the priority will be determined based on the position of the CSS style code.

2. How to set HTML with CSS style

  1. Set HTML tag style

One of the simplest ways to set HTML style is to directly add HTML tags Add CSS style properties. For example, add a style to the h1 tag:

<h1 style="color: #ff0000; font-size:28px;">Hello World!</h1>
Copy after login

Although this method is simple, it is not flexible enough. The code can also become too verbose if multiple tags need to be styled.

  1. Use internal style sheets

Internal style sheets refer to writing CSS style code in the <style> tag at the head of the HTML file , set the style of HTML tags through tag selectors. For example:

<!DOCTYPE html>
<html>
    <head>
        <title>Internal CSS Example</title>
        <style>
            h1{
                color: #ff0000;
                font-size: 28px;
            }
        </style>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>
Copy after login
  1. Using an external style sheet

External style sheet refers to writing the CSS style code in a separate CSS file, and then using the ## of the HTML tag The # tag refers to it. This method can make the web page code clearer and more concise, and less likely to be repeated. For example:

refers to the

style.css style file in the index.html file:

<!DOCTYPE html>
<html>
    <head>
        <title>External CSS Example</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>
Copy after login
in the

style.css Set the style in the file:

h1{
    color: #ff0000;
    font-size: 28px;
}
Copy after login
    Use the class selector
The class selector means to set the CSS style for the specified HTML tag by adding the class attribute to the HTML tag. . For example:

<!DOCTYPE html>
<html>
    <head>
        <title>Class Selector Example</title>
        <style>
            .red-text{
                color: #ff0000;
            }
        </style>
    </head>
    <body>
        <h1 class="red-text">Hello World!</h1>
        <p class="red-text">This is a test paragraph.</p>
    </body>
</html>
Copy after login
    Using the ID selector
The ID selector refers to setting the CSS style for the specified HTML tag by adding the id attribute to the HTML tag.

<!DOCTYPE html>
<html>
    <head>
        <title>ID Selector Example</title>
        <style>
            #my-heading{
                color: #ff0000;
            }
        </style>
    </head>
    <body>
        <h1 id="my-heading">Hello World!</h1>
    </body>
</html>
Copy after login
    Using attribute selectors
Attribute selectors refer to selecting and setting CSS styles by specifying attributes in HTML tags. For example:

<!DOCTYPE html>
<html>
    <head>
        <title>Attribute Selector Example</title>
        <style>
            a[href^="https"]{
                color: blue;
            }
        </style>
    </head>
    <body>
        <p>Visit my website at <a href="https://example.com">example.com</a>.</p>
        <p>Visit my blog at <a href="http://blog.example.com">blog.example.com</a>.</p>
    </body>
</html>
Copy after login
In the above code,

a[href^="https"]Select all tags whose href attribute value starts with "https" , set their font color to blue.

    Pseudo-class and pseudo-element selectors
The pseudo-class selector refers to setting the CSS style for the specified HTML tag state. For example,

:hover can add style settings when the mouse hovers over the specified label.

The pseudo-element selector refers to setting the CSS style for a part of the content in the HTML tag (such as the first line of the paragraph).

<!DOCTYPE html>
<html>
    <head>
        <title>Pseudo-class and Pseudo-element Selector Example</title>
        <style>
            a:hover{
                color: blue;
            }
            p::first-line{
                font-size: 24px;
            }
        </style>
    </head>
    <body>
        <p>This is the first line.
        This is the second line.
        This is the third line.</p>
        <a href="#">This is a link.</a>
    </body>
</html>
Copy after login
In the above code,

:hoverselect to set a blue font for the label when the mouse hovers over it. ::first-lineSet the font size of the first line of the paragraph to 24px.

3. Summary

This article introduces how to set HTML styles through CSS, including directly adding styles to HTML tags, using internal style sheets, using external style sheets, using class selectors, and using ID selectors, using attribute selectors, pseudo-classes and pseudo-element selectors. By choosing different methods, we can be more flexible with styling in different situations. At the same time, understanding the concept of CSS style priority is also one of the foundations of using CSS styles to set HTML.

The above is the detailed content of How to use CSS to set HTML. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!