哪种方法更好?CSS类或ID?

2022-10-19前端开发问题
4

本文介绍了哪种方法更好?CSS类或ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

让我们考虑这两种编写相同代码的方式:

Let's consider these 2 ways of writing the same code:

方法一

<div id="header">
    <div id="user">
        <a id="userName">Username</a>
        <a id="userImage">Userimage</a>
    </div>
</div>

方法二

<div id="header">
    <div class="user">
        <a class="name">Username</a>
        <a class="image">Userimage</a>
    </div>
</div>

方法一的CSS

#userName { color: white; }
#userImage { height: 50px; width: 50px; }

方法2的CSS

#header div.user a.name { color: white; }
#header div.user a.image { height: 50px; width: 50px; }

在我看来,方法 2 更简洁,因为您永远不会得到像 userImageInnerBox 这样的 ID.现在从技术上讲,哪种方法最好,为什么?

It seems to me that Method 2 is cleaner, since you will never end up with IDs like userImageInnerBox. Now technically speaking which is the best method and why?

推荐答案

黄金法则是这样的:chrome 元素使用 id,content 元素使用 class.所以方法2更好.

The golden rules goes as this: use id for chrome elements, use class for content elements. So method 2 is the better.

您可以在 css-discuss 上阅读这篇文章以获得灵感:http://css-discuss.incutio.com/wiki/Classes_Vs_Ids

You can read this article on css-discuss for inspiration: http://css-discuss.incutio.com/wiki/Classes_Vs_Ids

没有什么可以阻止您在独特的内容元素上使用 id 属性,并且在某些情况下,它可能是加速 javascript DOM 遍历的好方法.然而,出于造型目的,它被许多人认为是不好的做法.

There is nothing that stops you from using id attributes on unique content elements, and in some cases it can be a nice way to speed up javascript DOM traversals. For styling purposes, however, it is considered by many as bad practice.

要考虑的要点是:

  1. 类可以用于多重继承,id必须是唯一的
  2. 如果您需要将继承与 id 样式配对使用,选择器的特殊性可能会成为一场噩梦

每当我在非 Chrome 元素上使用 id 属性时,它纯粹是为了快速访问 javascript,而不是为了样式.

Whenever I use id attributes on non-chrome elements it is purely for fast javascript access, and never for styling.

这篇关于哪种方法更好?CSS类或ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13