css - div 内部的 padding-top

标签 css

我有一个很奇怪的问题。我想在矩形中做矩形,但不知道为什么,我无法设置 inside-rectangle 的 padding-top。谁能告诉我为什么?

#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>

内边距总是在元素的内部。边距在元素之外

如果您希望内部矩形向下移动,则需要向外部 矩形添加填充。

或者您的意思是问为什么内部矩形的边距不起作用?它应用在外部…… block 级元素之间。由于内部矩形是外部矩形的子项,因此边距不会在父项/子项之间联合。它在 parent 之间得到应用。请注意在代码片段的第 4 个示例中,边距如何应用于同一级别的其他元素,但它不会在父/子元素之间应用。

关于css - div 内部的 padding-top,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38984228/

相关文章:

javascript - 如果容器高度小于固定值,则隐藏元素

css - div容器适配屏幕高度

javascript - 防止 Internet Explorer 11 中的过度滚动

html - 拆分背景居中的内容

html - 如何更改 bootstrap css 文件中文件输入的背景?

javascript - 使用 HTML、CSS、javascript 如何在桌面和移动 View 中管理轮播 slider ?https ://i. stack.imgur.com/yzW5B.jpg

css - 有没有办法检查网页上正在使用或未使用哪些 CSS 样式?

html - 如何使用css设置相同的高度和宽度的图像?不是背景图片

css - CSS float 元素的奇怪行为

javascript - React - 向下滚动时向上滑动固定导航栏,向上滚动时向下滑动