CSS class и id
5 отговора
0 прегледа
Re: CSS class и id
Разликата е, че всяко id, което задаваш в дадения html документ трябва да има различна стойност за всеки елемент, на който е дефинирано. Това не важи за class.
Да приемем, че в един html документ имаш следния div:
<div id="MyDiv">текст, намиращ се в този div</div>
Това означава, че ако дефинираш id на други елементи, то трябва да е с различна стойност, т.е. да не се повтарят еднакви id-та. Докато при class можеш да зададеш на колкото си искаш елементи един и същ class. Например: class="ImportantText"
Понятно?
------------------------------------------
Да приемем, че в един html документ имаш следния div:
<div id="MyDiv">текст, намиращ се в този div</div>
Това означава, че ако дефинираш id на други елементи, то трябва да е с различна стойност, т.е. да не се повтарят еднакви id-та. Докато при class можеш да зададеш на колкото си искаш елементи един и същ class. Например: class="ImportantText"
Понятно?
------------------------------------------
Редактирано от The giver and the taker на
май не схванах
това си работи идеално
<html>
<head>
<style type="text/css">
#center
{
text-align:center;
}
p#center
{
text-align:center;
color:red;
}
</style>
</head>
<body>
<h1 id="center">This heading will not be affected</h1>
<p id="center">This paragraph will be center-aligned.</p>
<p id="center">This paragraph will be center-aligned. ohohoho</p>
<div id="center">This paragraph will be center-aligned too.
alabala</div>
</body>
</html>
като заменя id с class, резултатът е същия
<html>
<head>
<style type="text/css">
.center
{
text-align:center;
}
p.center
{
text-align:center;
color:red;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be center-aligned.</p>
<p class="center">This paragraph will be center-aligned. ohohoho</p>
<div class="center">This paragraph will be center-aligned too.
alabala</div>
</body>
</html>
<html>
<head>
<style type="text/css">
#center
{
text-align:center;
}
p#center
{
text-align:center;
color:red;
}
</style>
</head>
<body>
<h1 id="center">This heading will not be affected</h1>
<p id="center">This paragraph will be center-aligned.</p>
<p id="center">This paragraph will be center-aligned. ohohoho</p>
<div id="center">This paragraph will be center-aligned too.
alabala</div>
</body>
</html>
като заменя id с class, резултатът е същия
<html>
<head>
<style type="text/css">
.center
{
text-align:center;
}
p.center
{
text-align:center;
color:red;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be center-aligned.</p>
<p class="center">This paragraph will be center-aligned. ohohoho</p>
<div class="center">This paragraph will be center-aligned too.
alabala</div>
</body>
</html>
Re: май не схванах
Така е по спецификация. Т.е. това което си постнал, ако се опиташ да го валидираш, няма да мине.
Цинична поезия
Цинична поезия
Re: май не схванах
NikiFin, и двата кода работят еднакво, но горният е грешен според стандартите на World Wide Web Consortium(W3C). Препоръчително е да се съобразяваш със стандартите, установени от тях.
------------------------------------------
------------------------------------------