css教程:css指令,兼容,注释,selector

2016-02-19 23:39 6 1 收藏

清醒时做事,糊涂时读书,大怒时睡觉,无聊时关注图老师为大家准备的精彩内容。下面为大家推荐css教程:css指令,兼容,注释,selector,无聊中的都看过来。

【 tulaoshi.com - Web开发 】

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)link rel="stylesheet" type="text/css" href="sheet1.css" media="all" /link标记的用意是允许将html与其他文档相关联。Css用link将css文档与html文档想关联。Css文档虽然不是html的一部分,但是被html使用,从外部style sheets引入它。Link在head元素内,但是不能放在任意head子元素的内部,比如title。Css文档的后缀名虽然不要求,但是有些浏览器不能识别非.css的文件。Link的属性:    rel:代表relation,设为stylesheet。    type:描述数据的类型,设为text/css,告诉浏览器style sheet是css格式的。以后还会有其他的style sheet,比如xsl。    href:style sheet的url。    Media:指定style sheet的使用范围。下列大多数值还不被任何浏览器支持,常用的是all,print,screen。Opera支持projection。可以为media指定多个值,比如media="screen, projection"all Use in all presentational media. aural Use in speech synthesizers, screen readers, and other audio renderings of the document. braille Use when rendering the document with a Braille device. embossed Use when printing with a Braille printing device. handheld Use on handheld devices like personal digital assistants or web-enabled cell phones. print Use when printing the document for sighted users and also when displaying a "print preview" of the document. projection Use in a projection medium, such as a digital projector used to present a slideshow when delivering a speech. screen Use when presenting the document in a screen medium like a desktop computer monitor. All web browsers running on such systems are screen-medium user agents. tty Use when delivering the document in a fixed-pitch environment like teletype printers. tv Use when the document is being presented on a television.    Title:利用title定义多个css文档相互替换的关系。       比如存在如下定义:    link rel="stylesheet" type="text/css" href="sheet1.css" title="Default" / link rel="alternate stylesheet" type="text/css" href="bigtext.css" title="Big Text" / link rel="alternate stylesheet" type="text/css" href="zany.css" title="Crazy colors!" /那么能同时支持多个css定义的浏览器中会有如下表现:       还可以通过将title设定为相同的value来分组:
link rel="stylesheet" type="text/css"
   href="sheet1.css" title="Default" media="screen" /
link rel="stylesheet" type="text/css"
   href="print-sheet1.css" title="Default" media="print" /
link rel="alternate stylesheet" type="text/css"
   href="bigtext.css" title="Big Text" media="screen" /
link rel="alternate stylesheet" type="text/css"
   href="print-bigtext.css" title="Big Text" media="print" /
上面的表述意为:css被title分为两组,default和Big Text。又每一组又被分为print和screen。
 如果有多个link元素,那么只有rel等于stylesheet的link可用。如果可用的link有多个,就会将它们同时作用于html文档,如下:link rel="stylesheet" type="text/css" href="basic.css" / link rel="stylesheet" type="text/css" href="splash.css" / style是引入style sheet最通用的方式。style type="text/css"type:style总是使用type属性,当使用css时,type的值是text/css。Media:与link中一样。 style以style type="text/css"开头,以/style结束,中间是多个styles。这些styles或者指向style sheet文档,或者以内嵌的方式表达。Style元素可以包含多个styles,也可以通过@import指令引入多个指向外部style sheet的链接。用法:
style type="text/css"
 @import url(styles.css); /* @import comes first */@import url(blueworld.css);@import url(zany.css); h1 {color: gray;} /style可见其作用类似link,l         通知浏览器将外部style sheet载入。l         并且可以载入多个style sheet。区别是l         位置与语法不同。@import被包含在style元素中,并且必须在其他css规则之前。l         每一个import的style sheet都会被使用,没有替代规则。相对于link的media属性,import有:@import url(sheet2.css) all; @import url(blueworld.css) screen; @import url(zany.css) projection, print;@import的重要用途:在导入的某个style sheet A中,A需要也使用外部的style sheet,这时link元素显然无用。比如css文档中,是不可能出现link元素的,这时使用@import,如下:@import url(http://example.org/library/layout.css); @import url(basic-text.css); @import url(printer.css) print; body {color: red;} h1 {color: blue;}浏览器对不能识别的tag一律忽略。但是如果浏览器不能识别style元素,style会以普通文本的形式出现在网页的最上面。解决方案:在style里面加上注释符号,这样旧版本的浏览器不会以文本方式显示,新版本浏览器可以正确使用style元素。具体如下:style type="text/css"!-- @import url(sheet2.css); h1 {color: maroon;} body {background: yellow;} --/stylecss的注释类似c:/* This is a CSS1 comment */Comments can span multiple lines, just as in C++:/* This is a CSS1 comment, and it can be several lines long without any problem whatsoever. */但是注意:css的注释不能被嵌套。将style放到html元素描述的地方,就是inline stylep style="color: gray;"The most wonderful of all breakfast foods is  the waffle--a ridged and cratered slab of home-cooked, fluffy goodness... /p这个style属性是一个新属性,可以用到出现body元素中的所有元素上。可以看到style的值是一个字符串,使用和css一样的语法。但是这个字符串只能是一个风格声明块declaration block。不能将@import和css规则放到这个字符串中。就是说只能放css文档中出现在花括号中的文本。 注意:inline style不被推荐使用,在xhtml1.1中inline style是反对的deprecated。因为,它显示违背数据和显示分离的原则。这个原则也是使用css的原因。css核心的特点是将规则应用到元素集上的能力。 Css2规范种关于selector的部分,http://www.w3.org/TR/REC-CSS2/selector.html  css的模式匹配pattern matching规则(css规范,地址如上): Pattern Meaning Described in section * Matches any element. Universal selector E Matches any E element (i.e., an element of type E). Type selectors E F Matches any F element that is a descendant of an E element. Descendant selectors E F Matches any F element that is a child of an element E. Child selectors E:first-child Matches element E when E is the first child of its parent. The :first-child pseudo-class E:link
E:visited Matches element E if E is the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited). The link pseudo-classes E:active
E:hover
E:focus Matches E during certain user actions. The dynamic pseudo-classesE:lang(c) Matches element of type E if it is in (human) language c (the document language specifies how language is determined). The :lang() pseudo-classE + F Matches any F element immediately preceded by an element E. Adjacent selectorsE[foo] Matches any E element with the "foo" attribute set (whatever the value). Attribute selectorsE[foo="warning"] Matches any E element whose "foo" attribute value is exactly equal to "warning". Attribute selectorsE[foo~="warning"] Matches any E element whose "foo" attribute value is a list of space-separated values, one of which is exactly equal to "warning". Attribute selectorsE[lang|="en"] Matches any E element whose "lang" attribute has a hyphen-separated list of values beginning (from the left) with "en". Attribute selectorsDIV.warning HTML only. The same as DIV[class~="warning"]. Class selectors E#myid Matches any E element ID equal to "myid". ID selectors

来源:https://www.tulaoshi.com/n/20160219/1630305.html

延伸阅读
标签: Web开发
网页设计遇到最大的麻烦之一莫过于网页对不同浏览器的兼容性问题了,因为IE 6.0 / IE 7.0 / firefox 2 / Opera 9等主流浏览器对CSS解析不近相同,导致设计的页面效果不一样,所以用什么方法可以有效解决不同浏览器不同页面效果的问题呢?广泛被使用的方法就是CSS Hack,即使用特别的CSS定义显示网页在不同浏览器的设计风格,针对不同的浏览器去...
标签: Web开发
Cascading Style Sheets: The Definitive Guide, 2nd Edition is a thorough review of all aspects of CSS2.1 and a comprehensive guide to CSS implementation. The book includes new content on positioning, lists and generated content, table layout, user interface, paged media, and more. It explores in detail each individ...
前几天在做腾讯微博的微卖场的时候,遇到需要做图片居中的需求。也就是说,商品列表中的图片需要居中显示。因为图片是卖家自己把商品图片链接过来,商品图片的大小没有做限制和过滤。所以我们需要做的是,让图片在容器当中水平居中、垂直居中、图片自适应容器大小。图片原图大小和在容器中显示的大小有这样的关系: 假设容器大小为200像素*200...
标签: Web开发
CSS组合 你不必重复有相同属性的多个选择符,你只要用英文逗号(,)隔开选择符就可以了。 比如,你有如下的代码: h2 { color: red; } .22333 { color: red; } .22333com { color: red; } 则你可以这样写: h2,.22333,.22333com { color: red; } 使用组合,你可以一次定义多个CSS,为你节省很多字节和时间。 CSS嵌套 ...
标签: Web开发
如果使用注释的方法得当的话,为你的CSS文件添加注释可以在开发过程中给予你和其他人很大的帮助。最常见的是为CSS样式规则添加提示信息,不过使用注释对优化组织结构和提升效用也很有帮助。 提示和标签 这是注释最常用的途径,可以为自己或其他开发人员留下提示信息可以避免后期引起的不必要的困惑和麻烦。这种应用简洁性最...

经验教程

727

收藏

22
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部