css - Set equal vertical margins to any block element at once
问题描述
By default, most browsers will add a top and bottom margin of 1em
to many block elements, like ol
, ul
, blockquote
, h1
, h2
, etc.
Is there any short and clever selector that can change theses vertical margins (let say to 1.45em
) all at once, without having to list every element in the CSS file?
Note: This question have been marked as a [duplicate.] But I am not trying to select all block elements by their properties (which I know is imposible in CSS), nor do I want to select all block elements by listing them one by one. I am concern in creating an equal vertical (top and bottom) margin in all elements that by default create a new line, by using the simplest selector posible.
解决方案
到目前为止我发现的最好方法是使用通用选择器:
:root {
--custom-vertical-margin: 1.5rem;
}
* {
margin-top: var(--custom-vertical-margin);
margin-bottom: var(--custom-vertical-margin);
}
尽管通用选择器也将定位内联元素,但它们不会也不会受到垂直边距的影响。
这完成了问题所在:一次在任何块元素上设置相等的垂直边距,而不必在 CSS 文件中列出每个元素[with display:block
property],也不必尝试通过它们的属性来定位元素(这在 CSS 中是不可能的)。
推荐阅读
- c# - 内联 If-Else 引发 System.ArgumentException:参数类型不匹配
- ios - TableViewCell 的固定高度
- css - 无法在 WordPress 中更改 div class="entry-content" 的字体大小
- javascript - 可以使用交叉点观察器在目标的特定偏移顶部计算/触发吗?
- xpath - 使用自定义字符串的 xpath 多节点查询
- python - 在 HTML 文件中显示数据库中的项目
- plugins - 如何使用telegraf在centos机器中推送进程运行状态?
- ios - 打印 uitextfield 时崩溃
- spring - 导入 org.springframework.boot.test 无法解决
- xcode - Xcode 11 字体问题(react-native)