ESC
输入关键词搜索文章
目录

CSS

参考

其他相关笔记

重点章节

介绍

选择器

p.center {text-align:center;} 
/* 所有的 p 元素使用 class="center" 让该元素的文本居中 */
p{ }
/* 为所有 p 元素指定一个样式。 */
.marked{ }
/* 为所有 class="marked" 的元素指定一个样式。 */
.marked p{ }
/* 为所有 class="marked" 元素内的 p 元素指定一个样式。 */
p.marked{ }
/* 为所有 class="marked" 的 p 元素指定一个样式。 */
[title]
{
    color:blue;
}
/* 把包含标题(title)的所有元素变为蓝色 */
[title=runoob]
{
    border:5px solid green;
}
/* 改变标题title='runoob'元素的边框样式 */
input[type="text"]
{
    width:150px;
    display:block;
    margin-bottom:10px;
    background-color:yellow;
}
input[type="button"]
{
    width:120px;
    margin-left:35px;
    display:block;
}
/* 表单样式 */

如何插入

插入样式表的方法有三种:

外部:

<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>

内部:

<head>
<style>
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
</style>
</head>

内联样式:

<p style="color:sienna;margin-left:20px">这是一个段落。</p>

优先级

(内联样式)Inline style > (内部样式)Internal style sheet >(外部样式)External style sheet > 浏览器默认样式

!important

!important 与优先级无关,但它与最终的结果直接相关,使用一个 !important 规则时,此声明将覆盖任何其他声明。

使用 !important 是一个坏习惯,应该尽量避免,因为这破坏了样式表中的固有的级联规则 使得调试找 bug 变得更加困难了。

常用属性

背景

文本

runoob

h1 {text-align:center;}
p.date {text-align:right;}
p.main {text-align:justify;}

text-decoration

h1 {text-decoration:overline;}
h2 {text-decoration:line-through;}
h3 {text-decoration:underline;}

text-transform

p.uppercase {text-transform:uppercase;}
p.lowercase {text-transform:lowercase;}
p.capitalize {text-transform:capitalize;}

text-indent

p {text-indent:50px;}

font

Generic family 字体系列 说明
Serif Times New Roman Georgia
Sans-serif Arial Verdana
Monospace Courier New Lucida Console
p{font-family:"Times New Roman", Times, serif;}
p.normal {font-style:normal;}
p.italic {font-style:italic;}
p.oblique {font-style:oblique;}

链接

runoob

a:link {text-decoration:none;}
a:visited {text-decoration:none;}
a:hover {text-decoration:underline;}
a:active {text-decoration:underline;}

列表

尺寸

runoob

display

盒子模型

border

border-style:属性1,属性2,属性3,属性4

上->右->下->左

border-style:属性1,属性2,属性3

上->左右->下

border-style:属性1,属性2

上下->左右

border-style:属性1

对齐

runoob

.center {
    margin: auto;
    width: 50%;
    border: 3px solid green;
    padding: 10px;
}

注意: 如果没有设置 width 属性(或者设置 100%),居中对齐将不起作用。

img {
    display: block;
    margin: auto;
    width: 40%;
}

注释:绝对定位元素会被从正常流中删除,并且能够交叠元素。

提示: 当使用 position 来对齐元素时, 通常 <body> 元素会设置 margin 和 padding 。 这样可以避免在不同的浏览器中出现可见的差异。

.right {
    position: absolute;
    right: 0px;
    width: 300px;
    border: 3px solid #73AD21;
    padding: 10px;
}

定位

p.pos_fixed
{
    position:fixed;
    top:30px;
    right:5px;
}

高级特性

伪类

伪元素

h1:after
{
    content:url(smiley.gif);
}

@media

实例

下拉菜单

runoob

表单

runoob