美文网首页
HTML 日常了解6

HTML 日常了解6

作者: 卝婯 | 来源:发表于2018-06-10 18:50 被阅读0次

    固定定位

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>固定定位</title>
        <style type="text/css">
            .box1{
                width: 200px;
                height: 200px;
                background-color: red;
            }
            .box2{
                width: 200px;
                height: 200px;
                background-color: yellow;
                /*
                当元素的position属性设置fixed时,则开启了元素的固定定位
                固定定位也是一种绝对定位,它的大部分特点都和绝对定位一样
                不同的是:
                    固定定位永远都会相对于浏览器窗口进行定位
                    固定定位会固定在浏览器窗口某个位置,不会随滚动条滚动
                IE6不支持固定定位
                */
                position: fixed;
                left: 0px;
                top: 0px;
            }
            .box3{
                width: 200px;
                height: 200px;
                background-color: yellowgreen;
            }
        </style>
    </head>
    <body style="height: 5000px;">
        <div class="box1"></div>
    
        <div class="box4" style="width: 300px; height: 300px; background-color: orange; position: relative;">
            <div class="box2"></div>
        </div>
    
        <div class="box3"></div>
    </body>
    </html>
    
    

    元素的层级

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>元素的层级</title>
        <style type="text/css">
            .box1{
                width: 200px;
                height: 200px;
                background-color: red;
                position: relative;
                z-index: 2;
                opacity: 0.5;
                filter: alpha(opacity=50);
            }
            .box2{
                width: 200px;
                height: 200px;
                background-color: yellow;
                /*开启绝对定位*/
                position: absolute;
                /*设置偏移量*/
                top: 100px;
                left: 100px;
                /*
                如果定位元素的层级是一样,则下边的元素会盖住上边的
                通过z-index属性可以用来设置元素的层级
                可以为z-index指定一个正整数作为值,该值将会作为当前元素的层级,层级越高,越优先显示
                
                对于没有开启定位的元素不能使用z-index
                */
                z-index: 25;
                opacity: 0.5;
                filter: alpha(opacity=50);
            }
            .box3{
                width: 200px;
                height: 200px;
                background-color: yellowgreen;
                /*position: relative;
                z-index: 3;*/
                position: absolute;
                top: 200px;
                left: 200px;
                z-index: 30;
                /*
                设置元素的透明背景
                opacity可以用来设置元素背景的透明,它需要一个0-1之间的值
                    0 表示完全透明
                    1 表示完全不透明
                    0.5 表示半透明
                */
                opacity: 0.5;
                /*
                opacity属性在IE8及以下的浏览器中不支持
                IE8及以下的浏览器需要使用如下属性代替
                    alpha(opacity=透明度)
                透明度,需要一个0-100之间的值
                    0 表示完全透明
                    100 表示完全不透明
                    50 半透明
                这种方式支持IE6,但是这种效果在IE Tester中无法测试
                */
                filter: alpha(opacity=50);
            }
            .box4{
                width: 200px;
                height: 200px;
                background-color: orange;
                /*开启相对定位*/
                position: relative;
                /*父元素的层级再高,也不会盖住子元素*/
                z-index: 20;
                top: 500px;
            }
            .box5{
                width: 100px;
                height: 100px;
                background-color: skyblue;
                /*开启绝对定位*/
                position: absolute;
                z-index: 10;
            }
        </style>
    </head>
    <body>
        <div class="box1"></div>
    
        <div class="box2"></div>
    
        <div class="box3"></div>
    
        <div class="box4">
            <div class="box5"></div>
        </div>
    </body>
    </html>
    

    背景

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>背景</title>
        <style type="text/css">
            .box1{
                width: 1024px;
                height: 724px;
                margin: 0 auto;
                /*设置背景样式*/
                background-color: #bfa;
                /*
                使用background-image来设置背景图片
                语法:background-image:url(相对路径);
                
                - 如果背景图片大于元素,默认会显示图片的左上角
                - 如果背景图片和元素一样大,则会将背景图片全部显示
                - 如果背景图片小于元素大小,则会默认将背景图片平铺以充满元素
                
                可以同时为一个元素指定背景颜色和背景图片,这样背景颜色将会作为背景图片的底色
                一般情况下设置背景图片时都会同时指定一个背景颜色
                */
                background-image: url(img/1.png);
                /*
                background-repeat用于设置背景图片的重复方式
                可选值:
                    repeat,默认值,背景图片会双方向重复(平铺)
                    no-repeat,背景图片不会重复,有多大就显示多大
                    repeat-x, 背景图片沿水平方向重复
                    repeat-y,背景图片沿垂直方向重复
                */
                background-repeat: repeat-y;
            }
        </style>
    
        <!-- <link rel="stylesheet" type="text/css" href="css/bgstyle.css"> -->
    </head>
    <body>
        <div class="box1"></div>
    </body>
    </html>
    

    背景偏移与定位

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>背景偏移与定位</title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            .box1{
                /*width: 500px;*/
                height: 500px;
                margin: 0 auto;
                /*设置一个背景颜色*/
                background-color: #bfa;
                /*设置一个背景图片*/
                background-image: url(img/4.png);
                /*设置图片不重复*/
                background-repeat: no-repeat;
                /*
                背景图片默认是贴着元素的左上角显示
                通过background-position可以调整背景图片在元素中的位置
                可选值:
                    该属性可以使用 top right left bottom center中的两个值来指定一个背景图片的位置
                    top left 左上
                    bottom right 右下
                    如果只给出一个值,则第二个值默认是center
                也可以直接指定两个偏移量
                    第一个值是水平偏移量
                        - 如果指定的是一个正值,则图片会向右移动指定的像素
                        - 如果指定的是一个负值,则图片会向左移动指定的像素
                    第二个是垂直偏移量
                        - 如果指定的是一个正值,则图片会向下移动指定的像素
                        - 如果指定的是一个负值,则图片会向上移动指定的像素
                */
                /*background-position: -50px -50px;*/
                background-attachment: fixed;
            }
            body{
                height: 5000px;
                background-image: url(img/3.png);
                background-repeat: no-repeat;
                /*
                background-attachment用来设置背景图片是否随页面一起滚动
                可选值:
                    scroll,默认值,背景图片随着窗口滚动
                    fixed,背景图片会固定在某一位置,不随页面滚动
                不随窗口滚动的图片,我们一般都是设置给body,而不设置给其他元素
                */
                background-attachment: fixed;
            }
        </style>
    </head>
    <body>
        <div class="box1"></div>
    </body>
    </html>
    

    背景固定

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>背景固定</title>
        <style type="text/css">
            body{
                height: 5000px;
                background-image: url(img/2.jpg);
                background-repeat: no-repeat;
                background-position: center;
                /*
                当背景图片的background-attachment设置为fixed时,
                背景图片的定位永远相对于浏览器的窗口
                */
                background-attachment: fixed;
            }
        </style>
    </head>
    <body>
        <p>
            在我的后园,可以看见墙外有两株树,一株是枣树,还有一株也是枣树。 这上面的夜的天空,奇怪而高,我生平没有见过这样奇怪而高的天空。他仿佛要离开人间而去,使人们仰面不再看见。然而现在却非常之蓝,闪闪地䀹着几十个星星的眼,冷眼。他的口角上现出微笑,似乎自以为大有深意,而将繁霜洒在我的园里的野花草上。 我不知道那些花草真叫什么名字,人们叫他们什么名字。我记得有一种开过极细小的粉红花,现在还开着,但是更极细小了,她在冷的夜气中,瑟缩地做梦,梦见春的到来,梦见秋的到来,梦见瘦的诗人将眼泪擦在她最末的花瓣上,告诉她秋虽然来,冬虽然来,而此后接着还是春,蝴蝶乱飞,蜜蜂都唱起春词来了。她于是一笑,虽然颜色冻得红惨惨地,仍然瑟缩着。 枣树,他们简直落尽了叶子。
        </p>
        <p>
            在我的后园,可以看见墙外有两株树,一株是枣树,还有一株也是枣树。 这上面的夜的天空,奇怪而高,我生平没有见过这样奇怪而高的天空。他仿佛要离开人间而去,使人们仰面不再看见。然而现在却非常之蓝,闪闪地䀹着几十个星星的眼,冷眼。他的口角上现出微笑,似乎自以为大有深意
        </p>
        <p>
            在我的后园,可以看见墙外有两株树,一株是枣树,还有一株也是枣树。 这上面的夜的天空,奇怪而高,我生平没有见过这样奇怪而高的天空。他仿佛要离开人间而去,使人们仰面不再看见。然而现在却非常之蓝,闪闪地䀹着几十个星星的眼,冷眼。他的口角上现出微笑,似乎自以为大有深意,
        </p>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:HTML 日常了解6

          本文链接:https://www.haomeiwen.com/subject/nqkqeftx.html