2021-06-15 16:21:20 +02:00
---
id: bad87eee1348bd9aede07836
2021-06-27 23:51:13 +05:30
title: Definir o id de um elemento
2021-06-15 16:21:20 +02:00
challengeType: 0
videoUrl: 'https://scrimba.com/c/cN6MEc2'
forumTopicId: 18279
dashedName: set-the-id-of-an-element
---
# --description--
2021-06-27 23:51:13 +05:30
Além das classes, cada elemento HTML também pode ter um atributo `id` .
2021-06-15 16:21:20 +02:00
2021-06-27 23:51:13 +05:30
Existem vários benefícios em usar atributos `id` : você pode usar um `id` para estilizar um único elemento como também pode selecionar e modificar elementos específicos com JavaScript (você aprenderá mais sobre isso depois).
2021-06-15 16:21:20 +02:00
2021-06-27 23:51:13 +05:30
Os atributos `id` devem possuir valores únicos. Os navegadores não exigem que você faça isso, mas é uma prática recomendada e amplamente aceita. Portanto, não dê a mais de um elemento o mesmo atributo `id` .
2021-06-15 16:21:20 +02:00
2021-06-27 23:51:13 +05:30
Aqui está um exemplo de como você dá a um elemento `h2` o id de `cat-photo-app` :
2021-06-15 16:21:20 +02:00
```html
< h2 id = "cat-photo-app" >
```
# --instructions--
2021-06-27 23:51:13 +05:30
Dê ao elemento `form` o id `cat-photo-form` .
2021-06-15 16:21:20 +02:00
# --hints--
2021-06-27 23:51:13 +05:30
O elemento `form` deve ter o atributo id com o valor `cat-photo-form` .
2021-06-15 16:21:20 +02:00
```js
assert($('form').attr('id') === 'cat-photo-form');
```
# --seed--
## --seed-contents--
```html
< link href = "https://fonts.googleapis.com/css?family=Lobster" rel = "stylesheet" type = "text/css" >
< style >
.red-text {
color: red;
}
h2 {
font-family: Lobster, monospace;
}
p {
font-size: 16px;
font-family: monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
.smaller-image {
width: 100px;
}
.silver-background {
background-color: silver;
}
< / style >
< h2 class = "red-text" > CatPhotoApp< / h2 >
< main >
< p class = "red-text" > Click here to view more < a href = "#" > cat photos< / a > .< / p >
2021-09-22 08:34:59 -07:00
< a href = "#" > < img class = "smaller-image thick-green-border" src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt = "A cute orange cat lying on its back." > < / a >
2021-06-15 16:21:20 +02:00
< div class = "silver-background" >
< p > Things cats love:< / p >
< ul >
< li > cat nip< / li >
< li > laser pointers< / li >
< li > lasagna< / li >
< / ul >
< p > Top 3 things cats hate:< / p >
< ol >
< li > flea treatment< / li >
< li > thunder< / li >
< li > other cats< / li >
< / ol >
< / div >
< form action = "https://freecatphotoapp.com/submit-cat-photo" >
< label > < input type = "radio" name = "indoor-outdoor" checked > Indoor< / label >
< label > < input type = "radio" name = "indoor-outdoor" > Outdoor< / label > < br >
< label > < input type = "checkbox" name = "personality" checked > Loving< / label >
< label > < input type = "checkbox" name = "personality" > Lazy< / label >
< label > < input type = "checkbox" name = "personality" > Energetic< / label > < br >
< input type = "text" placeholder = "cat photo URL" required >
< button type = "submit" > Submit< / button >
< / form >
< / main >
```
# --solutions--
```html
< link href = "https://fonts.googleapis.com/css?family=Lobster" rel = "stylesheet" type = "text/css" >
< style >
.red-text {
color: red;
}
h2 {
font-family: Lobster, monospace;
}
p {
font-size: 16px;
font-family: monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
.smaller-image {
width: 100px;
}
.silver-background {
background-color: silver;
}
< / style >
< h2 class = "red-text" > CatPhotoApp< / h2 >
< main >
< p class = "red-text" > Click here to view more < a href = "#" > cat photos< / a > .< / p >
2021-06-27 23:51:13 +05:30
2021-09-22 08:34:59 -07:00
< a href = "#" > < img class = "smaller-image thick-green-border" src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt = "A cute orange cat lying on its back." > < / a >
2021-06-27 23:51:13 +05:30
2021-06-15 16:21:20 +02:00
< div class = "silver-background" >
< p > Things cats love:< / p >
< ul >
< li > cat nip< / li >
< li > laser pointers< / li >
< li > lasagna< / li >
< / ul >
< p > Top 3 things cats hate:< / p >
< ol >
< li > flea treatment< / li >
< li > thunder< / li >
< li > other cats< / li >
< / ol >
< / div >
2021-06-27 23:51:13 +05:30
2021-06-15 16:21:20 +02:00
< form action = "https://freecatphotoapp.com/submit-cat-photo" id = "cat-photo-form" >
< label > < input type = "radio" name = "indoor-outdoor" checked > Indoor< / label >
< label > < input type = "radio" name = "indoor-outdoor" > Outdoor< / label > < br >
< label > < input type = "checkbox" name = "personality" checked > Loving< / label >
< label > < input type = "checkbox" name = "personality" > Lazy< / label >
< label > < input type = "checkbox" name = "personality" > Energetic< / label > < br >
< input type = "text" placeholder = "cat photo URL" required >
< button type = "submit" > Submit< / button >
< / form >
< / main >
```