文章地址 https://www.cnblogs.com/sandraryan/

案例:用diiv嵌套多个正方形,配合盒模型相关知识,使每个div在他的父元素上居中。(每个div中心点对齐)

涉及到margin的各种合并问题。

(触发BFC是更好的解决方案等,为做练习此处只考虑margin)

此处给div的父级加个border就好了

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style type="text/css">
.first {
width: 600px;height: 600px;
background-color: red;
border: 1px dashed black;
}
.second {
width: 500px;height: 500px;
background-color: orange;
margin: 50px;
/* 使second box在first box上居中显示,first设置了边框所以margin不会合并*/
border: 1px solid orange;
}
.third {
width: 400px;height: 400px;
background-color: yellow;
/* 此时third和second如果设置margin-top会合并 */
border: 1px solid yellow;
margin: 48px;
/* 加border就可以使margin-top恢复作用 */
/* 垂直关系视图布局 直接父级元素提供位置的参考 */
}
.forth {
width: 300px;height: 300px;
background-color: green;
border: 1px solid green;
margin: 48px;
}
.fifth {
width: 200px;height: 200px;
background-color: lightblue;
border: 1px solid lightblue;
margin: 48px;
}
.center {
width: 100px;height: 100px;
background-color: purple;
margin: 50px;
}
</style>
</head>
<body>
<div class="first">
<div class="second">
<div class="third">
<div class="forth">
<div class="fifth">
<div class="center"></div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

图片:

用diiv实现多个方块居中嵌套--margin的更多相关文章

  1. 用diiv实现多个方块居中嵌套--padding

    文章地址 https://www.cnblogs.com/sandraryan/ 案例:用diiv嵌套多个正方形,配合盒模型相关知识,使每个div在他的父元素上居中.(每个div中心点对齐) 涉及到m ...

  2. word标题文字居中浅谈

    在Word排版时,要将标题在文档居中,是有区别的,如下图 在回车键后,在选择标题居中,我们常认为标题就是在整个文档居中了,但是实际上只是在回车键到右边区域居中而已,如上图红色方块居中. 只有在标题文字 ...

  3. 正确的使用margin:0 auto与body{text-align:center;}实现元素居中(转)

    body{text-align:center}与margin:0 auto的异同? text-align是用于设置或对象中文本的对齐方式.一般情况下我们设置文本对齐方式的时候需要用此属性进行设置 我们 ...

  4. 【转】css布局居中和CSS内容居中区别和对应DIV CSS代码

    原文地址:http://www.divcss5.com/jiqiao/j771.shtml css布局居中和CSS内容居中区别和对应DIV CSS代码教程与图文代码案例篇 对于新手来说DIV CSS布 ...

  5. 小 div在大 div中左右上下居中

    <!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content=& ...

  6. html+css--水平居中总结(不定宽块状元素方法)(一)

    来源:http://www.imooc.com/code/6363 在实际工作中我们会遇到需要为“不定宽度的块状元素”设置居中,比如网页上的分页导航,因为分页的数量是不确定的,所以我们不能通过设置宽度 ...

  7. 常用布局,div竖直居中

    常用两列布局,多列布局和div竖直居中 body { margin:; padding:; } .w200 { width: 200px; } .mar-left200 { margin-left: ...

  8. CSS中的各种居中方法总结

    CSS中的居中可分为水平居中和垂直居中.水平居中分为行内元素居中和块状元素居中两种情况,而块状元素又分为定宽块状元素居中和不定宽块状元素居中.下面详细介绍这几种情况. 一.水平居中 1.行内元素居中 ...

  9. 设置div控件居中的方法

    margin是设置外边距的,它有四个值,margin:4px 5px 5px 5px;分别表示上边距,右边距,下边距,和左边距,是从上按顺时针设置的,如果单个设置,他又有margin-top:3px: ...

随机推荐

  1. lingo 出现63. MODEL IS ILL DEFINED 解决办法

    63. MODEL IS ILL DEFINED. CHECK FOR UNDEFINED INDICES AND/OR CONDITIONS IN EXPRESSION: EXPRESSION. 还 ...

  2. 实现移动端上拉加载和下拉刷新的vue插件(mescroll.js)

    做一个简单的移动端展示项目,后台分页后前端加载,实现上拉加载下一页,找了下,还是用这个mescroll.js插件好一点 1.npm安装 npm install --save mescroll.js / ...

  3. tablespaces

    select * from user_tablespaces; select username,default_tablespace from user_users;

  4. 新一代互联网传输协议QUIC浅析

    QUIC(Quick UDP Internet Connection)是谷歌制定的一种互联网传输层协议,它基于UDP传输层协议,同时兼具TCP.TLS.HTTP/2等协议的可靠性与安全性,可以有效减少 ...

  5. Python之其他数据类型

    1.可命名元组:namedtuple 由nametuple可创建一个包含tuple所有功能以及其他功能的类型 class Mytuple(__builtin__.tuple) | Mytuple(x, ...

  6. vagrant up 时提示错误 cound not open file

    根据教程:https://laravel-china.org/docs/laravel-development-environment/5.5/development-environment-wind ...

  7. Spring新特性_泛型依赖注入

    泛型依赖注入 package com.tanlei.spring.generic; import org.springframework.beans.factory.annotation.Autowi ...

  8. oracle在不知道用户密码的时候,怎么样跳转到另外一个用户执行操作后并不影响该用户?

    我们通过如下的方法,可以安全使用该用户,然后再跳转回来,在某些时候比较有用 需要Alter user权限或DBA权限: SQL> select password from dba_users w ...

  9. 使用php simple html dom parser解析html标签

    转自:http://www.blhere.com/1243.html 使用php simple html dom parser解析html标签 用了一下 PHP Simple HTML DOM Par ...

  10. List容器-LinkedList链表

    LinkedList--链表 特点: 删除,增加 用LinkedList性能高  层次查找不适合       查询用ArrayList  数组下标查找  插入和删除慢缺点是要做移位操作 总结:Link ...