识别CSS的HTML元素的造型和定位 [英] Identifying html elements in CSS for styling and positioning

查看:164
本文介绍了识别CSS的HTML元素的造型和定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们能为文本框,文本区,按钮等采用类,ID等属性来识别元素提供的造型和定位的CSS。

I know we can provide styling and positioning in CSS for text box, text area, buttons etc. using "Class" , "Id" etc. attributes to identifying an element.

我的问题是:
我们如何决定何时使用一个特定的属性?
有没有使用一个比其他(类VS Id与除外)的任何优势?
为什么我们必须识别元素的多种方式?为什么没有一个标准的方法来识别,例如,可以说身份证?

My question is: How do we decide when to use a particular attribute? Is there any advantage of using one over other (class vs Id vs other)? Why do we have multiple ways of identifying elements? Why not have one standard way to identifying such as, lets say "Id"?

在此先感谢

推荐答案

您不能到处因为只有一个元素被允许有一个给定的ID使用ID,所以如果你想在同一样式应用到五个不同的元素,你必须创建五个不同的规则,这是毫无意义的。

You can't use ID everywhere because only one element is allowed to have a given ID, so if you want to apply the same style to five different elements, you'd have to create five different rules, which is pointless.

通常最好使用基线定义您希望某些元素类型出现的一般的(链接列表,表格等)。然后,如果你有,你想这些元素有不同的风格,应用类的元素,以确定那个地方(导航栏,例如),并使用自定义样式来修改该区域中元素的行为具体地方:

It's usually best to use a baseline definition for how you want certain element types to appear generally (links, lists, tables, etc.). Then if you have specific places where you want those elements to have different styles, apply a class to an element to identify that place ("navigation-bar", e.g.), and use custom styles to modify the behavior of elements within that area:

li {/* How you generally want list items to appear */}
.navigation-bar li {/* How you want list items to appear in the nav bar */}

由于这样的样式层叠,属性由第二个规则,因为第二个选择是更为具体将覆盖第一设置的属性设置。

Due to the way the styles "cascade", properties set by the second rule will override the properties set by the first because the second selector is more specific.

据的为什么的有可供选择元素多种方式,那是因为他们是pretty在一个点或另一个备受大家有用。 CSS有助于降低维护成本,因为你不必重复在一堆不同的地方相同的样式信息。它只能这样做,如果您能想出一个规则,它使你想要的元素搭配在各种不同的情况。

As far as why there are multiple ways available to select an element, it's because they're pretty much all useful at one point or another. CSS helps reduce maintenance costs because you don't have to repeat the same styling information in a bunch of different places. It can only do this if you can come up with a rule that makes it possible to match the elements you want in a variety of different situations.

这篇关于识别CSS的HTML元素的造型和定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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