饥人谷技术博客

CSS 杂谈

2017-07-13  本文已影响23人  _空空

CSS 杂谈

文档流 Normal Flow

宽高如何确定

    <style type="text/css">
    body {
        font-size: 0;
    }

    span {
        /* 
        内联元素之间的间距问题:当行内元素之间有“回车”、“tab”、“空格”时就会出现空隙
        解决办法:
            1. 写在一行,之间不要有空格之类的符号
            2. 使用 font-size。设置内联元素的父元素字体大小为 0,然后设置内联元素字体大小 
        */
        font-size: 15px;
        border: 1px solid green;
    }

    span:nth-child(1) {
        line-height: 100px;
        font-size: 30px;
        /*padding: 40px 0;*/
    }
    </style>
</head>
<body>

<span>文字文字文字文字文字文字</span>
<span>文字文字文字文字文字文字</span><br>
<span style="background-color: pink">文字文字文字文字文字文字</span>

</body>
    <style type="text/css">
    div {
        border: 10px solid red;
    }

    .contain {
        width: 300px;
        border-color: lightgreen;
    }

    .contain > div:nth-child(1) {
        /* 宽度 %:定义基于包含块(父元素)宽度的百分比宽度/高度 */
        width: 100%;   
        /* 
        不要设置 width: 100%; 很容易出问题
        widtd 的默认值为 auto,它会自动计算的
        */
    }
    </style>
</head>
<body>

<div class="contain">
    <div>文字文字文字文字文字文字</div>
    <div>文字文字文字文字文字文字</div>
    <div>文字文字文字文字文字文字</div>
</div>

</body>
    <style type="text/css">
    /*
    使用 inline-block 元素的时候,常会遇到两个 bug:
    1. 两个inline-block 元素之间如果有空格、回车、tab,那么在页面上就有一个空隙
       解决办法:将父元素的 font-size 设置为 0,然后在 inline-block 元素中将 font-size 设置为需要的大小 

    2. 两个不同高度的 inline-block 元素无法对齐,或者下面无缘无故多出几像素
       解决办法:改变 inline-block 元素的 vertical-align,一般改为 top 或 middle
    */
    .box {
        font-size: 0;
        background-color: lightblue;
    }

    .box > img {
        vertical-align: top;
        width: 100px;              
    }
    </style>
</head>
<body>
<div class="box">
    <\img src="http://t.388g.com/uploads/allimg/160412/4-160412123F3.jpg">
    <\img style="width: 140px" src="http://t.388g.com/uploads/allimg/160412/4-160412123F3.jpg">
</div>
</body>

居中(水平、垂直)

文档流中的元素

水平居中

    <style type="text/css">
    .box {
        text-align: center;
        background-color: lightblue;
    }

    .box > div {
        /* 通过设置 margin 来居中 */
        margin: 0 100px;
        border: 1px solid red;
    }
    </style>
</head>
<body>

<div class="box">
    <span style="border: 1px solid black;">行内元素行内元素行内元素</span>
    <div>块状元素块状元素块状元素</div>
</div>

</body>

垂直居中

    <style type="text/css">
    .box {
        padding: 100px 0;
        background-color: lightblue;
        /*line-height: 300px;*/

    }

    .box > span {
        line-height: 300px;
    }
    </style>
</head>
<body>

<div class="box">
    <!-- <span style="border: 1px solid black;">行内元素行内元素行内元素</span> -->
    <div style="border: 1px solid red;">块状元素块状元素块状元素</div>
</div>

</body>

不在文档流中的元素

    <style type="text/css">
    .parent {
        position: relative;
        height: 300px;
        border: 1px solid red;
    }

    .parent > .child {
        position: absolute;
        width: 200px;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        border: 1px solid lightgreen;
    }

    table,
    .table
    {
        width: 500px;
        height: 300px;
        margin-top: 30px;
        border: 1px solid red;
    }

    table .child,
    .table .child
    {
        width: 200px; 
        margin: 0 auto; 
        border: 1px solid lightgreen;
    }

    .table {
        display: table;
    }

    .tr {
        display: table-row;
    }

    .td {
        display: table-cell;
        vertical-align: middle;
    }
    </style>
</head>
<body>

<div class="parent">
    <div class="child">
        文字文字文字文字文字文字文字文字文字文字
        文字文字文字文字文字文字文字文字文字文字
        文字文字文字文字文字文字文字文字文字文字
    </div>
</div>

<table>
    <tr>
        <td>
           <div class="child">
                文字文字文字文字文字文字文字文字文字文字
                文字文字文字文字文字文字文字文字文字文字
                文字文字文字文字文字文字文字文字文字文字
            </div> 
        </td>
    </tr>
</table>

<div class="table">
    <div class="tr">
        <div class="td">
            <div class="child">
                文字文字文字文字文字文字文字文字文字文字
                文字文字文字文字文字文字文字文字文字文字
                文字文字文字文字文字文字文字文字文字文字
            </div>
        </div>
    </div>
</div>

</body>

布局(一栏、两栏、三栏)

IE: float
非IE: flex

参考

上一篇 下一篇

猜你喜欢

热点阅读