`

CSS的视觉效果:width,padding,margin

阅读更多
1.包含元素的整体大小: [width] + padding + margin (三样全是包含元素的)
  (可能会因为内部元素比包含元素的width大,而使包含元素的[width]变大左)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" /><head>
<title>Expanding block level elements</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
/* set up for demo rules */
* {margin:0px; padding:0;}
body {padding-left:20px; background-color:#FFC;}
#ruler {position:relative; left:-51px; top:0px; margin-bottom:5px;}

/*DEMO OF BOX MODEL ISSUES */
#column {
	width:170px; 
	background:#FCC; /* not visible in this first step */
	padding:10px;
	margin:20px;
	}

#column_inner {
background:red;
}

h4 {background:#CCF;}
p  {background:#CFC;}
	
</style>
</head>
<body>
<img id="ruler" src="images/ruler_1000px.gif" alt="ruler" />
<div id="column">
  <div id="column_inner">
    <h4>An h4 heading</h4>
    <p>The heading and this paragraph sit in a column with a width of 170px. Because they are block level elements, their boxes
      expand horizontally to fill their containing element.</p>
  </div>
</div>
</body>
</html>




2.内部无素在没有width时会填满包含元素.
(不论包含元素有没有定义width)
~~~例子见上

3.内部元素有width,如果width + padding + margin (内部元素的) >包含无元素的width,就会使包含元素变大,去适应.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" /><head>
<title>Expanding block level elements</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
/* set up for demo rules */
* {margin:0px; padding:0;}
body {padding-left:20px; background-color:#FFC;}
#ruler {position:relative; left:-51px; top:0px; margin-bottom:5px;}

/*DEMO OF BOX MODEL ISSUES */
#column {
	width:170px; 
	background:#FCC; /* not visible in this first step */
	
	}

#column_inner {
background:red;
width:100px;
padding:20px;
margin:20px;
}

h4 {background:#CCF;}
p  {background:#CFC;}
	
</style>
</head>
<body>
<img id="ruler" src="images/ruler_1000px.gif" alt="ruler" />
<div id="column">
  <div id="column_inner">
    <h4>An h4 heading</h4>
    <p>The heading and this paragraph sit in a column with a width of 170px. Because they are block level elements, their boxes
      expand horizontally to fill their containing element.</p>
  </div>
</div>
</body>
</html>



4.位置的偏移 = 内部元素的margin + 包含元素的padding
margin 是与起始位置的偏移
padding 是除内部内容外剩余的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" /><head>
<title>Expanding block level elements</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
/* set up for demo rules */
* {margin:0px; padding:0;}
body {padding-left:20px; background-color:#FFC;}
#ruler {position:relative; left:-51px; top:0px; margin-bottom:5px;}

/*DEMO OF BOX MODEL ISSUES */
#column {
	width:170px; 
	background:#FCC; /* not visible in this first step */
	padding:10px;
	}

#column_inner {
background:red;
padding:20px;
margin:20px;
}

h4 {background:#CCF;}
p  {background:#CFC;}
	
</style>
</head>
<body>
<img id="ruler" src="images/ruler_1000px.gif" alt="ruler" />
<div id="column">
  <div id="column_inner">
    <h4>An h4 heading</h4>
    <p>The heading and this paragraph sit in a column with a width of 170px. Because they are block level elements, their boxes
      expand horizontally to fill their containing element.</p>
  </div>
</div>
</body>
</html>




以下是#column 为width:180px;时的效果



5. 如果内部的元素比其包含的元素大得多的话,内部元素会部分跳出包含元素.
(好像有点与3矛盾的样子,可能是表达得不够好~~~)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" /><head>
<title>Expanding block level elements</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="text_n_colors.css" media="all" rel="stylesheet" />

<style>
* {margin:0px; padding:0;}
body {padding-left:20px; background-color:#FFC;}
#ruler {position:relative; left:-51px; top:0px; margin-bottom:5px;}

#content {
width:500px;
}
#content_inner {
width:800px;
}

</style>

</head>
<body>
<img id="ruler" src="images/ruler_1000px.gif" alt="ruler" />

<div id="content">

	<h2>About This Layout</h2>
    <p>The four structural divs&mdash;header, nav,
      content and footer&mdash;nest inside a fixed
      width containing div. The nav column is fixed width and floated, and the content column is auto width, unfloated. The footer is cleared
      so it sits beneath whichever of the 
      columns is longest.</p>
	  <p>Auto left and right margin settings are applied
	    to the fixed-width containing div, which makes
	    the layout center in a wide browser window.</p>

	<div id="content_inner">
	
    <h3>The Concept</h3>
    <p>The four structural divs&mdash;header, nav,
      content and footer&mdash;nest inside a fixed
      width containing div. The nav column is fixed width and floated, and the content column is auto width, unfloated. The footer is cleared
      so it sits beneath whichever of the 
      columns is longest.</p>
	  <p>Auto left and right margin settings are applied
	    to the fixed-width containing div, which makes
	    the layout center in a wide browser window.</p>

	</div><!-- end content_inner -->
	
	 <h3>The Concept</h3>
    <p>The four structural divs&mdash;header, nav,
      content and footer&mdash;nest inside a fixed
      width containing div. The nav column is fixed width and floated, and the content column is auto width, unfloated. The footer is cleared
      so it sits beneath whichever of the 
      columns is longest.</p>
	  <p>Auto left and right margin settings are applied
	    to the fixed-width containing div, which makes
	    the layout center in a wide browser window.</p>
		
</div><!-- end content -->

</body>
</html>

[1]外部元素



[2]内部元素




  • 大小: 30.3 KB
  • 大小: 28.4 KB
  • 大小: 34.8 KB
  • 大小: 27.6 KB
  • 大小: 54.2 KB
  • 大小: 28.8 KB
  • 大小: 45.5 KB
  • 大小: 34.3 KB
  • 大小: 54.3 KB
  • 大小: 36.2 KB
  • 大小: 47.7 KB
  • 大小: 22.5 KB
  • 大小: 85.6 KB
  • 大小: 65.1 KB
分享到:
评论

相关推荐

    Css padding和margin区别

    这个一个CSS padding和margin的例子,对初学者很有帮助的哦~ .divcss3{border:1px solid #F00;width:400px;margin-left:15px; padding-left:35px;} .box2 { margin-left:10px; padding-left:15px; width:300px...

    DIV CSS常用的网页布局代码

    单行一列以下是引用片段:body{margin:0px;padding:0px;text-align:center;} #content{margin-left:auto;margin-right:auto;width:400px;width:370px;}两行一列以下是引用片段:body{margin:0px;padding:0px;text-...

    初写静态网页

    margin: 0; padding: 0; float: left; } /*专家团队*/ .showtTitle { width: 455px; height: 70px; padding: 0; margin: 0 auto; } .tcontain { width: 120px; height: 3px; padding: 0; margin: 62...

    css中子元素设置margin-top为什么影响了父元素

    本文介绍了css中子元素设置margin-top为什么... margin: 0px; padding: 0px; } .show{ margin: 0px auto; width: 200px; height: 100px; background-color: #999999; } .show h2{ margin-top: 50px; cursor:

    CSS3效果:自定义“W”形运行轨迹实例

    整理文档,搜刮出一个CSS3效果:“W”形运行轨迹实例的代码,稍微整理精简一下做下分享。 &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head lang=en&gt; &lt;meta charset=UTF-8&gt; &lt;title&gt;&lt;/title&gt; &lt;...

    Div+CSS多列布局方法

    margin:0; padding:0;height:30px} .myDiv ul li{ width:100px; float:left; border-right:1px solid #000; height:30px} .myDiv1{ width:300px; border:1px solid #000;border-bottom:0; height:90px} .myDiv1 ul{...

    网页制作代码+课程总结

    margin: 0px; padding: 0px; } #container{ text-align:left; padding:0px; width:1400px; position:relative; margin-top:0px; margin-right:auto; margin-bottom:0px; margin-left:auto; } #top{ ...

    【JavaScript源代码】JavaScript实现简单拖拽效果.docx

    JavaScript实现简单拖拽效果  本文实例为大家分享了JavaScript实现简单...// html  &lt;div&gt;   我是个蓝色的盒子&lt;/p&gt; &lt;/div&gt; CSS: CSS *{margin: 0;padding: 0;} div{ width: 100px; height: 100px; bac

    div-css-漂亮的导航条

    margin:0; padding:0; font: bold 11px/1.5em Verdana; } h2 { font: bold 14px Verdana, Arial, Helvetica, sans-serif; color: #000; margin: 0px; padding: 0px 0px 0px 15px; } img { border: none; } ...

    CSS常用网站布局实例

    body { margin: 0px; padding: 0px; text-align: center;} #content-top { margin-left:auto; margin-right:auto; width: 400px; width: 370px;} #content-end {margin-left:auto; margin-right:auto; width: 400...

    CSS使用特效

    margin: 0; padding: 0; border: 0 none; outline: 0; } body{ font-family: "Lucida Grande", "Verdana", sans-serif; font-size: 12px; } p{ margin: 20px 0 40px 0; } h1{ font-size: 30px; font-...

    css行内元素padding,margin,width,height没有变化

    主要介绍了css行内元素设置padding,margin,width,height没有变化的解决方法,需要的朋友可以参考下

    CSS入门教程:计算CSS盒模型宽和高

    #test{margin:10px;padding:10px;width:100px;height:100px;} 如上一段的代码,很多时候我们会把它所占的位置计算成width:120px,height:120px,因为在正常的理解下,padding是内边距,应该是包括在width里面的,而...

    css属性width默认值width: auto与width: 100%区别详解

    子元素有margin、border、padding时,会减去子元素content区域相对应的width值 父元素的content = 子元素(content + padding + border + margin ) width: 100% 强制将子元素的content区域 撑满 父元素的content...

    一些常用的DIV+CSS的网页布局所用的代码段

    单行一列以下是引用片段:body { margin: 0px; padding: 0px; text-align: center; } #content { margin-left:auto; margin-right:auto; width: 400px; width: 370px; } 两行一列以下是引用片段:body { margin: 0px...

    scrollbar_js实现竖向滚动条

    margin:20px auto;} .picul{ width:100%; float:left;} .picul li{ width:115px; height:115px; float:left; margin:0 7px 25px 7px; padding:1px; border:1px solid #ccc;} .picul li a{ display:block;} .picul li...

    jsp页面美化经典-css翻页代码

    MARGIN: 3px; PADDING-TOP: 3px; TEXT-ALIGN: center } DIV.digg A { BORDER-RIGHT: #aaaadd 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #aaaadd 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2...

    media-queries

    #gallery{ margin:36px 8px 0 8px;} .CALLERY_CONTENT{ width:38%;} .menu1 li{ width:22%; margin-right:1%; background:#ccc; color:#000;} .menu1 li.hover{ width:22%; margin-right:1%;} /*certificates*/ #...

    div宽度设置width:100%后再设置padding或margin超出父元素的解决办法

    本文介绍的是利用CSS3的新属性box-sizing,解决div宽度设置width:100%后再设置padding或margin超出父元素的问题,有需要的朋友们可以参考借鉴。 语法 box-sizing: content-box|border-box|inherit; 值一、content-...

Global site tag (gtag.js) - Google Analytics