用diiv实现多个方块居中嵌套--margin
文章地址 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的更多相关文章
- 用diiv实现多个方块居中嵌套--padding
文章地址 https://www.cnblogs.com/sandraryan/ 案例:用diiv嵌套多个正方形,配合盒模型相关知识,使每个div在他的父元素上居中.(每个div中心点对齐) 涉及到m ...
- word标题文字居中浅谈
在Word排版时,要将标题在文档居中,是有区别的,如下图 在回车键后,在选择标题居中,我们常认为标题就是在整个文档居中了,但是实际上只是在回车键到右边区域居中而已,如上图红色方块居中. 只有在标题文字 ...
- 正确的使用margin:0 auto与body{text-align:center;}实现元素居中(转)
body{text-align:center}与margin:0 auto的异同? text-align是用于设置或对象中文本的对齐方式.一般情况下我们设置文本对齐方式的时候需要用此属性进行设置 我们 ...
- 【转】css布局居中和CSS内容居中区别和对应DIV CSS代码
原文地址:http://www.divcss5.com/jiqiao/j771.shtml css布局居中和CSS内容居中区别和对应DIV CSS代码教程与图文代码案例篇 对于新手来说DIV CSS布 ...
- 小 div在大 div中左右上下居中
<!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" content=& ...
- html+css--水平居中总结(不定宽块状元素方法)(一)
来源:http://www.imooc.com/code/6363 在实际工作中我们会遇到需要为“不定宽度的块状元素”设置居中,比如网页上的分页导航,因为分页的数量是不确定的,所以我们不能通过设置宽度 ...
- 常用布局,div竖直居中
常用两列布局,多列布局和div竖直居中 body { margin:; padding:; } .w200 { width: 200px; } .mar-left200 { margin-left: ...
- CSS中的各种居中方法总结
CSS中的居中可分为水平居中和垂直居中.水平居中分为行内元素居中和块状元素居中两种情况,而块状元素又分为定宽块状元素居中和不定宽块状元素居中.下面详细介绍这几种情况. 一.水平居中 1.行内元素居中 ...
- 设置div控件居中的方法
margin是设置外边距的,它有四个值,margin:4px 5px 5px 5px;分别表示上边距,右边距,下边距,和左边距,是从上按顺时针设置的,如果单个设置,他又有margin-top:3px: ...
随机推荐
- Linux下安装配置git
参考博客: https://www.cnblogs.com/luhouxiang/p/5801853.html但执行git --version命令会出现 git version 1.8.3.1 不是最 ...
- pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
pymysql错误: pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query') pymy ...
- 【JZOJ3211】【SDOI2013】随机数生成器
╰( ̄▽ ̄)╭ 小 W喜欢读 书,尤其喜欢读 书,尤其喜欢读<约翰克里斯 朵夫>. 最近小 W准备读一本新书,这本一共有 p页, 页码范围为 0..p -1. 小 W很忙,所以每天只能读一 ...
- htmlhomework2
<!DOCTYPE html> register register username: password: birth: gender: male female
- compass与css sprite(雪碧图)
什么是css sprite? css sprite,中文叫雪碧图,也有人喊CSS精灵,就是一种背景拼合的技术,然后通过background-position来显示雪碧图中需要显示的图像. MDN相关链 ...
- MUI - 打开页面默认弹出键盘及返回关闭键盘
打开页面默认弹出键盘及返回关闭键盘 http://www.cnblogs.com/phillyx/ (function(keyboard) { var openSoftKeyboard = funct ...
- 【JZOJ4820】【NOIP2016提高A组模拟10.15】最大化
题目描述 输入 输出 样例输入 3 2 4 0 -10 8 -2 -2 样例输出 4 数据范围 解法 枚举两条扫描线,在这两条扫描线之间的矩阵,可以将之转化为一个序列b[i]=a[i][1..m]. ...
- AtCoder Regular Contest 084 C - Snuke Festival【二分】
C - Snuke Festival ....最后想到了,可是不应该枚举a[],这样要二重循环,而应该枚举b[],这样只需一重循环... #include<iostream> #inclu ...
- hdu3549 最大流
#include<stdio.h> #include<string.h> #include<queue> #define MAXN 1010 using names ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十一章:模板测试
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十一章:模板测试 代码工程地址: https://github.co ...