一、css基础语法
选择器通常是您需要改变样式的 HTML 元素。
css支持选择器分组:h1,h2,h3,h4,h5,h6 {color: green; }
子元素可以从父元素继承属性:body { font-family: Verdana, sans-serif; }
则其子元素默认为p, td, ul, ol, li, dl, dt, dd { font-family: Verdana, sans-serif; }
派生选择器允许你根据文档的上下文关系来确定某个标签的样式。
例如:li strong { font-style: italic; font-weight: normal; }(strong标签为li标签内的元素)
在现代布局中,id 选择器常常用于建立派生选择器。
例如:#sidebar p { (id 为sidebar的元素可能为div、表格,或其他块级元素)
font-style: italic;
text-align: right;
margin-top: 0.5em;
}
和 id 一样,class 也可被用作派生选择器:
例如:.fancy td {
color: #f60;
background: #666;
}
元素也可以基于它们的类而被选择:
例如:td.fancy { (以 fancy 标注的单元格都会是带有灰色背景的橙色)
color: #f60;
background: #666;
}
<td class="fancy"
>
属性选择器:对带有指定属性的 HTML 元素设置样式
例如:[title=W3School] (对含有title属性的元素,设置样式)
{
border:5px solid blue;
}
input[type="text"] (对input元素内,属性为text的设置样式)
{
width:150px;
display:block;
margin-bottom:10px;
background-color:yellow;
font-family: Verdana, Arial;
}
二、创建css
外部样式表
<head>
<link
rel="stylesheet" type="text/css" href="mystyle.css
" />
</head>
内部样式表
<head>
<style type="text/css">
hr {color: sienna;}
p {margin-left: 20px;}
body {background-image: url("images/back40.gif");}
</style>
</head>
内联样式
<p style
="color: sienna; margin-left: 20px">
This is a paragraph
</p>