CSS入门

CSS基础教程
本文介绍了CSS的基本概念及其使用方法,包括内部样式表、外部样式表、选择器等,并详细讲解了如何利用CSS实现页面布局和样式的精确控制。

CSS入门

以下内容来自于CSS the missing manual

参看链接:

译者:http://yulimin.iteye.com/blog/71162

原版:http://www.china-pub.com/computers/common/info.asp?id=35381

什么是CSS

CSS = Cascading Style Sheets

CSS中文称为层叠式样式表

CSS可以做什么

CSS可以将画面的内容和格式分离,使HTML代码更清晰,更容易维护

示例解释

p { color: red; font-size: 1.5em; }

Internal CSS

<style type="text/css">
h1 {
color: #FF7643;
font-face: Arial;
}
p {
color: red;
font-size: 1.5em;
}
</style>

External CSS

HTML:
<link rel="stylesheet" type="text/css" href="css/global.css">

XHTML:
<link rel="stylesheet" type="text/css" href="css/global.css" />

CSS:

<style type="text/css">
@import url(css/global.css);
</style>

Selector

Tag Selectors:Page-Wide Styling(页面范围内的格式)

h2 {
color: #FFC;
margin-bottom: 0;
padding: 5px 0px 3px 25px;
background-color:#999;
}

Class Selectors:Pinpoint Control(精确控制)

.special {
color:#FF0000;
font-family:"Monotype Corsiva";
}
<p class="special">

ID Selectors: Specific Page Element(特定的Page对象)

#banner {
background: #CC0000;
height: 300px;
width: 720px;
}
<p id="copyright">

Descendent Selector:

h1 strong { color: red; }
Here any <strong> tag inside an h1 is red, but other instances of the <strong> tag on the page, aren't affected.

p.intro a { color: yellow; }
Apply this style to every link (a) that's a descendent of a paragraph (p) that has the intro class applied to it. Note that there's no space between p and .intro, which tells CSS that the intro class applies specifically to the <p> tag.
p .intro a { color: yellow; }
an <a> tag inside of any tag styled with the .intro class, that's itself a descendent of a <p> tag.

.intro a {color: yellow; }
This selector indicates any <a> tag inside of any other tag<div>, <h1>, <table>, and so onwith the .intro class applied to it.

Group Selectors:

h1, h2, h3, h4, h5, h6 { color: #F1CD33; }

Universal Selector:

* { font-weight: bold; }

Pseudo-Class and Pseudo Element

a:link selects any link that your guest hasn't visited yet, while the mouse isn't hovering over or clicking it. This style is your regular, unused Web link.

a:visited is a link that your visitor has clicked before, according to the Web browser's history. You can style this type of link differently than a regular link to tell your visitor, "Hey, you've been there already!"

a:hover lets you change the look of a link as your visitor passes the mouse over it. The rollover effects you can create aren't just for funthey can provide useful visual feedback for buttons on a navigation bar.

a:active lets you determine how a link looks as your visitor clicks. In other words, it covers the brief nanosecond when someone's pressing the mouse button, before releasing it.

:before It lets you add content preceding a given element
p.tip:before {content: "HOT TIP!" }

:after pseudo-element adds generated contentbut after the element
p.tip:after {content: "HOT TIP!" }

:first-child pseudo-element lets you select and format just the first of however many children an element may have.
li:first-child { font-weight: bold; }

:focus applies when the visitor does something to indicate her attention to a Web page elementusually by clicking or tabbing into it.

input:focus { background-color: #FFFFCC; }

Advanced Selector:

Child Selector

与Descendent Selector不同的地方是,Child Selector可以定位到特定的元素

body > p

Adjacent Siblings

h2 + p

select every paragraph following each <h2> tag

Attribute Selectors
a[href="http://www.cosmofarmer.com"]{ color:red; font-weight:bold; }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值