css派生选择器,ID选择器,类选择器,属性选择器以及css的创建

派生选择器

比如说你需要列表中的strong元素变成斜体字,而不是通常的粗体字,我们可以:

li strong {

      font-style:italic;

      font-weight:normal;

}

则在body中:

<p><strong>我是粗体字,不是斜体字,因为我不在列表当中,所以这个规则对我不起作用</strong></p>

<ol>

<li><strong>我是斜体字,不是粗体字。这是因为 strong 元素位于 li 元素内</srong></li>

</ol>

通过这样就可以指定在特定地方的样式

ID选择器

ID选择器可以为标有特定的ID和HTML元素指定特定的样式。

ID选择器以“#”来定义。

ex:

#red{color:red;}

#green{color:green;}

html:
<p id="red">this is red.</p>

<p id="green>this is green.</p>

#ID选择器与派生选择器的一起使用;

ex:

#sidebar p {
	font-style: italic;
	text-align: right;
	margin-top: 0.5em;
	}

#sidebar h2 {
	font-size: 1em;
	font-weight: normal;
	font-style: italic;
	margin: 0;
	line-height: 1.5;
	text-align: right;
	}

则ID是sideba内的h2元素的样式和其他h2样式均不一样。

***css类选择器

在css中,类选择器以一个点号显示。

ex:

.center{text-align:center}

则所有拥有center类的HTML元素均居中。

html:
<h1 class="center">

this is csdn.

</h1>

<p class="center">

this is csdn@jiangyi</p>

至于类名的起名,通java等编程语言,总之,第一个字母不能用数字

-------------以下补充于2018-7-21  by  jiangyi-------------------------------------------------

还可以有多类选择器

ex:

.important{font-weight:bold;}

.warning{font-style:italic;}

.important.warning{background:silver;}

使用时:

<p class="important warning">csdn</p>

$注意:在class值中,要用空格号分开。值的顺序无所谓,即也可以写作:"warning important"

如果包含了一个没有的类名,则正常情况下匹配会失败,但是只要class中包含类名,也可以。

ex:

.important.urgent{background:silver;}

则以下也可以匹配:

<p class="important urgent warning">csdn</p>

***class和派生选择器结合

ex:

.fancy td{

    color:red;

    background:black;

}

则在比<td>的更大的元素的class是fancy,就可以使其中的<td>满足上述颜色。

这个元素可以是<table>或者一个<div>

当然,元素也可以基于他们的类被选择:

td.fancy{

    color:red;

    background:black;

}

html:

<td class="fancy">

***属性选择器

可以为指定属性的HTML元素设置样式,而不仅限于class和ID属性。

$:注意:要规定<!DOCTYPE>

ex:

[title]

{

color:red;

}

html:

<h1 title="hello csdn">csdn</h1>

<h1>csdn</h1>

则第一个csdn应用了红色,而第二个csdn并没有。

tilte属性可以显示提示,即将鼠标放在上面的时候会显示提示,拿掉则消失。

***属性和值选择器

ex:

[title=csdn]

{

border:5px solid blue;

}

html:

<a title="csdn" href="www.csdn.com"/>csdn</a>

则会对csdn加上蓝色实心边框。

***属性和值选择器-包含多个值

可以为包含指定值的title属性的所有元素设置样式。适用于有空格分离的属性值。

ex:

[title~=csdn]

{

color:red;

}

html:

<h2 title="hello csdn">csdn</h2>

<p title="csdn nice">csdn is the best</p>

则上述内容将会应用样式。

ex2;

[lang|=en]

{

color:red;

}

html:

<p lang="en">hello</p>

$:注意:lang 是language的缩写,语言,对语言进行了限定。

----------------补充于2018-7-22 by jiangyi---------------------------------------------

1.可以仅对含某属性的元素应用样式

a[href]{color:red}

2.可以对同时包含一些属性的元素应用样式

a[href][title]{color:red}

还可以与特定的值放在一起:

a[href="http://www.csdn.com"/][title="csdn"]{color:red}

***设置表单的样式:

input[type="text"]
{
  width:150px;
  display:block;
  margin-bottom:10px;
  background-color:yellow;
  font-family: Verdana, Arial;
}

input[type="button"]
{
  width:120px;
  margin-left:35px;
  display:block;
  font-family: Verdana, Arial;
}

选择器描述

[attribute]用于选取带有指定属性的元素。

[attribute=value]用于选取带有指定属性和值的元素。

[attribute~=value]用于选取属性值中包含指定词汇的元素。

[attribute|=value]用于选取带有以指定值开头的属性值的元素,该值必须是整个单词

[attribute^=value]匹配属性值以指定值开头的每个元素。

[attribute$=value]匹配属性值以指定值结尾的每个元素。

[attribute*=value]匹配属性值中包含指定值的每个元素。

ex1:

<!DOCTYPE html>
<html>
<head>
<style>
div[class ^="test"]
{
background:red;
}
</style>
</head>
<body>
<div class="test">hello jiang yi</div>
</body>
</html>

ex2:

<!DOCTYPE html>
<html>
<head>
<style> 
div[class*="test"]
{
background:#ffff00;
}
</style>
</head>
<body>

<div class="first_test">第一个 div 元素。</div>
<div class="second">第二个 div 元素。</div>
<div class="test">第三个 div 元素。</div>
<p class="test">这是段落中的文本。</p>

</body>
</html>

***如何创建css

即如何插入,主要有三种:外部,内部,内联

外部样式表:

在<head>中用<link>标签

<link rel="stylesheet" type="text/css" href="mystyle.css"/>

浏览器会从文件 mystyle.css 中读到样式声明,并根据它来格式文档。

外部样式表可以在任何文本编辑器中进行编辑。文件不能包含任何的 html 标签。样式表应该以 .css 扩展名进行保存。

关于<link>的属性:

href:规定链接文档的位置

hreflang:规定链接文档的语言

media:规定链接文档将显示在什么样的设备上

rel:规定当前文档毅被链接的文档之间的关系。

type:规定被链接文档的mime类型。

猜你喜欢

转载自blog.csdn.net/weixin_41060905/article/details/81127199