简体   繁体   English

CSS Div 填充

[英]CSS Div Padding

I am struggling with what I suspect is a very basic concept with CSS (which I am just now learning).我正在努力解决我怀疑是 CSS(我刚刚学习)的一个非常基本的概念。 As you can see from my code below I have a very basic div element and a css file that sets the height of the div, and attempts to set the padding.从下面的代码中可以看出,我有一个非常基本的 div 元素和一个 css 文件,用于设置 div 的高度,并尝试设置填充。 What I do not understand is why the text will not stay centered vertically in the middle of the div when the padding-top and the padding-bottom are the same (10px).我不明白为什么当 padding-top 和 padding-bottom 相同(10px)时,文本不会在 div 中间垂直居中。

<div id="div_header">
    <h1>Website TITLE</h1>
</div>

And here is the css:这是 css:

#div_header{
background:#E07A1A;
font-family: var(--global-font-family);
font-size: 1.6rem;
font-weight: 300;
color: var(--global-text-color);
height: 60px;
width: 100%;
padding-left: 20px;
Padding-top: 10px;
padding-bottom: 10px;
}

With the settings as shown, this is what it looks like:使用如图所示的设置,这就是它的样子:

在此处输入图像描述

What am I doing wrong?我究竟做错了什么?

By default, all text header tags have margin above and below them.默认情况下,所有文本 header 标签的上下都有边距。 The above margin is essentially pushing your text down within your container.上面的边距本质上是将您的文本向下推到容器中。 If you inspect the header in your browser's console this should be visible.如果您在浏览器的控制台中检查 header,这应该是可见的。

Add this snippet outside of your #div_header styles to fix it:在你的#div_header styles 之外添加这个片段来修复它:

h1 {
    margin: 0;
}

On a side note, HTML 5 introduced semantic tags including a header tag that might suit your needs better.另一方面,HTML 5 引入了语义标签,包括可能更适合您的需求的header标签。 For example:例如:

<header class="header">
    <h1 class="header__title">Website TITLE</h1>
</header>

Additionally, it is usually better to use classes instead of IDs as IDs will create an unnecessary degree of specificity , which could make it harder for you to overwrite, should you need to later on.此外,通常最好使用类而不是 ID,因为 ID 会产生不必要的特异性,如果您以后需要,这可能会使您更难覆盖。 Using the snippet above then, your CSS would look as so:然后使用上面的代码段,您的 CSS 将如下所示:

.header {
    background:#E07A1A;
    font-family: var(--global-font-family);
    font-size: 1.6rem;
    font-weight: 300;
    color: var(--global-text-color);
    height: 60px;
    width: 100%;
    padding-left: 20px;
    padding-top: 10px;
    padding-bottom: 10px;
}

.header__title {
    margin: 0;
}

There are many naming conventions for CSS, BEM is used above. CSS有很多命名约定,上面使用了BEM

This is due to the default margin rules for the h1 tag.这是由于h1标签的默认边距规则。 The margin needs to be overridden to 0, or set to display: inline (as an alternative).边距需要被覆盖为 0,或设置为display: inline (作为替代)。 Just add this to your css:只需将此添加到您的 css 中:

#div_header h1 {
    margin: 0; /* or display: inline */
}

 #div_header { background: #e07a1a; font-family: var(--global-font-family); font-size: 1.6rem; font-weight: 300; color: var(--global-text-color); height: 60px; width: 100%; padding-left: 20px; padding-top: 10px; padding-bottom: 10px; } #div_header h1 { margin: 0; }
 <div id="div_header"> <h1>Website TITLE</h1> </div>

This should work for you, use below code这应该适合你,使用下面的代码

 #div_header { background: #E07A1A; font-family: var(--global-font-family); font-size: 1.6rem; font-weight: 300; color: var(--global-text-color); height: 60px; width: 100%; display: flex; align-items: center; padding-left: 10px; }
 <div id="div_header"> <h1>Website TITLE</h1> </div>

Remember - Use minimal but good CSS.请记住 - 使用最少但很好的 CSS。

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

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