简体   繁体   English

孩子的边距不尊重父 div

[英]Child's margin not respecting the parent div

I have made a div that encloses another div inside it.我制作了一个 div,其中包含另一个 div。 Following is the JSX code for it.以下是它的 JSX 代码。

<div className="header-container">
  <div className="title">
    Make a List
  </div>
<div>

Following is the CSS以下是 CSS

.header-container{
  height: 50px;
  width: 100%;
  background-color: black;
 }



.title{
   color: white;
   margin: 20px;
 }

Following is the result以下是结果

在此处输入图像描述

Why is the black div moving down after applying a margin to the inside div?为什么在对内部 div 应用边距后黑色 div 会向下移动? The margin should be from the parent div but it's not following that?边距应该来自父 div,但它没有遵循?

That's just how margins work - if your child has a margin so large that it reaches outside the parent, it will "push" the parent away from the top.这就是边距的工作原理 - 如果您的孩子的边距如此之大以至于超出了父级,它会将父级“推”离顶部。

Give the parent some padding, and then the margin will have something to push against.给父母一些填充,然后边距会有一些东西可以推动。

Generally if you want to move something away from its siblings, use margin, if you want it to move away from the edges of its parents, use padding.通常,如果您想将某些东西从其兄弟姐妹中移开,请使用边距,如果您希望它远离其父母的边缘,请使用填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM