作者:mobiledu2502870743 | 来源:互联网 | 2023-05-18 04:22
Soaprettybasicquestion:isitokaytouseclasseslikeinlinestyles?所以这是一个非常基本的问题:使用内联样式这样的类
So a pretty basic question: is it "okay" to use classes like inline styles?
所以这是一个非常基本的问题:使用内联样式这样的类是否“没问题”?
Basically what I've done is build a "bootstrap"; but instead of doing so by element, I've done it by property. So basically, the CSS is nothing but a set of pre-defined styles.
基本上我所做的就是建立一个“引导程序”;但是我没有通过元素这样做,而是通过属性完成的。所以基本上,CSS只不过是一组预定义的样式。
For example:
.margin-left-1 {margin-left:1px;}
.margin-right-1 {margin-right:1px;}
.bg-blue {background-color:blue;}
.padding-5 {padding: 5px;}
.radius-10 {border-radius:10px;}
.width-10 {width:10%;}
.width-10px {width:10px;}
.font-arial {font-family: 'Arial';}
etc.
This makes it so you can quickly & easily build the website and its styles right in the HTML. However, is this a bad practice? Should I simply have styles for things like border-radius, colors, buttons, etc and leave the padding, margin, etc. (general box layout properties) defined within classes/id's?
这使得您可以在HTML中快速轻松地构建网站及其样式。但是,这是一种不好的做法吗?我应该简单地为border-radius,colors,buttons等等设置样式,并保留在classes / id中定义的填充,边距等(一般框布局属性)?
5 个解决方案
2
Its the exact opposite of what you are supposed to do. CSS selector names should nor represent selector content but rather the nature of the object they are supposed to describe (style). In other words selector names should be as abstract as possible in regards to how much they depend on the selector content itself.
它与你应该做的完全相反。 CSS选择器名称也应该代表选择器内容,而不是它们应该描述的对象(样式)的性质。换句话说,选择器名称应该尽可能抽象,因为它们依赖于选择器内容本身。
Here is a practical problem which you will run into if you decide to use your system. What if you decide to change the value of your margin-right
attribute for the .margin-right-1
class? Following your logic you would now have to change its name to lets say margin-right-10
and not only that but you would also be forced to update your complete HTML code in order to replace references to the margin-right-1
with margin-right-10
class.
如果您决定使用系统,这将是一个实际问题。如果您决定更改.margin-right-1类的margin-right属性值,该怎么办?按照你的逻辑,你现在必须改变它的名字,让我们说margin-right-10,不仅如此,你还将被迫更新你的完整HTML代码,以便用margin-替换margin-right-1的引用。右10班。
However if you define a class selector named, lets say, .wrapper
you could redefine its attributes later however you like.
但是,如果你定义了一个名为的选择器,比如说.wrapper,你可以随后重新定义它的属性。
Not only that but what will you do if your selector also has a margin-left
attribute? Which one would become dominant in order to give name to that particular selector?
不仅如此,如果您的选择器还具有margin-left属性,您会怎么做?为了给那个特定的选择器命名,哪一个会成为主导?