简体   繁体   English

css中的类与ID(混淆)

[英]Class vs. ID in css (confusion)

i know i might be asking the same question as other used to ask but i'm really confused with when to use ID and class. 我知道我可能会问其他人曾经问过同样的问题,但我真的很困惑何时使用ID和类。 I have googled about it and read some posted questions on Stackoverflow here as well but still have doubt about it. 我已经在Google上搜索过,并在此处阅读了有关Stackoverflow的一些已发布的问题,但仍有疑问。 I understand that ID can only be used once in a page and class can be use more than one within a class 我知道ID只能在一个页面中使用一次,而类可以在一个类中使用多个

The below here is CSS style sheet i've practiced on: 下面是我练习的CSS样式表:

html,body{
    padding:0px;
    margin:0px;
    height:100%;
    background-color:#E9E9E9;
}
.infoBoxPad{
    background-color:#DFDFDF;
    width:990px;
    height:19px;
    border:solid thin #CCC;
    margin:auto;
    display:block;
}
#info1{
    float:left;
    width:125px;
    height:16px;
    line-height:15px;
    border-right:thin solid #999;
    padding-left:20px;
    padding-right:20px;
    display:block;
}
#info2{
    float:left;
    width:236px;
    height:15px;
    line-height:15px;
    border-right:thin solid #999;
    padding-left:20px;
    padding-right:20px;
    display:block;
}
#info2-link{
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
    color:#000;
    margin-left:11px;
    margin-right:11px;
    float:left;
    text-decoration:none;
}
#info2-link:hover{
    text-decoration:underline;
    color:#03F;
}
#info3{
    float:left;
    width:183px;
    height:16px;
    line-height:15px;
    padding-left:20px;
    padding-right:20px;
    display:block;
}
#info3-link{
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
    color:#000;
    margin-right:15px;
    float:left;
    text-decoration:none;
}
#info3-link:hover{
    text-decoration:underline;
    color:#03F;
}
#info-text-bold{
    float:left;
    font-family:Arial, Helvetica, sans-serif;
    font-size:13px;
    font-weight:600;
    padding-right:20px;
    color:#000;
    text-decoration:none;
}
#info-text-bold1{
    float:left;
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
    font-weight:600;
    padding-right:20px;
    color:#000;
    text-decoration:none;
}
#info-text-bold1:hover{
    text-decoration:underline;
    color:#03F;
}
#info1-novice{
    color:#09F;
    text-decoration:none;
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
    font-weight:600;
}
#info1-novice:hover{
    text-decoration:underline;
}
.header{
    background-color:#FFF;
    width:990px;
    height:80px;
    position:relative;
    display:block;
    margin:auto;
}
#header-logo{
    margin-left:10px;
    margin-top:20px;
}
.nav-bar-wrapper{
    background-image:url(nav-bar.gif);
    background-color:#333;
    width:990px;
    height:40px;
    margin:auto;
}
#nav-bar{
    margin:0;
    padding:0;
}
#nav-bar ul{
    margin:0 0 0 20px;
    padding:0;
}
#nav-bar li{
    margin:0;
    padding:0;
    list-style:none;
    float:left;
    position:relative;
}
#nav-bar ul li a{
    text-align:center;
    font-family:Arial, Helvetica, sans-serif;
    font-size:14px;
    font-weight:600;
    text-decoration:none;
    color:#FFF;
    width:105px;
    height:40px;
    display:block;
    line-height:35px;
    border-right:1px solid #FFF;
}
#nav-bar ul li:hover{
    background-color:#666;
    height:35px;
}
.content{
    width:990px;
    height:1000px;
    background-color:#FFF;
    position:relative;
    margin:auto;
}
#earn-point{
    background-image:url(earnNow.gif);
    width:300px;
    height:60px;
}
#content-earnpoint{
    background-color:#fffbe8;
    width:278px;
    height:100px;
    border:solid thin #F90;
    display:block;
    padding:10px;
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
}
/**************************************************************************************************************Left Column*/
#leftcolumn{
    background-color:#FFF;
    width:300px;
    margin:25px 10px 0px 10px;
    float:left;
}
#guessing-game{
    width:278px;
    height:37px;
    margin-top:25px;
}
#guessing-game-image{
    float:left;
    margin-right:5px;
}
#guessing-game-text{
    float:left;
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
}
#guessing-game-text1{
    color:#00C;
    text-decoration:none;
    font-weight:600;
}
#guessing-game-text1:hover{
    text-decoration:underline;
}
/***********************************************************************************************************Right Column*/
#rightcolumn{
    background-color:#FFF;
    width:640px;
    margin:25px 10px 0px 0px;
    float:left;
}
#advert{
    width:640px;
    height:80px;
    margin-bottom:10px;
}
#earn-reward{
    background-color:#FC3;
    width:640px;
    height:16px;
    padding:5px;
    font-family:Arial, Helvetica, sans-serif;
    font-size:18px;
    line-height:14px;
}
#total-reward-points{
    background-image:url(total%20reward%20points-background.gif);
    width:268px;
    height:184px;
    margin:20px 0px 0px 0px;
    padding:9px 15px 0px 15px;
    border:1px thin solid;
}
#image-counter-heading{
    margin:5px 10px 10px 0px;
    display:inline;
}
#total-update-counter{
    background-color:#F00;
    width:264px;
    height:33px;
    margin:0px;
    padding:8px 0px 5px 0px;
}
#homecounter-heading-wrapper{
    width:268px;
    height:48px;
    margin:17px 0px 0px 0px;
    display:block;
}
#homecounter-heading{
    float:left;
}
#online-shopping{
    width:650px;
    height:30px;
    margin-bottom:5px;
    padding-top:15px;
    padding-left:10px;
    display:block;
}
#onlineshopping-logo{
    margin-top:5px;
}

And html here: 和html在这里:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Smile City</title>
<link href="smilecity style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div class="infoBoxPad">
<!--info1-->
<div id="info1"><div id="info-text-bold">Jesso</div>
<img src="novice.gif" />&nbsp;&nbsp;<a href="#" id="info1-novice">Novice</a></div>
<!--info2-->
<div id="info2">
<a href="#" id="info2-link">Points:118</a>
<a href="#" id="info2-link">Entries:29</a>
<a href="#" id="info2-link">Exp:108</a>
</div>
<!--info3-->
<div id="info3">
<a href="#" id="info3-link">Account Setting</a>
<a href="#" id="info-text-bold1">Sign Out</a>
</div>
</div><!--End infoBoxPad-->

<div class="header">
<img src="smilecity logo.gif" id="header-logo" />
</div><!--End header-->

<div class="nav-bar-wrapper">

<div id="nav-bar">

<ul class="nav-bar">
<li><a href="#">Home</a></li>
</ul>

<ul class="nav-bar">
<li><a href="#">Join</a></li>
</ul>

<ul class="nav-bar">
<li><a href="#">Earn</a></li>
</ul>


<ul class="nav-bar">
<li><a href="#">Redeem</a></li>
</ul>


<ul class="nav-bar">
<li><a href="#">Account</a></li>
</ul>

<ul class="nav-bar">
<li><a href="#">Help</a></li>
</ul>

</div><!--End nav-bar-->
</div><!--End nav-bar-wrapper-->

<div class="content">
<!--
-->

<div id="leftcolumn">
<div id="earn-point"></div>
<div id="content-earnpoint">
<strong>Hi Jesse, </strong>here's what you can do to earn points right now:
<div id="guessing-game">
<img src="gameYellowBg.gif" id="guessing-game-image" />
<a href="#" id="guessing-game-text1">Play the Guessing Game</a> and you could win up to 105,000 points.
</div>
</div><!--End Content-earnpoint-->

<div id="total-reward-points">
<img src="counterHeading1.gif" id="image-counter-heading" />
<div id="total-update-counter"></div>

<div id="homecounter-heading-wrapper">
<img src="homeCounterHeading2.gif" id="homecounter-heading" />
<img src="checkSml_sc.jpg" id="homecounter-heading" />
</div>

</div><!--End Total reward points-->

</div><!--End Left column-->

<div id="rightcolumn">
<div id="advert"><img src="ads.jpg" /></div>

<div id="earn-reward">
<strong>Earn Rewards</strong>
</div>

<div id="online-shopping">

<img src="onlineShoppingHome.gif" id="onlineshopping-logo" />

</div><!--End Online-shopping-->

</div><!--End Right Column-->

</div><!--End Content-->

</body>
</html>

I think i've made a mess in this css style sheet there since i've use heaps of IDs. 我认为我已经在这个css样式表中弄得一团糟,因为我已经使用了大量的ID。 Please guys, tell me when/how to use ID and class? 请大家,告诉我何时/如何使用ID和课程? Thanks in advance! 提前致谢!

Edit:How about this picture? 编辑:这张照片怎么样? is that how you define when to use ID and Class? 你是如何定义何时使用ID和Class的? 替代文字

My philosophy: use id's a little, use classes a lot. 我的理念:使用id一点,使用很多类。

What I mean by that is only give id's to elements that need them: form elements, primary sectioning elements, unique items 我的意思是只给出需要它们的元素的id:表单元素,主要部分元素,唯一的项目

As for classes, I try to provide as many useful descriptive classes as possible. 至于类,我尝试提供尽可能多的有用的描述性类。 The key is to make sure they're consistent and semantic. 关键是要确保它们是一致的和语义的。

If you need numbers in your id's or classes, you're probably doing it wrong. 如果你的id或类中需要数字,那么你可能做错了。 Instead of labeling every list item within a list: 而不是标记列表中的每个列表项:

<ul>
  <li id="item-1"></li>
  <li id="item-2"></li>
  ...
</ul>

just label the list itself: 只是标记列表本身:

<ul id="special-list">
  <li></li>
  <li></li>
  ...
</ul>

as you can access any item within the list using the list id ( #special-list ) followed by :nth-child() , or li + li... 因为您可以使用列表ID( #special-list )访问列表中的任何项目,然后是:nth-child()li + li...

Class: a number of persons or things regarded as forming a group by reason of common attributes, characteristics, qualities, or traits; 阶级:由于共同属性,特征,品质或特征而被视为组成群体的一些人或事物; kind; 类;

ID: IDentification. ID:身份识别。

class = multiple instances to be styled. class =要设置样式的多个实例。

id = unique instance to be styled (once per document). id =要设置样式的唯一实例(每个文档一次)。

I tend to use IDs for positioning or in case I will need to select a specific element through javascript. 我倾向于使用ID进行定位,或者我需要通过javascript选择特定元素。 Classes are more for applying styles to things you will reuse over and over. 类更适用于将样式应用于您将重复使用的事物。

In your case things like the header and navigation should be an ID. 在你的情况下,标题和导航之类的东西应该是一个ID。 Also be sure not to name a class the same as an ID, as in your nav-bar: 另外,请确保不要像在导航栏中那样将类命名为ID:

<div id="nav-bar">

<ul class="nav-bar">

An ID refers to a specific element, while CLASS refers to a type of element. ID指的是特定元素,而CLASS指的是一种元素。

When your CSS says 当你的CSS说

#searchBox{...}

You are saying there will be one item called with the ID of "searchBox" and this is how you want it styled. 你说有一个叫ID为“searchBox”的项目,这就是你想要它的样式。

<div id="searchBox">Search Box Content</div>

When your CSS says 当你的CSS说

.redBorder{...}

That means you will have multiple entities, that all are of the class "redBorder" 这意味着你将有多个实体,所有实体都属于“redBorder”类

<div id="item1" class="redBorder">Content1</div>
<div id="item2" class="redBorder">Content2</div>
<div id="item3" class="redBorder">Content3</div>

instead of 代替

#info1{
    float:left;
    width:125px;
    height:16px;
    line-height:15px;
    border-right:thin solid #999;
    padding-left:20px;
    padding-right:20px;
    display:block;
}
#info2{
    float:left;
    width:236px;
    height:15px;
    line-height:15px;
    border-right:thin solid #999;
    padding-left:20px;
    padding-right:20px;
    display:block;
}

<div id="info1">...</div>
<div id="info2">...</div>

try 尝试

.info{
    float:left;
    line-height:15px;
    border-right:thin solid #999;
    padding-left:20px;
    padding-right:20px;
    display:block;
}
#info1{
    width:125px;
    height:16px;
}
#info2{
    width:236px;
    height:15px;
}

<div id="info1" class="info">...</div>
<div id="info2" class="info">...</div>

IDs should always be unique, classes don't need to be. ID应始终是唯一的,不需要类。 Simple as that. 就那么简单。 Anything else is preference. 其他任何东西都是偏好。

Having said that, I think it looks neater to use child selectors as much as possible. 话虽如此,我认为尽可能多地使用儿童选择器。 So instead of .info2-link use #info2 a . 因此,而不是.info2-link使用#info2 a That way you don't need to add classes to everything. 这样你就不需要为所有东西添加类了。

You are not supposed to have multiple IDs. 您不应该有多个ID。 Basically when in doubt just use class. 基本上当有疑问时只需使用课程。 IDs are good for specific elements that you need to access. ID适用于您需要访问的特定元素。 So it would be like a parent type of element where theres no html inside. 所以它就像一个父类型的元素,里面没有html。 At least thats what i do. 至少那就是我做的。

ID's are faster (with rendering) mind you. ID更快(有渲染)提醒你。 Use an ID whenever it's a unique element with unique styling. 只要它是具有独特样式的独特元素,就使用ID。 If you want to apply the exact same styling somewhere else as well, make it a class so that it only needs to be loaded once. 如果您想在其他地方应用完全相同的样式,请将其设为类,以便只需加载一次。

An ID is unique where as the word "class" applies to a group of something in every definition of the word so use it as such. ID是唯一的,因为单词“class”适用于单词的每个定义中的一组内容,因此请使用它。 It's a repeatable pattern in CSS. 它是CSS中可重复的模式。 An ID identifies a unique object that doesn't appear anywhere else on the page where as a class is just something you can slap onto anything. ID标识一个唯一的对象,该对象不会出现在页面的任何其他位置,因为类只是可以打到任何东西上的东西。

I think of classes as the framework, and id's as the finishing touches. 我认为类是框架,而id是最后的接触。 Classes can be recycled over and over again, but id's are usable only once (I think most browsers let you use them multiple times though, which isn't very helpful). 类可以一遍又一遍地循环使用,但id只能使用一次(我想大多数浏览器允许你多次使用它们,这不是很有帮助)。 I mainly use classes to organize my elements: 我主要使用类来组织我的元素:

.red
{
  color: rgb(255, 0, 0);
}

.underline
{
  text-decoration: underline;
}

#floatRight
{
  float: right;
  clear: both;
  padding: 30px;
}

#redBorder
{
  border: 2px solid rgb(255, 0, 0);
}

This would be valid CSS, as expected. 正如预期的那样,这将是有效的CSS。 The cool thing is that you can use classes multiple times inside of an element, but id's are single-use: 很酷的是你可以元素中多次使用类,但id是单用的:

<span id="floatRight" class="red underline">Hello!</span>

This would produce a red block of text that's underlined and floating to the right. 这将生成一个带有下划线并向右浮动的红色文本块。

<span id="floatRight redBorder" class="red underline">Hello!</span>

This shouldn't make a red border, as id's are unique to a single element (JavaScript uses that to find elements). 不应该是一个红色边框,因为id是单个元素所独有的(JavaScript使用它来查找元素)。

Id's are like license plates: they identify certain cars. Id就像牌照:它们识别某些车辆。 It would be useless to let people use identical license plates for multiple cars, so id's are the same way. 让人们为多辆车使用相同的车牌是没用的,所以id也是一样的。 You use them uniquely. 你唯一地​​使用它们。

I'm not sure if that explains it... 我不确定这是否解释了......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM