css - 如何将两个css类优化为一个
问题描述
我有一个用 react 制作的项目,我想优化 css。
我有这个代码:
.class-1 {
margin-top: 15px;
}
.class-2 {
margin-bottom: 15px;
}
在构建过程中是否有可能进行这样的优化?
.class-3 {
margin: 15px 0 15px 0;
}
解决方案
You can add both classes to your HTML element or combine the margins into one class like this:
.class1 {
margin-top: 15px;
background-color: skyblue;
}
.class2 {
margin-bottom: 15px;
}
.class3 {
margin: 15px 0;
background-color: red;
}
<div class='class1 class2'>My element with 2 classes</div>
<div class='class3'>My element with 1 class</div>
It all depends if you want to keep the classes separate for other areas of your code, or if you will never need those margins in separate classes.
推荐阅读
- javascript - 滚动页面后自定义光标位置错误
- javascript - 为什么我们不经常在 react-native 中使用继承?
- java - 方法:获取文本文件并将其存储在二维数组中
- javascript - 如何删除/禁用javascript中的功能?
- r - 历史 GTFS 数据
- ecmascript-6 - 使用数组解构/不确定如何
- unetstack - 使用 unetsack 代理将新属性添加到 Subnero 研究版调制解调器中
- flutter - 构造函数中的最终初始化错误
- html - 如何在我的 TextEdit/Mac 中的 Index.html 上添加我的 html 代码。我在 index.html 中的内容不允许我进行任何编辑
- javascript - MouseEvent.mozPressure 已弃用。改用 PointerEvent.pressure