饥人谷技术博客

CSS selector

2016-07-28  本文已影响0人  老虎爱吃母鸡
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        [lang~="en-us"] {
            color: red;
        }
        [lang|="zh"] {
            color: blue;
        }
    </style>
</head>
<body>
        <a href="#">English:</a>
        <span lang="en-us en-gb en-au en-nz">Hello World!</span><br>
        <a href="#">Portuguese:</a>
        <span lang="pt">Olá Mundo!</span><br>
        <a href="#">Chinese (Simplified):</a>
        <span lang="zh-CN">世界您好!</span><br>
        <a href="#">Chinese (Traditional):</a>
        <span lang="zh-TW">世界您好!</span><br>
</body>
</html>
属性选择器
- 伪类选择器,`a:hover {}`
  伪类选择器表示的是原本存在的元素,相当于给这些元素加上一个类,在CSS3中用":"和 "::"区分伪类和伪元素,但是一般浏览器两种方式都兼容
- 伪元素选择器,`p::first-letter {}`
  伪元素选择器表示的是原本不存在的元素,相当于加上一个元素,注意:`::after`和`::before`中的content的属性,该属性的值有:
    - <string>,文本内容
    - <uri> url(),外部资源(比如图片),如果该资源不能显示,它就会被忽略或显示一些占位
    - attr(x),将元素的X属性以字符串形式返回
    - open-quote|close-quote,引号
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .bd::before {
           content: "(我是前面的字符串)";
        }

        .bd::after {
            content: "(" attr(href) ")";
        }

        .jrg::before {
            content: url(https://img.haomeiwen.com/i2348761/f10b477e82e5ac19.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240);
        }
        
        q::before {
            content: open-quote;
        }

        q::after {
            content: close-quote;
        }
    </style>
</head>
<body>
    <a href="http://baidu.com" class="bd">百度</a><br>
    <a href="http//jirengu.com" class="jrg">饥人谷</a><br>
    <q>我是引用</q>
</body>
</html>
content属性
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        div :first-of-type {
            background-color: lime;
        }
    </style>
</head>
<body>
    <div>
        <span>This span is first!</span>
        <span>This span is not. :(</span>
        <span>what about this <em>nested element</em>?</span>
        <strike>This is another type</strike>
        <span>Sadly, this one is not...</span>
    </body>
</html>
:first-of-type
<style>
.item1:first-child{ color: red;}
.item1:first-of-type{ background: blue;}
</style> 
<div class="ct"> 
<p class="item1">aa</p>
<h3 class="item1">bb</h3> 
<h3 class="item1">ccc</h3> 
</div>
1. `<p class="item1">aa</p>`是其父元素下的第一个子元素,所以`.item1:first-child{ color: red;}`生效了。
2. `.item1:first-child{ color: red;}`和`<h3 class="item1">bb</h3> `分别是父元素下其类型元素的第一个,所以`.item1:first-of-type{ background: blue;}`生效了。

The text-align CSS property describes how inline content like text is aligned in its parent block element. text-align does not control the alignment of block elements, only their inline content.---MDN

text-align: center的作用在块级元素上,他的作用是让行内元素相对与他的父元素居中显示

上一篇下一篇

猜你喜欢

热点阅读