与ID myDiv元件应该使用
div#myDiv
。
除了这些简单的选择器之外,CSS还包括(自从以前的版本以来已经做过)一系列更复杂的选择器。 例如,您可以使用选择器input [type =“ text”]来选择仅包含具有text值的属性类型的输入元素。 或者,当鼠标:hover
在元素上时,可以使用伪类:hover
为元素设置样式,例如: a:hover { color: red; }
a:hover { color: red; }
。
这些选择器还有很多,提供所有这些选择器可以使选择要设置样式的元素更加容易。 借助CSS3,甚至还添加了更多新的选择器,所有这些选择器使开发人员的工作更加轻松,并减少了他们需要编写HTML和JavaScript数量。
属性选择器
E [foo ^ =“ bar”]
选择一个元素E,其
foo
属性完全以字符串
bar
开头
E [foo $ =“ bar”]
选择一个元素E,其
foo
属性完全以字符串
bar
结尾
E [foo * =“ bar”]
选择一个元素E,其
foo
属性包含字符串
bar
结构化伪类
E:根
选择元素E,即文档的根(标记)
E:第n个孩子(n)
选择元素E,其父元素的第n个子元素
E:倒数第二个孩子(n)
选择一个元素E,其父元素的第n个子元素
E:nth-of-type(n)
选择元素E,其类型的第n个同级
E:类型的倒数第(n)
选择元素E,其类型的最后第n个同级
E:最后一个孩子
选择一个元素E,它是其父元素的最后一个子元素(请注意,E:first-child先前是在CSS2中定义的)
E:第一类
选择元素E,其类型的第一个同级
E:最后一类
选择元素E,其类型的最后一个同级
E:独生子女
选择一个元素E,它是其父元素的唯一子元素
E:唯一类型
选择元素E,它是该类型的唯一同级
E:空
选择一个没有子元素的元素E(包括文本节点)
target
伪类
E:目标
选择元素E,它是引用URI的目标
UI元素声明伪类
E:启用
选择一个启用的用户界面元素E
E:禁用
选择一个禁用的用户界面元素E
E:已检查
选择已选中或选中的用户界面元素E(单选按钮或复选框)
Negation
伪类
E:没有
选择与简单选择器s不匹配的元素E
通用同级组合器
E〜F
选择一个元素F,之后是元素E
浏览器对新的属性选择器和常规的同级组合器的支持非常出色,因为它们可在几乎所有现代Web浏览器上使用。 大多数浏览器的最新版本均包含对新伪类的支持,但您可能需要包括旧浏览器(如Internet Explorer 6/7和Firefox 3.0)的后备功能。
新效果
尽管新选择器可能是最大程度减轻开发人员头痛的功能,但人们最想看到的增强功能是可用的闪亮新效果。 这些通过一系列新CSS属性得以促进,包括:
背景(现在支持多种背景)
背景剪辑
背景起源
背景尺寸
边界半径(圆角)
边框图像
边框颜色(渐变边框)
盒子阴影(在没有图像的盒子上投影)
盒子大小
不透明
轮廓偏移
调整大小
文字溢出
文字阴影
自动换行
您将在本教程结尾创建的示例HTML5 / CSS3网页将展示其中的一些新效果。
多列布局
CSS3多列布局允许文本分布在许多列中,就像您在报纸上看到的那样。 可以通过两种方式完成此操作,或者使用column-width属性(在此属性中定义每列的宽度)(列数由容器可用的空间确定),或者使用column-count(在其中定义)应该使用的列数。 然后,宽度将根据每列可用的空间而变化。
多列布局的其他功能包括column-gap属性,该属性允许开发人员定义使用column-width方法时列之间应存在的空间。 另一个有用的添加是column-rule属性,该属性允许在列之间放置边框样式的规则。 最后,还有column-space-distribution属性,该属性确定如何在列之间分配剩余空间。
Mozilla和WebKit浏览器当前支持多列布局。 目前,这些是通过分别以-moz或-webkit前缀的临时专有属性来实现的。 规范完成后,这些浏览器最终将移至CSS标准属性。
网络字体
Web字体实际上是为CSS2提出的,并且自版本5开始就可以在Microsoft Internet Explorer中使用。不幸的是,这需要使用专有的.eot(嵌入式开放类型)字体格式,并且没有其他浏览器供应商选择实现它。 结果,Web字体从未真正在基于CSS2的网站上流行。
但是,对于最新版本的Firefox,Safari,Chrome和Opera,您现在可以使用@ font-face规则在网页上使用任何许可的.ttf(TrueType)或.otf(OpenType)字体。 @ font-face规则的示例如下: @font-face { font-family: Alexa; src: url('Alexa.otf'); }
@font-face { font-family: Alexa; src: url('Alexa.otf'); }
@font-face { font-family: Alexa; src: url('Alexa.otf'); }
。
现在,您可以在自己CSS规则中使用此字体,例如: article p { font-family: Alexa, Arial, Helvetica, sans-serif; }
article p { font-family: Alexa, Arial, Helvetica, sans-serif; }
。
请记住,字体就像图像,如果不是您自己的字体,则可能需要获得许可才能在Web上使用它们。 另外,您可以付费(或免费下载)一些免版税字体,这些字体可以根据需要包含在您的网页中。
其他新功能
CSS3还包括许多其他新功能,包括对新颜色值的支持,特别是HSL(色相,饱和度,亮度)和两个具有alpha属性的颜色值-RGBA(红,绿,蓝,阿尔法)和HSLA(色相,饱和度,亮度) , Α)。 媒体查询可让您根据视口大小为不同的设备定义不同的样式。 例如,您可以为视口小于500像素的设备(例如智能手机,PDA或其他移动设备)提供特定样式。 CSS3的语音模块允许您控制屏幕阅读器的语音属性,包括语音音量,平衡,速率,压力等。
放在一起:示例HTML5 / CSS3页面
在此阶段,您必须着急处理并开始创建使用HTML5和CSS3必须提供的强大新功能的页面。 在本节中,您将创建一个包含许多这些新功能HTML页面。 本教程的这一部分将页面的开发分为以下几个小节:
基本HTML5页面结构
使用新的语义元素
介绍元素
Web表单增强
元素和2D绘图API
整个源代码将构建到一个名为index.htmlHTML文件中(请参阅可下载的资源 )。 您可以从计算机上的任何位置打开此文件。 它不需要部署到Web服务器即可加载。 某些浏览器可能会向您发出有关在本地运行脚本的警告,因此请确保在询问时保持动态脚本功能不变。
在此过程中,页面CSS规则将添加到名为styles.css的外部样式表文件中。 您将在此示例页面中看到一些CSS3新属性的示例,例如border-radius,text-shadow和box-shadow。
无论如何,这足以完成客房整理工作,让我们开始使用一些HTML5!
基本HTML5页面结构
您可能已经知道,Web浏览器可以根据HTML文档中是否有有效的文档类型以不同的模式运行。 如果未找到有效的doctype,则浏览器将以怪癖模式运行,其中维护了一些非标准功能以实现向后兼容。 如果找到有效的文档类型,则浏览器将根据W3C和IETF标准以标准模式运行。
HTML5的doctype非常简单:
。
您应该在创建的每个HTML5页面的顶部都包含此行。 让我们继续并定义index.html页面的基本轮廓。 清单1中提供了相关代码 。
清单1.基本HTML5文档结构
HTML5 Demo
您可能会注意到,与最近的XHTML不同,HTML开头的标记不再需要xmlns
或xml:lang
属性,仅lang
属性就足够了。 同样, 元素具有一个新的速记属性字符集,用于定义页面的字符编码。 值得指出的是,这些更改使它变得更容易并且减少了执行简单任务所需的代码量。 旧方法仍然完全有效。
您可能还想知道为什么 元素不具有type
属性。 CSS是目前唯一受支持的样式表类型,如果未提供,则所有现代浏览器都将假定该类型为text / css,因此不是必需的。 同样,如果您愿意,提供type
属性也是完全可以接受的。
清单1中的最后一点是
Listing 9 adds a block of JavaScript to the page's head element, defining a function named loadCanvas
. This function instantiates a variable named canvas by selecting the element with the unique ID my_canvas. It then uses an if statement to determine if it can get a canvas context from this element. If it can, then the element is supported by the browser; if not, there is no support for the browser and the canvas should be hidden and replaced by the error message. This is taken care of in the else block of the if statement, towards the end of the function.
If canvas supported is detected, the function gets a 2D context named ctx, which is then used to draw shapes on the canvas. In this function, five shapes are created. First, the ctx.beginPath()
function is called to start a path drawing. Next a full circular arc is drawn, with a radius of 150 pixels, near the center of the canvas, using the ctx.arc()
function. The fill style is set to a yellow color, before the ctx.fill()
and ctx.stroke()
functions are called, creating a large yellow circle with a black outline. This is the main face.
The fill style is set to black as the next shapes to be drawn are the eyes. This time full circles are created using ctx.arc()
which have a radius of just 15 pixels, and the ctx.fill()
function is called to draw them.
The next block of code creates the nose, which is an arc that is not a full circle and is placed below and centered between the eyes that were just created. The nose is not to be filled, so the ctx.stroke()
function is used instead.
Finally, a large semi-circle arc is stroked below the nose to create the mouth. The result should look the same in all browsers that support the element (most modern browsers including Firefox, Safari, Chrome, and Opera—even Internet Explorer—can support it with the help of some clever JavaScript libraries).
The final change you need to make to index.html is to change the
element to call the
loadCanvas()
function when the window has finished loading. Simply replace the opening tag with the following line:
.
Finally, just add the following rules to the styles.css file to add some pretty box shadowing to the canvas container (see Listing 10 ).
Listing 10. Canvas CSS rules
article > section div#no-canvas {
display: none;
}
canvas {
border: 1px solid #888;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
-moz-box-shadow: 10px 10px 5px #888;
-webkit-box-shadow: 10px 10px 5px #888;
box-shadow: 10px 10px 5px #888;
background-color: #eee;
}
The final result looks like the image in Figure 8 .
Figure 8. The smiley face canvas
That concludes the sample page for this tutorial. In this section, you have used HTML5 and CSS3 to work with the new HTML5 semantic elements, harnessed some of CSS3's pretty new effects, watched video in the browser without any Flash plug-in, seen some new form widgets, and created a smiley face graphic on a canvas using JavaScript APIs.
摘要
This tutorial serves as a hands-on introduction to HTML5 and CSS3 development. HTML5 is very much at an early stage of development, and it will be interesting to see how the new features it proposes are adopted by the different browser vendors. At present, of the major browsers, Opera, Safari, Firefox, and Chrome are providing support for more enhancements with each release, and one would hope to see the bulk of HTML5 features supported by the end of 2010.
Several issues may stop HTML5 from becoming widespread in the near future, however. The first real issue is the lack of support of it from Microsoft's Internet Explorer, the most widely used Web browser on the market. It is unlikely that developers will be able to test any HTML5 features on IE until the first beta version of IE9 is released. At least for now, sites developed for HTML5 degrade quite gracefully on IE8, and with a bit of extra work, fallbacks can be put in place to provide workarounds for IE users.
Another major issue is the one surrounding video codecs and containers. The way things stand, the element will not replace Flash video as the video standard for the Web. With different browsers backing different codecs, it's still much easier to use Flash than it is to encode your videos for Theora and H.264. Here's hoping that some kind of breakthrough is made this year on HTML5 video. In summary, HTML5 and CSS3 are exciting standards, and you can start future-proofing your Web sites to be compliant with these new specifications right now. Following the steps outlined in this tutorial, you should be well versed to move forward and explore some of the other interesting features HTML5 has to offer.
翻译自: https://www.ibm.com/developerworks/web/tutorials/wa-html5/index.html
html5和css3