热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用类样式作为内联样式?-Useclassstylesasinlinestyles?

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 个解决方案

#1


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属性,您会怎么做?为了给那个特定的选择器命名,哪一个会成为主导?

#2


1  

I suggest you to use LESS.

我建议你用LESS。

You can then create so called "mixins" and use them in your styles. E.g.

然后,您可以创建所谓的“mixins”并在您的样式中使用它们。例如。

.rounded-corners (@radius: 5px) {
  border-radius: @radius;
  -webkit-border-radius: @radius;
  -moz-border-radius: @radius;
}

#header {
  .rounded-corners;
}
#footer {
  .rounded-corners(10px);
}

#3


1  

There has been a lot of discussion about what constitutes a good class name. Community consensus and the HTML spec itself suggest that your class names describe what the element is, not how it should look.

关于什么构成一个好的班级名称已经有很多讨论。社区共识和HTML规范本身表明您的类名称描述了元素是什么,而不是它应该是什么样子。

There are no additional restrictions on the tokens authors can use in the class attribute, but authors are encouraged to use values that describe the nature of the content, rather than values that describe the desired presentation of the content.

作者可以在class属性中使用的令牌没有其他限制,但鼓励作者使用描述内容性质的值,而不是描述内容所需表示的值。

Also see the Semantic HTML page on Wikipedia.

另请参阅Wikipedia上的语义HTML页面。

#4


1  

Imagine that you then decide to use the same HTML for your mobile website, but a different stylesheet. Let's say you have an article container that you've set to 800px wide. Rather than having .article {width: 800px} you'll have .width-800 {width: 400px}. How confused will you be when trying to write and debug your new layout?

想象一下,然后您决定为您的移动网站使用相同的HTML,但使用不同的样式表。假设您有一个文章容器,您将其设置为800px宽。而不是.article {width:800px}你将拥有.width-800 {width:400px}。在尝试编写和调试新布局时,您会感到困惑吗?

Also, maybe you don't want all elements that were 800px wide to now be 400px, just articles. So using your method, to target your article container, you'd have to chain together all the different classes that are used by it, e.g:

此外,也许你不希望800px宽的所有元素现在都是400px,只是文章。因此,使用您的方法来定位您的文章容器,您必须将它使用的所有不同类链接在一起,例如:

.float-left.border-solid-black.padding-10.bg-blue.font-arial.width-800 {
    width: 400px;
}

This much longer selector means that:

这个更长的选择器意味着:

  • It takes up more room (file-weight) in the CSS file, so slower page loads
  • 它在CSS文件中占用更多空间(文件权重),因此页面加载速度较慢

  • It's slower to parse nodes via XPath or Javascript
  • 通过XPath或Javascript解析节点的速度较慢

  • It's more brittle to changes, as you don't know if any other divs may be affected (if they happen to also have the same classes applied)
  • 它更易于更改,因为您不知道是否有任何其他div可能会受到影响(如果它们碰巧也应用了相同的类)

  • It's much harder to debug and isolate bug-fixes (not negatively impact other areas on the page by mistake)
  • 调试和隔离错误修复更加困难(不会对页面上的其他区域产生负面影响)

#5


0  

Generally you want to use classes to say what your text is, and then use CSS to say how such things should be displayed. This way if you decide you want your headings to have 0.5em of space beneath them instead of 0.3em of space, it's an easy thing (especially with a tool like FireBug).

通常,您希望使用类来说明文本是什么,然后使用CSS来说明应该如何显示这些内容。这样一来,如果你决定要让你的标题下面有0.5em的空间而不是0.3em的空间,这很容易(特别是像FireBug这样的工具)。

Using them the way you've indicated isn't taking advantage of this separation. It's brittle and has no additional functionality beyond just putting inline styles all over the place.

按照你指示的方式使用它们并没有利用这种分离。它很脆弱,除了在整个地方放置内联样式之外没有其它功能。

That said, you will usually have a few utility classes like this (e.g. .collapsed { display: none }) for toggling and such.

也就是说,你通常会有一些这样的实用程序类(例如.collapsed {display:none})用于切换等等。


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 标题: ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • 本文介绍了一款名为TimeSelector的Android日期时间选择器,采用了Material Design风格,可以在Android Studio中通过gradle添加依赖来使用,也可以在Eclipse中下载源码使用。文章详细介绍了TimeSelector的构造方法和参数说明,以及如何使用回调函数来处理选取时间后的操作。同时还提供了示例代码和可选的起始时间和结束时间设置。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • 本文总结了在编写JS代码时,不同浏览器间的兼容性差异,并提供了相应的解决方法。其中包括阻止默认事件的代码示例和猎取兄弟节点的函数。这些方法可以帮助开发者在不同浏览器上实现一致的功能。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • 本文介绍了Java的公式汇总及相关知识,包括定义变量的语法格式、类型转换公式、三元表达式、定义新的实例的格式、引用类型的方法以及数组静态初始化等内容。希望对读者有一定的参考价值。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • Sleuth+zipkin链路追踪SpringCloud微服务的解决方案
    在庞大的微服务群中,随着业务扩展,微服务个数增多,系统调用链路复杂化。Sleuth+zipkin是解决SpringCloud微服务定位和追踪的方案。通过TraceId将不同服务调用的日志串联起来,实现请求链路跟踪。通过Feign调用和Request传递TraceId,将整个调用链路的服务日志归组合并,提供定位和追踪的功能。 ... [详细]
author-avatar
mobiledu2502870743
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有