CSS3入门之边框与背景

时间:2022-09-20 15:46:55

CSS3入门之边框与背景

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

1、前言

CSS3作为CSS的最新版本,在展示效果上有非常大的提升,接下来,我们就一起领略一下CSS3的风采吧。

2、CSS3边框

2.1、border-radius(用于设置圆角边框)

在CSS2时代,要想实现圆角边框,是一件非常麻烦的事情。一种实现方式是使用一个背景图片,为了实现伸缩效果,还需要至少3张图片拼凑,相当麻烦。另外一种实现方式是使用多个div重叠来实现圆角。

在CSS3中,有一个非常简单的属性,那就是border-radius。

语法: border-radius: 1-4 length|% / 1-4 length|%;

border-radius: 10px;
//等价于
border-top-left-radius:10px;
border-top-right-radius:10px;
border-bottom-right-radius:10px;
border-bottom-left-radius:10px;

<div style="border-radius:10px;">
演示圆角边框
</div>
演示圆角边框

兼容性说明: IE9+,Chrome,FF,Safari,Oprea

div
{
border:2px solid;
border-radius:25px;
-moz-border-radius:25px; /* Old Firefox */
}

2.2、box-shadow(用于添加边框阴影)

语法: box-shadow: h-shadow v-shadow blur spread color inset;,其中h-shadow和v-shadow是必须设置,允许负值。【参数说明:水平阴影的位置,垂直阴影的位置,模糊距离,阴影的尺寸,阴影的颜色,外部引用(outset)改为内部阴影】

<div style="border-radius:10px; border: 1px solid red;">
演示圆角边框
</div>
简单阴影
<div style="box-shadow: -2px -2px red;">
简单阴影
</div>
简单阴影
<div style="box-shadow: -2px -2px 10px red;">
带模糊效果的阴影
</div>
带模糊效果的阴影
<div style="box-shadow: 2px 2px 10px 10px red;">
带模糊效果指定尺寸的阴影
</div>
带模糊效果指定尺寸的阴影
<div style="box-shadow: 2px 2px 10px 10px red inset;">
内部阴影
</div>
内部阴影

兼容性说明: IE9+,Chrome,FF,Safari,Oprea

2.3、border-image(CSS3边框图片)

border-image是简写属性,全部是:

border-image-source  //背景图片源
border-image-slice //图片边框内偏移
border-image-width //图片边框的宽度
border-image-outset //边框图像区域超出边框的量
border-image-repeat //边框是否适应平铺(repeated)、铺满(rounded)、拉伸(stretched)

<div style="border-width:10px;border-image: url(http://www.w3school.com.cn/i/border.png) 10 10 round;">
简单图片边框
</div>
简单图片边框
<div style="border-width:10px;border-image: url(http://www.w3school.com.cn/i/border.png) 10 10 50 round">
完全设置的图片边框
</div>
完全设置的图片边框

兼容性说明: Chrome,FF,Safari,Oprea

div
{
border-image:url(border.png) 30 30 round;
-moz-border-image:url(border.png) 30 30 round; /* 老的 Firefox */
-webkit-border-image:url(border.png) 30 30 round; /* Safari 和 Chrome */
-o-border-image:url(border.png) 30 30 round; /* Opera */
}

3、CSS3背景

整体兼容性

以下CSS背景的特性,全部支持IE9+,FF,Chrome,Safari,Oprea

3.1、background-size(用于规定背景图片的尺寸)

在以前的CSS中,背景图片的大小,是由图片本身的大小决定的。在CSS3中,有一个简单的CSS样式可以设置背景图片的大小,允许我们在不同的环境中重复使用背景图片。可以以像素或百分比规定尺寸。

<div style="
background-image:url(http://www.w3school.com.cn/i/bg_flower_small.gif);
background-size: 50% 70%;
background-repeat:no-repeat;">
</div>
简单设置背景图大小

3.2、background-origin(规定背景图片的定位区域)

盒子模型示意图:

CSS3入门之边框与背景

background-origin属性则可以设置背景图片放置于哪个区域上(content-box,padding-box,border-box)

<div style="width:66px;height:125px;
background-image:url(http://www.w3school.com.cn/i/bg_flower_small.gif);
background-origin:content-box;
padding: 20px;border:20px solid red;"></div>
 
<div style="width:66px;height:125px;
background-image:url(http://www.w3school.com.cn/i/bg_flower_small.gif);
background-origin:border-box;
padding: 20px;border:20px solid red;"></div>
 
<div style="width:66px;height:125px;
background-image:url(http://www.w3school.com.cn/i/bg_flower_small.gif);
background-origin:padding-box;
padding: 20px;border:20px solid red;"></div>
 

3.3、多重背景

可以针对标签设置多个背景,用法如下:

body
{
background-image:url(bg_flower.gif),url(bg_flower_2.gif);
}