gpt4 book ai didi

html - css 中的类与 ID(混淆)

转载 作者:搜寻专家 更新时间:2023-10-31 22:04:23 29 4
gpt4 key购买 nike

我知道我可能会问与其他人曾经问过的相同的问题,但我真的很困惑何时使用 ID 和类。我已经用谷歌搜索了它,也在这里阅读了一些关于 Stackoverflow 的问题,但仍然对此有疑问。我知道 ID 只能在一个页面中使用一次,而类可以在一个类中使用多个

下面是我练习过的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;
}

这里是 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>

我想我已经把这个 css 样式表搞得一团糟,因为我使用了大量的 ID。请伙计们,告诉我什么时候/如何使用 ID 和类?提前致谢!

编辑:这张照片怎么样?这就是您定义何时使用 ID 和 Class 的方式吗? alt text

最佳答案

我的理念:少用 id,多用类。

我的意思是只为需要它们的元素提供 id:表单元素、主要部分元素、唯一项

至于类,我尽量提供尽可能多的有用的描述性类。关键是要确保它们是一致的和语义化的。

如果您的 ID 或类中需要数字,您可能做错了。而不是标记列表中的每个列表项:

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

只需标记列表本身:

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

因为您可以使用列表 ID (#special-list) 后跟 :nth-child()li 来访问列表中的任何元素+ 李...

关于html - css 中的类与 ID(混淆),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4159358/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com