盒子模型示例代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了盒子模型示例代码相关的知识,希望对你有一定的参考价值。
参考技术A --定义样式 border-style:<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style type="text/css">
.block
width: 200px;
height:200px;
padding: 30px;
background-color: red;
border-style:solid;
border-width:10px;
border-color:green;
//border:10px solid yellow;
//border-radius: 20px;
//margin:auto;
</style>
</head>
<body>
<div class="block">border</div>
</body>
</html>
--兄弟元素margin合并
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
body
background: pink;
.div
//overflow: hidden;
//margin: 0 auto;
width: 300px;
height: 600px;
background: #aaa;
.div1
width: 200px;
height: 200px;
background: red;
//margin: 50px 0px;
//float: left;
.div2
width: 200px;
background: green;
//margin: 50px 0px;
height: 200px;
//float: left;
</style>
</head>
<body>
<div class="div">
<div class="div1"></div>
<div class="div2"></div>
</div>
</body>
</html>
----父子元素margin合并
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style type="text/css">
body
background: pink;
.div1
//margin-top: 20px;
width: 450px;
height: 450px;
background: #aaa;
//overflow:hidden;
//border:1px solid red;
.div2
width: 200px;
height: 200px;
background: red;
//margin-top: 50px;
</style>
</head>
<body>
<div class="div1">
<div class="div2"></div>
</div>
</body>
</html>
--box-sizing
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style type="text/css">
.group
/* background-color: blue; */
overflow: hidden;
.block
width: 33.33%;
padding: 20px;
float: left;
//box-sizing: border-box ;
.red
background-color: red;
//box-sizing: conten-box ;
//overflow: hidden;
//outline:#00FF00 dotted thick;
.green
background-color: green;
.gray
background-color: gray;
//box-sizing: border-box ;
</style>
</head>
<body>
<div class="group">
<div class="block red">1</div>
<div class="block green">2</div>
<div class="block gray">3</div>
</div>
</body>
</html>
--盒子显示(display)类型
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
.background
background-color:#F60;
margin-bottom:5px;
margin-left:5px;
height:50Px;
.display1
display:block;
.display2
display:inline ;
//display:inline-block;
</style>
<div class="background ">Div默认为 block</div>
<div class="background ">Div默认为 block</div>
<div class="background ">Div默认为 block</div>
<div class="background display2">修改为 inline</div>
<div class="background display2">修改为 inline</div>
<div class="background display2">修改为 inline</div>
<span class="background display1">加了block属性</span>
<span class="background display1">加了block属性</span>
<span class="background display1">加了block属性</span>
<span class="background">没加block属性</span>
<span class="background">没加block属性</span>
<span class="background">没加block属性</span>
</body>
</html>
css盒子模型
资料来源于:B站尚硅谷JavaWeb教程(全新技术栈,全程实战) ,本人才疏学浅,记录其笔记以供他日回顾
视频链接
知识点总结
<!--
IE浏览器:实际尺寸 = width
chrome浏览器:实际尺寸= width+左右borderwidth+padding
CSS盒子模型:
1.border 边框
2.margin 间距
3.padding 填充
-->
代码示例
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#div1
width:400px;
height:400px;
background-color:greenyellow;
/* 1. border 边框样式 */
border-width:1px; /*边框粗细*/
border-style:solid; /*边框样式:solid(实线) , dotted(点状线) ... */
border-color:blue; /*边框颜色*/
/* border:4px double blue;*/
/* border-top : 4px dotted blue;*/
#div2
width:150px;
height:150px;
background-color:darkorange;
margin-top:100px;
margin-left:100px;
/*margin:100px 100px 50px 150px;*/ /* 一个值,四个方向统一;两个值:上下、左右;三个值:上、左右、下;四个值:上右下左 */
/* padding : 填充 */
padding-top:50px;
padding-left:50px;
#div3
width:100px;
height:100px;
background-color:aquamarine;
/*
margin-top:50px;
margin-left:50px;
*/
#div4
width:200px;
height:200px;
margin-left:100px;
background-color:greenyellow;
/*加了这个属性后,div就紧贴边缘线了,不加的话,div与左侧和上方会有一条缝隙 */
body
margin:0;
padding:0;
</style>
</head>
<body>
<div id="div1">
<div id="div2">
<div id="div3"> </div>
</div>
</div>
<div id="div4"> </div>
</body>
</html>
以上是关于盒子模型示例代码的主要内容,如果未能解决你的问题,请参考以下文章
CSS盒子模型外边距 ③ ( 插入图片 | 插入图片位置移动 - 修改外边距 | 背景图片 | 背景图片移动位置 - 修改背景位置 background-position )
跟着盒子的代码设计示例,一起对面向对象的设计模式之SOLID原则加深理解