博客列表 >HTML元素的对齐方式CSS盒模型的类型和作用以及定位的使用 2018-8-17 19:30

HTML元素的对齐方式CSS盒模型的类型和作用以及定位的使用 2018-8-17 19:30

朝闻道的博客
朝闻道的博客原创
2018年08月17日 19:49:50625浏览

代码实例如下:

实例

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>CSS盒模型</title>
    <style>
        .box1 {
            width: 300px;
            height: 300px;
            background-color: red;
            margin: auto;
            padding: 20px;
            border: 8px solid black; 
        }

        .box2 {
            width: 300px;
            height: 300px;
            background-color: blue;
            margin:auto;margin-top: 20px;
            padding: 10px;
            border: 10px solid violet;
            border-radius: 5px;

        }
    </style>
</head>
<body>
<div class="box1" style="color: black;text-align: center;line-height:300px;">php中文网在我手</div>
<div class="box2" style="color: black;text-align: center;line-height:300px;">天下我有</div>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>元素对齐方式</title>
</head>
<body>
<h3>元素常用的四种对齐方式</h3>
1. 子元素是单行行内元素: 如a, span <br>
a:水平居中: 在父元素应用: text-align: center;<br>
b:垂直居中: 在行内子元素上设置行高与父元素等高: 如红色图块所示

<style>
    .box1 {
        width: 200px;
        height: 200px;
        background-color: red;
        text-align: center;
    }
    .box1 a {
        line-height: 200px;
        color: black;

    }
</style>
<div class="box1">
    <a  href="http://www.php.cn">php中文网</a>
</div>
<hr>

2. 子元素是多行的内联文本 <br>
a:水平居中: 在父元素应用: text-align: center;<br>
b:垂直居中: 在父元素: display:table-cell;将块级元素转为表格的形式如绿色图块所示


<style>
    .box2 {
        width: 200px;
        height: 200px;
        background-color: green;
        text-align: center;  /*水平居中*/
        display: table-cell;
        vertical-align: middle; /*垂直居中*/
    }
</style>
<div class="box2">
    <span>php中文网</span> <br>
    <span>好好学习,天天向上</span>
</div>
<hr>
3.子元素是块元素 <br>
a: 水平居中: 子元素设置左右外边距自动适应容器margin: auto;
b:垂直居中: 在父元素: display:table-cell;如蓝色图块中的珊瑚色图块所示
<style>
    .box3 {
        width: 200px;
        height: 200px;
        background-color: blue;
        display: table-cell;
        vertical-align: middle;/* 表格中垂直居中*/
    }
    .box3 .child {
        width: 100px;
        height: 100px;
        background-color: coral;
        margin: auto;  /*水平居中*/
    }
</style>
<div class="box3">
    <div class="child" style="text-align: center;line-height: 100px;">学习快乐</div>
</div>
<hr>
4. 子元素是不定宽的块元素
a: 水平居中: 子元素转为行内元素,父级加: text-align:center
b: 垂直居中: 在父元素: display:table-cell;
<style>
    .box4 {
        width: 200px;
        height: 200px;
        background-color: violet;
        text-align: center; /*水平居中*/
        display: table-cell;
        vertical-align: middle;
        /*vertical-align: bottom; /*位于底部*/*/

    }
    ul {
        margin: 0;
        padding-left: 0;
    }
    .box4 li {
        display: inline;  /*将块元素转为行内元素*/
    }
</style>

<div class="box4">
    <ul>
        <li><a href="">1</a></li>
        <li><a href="">2</a></li>
        <li><a href="">3</a></li>
        <li><a href="">4</a></li>
        <li><a href="">5</a></li>
    </ul>
</div>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>绝对定位的使用和注意事项</title>
    <style>
     
        .box {
            width: 600px;
            height: 600px;
            margin: auto;
            /*background-color: wheat;*/
            /*定位父级必须设置定位属性*/
            position: relative;
        }
        .box1 {
            width: 200px;
            height: 200px;
            background-color: blue;
            /*绝对定位元素会脱离文档流*/
            position: absolute;
            top:0;
            left: 200px;

        }
        .box2 {
            width: 200px;
            height: 200px;
            background-color: green;
            position: absolute;
            top:200px;
            left:0;
        }
        .box3 {
            width: 200px;
            height: 200px;
            background-color: black;
            position: absolute;
            top: 200px;
            left: 400px;
        }
        .box4 {
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            top: 400px;
            left: 200px;

        }

        .box5 {
            width: 200px;
            height: 200px;
            background-color: yellow;
            position: absolute;
            top: 200px;
            left: 200px;

        }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
    <div class="box4"></div>
    <div class="box5"></div>
</div>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

手抄笔记照片如下:

IMG_20180817_194319.jpg


IMG_20180817_194332.jpg


IMG_20180817_194341.jpg



声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议