css - Margin-Top 不适用于 span 元素?

谁能告诉我我的编码错误吗?一切正常,唯一的问题是顶部没有边距。

HTML:

<div id="contact_us"> <!-- BEGIN CONTACT US -->
  <span class="first_title">Contact</span>
  <span class="second_title">Us</span>
  <p class="content">For any questions whatsoever please contact us through the following e-mail address:</p></br></br>
  <p class="e-mail">info@e-mail.com</p></br></br></br></br>
  <p class="read_more"><a href="underconstruction.html">Read More</a></p>
</div> <!-- END CONTACT US -->

CSS:

span.first_title {
  margin-top: 20px;
  margin-left: 12px;
  font-weight: bold;
  font-size: 24px;
  color: #221461;
}

span.second_title {
  margin-top: 20px;
  font-weight: bold;
  font-size: 24px;
  color: #b8b2d4;
}   

最佳答案

div 不同,p 1 是 Block Level可以在所有方面占用 margin 的元素,span2 不能,因为它是 Inline仅水平占据边距的元素。

来自 specification :

Margin properties specify the width of the margin area of a box. The 'margin' shorthand property sets the margin for all four sides while the other margin properties only set their respective side. These properties apply to all elements, but vertical margins will not have any effect on non-replaced inline elements.

Demo 1 (垂直 ​​margin 未应用,因为 spaninline 元素)

解决方案?制作你的 span 元素,display: inline-block;display: block;.

Demo 2

建议您使用 display: inline-block;,因为它将是 inline 以及 block。仅将其设置为 block 将导致您的元素呈现 on another line , 因为 block 级别元素占用页面上 100% 的水平空间,除非它们是 inline-block 或者它们是 floated leftright


1. Block Level Elements - MDN 来源

2。 Inline Elements - MDN 资源

https://stackoverflow.com/questions/11700985/

相关文章:

css - 如何将 div 与其父级的顶部对齐但保持其内联 block 行为?

html - 我可以将 CSS 样式应用于元素名称吗?

html - 如何仅使用 CSS/HTML 隐藏图像损坏的图标?

html - 为什么我的列表项元素符号与 float 元素重叠

javascript - 如何替换 div 元素中的文本?

html - 为什么我的 CSS3 媒体查询在移动设备上不起作用?

html - 您如何确定网站建立在什么技术之上?

html - Flexbox vs Twitter Bootstrap(或类似框架)

html - 如何在 Markdown 表中编写列表?

html - Force to open "Save As..."popup open at tex