div的顶部div div

问题描述:

我真的很奇怪的问题。我想做矩形矩形,但不知道为什么,我不能设置内部矩形的填充顶部。有人可以告诉我为什么吗?div的顶部div div

#outsideRect{ 
 
    width: 260px; 
 
    height: 440px; 
 
    background-color: #4084dd; 
 
    border-radius: 10px; 
 
} 
 
#insideRect{ 
 
    width: 200px; 
 
    height: 60px; 
 
    background-color: ghostwhite; 
 
    border-radius: 10px; 
 
    margin: 50px auto; 
 

 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Calculator</title> 
 
    <link rel="stylesheet" href="css/style.css"> 
 
    
 
    <script src="scripts/jquery-3.1.0.js"></script> 
 
    <script src="scripts/script.js"></script> 
 
    
 
</head> 
 
<body> 
 
    
 
    <div id="page"> 
 
     <div id="outsideRect"> 
 
      <div id="insideRect"> 
 
       as 
 
      </div> 
 

 
     </div> 
 
    </div> 
 
    
 
</body> 
 
</html>

#outsideRect{ 
 
    width: 160px; 
 
    height: 440px; 
 
    background-color: #4084dd; 
 
    border-radius: 10px; 
 
} 
 
#insideRect{ 
 
    width: 100px; 
 
    height: 60px; 
 
    background-color: ghostwhite; 
 
    border-radius: 10px; 
 
    margin: 50px auto; 
 
} 
 

 
.page { display: inline-block; } 
 

 
#outsideRect2{ 
 
    width: 160px; 
 
    height: 440px; 
 
    background-color: #4084dd; 
 
    border-radius: 10px; 
 
    padding-top: 20px; 
 
} 
 
#insideRect2{ 
 
    width: 100px; 
 
    height: 60px; 
 
    background-color: ghostwhite; 
 
    border-radius: 10px; 
 
    margin: 0 auto; 
 
} 
 

 
#outsideRect3{ 
 
    width: 160px; 
 
    height: 440px; 
 
    background-color: #4084dd; 
 
    border-radius: 10px; 
 
} 
 
#insideRect3{ 
 
    width: 100px; 
 
    height: 60px; 
 
    background-color: ghostwhite; 
 
    border-radius: 10px; 
 
    margin: 0 auto; 
 
    padding-top: 20px; 
 
} 
 

 

 
#insideRect4{ 
 
    width: 100px; 
 
    height: 60px; 
 
    background-color: ghostwhite; 
 
    border-radius: 10px; 
 
    margin: 50px auto; 
 
} 
 

 

 
#insideRect5{ 
 
    width: 100px; 
 
    height: 60px; 
 
    background-color: ghostwhite; 
 
    border-radius: 10px; 
 
    margin: 0 auto; 
 
}
<div class="page"> 
 
     <div id="outsideRect"> 
 
      <div id="insideRect"> 
 
       margin on inner rect 
 
      </div> 
 

 
     </div> 
 
    </div> 
 

 
    <div class="page"> 
 
     <div id="outsideRect2"> 
 
      <div id="insideRect2"> 
 
       padding on outer rect 
 
      </div> 
 

 
     </div> 
 
    </div> 
 

 
    <div class="page"> 
 
     <div id="outsideRect3"> 
 
      <div id="insideRect3"> 
 
       padding on inner rect 
 
      </div> 
 

 
     </div> 
 
    </div> 
 

 

 

 
    <div class="page"> 
 
     <div id="outsideRect"> 
 
      <div id="insideRect4"> 
 
       margin on inner rect 
 
      </div> 
 
      
 
      
 
      <div id="insideRect5"> 
 
       no margin or padding on inner rect 
 
      </div> 
 

 
     </div> 
 
    </div>

填充总是内部的元素。保证金是外部的元素。

如果要内部矩形向下移动,您想要将填充添加到外部矩形。

或者你的意思是问你为什么你的内部矩形边缘不工作?它在块级元素之间的外部应用。由于内部矩形是外部矩形的子元素,因此边距不会在父/子之间得到结合。它会在父母之间得到应用。请注意,片段中的第4个示例中的margin如何应用于同一级别的其他元素,但不会在父级/子级之间应用。

+0

YAS,这就是解决方案:)但是为什么它在insideRect dosent与宣称的 “填充顶” 的作品? –

+0

因为填充是*里面的元素。因此,在内部矩形中添加“padding-top”可将“as”添加到内部矩形中。 – Scott

#outsideRect{ 
 
    width: 260px; 
 
    height: 440px; 
 
    background-color: #4084dd; 
 
    border-radius: 10px; 
 
} 
 
#insideRect{ 
 
    width: 200px; 
 
    height: 60px; 
 
    background-color: ghostwhite; 
 
    border-radius: 10px; 
 
    margin: 50px auto; 
 
    padding-top: 30px; 
 

 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Calculator</title> 
 
    <link rel="stylesheet" href="css/style.css"> 
 
    
 
    <script src="scripts/jquery-3.1.0.js"></script> 
 
    <script src="scripts/script.js"></script> 
 
    
 
</head> 
 
<body> 
 
    
 
    <div id="page"> 
 
     <div id="outsideRect"> 
 
      <div id="insideRect"> 
 
       as 
 
      </div> 
 

 
     </div> 
 
    </div> 
 
    
 
</body> 
 
</html>

解决方案中的填充,而不在您的解决方案的任何问题进行添加。

正如Scott所提到的,margin在元素之外,而padding在元素之内。

如果您想在解决方案中使用边距,您需要添加位置。

例如:

#outsideRect{ 
 
    width: 260px; 
 
    height: 440px; 
 
    background-color: #4084dd; 
 
    border-radius: 10px; 
 
} 
 
#insideRect{ 
 
    position: absolute; 
 
    width: 200px; 
 
    height: 60px; 
 
    background-color: ghostwhite; 
 
    border-radius: 10px; 
 
    margin: 50px auto; 
 

 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Calculator</title> 
 
    <link rel="stylesheet" href="css/style.css"> 
 
    
 
    <script src="scripts/jquery-3.1.0.js"></script> 
 
    <script src="scripts/script.js"></script> 
 
    
 
</head> 
 
<body> 
 
    
 
    <div id="page"> 
 
     <div id="outsideRect"> 
 
      <div id="insideRect"> 
 
       as 
 
      </div> 
 

 
     </div> 
 
    </div> 
 
    
 
</body> 
 
</html>