02_CSS3

盒模型

关于盒模型存在两种形式,分别是W3C标准盒模型和IE盒模型,如下图所示,其区别主要在于宽度和高度的计算方式,CSS3对盒模型做出了新的定义,即允许开发人员指定盒子宽度和高度的计算方式。


IE盒模型只会出现在IE5版本和IE6+的怪异模式中。

box-sizing:content-box | border-box

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        .div1{
            width: 100px;
            height: 100px;
            padding: 20px;
            margin: 20px;
            border: 20px solid red;
            /*border-box:width和height包含padding和border*/
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }
        .div2{
            width: 100px;
            height: 100px;
            padding: 20px;
            margin: 20px;
            border: 20px solid red;
            /*content-box:width和height不包含padding和border*/
            -webkit-box-sizing: content-box;
            -moz-box-sizing: content-box;
            box-sizing: content-box;
        }
    </style>
</head>
<body>
    <div class="div1"></div>
    <div class="div2"></div>
</body>
</html>

背景

背景在CSS3中也得到很大程度的增强,比如背景图片尺寸、背景裁切区域、背景定位参照点、多重背景等。
  cover 会使“最大”边,进行缩放,另一边同比缩放,铺满容器,超出部分会溢出。
  contain 会使“最小”边,进行缩放,另一边同比缩放,不一定铺满容器,会完整显示图片。
  background-size会以background-clip设定的盒模型计算
  背景图片尺寸在实际开发中应用十分广泛。

例:background-clip属性

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            float: left;
        }
        .div1{
            width: 200px;
            height: 200px;
            padding: 20px;
            margin: 10px;
            border:5px dashed red;
            background-color: yellow;

            /*默认的背景填充区域*/
            /*可以修改我的背景区域*/
            background-clip: content-box;
        }
        .div2{
            width: 200px;
            height: 200px;
            padding: 20px;
            margin: 10px;
            border:5px dashed red;
            background-color: yellow;

            /*可以修改我的背景区域*/
            background-clip: padding-box;
        }
        .div3{
            width: 200px;
            height: 200px;
            padding: 20px;
            margin: 10px;
            border:5px dashed red;
            background-color: yellow;

            /*可以修改我的背景区域*/
            background-clip: border-box;
        }

    </style>
</head>
<body>
    <div class="div1"></div>
    <div class="div2"></div>
    <div class="div3"></div>
</body>
</html>

例:background-origin和background-size属性

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        div{
            float: left;
        }
        .div1{
            width: 200px;
            height: 200px;
            padding: 50px;
            margin: 10px;
            border: 10px dashed red;
            background-image: url(images/bg.jpg);
            background-color: yellow;
            background-repeat: no-repeat;
            background-origin: border-box;
        }
        .div2{
            width: 200px;
            height: 200px;
            padding: 50px;
            margin: 10px;
            border: 10px dashed red;
            background-image: url(images/bg.jpg);
            background-color: yellow;
            background-repeat: no-repeat;
            background-origin: padding-box;
        }
        .div3{
            width: 200px;
            height: 200px;
            padding: 50px;
            margin: 10px;
            border: 10px dashed red;
            background-image: url(images/bg.jpg);
            background-color: yellow;
            background-repeat: no-repeat;
            background-origin: content-box;
        }
        .div4{
            width: 200px;
            height: 200px;
            padding: 50px;
            margin: 10px;
            border: 10px dashed red;
            background-image: url(images/bg.jpg);
            background-color: yellow;
            background-repeat: no-repeat;
            background-origin: padding-box;
            background-clip: content-box;
        }
        .div5{
            width: 100px;
            height: 200px;
            padding: 50px;
            margin: 10px;
            border: 10px dashed red;
            background-image: url(images/bg.jpg);
            background-color: yellow;
            background-repeat: no-repeat;
            background-size: 100% 100%;
        }
        .div6{
            width: 100px;
            height: 200px;
            padding: 50px;
            margin: 10px;
            border: 10px dashed red;
            background-image: url(images/bg.jpg);
            background-color: yellow;
            background-repeat: no-repeat;
            background-size: 100% auto;
        }
        .div7{
            width: 100px;
            height: 200px;
            padding: 50px;
            margin: 10px;
            border: 10px dashed red;
            background-image: url(images/bg.jpg);
            background-color: yellow;
            background-repeat: no-repeat;
            background-size: cover;
        }
        /*contain 会使“最小”边,进行缩放,另一边同比缩放,不一定铺满容器,会完整显示图片。*/
        .div8{
            width: 100px;
            height: 200px;
            padding: 50px;
            margin: 10px;
            border: 10px dashed red;
            background-image: url(images/bg.jpg);
            background-color: yellow;
            background-repeat: no-repeat;
            background-size: contain;
        }
        .div9{
            width: 130px;
            height: 130px;
            margin: 20px;
            position: relative;
            background-image: url("images/bg.jpg");
            background-repeat: no-repeat;
        }
        .div9::before,
        .div9::after{
            content: '';
            position: absolute;
            width: 100%;
            height: 100%;
            padding: 30px;
            box-sizing: border-box;
        }
        .div9::after{
            background-color: red;
            background-clip: content-box;
            background-origin: padding-box;
            left: 100px;
            background-image: url("images/bg.jpg");
            background-repeat: no-repeat;
        }
        .div9::before{
            background-clip: content-box;
            background-color: #FFF;
        }
    </style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
<div class="div4"></div>
<div class="div5"></div>
<div class="div6"></div>
<div class="div7"></div>
<div class="div8"></div>
<div class="div9"></div>
</body>
</html>

例:多重背景

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        body {
            background-color: #CCC;
        }
        .duo{
            width: 623px;
            height: 417px;
            margin: 100px auto;
            background: url("images/bg1.png") left top  no-repeat,
                        url("images/bg2.png") right top no-repeat,
                        url("images/bg3.png") right bottom no-repeat,
                        url("images/bg4.png") left bottom no-repeat,
                        url("images/bg5.png") center center no-repeat;
            background-color: #FFF;
        }
    </style>
</head>
<body>
    <div class="duo"></div>
</body>
</html>

渐变

渐变是CSS3当中比较丰富多彩的一个特性,通过渐变我们可以实现许多炫丽的效果,有效的减少图片的使用数量,并且具有很强的适应性和可扩展性。
  可分为线性渐变、径向渐变、重复渐变。
  线性渐变指沿着某条直线朝一个方向产生渐变效果。
  径向渐变指从一个中心点开始沿着四周产生渐变效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板</title>
    <style>
        body, ul, li, dl, dt, dd, h1, h2, h3, h4, h5 {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #F7F7F7;
        }

        .wrapper {
            width: 1000px;
            margin: 0 auto;
            padding: 20px;
            box-sizing: border-box;
        }

        header {
            padding: 20px 0;
            margin-bottom: 20px;
            text-align: center;
        }

        header h3 {
            line-height: 1;
            font-weight: normal;
            font-size: 28px;
        }

        .main {
            /*overflow: hidden;*/
        }

        .main:after {
            content: '';
            clear: both;
            display: block;
        }

        .main .item {
            width: 210px;
            height: 210px;
            margin: 0 30px 30px 0;
            display: flex;
            position: relative;
            background-color: #FFF;
            float: left;
            box-shadow: 2px 2px 5px #CCC;
        }

        .main .item:after {
            content: attr(data-brief);
            display: block;
            width: 100%;
            height: 100%;
            text-align: center;
            line-height: 210px;
            position: absolute;
            top: 0;
            left: 0;
            color: #FFF;
            font-family: '微软雅黑';
            font-size: 18px;
            background-color: rgba(170, 170, 170, 0);
            z-index: -1;
            transition: all 0.3s ease-in;
        }

        .main .item:hover:after {
            background-color: rgba(170, 170, 170, 0.6);
            z-index: 100;
        }

        .main .item:nth-child(4n) {
            margin-right: 0;
        }

        /*.main .item:nth-last-child(-n+5) {
            margin-bottom: 0;
        }*/

        /* 以上是骨架样式 */

        .linear-gradient {
            width: 180px;
            height: 50px;
            margin: auto;
        }
        /*标准写法*/
        .item:nth-child(1) .linear-gradient{
            background-image: linear-gradient(yellow,red);
        }
        /*多个颜色渐变*/
        .item:nth-child(2) .linear-gradient{
            background-image: linear-gradient(yellow , green , red , blue);
        }
        /*角度确定方向*/
        .item:nth-child(3) .linear-gradient{
            background-image: linear-gradient(0deg, yellow, red);
        }
        /*角度确定方向*/
        /*水平方向渐变*/
        /*正上方为0度,顺时针旋转*/
        .item:nth-child(4) .linear-gradient{
            background-image: linear-gradient(90deg, yellow, red);
        }
        .item:nth-child(5) .linear-gradient{
            background-image: linear-gradient(20deg, yellow, red);
        }
        /*关键字确定方向*/
        .item:nth-child(6) .linear-gradient{
            background-image: linear-gradient(to top, yellow, red);
        }
        .item:nth-child(7) .linear-gradient{
            background-image: linear-gradient(to top left, yellow, red);
        }
        .item:nth-child(8) .linear-gradient{
            background-image: linear-gradient(to left top, yellow, red);
        }
        /*控制渐变*/
        /*
            0-20:黄色
            20-40:红黄渐变色
            40-100:红色
        */
        .item:nth-child(9) .linear-gradient{
            background-image: linear-gradient(to left, yellow 20%, red 40%);
        }
        .item:nth-child(10) .linear-gradient{
            background-image: linear-gradient(to left, yellow 50%, red 50%);
        }
        .item:nth-child(11) .linear-gradient{
            background-image: linear-gradient(to left, yellow 5%,blue 50%, red 95%);
        }
    </style>
</head>
<body>
<div class="wrapper">
    <header>
        <h3>CSS3渐变</h3>
    </header>
    <div class="main">
        <div class="item" data-brief="标准写法">
            <div class="linear-gradient"></div>

        </div>
        <div class="item" data-brief="多个颜色渐变">
            <div class="linear-gradient"></div>
        </div>
        <div class="item" data-brief="角度确定方向">
            <div class="linear-gradient"></div>
        </div>
        <div class="item">
            <div class="linear-gradient"></div>
        </div>
        <div class="item">
            <div class="linear-gradient"></div>
        </div>
        <div class="item">
            <div class="linear-gradient"></div>
        </div>
        <div class="item">
            <div class="linear-gradient"></div>
        </div>
        <div class="item">
            <div class="linear-gradient"></div>
        </div>
        <div class="item">
            <div class="linear-gradient"></div>
        </div>
        <div class="item">
            <div class="linear-gradient"></div>
        </div>
        <div class="item">
            <div class="linear-gradient"></div>
        </div>
    </div>
</div>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,843评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,538评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,187评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,264评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,289评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,231评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,116评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,945评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,367评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,581评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,754评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,458评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,068评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,692评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,842评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,797评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,654评论 2 354

推荐阅读更多精彩内容

  • 1、属性选择器:id选择器 # 通过id 来选择类名选择器 . 通过类名来选择属性选择器 ...
    Yuann阅读 1,631评论 0 7
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 1,750评论 0 2
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,314评论 0 11
  • 转载请声明 原文链接 关注公众号获取更多资讯 这篇文章主要总结H5的一些新增的功能以及一些基础归纳,这里只是一个提...
    程序员poetry阅读 9,071评论 22 225
  • 话题背景:如今网页展示的姿势是这样的 图片来自:设计之家 炫酷的网页展示,支撑它的正是强大的CSS3,还有什么理由...
    aliensq阅读 2,035评论 0 2