CSS:层叠样式表,Cascading Style Sheets
CSS规则由两个主要的部分构成:选择器,以及一条或者多条声明
selector {declaration1;...;declarationN}
选择器通常是需要改变样式的HTML元素,每条声明由一个属性和一个值组成
margin:把左和右外边距设置为 auto,规定的是均等地分配可用的外边距。结果就是居中的元素:
.center {margin-left:auto; margin-right:auto;width:70%;//如果宽度是 100%,则对齐没有效果。background-color:#b0e0e6; }
.right {position:absolute;right:0px; width:300px; background-color:#b0e0e6; }
.right {float:right;width:300px; background-color:#b0e0e6; }
导航栏---为何链接区域变大了display:block属性的原因
display:block;//display:block - 把链接显示为块元素可使整个链接区域可点击(不仅仅是文本),同时也允许我们规定宽度。而display:inline内联样式则是不换行
<li><a href="#home">Home</a></li>
水平导航栏:将<li>的display改为inline内联即可(不换行)
selector1,selector2,selector3 {declaration1;...;declarationN}
li strong { font-style: italic; font-weight: normal; }
id 选择器:定义了id属性的元素,用#id作为选择器,如id为sidebar的元素下的<p>标签
#sidebar p { font-style: italic; }
div#sidebar p { font-style: italic; }
.fancy td { color: #f60; background: #666; }
td.fancy { color: #f60; background: #666; }
.class1.class2.fancy {//至少要同时包含,如果只有class1则不匹配 color: #f60; background: #666; }
input[type] { width:150px; display:block; margin-bottom:10px; background-color:yellow; font-family: Verdana, Arial; }
input[type="text"] { width:150px; display:block; margin-bottom:10px; background-color:yellow; font-family: Verdana, Arial; }
a[href][title] {color:red;}//同时拥有href和title属性的
<p class="important warning">This paragraph is a very important warning.</p>
如果写成 p[class="important"],那么这个规则不能匹配示例标记。
p[class="important warning"] {color: red;}
如果需要根据属性值中的词列表的某个词进行选择,则需要使用波浪号(~)。
假设您想选择 class 属性中包含 important 的元素,可以用下面这个选择器做到这一点:
p[class~="important"] {color: red;}
伪类:selector : pseudo-class {property: value}
a:link {color: #FF0000} /* 未访问的链接 */a:visited {color: #00FF00} /* 已访问的链接 */a:hover {color: #FF00FF} /* 鼠标移动到链接上 */a:active {color: #0000FF} /* 选定的链接 */
<p><b><a href="/index.html" target="_blank">这是一个链接。</a></b></p>