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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    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)

    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.

    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

    GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

    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.

    How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

    To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

    HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

    HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

    How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

    The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

    See all articles