作者归档:Rick

html中居中显示的几种方式

1. text-align: center; 水平居中
2. 设置line-height;垂直居中
3. display:table-cell;
   vertical-align:middle;
4.  display: flex;
     align-items: center;
     justify-content: center;
5.  背景图片居中 background: url('2.png') no-repeat 50% 50%/32px 32px;

源代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Center</title>
    <style>
        div {
            font-size: 12px;
            width: 100px;
            height: 100px;
            border: 1px solid red;
            margin-bottom: 5px;
        }

        img {width: 32px}

        .div1 {
            text-align: center;
        }

        .div2 {
            line-height: 100px;
        }

        .div3 {
            display:table-cell;
            vertical-align:middle;
        }

        .div4 {
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .div5 {
           background: url('2.png') no-repeat 50% 50%/32px 32px;
        }

    </style>
</head>
<body>
    <div class="div1">文字水平居中</div>
    <h5>line-height</h5>
    <div class="div2">文字垂直居中</div>
    <div class="div2"><img src="2.png" alt=""></div>

    <h5>table-cell</h5>
    <div class="div3">文字垂直居中,这个更好些,内联元素</div>
    <div class="div3"><img src="2.png" alt=""></div>

    <h5>flex</h5>
    <div class="div4">
        flex布局垂直水平居中,下一个div换行显示。
    </div>
    <div class="div4">
        <img src="2.png" alt="">
    </div>
    <h5>background-image</h5>

    <div class="div5">

    </div>

</body>
</html>

演示地址:
http://xhope.top/samples/html/center_middle.html

html背景图的显示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>background</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            background: url('1.jpg') no-repeat scroll -10px -10px/100px 100px content-box padding-box yellow;

            /*拆分
                background-image:url(1.jpg);
                background-repeat:no-repeat;
                background-attachment:scroll;
                background-position:-10px -0px;
                background-size:100px 100px;
                background-origin:content-box;
                background-clip:padding-box; 从padding(不包括padding)往外裁剪
                background-color: yellow;
            */

            padding: 20px;
            border: 20px dashed red;
        }
    </style>
</head>
<body>
    <div>content</div>
</body>
</html>

演示地址

http://xhope.top/samples/html/background.html

html css中的position的运用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>position</title>

    <style>

        .parent {
            position: relative;
            width: 210px;
            height: 250px;
            background-color: yellow
        }

        div {
            width: 200px;
            height: 200px;
        }

        #relative {
            background-color: red;
            position: absolute;
            left:10px;
            top:10px;
            z-index: 1;
        }

        .general {
            background-color: grey;
            width: 100%;
            height: 220px;
        }

        #absoulte {
           background-color: red;
           position: relative;
           left:10px;
           top:10px;
       } 
    </style>
</head>
<body>
    <div class="parent">
        <div id="relative">
            relative相对于自己原来的位置偏移 left:10px;top:10px;不占位
        </div>
        <div class="general"></div>
    </div>
    <hr>
     <div class="parent">
        <div id="absoulte">
            absoulte以父层做为定位基线的话,在父层应用absolute或relativ属性就可以,left:10px;top:10px;占位
        </div>
        <div class="general"></div>
    </div>

</body>
</html>

演示地址
http://xhope.top/samples/html/position.html

html viewport的学习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1">
    <title>width</title>
    <style>
        body {
            padding:0;
            margin:0;
        }
        .container {
            margin: 0;
            padding: 0;
            background-color: red;
        }

       /*  @media all and (max-device-width: 320px) {
           .container {
               width: 100px;
           }
       } */
    </style>
</head>
<body>
    <div class="container" id="container">
        hello world
    </div>
    <script>
        alert("window.innerWidth = " + window.innerWidth);
        alert("screen.width = " + screen.width);
        alert("container = " + document.getElementById('container').clientWidth);
    </script>
</body>
</html>

演示地址
http://xhope.top/samples/html/viewport.html

html中float的运用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>float</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: red;
        }

        #float_left {
            float: left;
        }

        #float_right {
            float: right;
            height: 250px;
        }

        .general {
            /* clear: left; */
            /* clear: right; */
            clear: both;
            background-color: grey;
            width: 100%;
            height: 300px;
        }
    </style>
</head>
<body>
    <div id="float_left">float left,不占位,可以通过clear:left清除</div>
    <div id="float_right">float left,不占位,可以通过clear:right清除</div>
    <div class="general"></div>
</body>
</html>

演示地址
http://xhope.top/samples/html/float.html