Table of Contents
1. Overview of CSS selectors
1.1. CSS functions
1.2, CSS selection type
1.3. Basic CSS syntax
2. Tags Selector
2.1 Tag Selector Overview
2.2. Advantages and disadvantages of using tag selectors:
2.3. So under what circumstances should the tag selector be used?
Home Web Front-end HTML Tutorial CSS tag selector (2)_html/css_WEB-ITnose

CSS tag selector (2)_html/css_WEB-ITnose

Jun 24, 2016 am 11:46 AM

1. Overview of CSS selectors

1.1. CSS functions

CSS language has two basic functions: matching and rendering

When browsing When the browser parses the CSS style, it should first determine which elements need to be rendered, that is, which HTML elements are matched. This operation is identified by the selector in the CSS style.

Only after matching a specific object, the browser can determine the rendering effect based on the CSS style declaration and present it on the page in time.

1.2, CSS selection type

  • Tag selector
  • ID selector
  • Class selector
  • Special selector
  • 1.3. Basic CSS syntax

    2. Tags Selector

    2.1 Tag Selector Overview

    Uses elements of document object type as selectors, such as p, div, span, etc.

    The most commonly used selector is the tag selector. Tag selectors can be used to find specific types of elements, such as paragraphs, hyperlinks, or heading elements. Simply specify the name of the element to which you want the style applied.

    Tag selectors are sometimes called element selectors or simple selectors.

    Such as:

    /*段落字体颜色为黑色*/p{    color:black;}/*超链接显示下划线*/a{    text-decoration:underline;}/*一级标题显示为粗体效果*/h1{    font-weight:bold;}
    Copy after login

    2.2. Advantages and disadvantages of using tag selectors:

    Advantages: Able to quickly Labels of the same type on the page have a unified style.

    Disadvantages: Differentiated styles cannot be designed and sometimes interfere with each other

    For example: If the following style is defined in the web style sheet, all div element objects are defined to have a width of 774px

    /*所有div元素对象定义为宽度为774px*/div{      width:774px;       }  
    Copy after login

    Then when using divs for layout, you need to redefine the width of each div object in the page, because not every div element object in the page has a width of 774px. , otherwise it will be a very troublesome thing774.

    2.3. So under what circumstances should the tag selector be used?

    If you want the label to define a default style, you can use the label selector. For example, when using the ul element, it will be automatically indented and come with a list symbol. Sometimes this style will cause trouble in the list layout. In this case, you can select the ul element as the label selector and clear the predefined style.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>标签选择器</title><link type="text/css" rel="stylesheet" href="css/test2.css" /></head><body><h1>标签选择器</h1><p>段落设置字体为蓝色</p><a href="#">超链接没有下划线</a><ul><li><a href="#">列表1</a></li><li><a href="#">列表2</a></li><li><a href="#">列表3</a></li></ul><img alt="狗粮" src="images/003.jpg" /><a href="#"><img alt="小狗" src="images/408330.jpg" /></a><table><tr><td>姓名:</td><td><input type="text" name="username" /></td></tr><tr><td>密码 :</td><td><input type="password" name="password" /></td></tr><tr><td><input type="submit" value="提交" /></td><td><input type="reset"  value="重置" /></td></tr></table></body></html>
    Copy after login

    @charset "utf-8";/* CSS Document *//*统一文档字体大小,行高,字体*/body{    font:12px Arial, Helvetica, sans-serif;    color:#000000;}/*段落字体颜色为蓝色*/p{    color:#0000FF;}/*一级标题显示为粗体效果*/h1{    font-weight:bold;}/*设置a标签的下划线为没有*/a{    text-decoration:none;}/*清除预定义样式*/ul{    margin:0px;    list-style:none;    }/*设置图像的边框为0*/img{    border:0px;}/*统一表格字体和行高*/table{    border:solid 5px #000000; /*边框实线,宽度为5px,颜色为黑色*/    font-size:12px;        /*字体大小为12px*/            color:#666;            /*字体颜色为中灰*/    line-height:200%;    /*行主为默认值的2倍*/}/*设置input标签的边框为实线,1个像素,颜色为浅灰*/input{    border:solid 1px #ddd;}
    Copy after login

    /*清除预定义样式*/ul{    margin:0px;    list-style:none;    }
    Copy after login

    If you want to unify the styles in the document tags, you can also Use tag selector.

    For example, unify the document font size, line height, and font, unify the font style of the table through the table tag selector, clear the underline of all hyperlinks through the a tag selector, and clear the web page through the img tag selector. The border of the image. When the image is embedded in the a element, that is, when it is used as a hyperlink object, the border will appear. Through the input tag selector, the unified input form border is light gray, etc.

    /*统一文档字体大小,行高,字体*/body{    font:12px Arial, Helvetica, sans-serif;}/*统一表格字体和行高*/table{    font-size:12px;        /*字体大小为12px*/            color:#666;            /*字体颜色为中灰*/    line-height:200%;    /*行主为默认值的2倍*/}/*设置a标签的下划线为没有*/a{    text-decoration:none;}/*设置图像的边框为0*/img{    border:0px;}/*设置input标签的边框为实线,1个像素,颜色为浅灰*/input{    border:solid 1px #ddd;}
    Copy after login

    For general structural elements such as div and span, it is not recommended to use tag selectors because general structural elements have a wide range of applications. Use tag selectors Will interfere with each other

    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    Will R.E.P.O. Have Crossplay?
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

    The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    See all articles