You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: curriculum/challenges/english/blocks/lecture-understanding-html-attributes/6708143cab2b583ecd3324f5.md
+51-7Lines changed: 51 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ challengeType: 19
5
5
dashedName: what-are-attributes
6
6
---
7
7
8
-
# --description--
8
+
# --interactive--
9
9
10
10
An attribute is a value placed inside the opening tag of an HTML element. Attributes provide additional information about the element or specify how the element should behave. Here is the basic syntax for an attribute:
11
11
@@ -15,27 +15,71 @@ An attribute is a value placed inside the opening tag of an HTML element. Attrib
15
15
16
16
The attribute name is followed by an equal sign (`=`) and a value in quotes. The value can be a string or a number, depending on the attribute.
17
17
18
-
Let's take a look at a few examples of common HTML attributes. The first example is the `href` attribute, which is used to specify the URL of a link:
18
+
This first example uses the `href` and `target` attributes. The `href` attribute specifies the URL of a link and the `target` attribute specifies where to open the link.
19
+
20
+
Change the `href="https://www.freecodecamp.org/news/"` to `href="https://www.freecodecamp.org"`. Now when you click on the link in the interactive editor, you will see the freeCodeCamp homepage in a new browser tab.
Without this attribute, the link would not work because there would be no destination URL. So you must include this `href` attribute to make the link functional. Other common attributes are the `src`, or source, and `alt`, or alternative, attribute - which is used to specify the source of an image and provide alternative descriptive text for the image, respectively:
28
+
:::
29
+
30
+
Without the `href` attribute, the link would not work because there would be no destination URL. So you must include this `href` attribute to make the link functional. The `target="__blank"` enables the link to open in a new browser tab. You will learn more about the `target` attribute in future lessons.
31
+
32
+
Other common attributes are the `src`, and `alt`, or alternative, attribute - which is used to specify the source of an image and provide alternative descriptive text for the image, respectively.
33
+
34
+
Change the `src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/cats.jpg"` to `src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/running-cats.jpg"`. Then change the `alt="Two tabby kittens sleeping together on a couch."` to `alt="Two cats running in the dirt."`.
35
+
36
+
:::interactive_editor
25
37
26
38
```html
27
-
<imgsrc="image.jpg"alt="A beautiful image" />
39
+
<imgsrc="https://cdn.freecodecamp.org/curriculum/cat-photo-app/cats.jpg"alt="Two tabby kittens sleeping together on a couch." />
28
40
```
29
41
42
+
:::
43
+
30
44
Similar to the `href` attribute, the `src` attribute is required because it specifies the image file to be displayed. The `alt` attribute is not required, but it is recommended for accessibility purposes. Accessibility means making sure that everyone, including those with disabilities, can use and understand things like websites, apps, and physical spaces. You will learn more about accessibility in the upcoming lessons.
31
45
32
-
Some attributes are a little unique with their syntax like the `checked` attribute shown here:
46
+
Some attributes are a little unique with their syntax like the `checked` attribute.
47
+
48
+
Try clicking on the checkbox in the preview window to see it alternate between a checked and unchecked state.
49
+
50
+
:::interactive_editor
33
51
34
52
```html
35
53
<inputtype="checkbox"checked />
36
54
```
37
55
38
-
In the following example, we have an `input` element with the `type` attribute set to `checkbox`. Inputs are used to collect data from users, and the `type` attribute specifies the type of input. In this case, this input is a checkbox. You will learn more about how inputs work in the upcoming lessons. The `checked` attribute is used to specify that the checkbox should be checked by default. The `checked` attribute does not require a value. If it is present, the checkbox will be checked by default. If the attribute is not present, the checkbox will be unchecked. This is known as a boolean attribute. You will learn more about booleans in general when you get to the JavaScript section. There are several common boolean attributes you will encounter in HTML, such as `disabled`, `readonly`, and `required`. These attributes are used to specify the state of an element, such as whether it is disabled, read-only, or required.
56
+
:::
57
+
58
+
In the following example, we have an `input` element with the `type` attribute set to `checkbox`. Inputs are used to collect data from users, and the `type` attribute specifies the type of input. In this case, this input is a checkbox. You will learn more about how inputs work in the upcoming lessons.
59
+
60
+
The `checked` attribute is used to specify that the checkbox should be checked by default. The `checked` attribute does not require a value. If it is present, the checkbox will be checked by default. If the attribute is not present, the checkbox will be unchecked. This is known as a boolean attribute. You will learn more about booleans in general when you get to the JavaScript section.
61
+
62
+
Remove the `checked` attribute from the `input` in the interactive editor, and you will see that the checkbox is no longer checked by default.
63
+
64
+
:::interactive_editor
65
+
66
+
```html
67
+
<inputtype="checkbox"checked />
68
+
```
69
+
70
+
:::
71
+
72
+
There are several common boolean attributes you will encounter in HTML, such as `disabled`, `readonly`, and `required`. These attributes are used to specify the state of an element, such as whether it is disabled, read-only, or required.
73
+
74
+
Here is an example of a text `input` element that is disabled by default. Try clicking on the `input` element in the preview window. Now remove the `disabled` attribute from the `input` element and you will see that the `input` is no longer disabled by default. You should now be able to click on it and type inside the field.
75
+
76
+
:::interactive_editor
77
+
78
+
```html
79
+
<inputtype="text"disabled>
80
+
```
81
+
82
+
:::
39
83
40
84
HTML has many attributes that can be used to customize the behavior and appearance of elements on a webpage. Understanding how to use attributes is essential for creating interactive and accessible web content. Over the next few lessons, you will learn about more HTML attributes and how to use them effectively in your web development projects.
0 commit comments