html – 当两者都可以工作时,在margin-top和margin-bottom之间的CSS最佳实践?

html – 当两者都可以工作时,在margin-top和margin-bottom之间的CSS最佳实践?,第1张

概述当您想要为放置在第一个元素之后的元素提供空间时,CSS最佳实践是什么? 假设这个HTML <div class="a-block">lorem</div><div class="another-block">ipsum</div> 你应该使用这个CSS吗? .a-block { margin-bottom: 10px; } 要么 .another-block { margin-top: 10px 当您想要为放置在第一个元素之后的元素提供空间时,CSS最佳实践是什么?

假设这个HTML

<div >lorem</div><div >ipsum</div>

你应该使用这个CSS吗?

.a-block { margin-bottom: 10px; }

要么

.another-block { margin-top: 10px; }

解决方法 我会使用 :nth-child宽度 margin-top

div:not(:first-child) {  margin-top: 10px}
<div >lorem</div><div >ipsum</div><div >ipsum</div><div >ipsum</div>
总结

以上是内存溢出为你收集整理的html – 当两者都可以工作时,在margin-top和margin-bottom之间的CSS最佳实践?全部内容,希望文章能够帮你解决html – 当两者都可以工作时,在margin-top和margin-bottom之间的CSS最佳实践?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1080401.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存