首页 > 其他分享 >css03 CSS Selectors

css03 CSS Selectors

时间:2024-05-28 20:45:06浏览次数:22  
标签:HTML css03 center element elements Selectors class CSS

https://www.w3schools.com/css/css_selectors.asp

A CSS selector selects the HTML element(s) you want to style.


CSS Selectors

CSS selectors are used to "find" (or select) the HTML elements you want to style.

We can divide CSS selectors into five categories:

This page will explain the most basic CSS selectors.


The CSS element Selector

The element selector selects HTML elements based on the element name.

Example

Here, all <p> elements on the page will be center-aligned, with a red text color: 

p {
  text-align: center;
  color: red;
}

The CSS id Selector

The id selector uses the id attribute of an HTML element to select a specific element.

The id of an element is unique within a page, so the id selector is used to select one unique element!

To select an element with a specific id, write a hash (#) character, followed by the id of the element.

Example

The CSS rule below will be applied to the HTML element with id="para1": 

#para1 {
  text-align: center;
  color: red;
}

Note: An id name cannot start with a number!


 

The CSS class Selector

The class selector selects HTML elements with a specific class attribute.

To select elements with a specific class, write a period (.) character, followed by the class name.

Example

In this example all HTML elements with class="center" will be red and center-aligned: 

.center {
  text-align: center;
  color: red;
}

You can also specify that only specific HTML elements should be affected by a class.

Example

In this example only <p> elements with class="center" will be red and center-aligned: 

p.center {
  text-align: center;
  color: red;
}

HTML elements can also refer to more than one class.

Example

In this example the <p> element will be styled according to class="center" and to class="large": 

<p class="center large">This paragraph refers to two classes.</p>

Note: A class name cannot start with a number!


The CSS Universal Selector

The universal selector (*) selects all HTML elements on the page.

Example

The CSS rule below will affect every HTML element on the page: 

* {
  text-align: center;
  color: blue;
}

The CSS Grouping Selector

The grouping selector selects all the HTML elements with the same style definitions.

Look at the following CSS code (the h1, h2, and p elements have the same style definitions):

h1 {
  text-align: center;
  color: red;
}

h2 {
  text-align: center;
  color: red;
}

p {
  text-align: center;
  color: red;
}

It will be better to group the selectors, to minimize the code.

To group selectors, separate each selector with a comma.

Example

In this example we have grouped the selectors from the code above: 

h1, h2, p {
  text-align: center;
  color: red;
}


All CSS Simple Selectors

SelectorExampleExample description
#id #firstname Selects the element with id="firstname"
.class .intro Selects all elements with class="intro"
element.class p.intro Selects only <p> elements with class="intro"
* * Selects all elements
element p Selects all <p> elements
element,element,.. div, p Selects all <div> elements and all <p> elements

Video: CSS Simple Selectors

 

标签:HTML,css03,center,element,elements,Selectors,class,CSS
From: https://www.cnblogs.com/emanlee/p/18218801

相关文章

  • css01 CSS Introduction
    https://www.w3schools.com/css/css_intro.asp CSSisthelanguageweusetostyleaWebpage.WhatisCSS?CSSstandsforCascadingStyleSheetsCSSdescribeshowHTMLelementsaretobedisplayedonscreen,paper,orinothermediaCSSsavesalotofw......
  • css02 CSS Syntax
    https://www.w3schools.com/css/css_syntax.aspACSSruleconsistsofaselectorandadeclarationblock.CSSSyntaxTheselectorpointstotheHTMLelementyouwanttostyle.Thedeclarationblockcontainsoneormoredeclarationsseparatedbysemicolo......
  • html+CSS部分基础运用8
    1.P147实验1,完成页面制作效果。图7-1木兰花令效果图2.P147实验2,完成页面制作效果。项目1<!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <linktype="text/css">  <title>木兰花令</title>......
  • html+CSS部分基础运用7
    项目1 设计简易灯箱画廊1.实验所需素材在trees文件夹中提供一个MP3文件和18个JPG文件,设计页面时可以使用。2.编程实现简易灯箱画廊,鼠标单击任一个图像超链接,在底部浮动框架中显示大图像,效果如图4-1所示的页面。图4-1简易灯箱画廊项目2 设计支持音频、视频播放的......
  • CSS 之 自定义属性(变量)
    一、简介​CSS的自定义属性,又称为CSS变量或级联变量,用于定义一个带有值的、可重复使用的CSS属性(变量)。其包含的值可以在其作用域内的任意属性上重复使用,在使用时需要借助var()函数获取自定义属性的值。当自定义属性的值发生变化时,所有使用该自定义属性的CSS属性都会随之变......
  • dumi2.x如何支持css Module?
    在dumi2.x中支持CSSModules是比较直接的,因为dumi基于Umi,而Umi自身就对CSSModules提供了良好支持。以下是基本步骤和注意事项来在dumi项目中使用CSSModules:安装依赖:确保你的项目中已经安装了相应的加载器,如style-loader、css-loader以及可能的less-loader、s......
  • CSS设置水平垂直居中
    在CSS中,有多种方法可以实现元素的水平垂直居中。以下是一些常见的方法:1.使用FlexboxFlexbox是CSS3引入的一个强大的布局模型,可以轻松地实现元素的水平垂直居中。css.container{display:flex;justify-content:center;/*水平居中*/align-items:cente......
  • 自定义CSS属性(@property)解决自定义CSS变量无法实现过渡效果的问题
    且看下面的代码:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><metaname="viewport"content="width=device-width,initial-scale=1.0"/><title>demot</t......
  • 【scss】基于:empty伪类的空元素回显及加载状态
    mixin.scss/**空内容height:占位高度**/@mixinempty-block($height:220px){display:block;position:relative;width:100%;min-height:$height;//图片高度background-image:url('~@/assets/images/default-img/no-data.svg');background-repeat......
  • 代码雨(coderain)源码(html5+css3+javascript,原创)
     大家看过黑客帝国的代码雨吗?本人自己写了一个,效果还可以。演示效果请见https://www.lanbaoshi.site/coderain.htm下面上代码:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="htt......