博主信息
博文 22
粉丝 0
评论 0
访问量 25871
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
box、element alignment、position(8月16日作业)
岑勋的博客
原创
900人浏览过


box模型:编程实现盒模型的基本要素: 内容,内外边距与边框

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>盒子模型</title>
    <style>
        div {
            padding: 0;
            margin: 0;
        }
        /*box1和box2初始大小相同,box1包含box2;box1因为设置了padding的缘故,width的实际大小要加上左右padding值,height实际大小同理。所以box1和box2不会完全重合*/
        div.box1 {
            border: 1px dashed red;
            width: 160px;
            height:160px;
            background: royalblue;
            padding: 20px;
        }
        div.box2 {
            width: 160px;
            height: 160px;
            background: #000;
        }
    </style>
</head>
<body>
    <!--编程实现盒模型的基本要素: 内容,内外边距与边框-->
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

运行实例 »

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

编程实现最常用的四种元素对齐方案

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>元素对齐</title>
    <style>
        /*1、子元素是单行行内元素时*/
       div.box1 {
           width: 12rem;
           height:4rem;
           background: #dbb004;
           text-align: center;
      }
        /*要实现水平居中,需把子元素行高设置为与父元素高度相同*/
        div.box1 a {
            line-height:4rem;
            text-decoration: none;
        }
        /*2、子元素是多个行内元素*/
        div.box2 {
            width: 200px;
            height: 100px;
            background: lightblue;
            text-align: center;
            display: table-cell;
            vertical-align: middle;
        }
        div.box2 a {
            color: white;
            text-decoration: none;
        }
        div.box3 {
            width: 200px;
            height: 100px;
            background: lightcyan;
            display: table-cell;
            vertical-align:middle;
        }
        div.box3 .small {
            width: 160px;
            height: 60px;
            background: lightcoral;
            margin: auto; /*设外边距挤到中间*/
            text-align: center;
        }
        div.box3 .small a {
            line-height: 60px;
            text-decoration: none;
            text-shadow: 1px 1px 1px white;
        }
        /*4、子元素是不定宽的块元素时*/
        div.box4 {
            width: 300px;
            height: 100px;
            background: lightskyblue;
            display: table-cell;
            vertical-align:bottom;
        }
        div.box4 ul li {
            width:20px;
            height: 20px;
            line-height: 20px;
            border: 1px solid green;
            border-radius: 50%;
            list-style:none;
            margin-left: 5px;
            text-align: center;
            float: left;


        }

    </style>
</head>
<body>
    <h3>编程实现最常用的四种元素对齐方案</h3>
    <h4>1、子元素是单行行内元素时,如<span><a></h4>
    <div class="box1">
        <a href="http://www.php.cn">PHP中文网</a>
    </div>
    <h4>2、子元素是多个行内元素时</h4>
    <div class="box2">
        <span>PHP中文网地址</span><br>
        <a href="http://www.php.cn">点我摸我</a>
    </div>
    <h4>3、子元素是块元素时</h4>
    <div class="box3">
       <div class="small">
           <a href="http://www.php.cn">PHP中文网</a>
       </div>
    </div>
    <h4>4、子元素是不定宽的块元素时</h4>
    <div class="box4">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>7</li>
            <li>8</li>
        </ul>
    </div>
</body>
</html>

运行实例 »

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





本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学