Class vs id in css (confusion)

I know that I can ask the same question as others who ask, but I'm really confused about when to use the ID and class. I tried this and read some published questions about Stackoverflow here, but still doubt it. I understand that an identifier can only be used once per page and a class can use more than one class

The following is the CSS stylesheet on which I practiced:

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 the html here:

<!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 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 made a mess in this CSS stylesheet since I use heaps of identifiers. Please guys tell me when / how to use id and class? Thanks in advance!

Edit: how about this picture? is how you determine when to use id and class? alt text

+3
8

: , .

, , - , : , ,

, . , .

id , , , . :

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

:

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

, (#special-list), :nth-child() li + li...

+5

: , , , ; ;

ID: .

class= .

id = ( ).

+2

, javascript. - , .

, , ID. , ID, :

<div id="nav-bar">

<ul class="nav-bar">
+1

, CLASS .

CSS

#searchBox{...}

, "searchBox", .

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

CSS

.redBorder{...}

, , "redBorder"

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

#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>

.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>
+1

, . . - .

, , . .info2-link #info2 a. , .

+1

. , , . , . , , html. , , .

+1

, id - . , id ( , , ). :

.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);
}

CSS, . , , id :

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

, .

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

, id (JavaScript ).

: . , , id . .

, ...

0

ID ( ) . , . , , .

An identifier is unique when the word "class" refers to a group of something in each definition of a word, so use it as such. This is a repeatable pattern in CSS. The identifier identifies a unique object that is not displayed anywhere on the page where, as a class, you can simply pounce on something.

0
source

Source: https://habr.com/ru/post/1774400/


All Articles