问题是如果你试图将margin-top添加到子元素(#slider-content-wrap或#inside-div-slider)中,那么它们就在父元素(#slider)中,margin-top将推送父元素不是儿童元素?
header,section,article,small,nav
{
display:block;
}
body
{
font-family: tahoma, verdana, arial, sans-serif;
font-size: 14px;
margin:0;
padding: 0;
background: #ffffff;
text-transform: capitalize;
box-sizing: content-box;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
-o-box-sizing: content-box;
}
h1,h2,h3,h4,h5,h6{
font-weight: bold;
font-size: 14px;
}
/************************************************
----------- Header --------
*************************************************/
#body-wrap{
margin-top: 0;
padding-top: 0;
width: 600px;
margin-left: auto;
margin-right: auto;
}
header{
margin-top: 0;
height: 60px;
background: red url('Images/headerBG.gif') repeat;
}
header h3,header ul {
padding-top: 0;
}
header h3{
float: left;
padding-left: 21px;
color: #ffffff;
}
#social-links{
float: right;
padding: 0;
padding-right: 20px;
}
#social-links li{
display: inline-block;
list-style: none;
padding-left: 13px;
}
#social-links li a{
-moz-transition-duration:1s;
-ms-transition-duration:1s;
-webkit-transition-duration:1s;
-o-transition-duration:1s;
transition:1s;
}
#social-links li a:hover{
-webkit-transform : scale(1.5);
-moz-transform : scale(1.5);
-ms-transform : scale(1.5);
-o-transform : scale(1.5);
transform : scale(1.5);
}
#social-links input[type=button] {
padding-right: 21px;
}
/************************************************
----------- nav --------
*************************************************/
nav{
clear: both;
height: 36px;
background: green url('Images/navBG.gif') repeat;
margin-bottom: 0;
}
nav ul {
list-style: none;
display: inline-block;
margin-top: 0;
margin-bottom: 0;
}
nav ul li {
padding-top: 10px;
}
#main-menu-ul{
margin-left: 0;
padding-left: 21px;;
float:left;
}
#main-menu-ul li
{
display: inline-block;
padding-left: 21px;
}
#main-menu-ul li a
{
color:#FFFFFF;
text-decoration: none;
-moz-transition-duration:1s;
-ms-transition-duration:1s;
-webkit-transition-duration:1s;
-o-transition-duration:1s;
transition-duration:1s;
}
#main-menu-ul li a:hover
{
color: #8f8f8f;
-webkit-transform : scale(1.5);
-moz-transform : scale(1.5);
-ms-transform : scale(1.5);
-o-transform : scale(1.5);
transform : scale(1.5);
}
#search-ul{
float:right;
padding-right: 22px;
}
#search-ul li {
display: inline-block;
}
#search-ul input[type=text]{
width: 92px;
height: 14px;
}
#search-ul li label {
color:#FFFFFF;
}
#search-ul li input[type=button]
{
padding: 0;
margin: 0;
font-size: 13px;
height: 20px;
margin-left: -4px;
}
/************************************************
----------- slider --------
*************************************************/
#slider
{
clear: both;
margin-top: 0;
height: 236px;
background: gray url('Images/SliderBG.png') repeat-x;
}
#slider-content-wrap{
clear: both;
width:560px;
height: 154px;
margin-left: 20px;
margin-right: 20px;
background-color: #3493ff;
}
#inside-div-slider{
margin-top: 20px;
background-color: #ffe132;
height: 100px;
width:360px;
}

<body>
<div id="body-wrap">
<header>
<h3>SITE NAME </h3>
<ul id="social-links">
<li><a href="http://www.yahoo.com"> <img src="Images/fb.gif"> </a> </li>
<li><a href="#"> <img src="Images/fb.gif"> </a> </li>
<li><a href="#"> <img src="Images/fb.gif"> </a> </li>
<li><a href="#"> <img src="Images/fb.gif"> </a> </li>
</ul>
</header>
<nav>
<ul id="main-menu-ul">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
</ul>
<ul id="search-ul">
<li> <label>Search : <input type="text" id="search-field"> </label> </li>
<li><input type="button" value="Go!"></li>
</ul>
</nav>
<!--********************Slider*******************-->
<section id="slider">
<div id="slider-content-wrap">
#slider-content-wrap
<div id="inside-div-slider">#inside-div-slider</div>
</div>
</section>
<!--********************Slider*******************-->
<section id="footer-section-1"></section>
<section id="footer-section-2"></section>
<footer></footer></div>
</body>
&#13;
答案 0 :(得分:3)
如果我正确理解你的问题,它与块格式化上下文有关。
孩子们浮动,因此与父母不同。
尝试将overflow:hidden
添加到父级,然后查看是否存在差异。
有关它的更多信息:https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context
答案 1 :(得分:1)
有两种方法可以消除这种行为。第一种方法是将overflow: hidden
应用于父容器,即#slider
,如下所示:
header,
section,
article,
small,
nav {
display: block;
}
body {
font-family: tahoma, verdana, arial, sans-serif;
font-size: 14px;
margin: 0;
padding: 0;
background: #ffffff;
text-transform: capitalize;
box-sizing: content-box;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
-o-box-sizing: content-box;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-weight: bold;
font-size: 14px;
}
/************************************************
----------- Header --------
*************************************************/
#body-wrap {
margin-top: 0;
padding-top: 0;
width: 600px;
margin-left: auto;
margin-right: auto;
}
header {
margin-top: 0;
height: 60px;
background: red url('Images/headerBG.gif') repeat;
}
header h3,
header ul {
padding-top: 0;
}
header h3 {
float: left;
padding-left: 21px;
color: #ffffff;
}
#social-links {
float: right;
padding: 0;
padding-right: 20px;
}
#social-links li {
display: inline-block;
list-style: none;
padding-left: 13px;
}
#social-links li a {
-moz-transition-duration: 1s;
-ms-transition-duration: 1s;
-webkit-transition-duration: 1s;
-o-transition-duration: 1s;
transition: 1s;
}
#social-links li a:hover {
-webkit-transform: scale(1.5);
-moz-transform: scale(1.5);
-ms-transform: scale(1.5);
-o-transform: scale(1.5);
transform: scale(1.5);
}
#social-links input[type=button] {
padding-right: 21px;
}
/************************************************
----------- nav --------
*************************************************/
nav {
clear: both;
height: 36px;
background: green url('Images/navBG.gif') repeat;
margin-bottom: 0;
}
nav ul {
list-style: none;
display: inline-block;
margin-top: 0;
margin-bottom: 0;
}
nav ul li {
padding-top: 10px;
}
#main-menu-ul {
margin-left: 0;
padding-left: 21px;
;
float: left;
}
#main-menu-ul li {
display: inline-block;
padding-left: 21px;
}
#main-menu-ul li a {
color: #FFFFFF;
text-decoration: none;
-moz-transition-duration: 1s;
-ms-transition-duration: 1s;
-webkit-transition-duration: 1s;
-o-transition-duration: 1s;
transition-duration: 1s;
}
#main-menu-ul li a:hover {
color: #8f8f8f;
-webkit-transform: scale(1.5);
-moz-transform: scale(1.5);
-ms-transform: scale(1.5);
-o-transform: scale(1.5);
transform: scale(1.5);
}
#search-ul {
float: right;
padding-right: 22px;
}
#search-ul li {
display: inline-block;
}
#search-ul input[type=text] {
width: 92px;
height: 14px;
}
#search-ul li label {
color: #FFFFFF;
}
#search-ul li input[type=button] {
padding: 0;
margin: 0;
font-size: 13px;
height: 20px;
margin-left: -4px;
}
/************************************************
----------- slider --------
*************************************************/
#slider {
clear: both;
margin-top: 0;
height: 236px;
background: gray url('Images/SliderBG.png') repeat-x;
overflow: hidden;
}
#slider-content-wrap {
clear: both;
width: 560px;
height: 154px;
margin-left: 20px;
margin-right: 20px;
background-color: #3493ff;
margin-top: 20px;
}
#inside-div-slider {
margin-top: 20px;
background-color: #ffe132;
height: 100px;
width: 360px;
}
&#13;
<body>
<div id="body-wrap">
<header>
<h3>SITE NAME </h3>
<ul id="social-links">
<li>
<a href="http://www.yahoo.com">
<img src="Images/fb.gif">
</a>
</li>
<li>
<a href="#">
<img src="Images/fb.gif">
</a>
</li>
<li>
<a href="#">
<img src="Images/fb.gif">
</a>
</li>
<li>
<a href="#">
<img src="Images/fb.gif">
</a>
</li>
</ul>
</header>
<nav>
<ul id="main-menu-ul">
<li><a href="#">Home</a>
</li>
<li><a href="#">About</a>
</li>
<li><a href="#">Portfolio</a>
</li>
<li><a href="#">Blog</a>
</li>
<li><a href="#">Contact</a>
</li>
</ul>
<ul id="search-ul">
<li>
<label>Search :
<input type="text" id="search-field">
</label>
</li>
<li>
<input type="button" value="Go!">
</li>
</ul>
</nav>
<!--********************Slider*******************-->
<section id="slider">
<div id="slider-content-wrap">
#slider-content-wrap
<div id="inside-div-slider">#inside-div-slider</div>
</div>
</section>
<!--********************Slider*******************-->
<section id="footer-section-1"></section>
<section id="footer-section-2"></section>
<footer></footer>
</div>
</body>
&#13;
或者第二种方法是将display: inline-block
应用于子元素,即#slider-Content-Wrap
,如下所示:
header,
section,
article,
small,
nav {
display: block;
}
body {
font-family: tahoma, verdana, arial, sans-serif;
font-size: 14px;
margin: 0;
padding: 0;
background: #ffffff;
text-transform: capitalize;
box-sizing: content-box;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
-o-box-sizing: content-box;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-weight: bold;
font-size: 14px;
}
/************************************************
----------- Header --------
*************************************************/
#body-wrap {
margin-top: 0;
padding-top: 0;
width: 600px;
margin-left: auto;
margin-right: auto;
}
header {
margin-top: 0;
height: 60px;
background: red url('Images/headerBG.gif') repeat;
}
header h3,
header ul {
padding-top: 0;
}
header h3 {
float: left;
padding-left: 21px;
color: #ffffff;
}
#social-links {
float: right;
padding: 0;
padding-right: 20px;
}
#social-links li {
display: inline-block;
list-style: none;
padding-left: 13px;
}
#social-links li a {
-moz-transition-duration: 1s;
-ms-transition-duration: 1s;
-webkit-transition-duration: 1s;
-o-transition-duration: 1s;
transition: 1s;
}
#social-links li a:hover {
-webkit-transform: scale(1.5);
-moz-transform: scale(1.5);
-ms-transform: scale(1.5);
-o-transform: scale(1.5);
transform: scale(1.5);
}
#social-links input[type=button] {
padding-right: 21px;
}
/************************************************
----------- nav --------
*************************************************/
nav {
clear: both;
height: 36px;
background: green url('Images/navBG.gif') repeat;
margin-bottom: 0;
}
nav ul {
list-style: none;
display: inline-block;
margin-top: 0;
margin-bottom: 0;
}
nav ul li {
padding-top: 10px;
}
#main-menu-ul {
margin-left: 0;
padding-left: 21px;
;
float: left;
}
#main-menu-ul li {
display: inline-block;
padding-left: 21px;
}
#main-menu-ul li a {
color: #FFFFFF;
text-decoration: none;
-moz-transition-duration: 1s;
-ms-transition-duration: 1s;
-webkit-transition-duration: 1s;
-o-transition-duration: 1s;
transition-duration: 1s;
}
#main-menu-ul li a:hover {
color: #8f8f8f;
-webkit-transform: scale(1.5);
-moz-transform: scale(1.5);
-ms-transform: scale(1.5);
-o-transform: scale(1.5);
transform: scale(1.5);
}
#search-ul {
float: right;
padding-right: 22px;
}
#search-ul li {
display: inline-block;
}
#search-ul input[type=text] {
width: 92px;
height: 14px;
}
#search-ul li label {
color: #FFFFFF;
}
#search-ul li input[type=button] {
padding: 0;
margin: 0;
font-size: 13px;
height: 20px;
margin-left: -4px;
}
/************************************************
----------- slider --------
*************************************************/
#slider {
clear: both;
margin-top: 0;
height: 236px;
background: gray url('Images/SliderBG.png') repeat-x;
}
#slider-content-wrap {
clear: both;
width: 560px;
height: 154px;
margin-left: 20px;
margin-right: 20px;
background-color: #3493ff;
display: inline-block;
margin-top: 20px;
}
#inside-div-slider {
margin-top: 20px;
background-color: #ffe132;
height: 100px;
width: 360px;
}
&#13;
<body>
<div id="body-wrap">
<header>
<h3>SITE NAME </h3>
<ul id="social-links">
<li>
<a href="http://www.yahoo.com">
<img src="Images/fb.gif">
</a>
</li>
<li>
<a href="#">
<img src="Images/fb.gif">
</a>
</li>
<li>
<a href="#">
<img src="Images/fb.gif">
</a>
</li>
<li>
<a href="#">
<img src="Images/fb.gif">
</a>
</li>
</ul>
</header>
<nav>
<ul id="main-menu-ul">
<li><a href="#">Home</a>
</li>
<li><a href="#">About</a>
</li>
<li><a href="#">Portfolio</a>
</li>
<li><a href="#">Blog</a>
</li>
<li><a href="#">Contact</a>
</li>
</ul>
<ul id="search-ul">
<li>
<label>Search :
<input type="text" id="search-field">
</label>
</li>
<li>
<input type="button" value="Go!">
</li>
</ul>
</nav>
<!--********************Slider*******************-->
<section id="slider">
<div id="slider-content-wrap">
#slider-content-wrap
<div id="inside-div-slider">#inside-div-slider</div>
</div>
</section>
<!--********************Slider*******************-->
<section id="footer-section-1"></section>
<section id="footer-section-2"></section>
<footer></footer>
</div>
</body>
&#13;
答案 2 :(得分:1)
<强>小提琴强>
<强> http://jsfiddle.net/9d8gq8r2/6/ 强>
#plider中的删除
clear: both;
margin-top: 0;
添加
position:absolute;
现在,如果您将margin-top
添加到#slider-content-wrap
,它就不会推送父
应该如下所示
#slider
{
height: auto;
background: gray url('Images/SliderBG.png') repeat-x;
position:absolute;
}
#slider-content-wrap{
width:560px;
height: 154px;
margin-left: 20px;
margin-right: 20px;
background-color: #3493ff;
margin-top: 20px//added
}
答案 3 :(得分:0)
margin-top不会“推送”父级,它会在您的示例中为子级和父级之间提供间距。孩子的填充顶部将增加孩子的顶部,然后将孩子内容推下来。