CSS声明-元素类型和ID还是仅ID? [英] CSS declaration - element type and ID or just ID?

查看:68
本文介绍了CSS声明-元素类型和ID还是仅ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何使用CSS声明?

How should I use CSS declaration?

这种方式:

div#main_content {
    width: 900px;
    border: 1px solid #CCC;
    margin: 20px 0 20px 0;
    padding: 40px;
    overflow: hidden;
}

或这种方式:

#main_content {
    width: 900px;
    border: 1px solid #CCC;
    margin: 20px 0 20px 0;
    padding: 40px;
    overflow: hidden;
}

在sublime text 2中,sublime linter package建议我不使用 div -仅#main_content,但在某些教程中我看到使用div#main_content更好,因为它更易于解析。

In sublime text 2, sublime linter package suggested me to use without the div - only #main_content, but on some tutorial I saw that it is better to use div#main_content as it is easier to parse.

您建议我使用什么? (我认为在这种情况下,类的结果将是相同的)。

What do you suggest I should use? (I think result will be the same in this case for the classes)

推荐答案

最好只使用id。页面上应该只有一个具有给定ID的元素,因此该元素类型是多余的。

Better to just use the id. There should only be one element on the page with a given id, so the element type is redundant.

错过元素类型的另一个原因是,如果您决定更改HTML(也许更改< div> 到更语义的内容,例如< p> < section> ),CSS选择器仍会匹配。

Another reason to miss off the element type is that if you decide to change your HTML (maybe changing the <div> to something more semantic like a <p> or <section>) the CSS selector will still match.

这篇关于CSS声明-元素类型和ID还是仅ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆