Blogger Information
Blog 39
fans 0
comment 0
visits 30829
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
盒模型的基本原理、元素对齐方案以及位置固定方法 2018年8月16日 22:30
南通税企通马主任的博客
Original
720 people have browsed it

第一个案例:编程实现盒模型的基本要素;

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>盒子模型</title>
    <style>
        .box1 {
            width: 300px;
            height: 300px;
            background-color: lightgreen;
            margin-bottom:30px
        }

        .box2 {
            width: 300px;
            height: 300px;
            background-color: lightcoral;
            margin-top: 50px;
        }
    </style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
运行实例 »

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

小结:案例里面是两个div,做了两个盒子,padding和margin都是0,body的margin是8;另外,盒子四个边的设置原则是上右下左的顺序,也可以上下或者左右一起设置,也可以直接both。

外边距在垂直方向上会塌陷,以数值大的元素的外边距为准,谁大谁不动,谁就牛逼!


第二个案例:编程实现最常用的四种元素对其方案;

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>四中元素对齐方式</title>
    <style>
        .box1{
            width:200px;
            height:200px;
            background-color: gray;
            text-align: center;
        }
        .box1 a{
            line-height: 200px;
        }
        .box2 {
            width: 200px;
            height: 200px;
            background-color: green;
            text-align: center;
            display: table-cell;
            vertical-align: middle;
        }
        .box3 {
            width: 200px;
            height: 200px;
            background-color: gold;
            display:table-cell;
            vertical-align: middle;
        }
        .box3 .child{
            width:100px;
            height: 100px;
            background-color: lightcoral;
            margin: auto;
        }
        .box4{
            width:200px;
            height:200px;
            background-color: lightblue;
            text-align: center;
            display: table-cell;
            vertical-align: bottom;
        }
        ul{
            margin: 0px;
            padding: 0px;
        }
        .box4 li{
            display:inline;
        }

    </style>

</head>
<body>
<h3>元素对齐方式</h3>
1、子元素是行内元素:且是单行,比如a,span等<br>
a:水平对齐,也叫水平居中,必须在父元素上应用<br>
b:垂直居中,在当前元素上,即行内子元素上设置行高与父元素同高就行<br>
<div class="box1"><a href="">php中文网</a></div>
<hr>
2、子元素是多行内联文本<br>
a:水平对齐,也叫水平居中,必须在父元素上应用<br>
b:垂直居中,在父元素中用(display:table-cell;)来实现<br>
c:将元素转换成表格属性之后,用vertical-align:middle实现表格内容居中<br>
<div class="box2">
    <span>php中文网</span><br>
    <span>www.php.cn</span>
</div>
<hr>
3、子元素是块元素<br>
a:水平居中:子元素设置左右外边距自动适应容器margin:auto;<br>
b:垂直居中: 在父元素: display:table-cell;+vertical-align: middle;<br>

<div class="box3">
    <div class="child"></div>
</div>
<hr>
4. 子元素是不定宽的块元素 <br>
a: 水平居中: 子元素转为行内元素,父级加: text-align:center <br>
b: 垂直居中: 在父元素: display:table-cell; +vertical-align: bottom;位于底部<br>

<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>

运行实例 »

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

小结:这是目前为止为数不多的我看了两遍就都掌握了的课程,对齐,对齐!!

第三个案例:编程实现用五个色块制作一个十字架(其中有一个是body背景色吧?);

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>盒模型制作十字架</title>
    <style>
        .box{
            width: 600px;
            height: 600px;
            background-color: darkmagenta;
            position:relative;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: lightcoral;
            position:absolute;
            top:0;
            left:200px;
        }
        .box2{
            width: 200px;
            height: 200px;
            background-color: lightseagreen;
            position:absolute;
            top:200px;
            left:0;
        }
        .box3{
            width: 200px;
            height: 200px;
            background-color: lawngreen;
            position:absolute;
            top:200px;
            left:400px;
        }
        .box4{
            width: 200px;
            height: 200px;
            background-color: lemonchiffon;
            position:absolute;
            top:400px;
            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>
</body>
</html>

运行实例 »

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

小结:终于完成到第五节课啦,还有一节课,现在开始再看一遍录播!!

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments