html - 当我们向文本添加上边距时,它会向 div 而不是文本添加边距

标签 html css reactjs jsx

<分区>

我在 reactjs 上有一个应用程序,当我向文本添加边距时,它会向 div 添加边距并且主 div 向下移动。 JSX 代码:

import React, {Component} from 'react';
import './Travel.css';
class Travel extends Component {
    render(){
            return(
                <div className='top'>
                    <div className='content'>
                        <span className='conth'>
                            <h1>A TRAVELS</h1>
                            <h1>YEARBOOK</h1>
                        </span>
                    </div>
                </div>
            );
    }
}
export default Travel;


CSS Code:
@import url(//fonts.googleapis.com/css?family=Open+Sans:800);
.top{
    height:789px;
    width: 100%;
    background-image: url('travel.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    z-index: -1;
}
.content h1{
    margin-top: 10px;
}
.conth{
    font-family: 'Open Sans';
    font-size: 43px;
    width: 500px;
    color: #ffffff;
}

请帮我解决这个问题。

最佳答案

您成为受害者的效果称为折叠边距。这是一个非常简单的例子:

body {
  min-height: 100vh;
  min-width: 100vw;
}

.outside {
  background-color: red;
  height: 300px;
  margin-top: 0;
  width: 400px;
}

.inside {
  background-color: orange;
  margin-top: 200px;
  height: 50%;
}
<body>
  <div class="outside">
    <div class="inside"></div>
  </div>
</body>

为防止这种情况,只需添加一个最小的 padding-top.outer:

body {
  min-height: 100vh;
  min-width: 100vw;
}

.outside {
  background-color: red;
  height: 300px;
  margin-top: 0;
  padding-top: 1px;
  width: 400px;
}

.inside {
  background-color: orange;
  margin-top: 200px;
  height: 50%;
}
<body>
  <div class="outside">
    <div class="inside"></div>
  </div>
</body>

CSS Tricks 有一篇关于此的不错的文章:

https://css-tricks.com/what-you-should-know-about-collapsing-margins/

关于html - 当我们向文本添加上边距时,它会向 div 而不是文本添加边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52171679/

上一篇:javascript - jQuery CSS 在 Ajax 加载后未触发 - 无法使用点击事件

下一篇:html - 垂直对齐中心不适用于 div 文本对齐

相关文章:

html - 调整菜单之间的差距

javascript - 无法关闭 JS 弹出窗口

reactjs - 将一行移动到顶部/底部或任何索引 ag-grid react

css - 试图在 div 中居中 asp imagebuttons

html - 如何让媒体查询/css 在 html 电子邮件中工作

javascript - 在网页上使用自定义字体的最新技术/最佳实践?

jquery - 让 css3 动画即使在悬停时也能播放?

javascript - 为什么我无法使用纯 JS 登录 Instagram?

javascript - 如何在 Electron 中使用 React Router?

jquery - 如何在不堆叠回调的情况下在 jQuery 中制作动画?