Responsive Web Dedign(一)
freeCodeCamp —- Responsive Web Dedign
Learn HTML by Building a Cat Photo App
The
h1
throughh6
heading elements are used to signify the importance of content below them. The lower the number, the higher the importance, soh2
elements have less importance thanh1
elements. Only use oneh1
element per page and place lower importance headings below higher importance headings.The
p
element is used to create a paragraph of text on websites. Create ap
element below yourh2
element and give it the following text:See more cat photos in our gallery
Commenting allows you to leave messages without affecting the browser display. It also allows you to make code inactive. A comment in HTML starts with
<!--
, contains any number of lines of text, and ends with-->
. For example, the comment<!-- TODO: Remove h1 -->
contains the textTODO: Remove h1
.HTML5 has some elements that identify different content areas. These elements make your HTML easier to read and help with Search Engine Optimization (SEO) and accessibility.
Identify the main section of this page by adding a
<main>
opening tag before theh1
element, and a</main>
closing tag after thep
element.In the previous step, you put the
h1
,h2
, comment, andp
elements inside themain
element. This is called nesting. Nested elements should be placed two spaces further to the right of the element they are nested in. This spacing is called indentation and it is used to make HTML easier to read.You can add images to your website by using the
img
element.img
elements have an opening tag without a closing tag. A tag for an element without a closing tag is known as a self-closing tag.HTML attributes are special words used inside the opening tag of an element to control the element’s behavior. The
src
attribute in animg
element specifies the image’s URL (where the image is located).Here is an example of an
img
element with asrc
attribute pointing to the freeCodeCamp logo:1
<img src="https://cdn.freecodecamp.org/platform/universal/fcc_secondary.svg">
All
img
elements should have analt
attribute. Thealt
attribute’s text is used for screen readers to improve accessibility and is displayed if the image fails to load. For example,<img src="cat.jpg" alt="A cat">
has analt
attribute with the textA cat
.You can link to another page with the anchor (
a
) element. For example,<a href='https://freecodecamp.org'></a>
would link tofreecodecamp.org
.A link’s text must be placed between the opening and closing tags of an anchor (
a
) element. For example,<a href="https://www.freecodecamp.org">click here to go to freeCodeCamp.org</a>
is a link with the textclick here to go to freeCodeCamp.org
.In the previous step you turned the words
link to cat pictures
into a link by placing them between opening and closing anchor (a
) tags. You can do the same to words inside of an element, such as ap
element.In the previous step you turned the words
link to cat pictures
into a link by placing them between opening and closing anchor (a
) tags. You can do the same to words inside of an element, such as ap
element.Add a
target
attribute with the value_blank
to the anchor (a
) element’s opening tag, so that the link opens in a new tab.In previous steps you used an anchor element to turn text into a link. Other types of content can also be turned into a link by wrapping it in anchor tags.
Turn the image into a link by surrounding it with necessary element tags. Use
https://freecatphotoapp.com
as the anchor’shref
attribute value.Before adding any new content, you should make use of a
section
element to separate the cat photos content from the future content.Take your
h2
, comment,p
, and anchor (a
) elements and nest them in asection
element.It is time to add a new section. Add a second
section
element below the existingsection
element.Within the second
section
element, add a newh2
element with the textCat Lists
.When you add a lower rank heading element to the page, it’s implied that you’re starting a new subsection.
After the last
h2
element of the secondsection
element, add anh3
element with this text:Things cats love:
After the
h3
element with theThings cats love:
text, add an unordered list (ul
) element. Note that nothing will be displayed at this point.Use list item (
li
) elements to create items in a list. Here is an example of list items in an unordered list:1
2
3
4<ul>
<li>milk</li>
<li>cheese</li>
</ul>After the unordered list, add a new image with an
src
attribute value set to:https://cdn.freecodecamp.org/curriculum/cat-photo-app/lasagna.jpg
And its
alt
attribute value to:A slice of lasagna on a plate.
The
figure
element represents self-contained content and will allow you to associate an image with a caption.Nest the image you just added within afigure
element.A figure caption (
figcaption
) element is used to add a caption to describe the image contained within thefigure
element. For example,<figcaption>A cute cat</figcaption>
adds the captionA cute cat
.After the image nested in the
figure
element, add afigcaption
element with text set to:Cats love lasagna.
Emphasize the word
love
in thefigcaption
element by wrapping it in an emphasisem
element.After the
figure
element, add anotherh3
element with the text:Top 3 things cats hate:
The code for an ordered list (
ol
) is similar to an unordered list, but list items in an ordered list are numbered when displayed.After the second
section
element’s lasth3
element, add an ordered list with these three list items:flea treatment
thunder
other cats
After the ordered list, add another
figure
element.Inside the
figure
element you just added, nest animg
element with asrc
attribute set tohttps://cdn.freecodecamp.org/curriculum/cat-photo-app/cats.jpg
.To improve accessibility of the image you added, add an
alt
attribute with the text:Five cats looking around a field.
After the last
img
element, add afigcaption
element with the textCats hate other cats.
The
strong
element is used to indicate that some text is of strong importance or urgent.In thefigcaption
you just added, indicate thathate
is of strong importance by wrapping it in astrong
element.It is time to add a new section. Add a third
section
element below the secondsection
element.Inside the third
section
element, add anh2
element with the text:Cat Form
Now you will add a web form to collect information from users.
After the
Cat Form
heading, add aform
element.The
action
attribute indicates where form data should be sent. For example,<form action="/submit-url"></form>
tells the browser that the form data should be sent to the path/submit-url
.Add an
action
attribute with the valuehttps://freecatphotoapp.com/submit-cat-photo
to theform
element.The
input
element allows you several ways to collect data from a web form. Likeimg
elements,input
elements are self-closing and do not need closing tags.Nest aninput
element in theform
element.There are many kinds of inputs you can create using the
type
attribute. You can easily create a password field, reset button, or a control to let users select a file from their computer.Create a text field to get text input from a user by adding the
type
attribute with the valuetext
to theinput
element.In order for a form’s data to be accessed by the location specified in the
action
attribute, you must give the text field aname
attribute and assign it a value to represent the data being submitted. For example, you could use the following syntax for an email address text field:<input type="text" name="email">
.Add the
name
attribute with the valuecatphotourl
to your text field.Placeholder text is used to give people a hint about what kind of information to enter into an input. For example,
<input type="text" placeholder="Email address">
.Add the placeholder textcat photo URL
to yourinput
element.To prevent a user from submitting your form when required information is missing, you need to add the
required
attribute to aninput
element. There’s no need to set a value to therequired
attribute. Instead, just add the wordrequired
to theinput
element, making sure there is space between it and other attributes.Use the
button
element to create a clickable button. For example,<button>Click Here</button>
creates a button with the textClick Here
.Add a
button
element with the textSubmit
below theinput
element. The default behavior of clicking a form button without any attributes submits the form to the location specified in the form’saction
attribute.Even though you added your button below the text input, they appear next to each other on the page. That’s because both
input
andbutton
elements are inline elements, which don’t appear on new lines.The button you added will submit the form by default. However, relying on default behavior may cause confusion. Add the
type
attribute with the valuesubmit
to thebutton
to make it clear that it is a submit button.You can use radio buttons for questions where you want only one answer out of multiple options.
Here is an example of a radio button with the option of
cat
:<input type="radio"> cat
. Remember thatinput
elements are self-closing.Before the text input, add a radio button with the option set as:
Indoor
label
elements are used to help associate the text for aninput
element with theinput
element itself (especially for assistive technologies like screen readers). For example,<label><input type="radio"> cat</label>
makes it so clicking the wordcat
also selects the corresponding radio button.Nest your
radio
button inside alabel
element.The
id
attribute is used to identify specific HTML elements. Eachid
attribute’s value must be unique from all otherid
values for the entire page.Add an
id
attribute with the valueindoor
to the radio button. When elements have multiple attributes, the order of the attributes doesn’t matter.Create another radio button below the first one. Nest it inside a
label
element withOutdoor
as thelabel
text. Give the radio button anid
attribute withoutdoor
as the value.Notice that both radio buttons can be selected at the same time. To make it so selecting one radio button automatically deselects the other, both buttons must have a
name
attribute with the same value.Add the
name
attribute with the valueindoor-outdoor
to both radio buttons.If you select the
Indoor
radio button and submit the form, the form data for the button is based on itsname
andvalue
attributes. Since your radio buttons do not have avalue
attribute, the form data will includeindoor-outdoor=on
, which is not useful when you have multiple buttons.Add a
value
attribute to both radio buttons. For convenience, set the button’svalue
attribute to the same value as itsid
attribute.The
fieldset
element is used to group related inputs and labels together in a web form.fieldset
elements are block-level elements, meaning that they appear on a new line.Nest the
Indoor
andOutdoor
radio buttons within afieldset
element, and don’t forget to indent the radio buttons.The
legend
element acts as a caption for the content in thefieldset
element. It gives users context about what they should enter into that part of the form.Add a
legend
element with the textIs your cat an indoor or outdoor cat?
above both of the radio buttons.Next, you are going to add some new form
input
elements, so add anotherfieldset
element directly below the currentfieldset
element.Add a
legend
element with the textWhat's your cat's personality?
inside the secondfieldset
element.Forms commonly use checkboxes for questions that may have more than one answer. For example, here’s a checkbox with the option of
tacos
:<input type="checkbox"> tacos
.Under the
legend
element you just added, add aninput
with itstype
attribute set tocheckbox
and give it the option of:Loving
Add an
id
attribute with the valueloving
to the checkbox input.There’s another way to associate an
input
element’s text with the element itself. You can nest the text within alabel
element and add afor
attribute with the same value as theinput
element’sid
attribute.Associate the text
Loving
with the checkbox by nesting only the textLoving
in alabel
element and giving it an appropriatefor
attribute.Add the
name
attribute with the valuepersonality
to the checkboxinput
element.While you won’t notice this in the browser, doing this makes it easier for a server to process your web form, especially when there are multiple checkboxes.
Add another checkbox after the one you just added. The
id
attribute value should belazy
and thename
attribute value should be the same as the last checkbox.Also add a
label
element to the right of the new checkbox with the textLazy
. Make sure to associate thelabel
element with the new checkbox using thefor
attribute.Add a final checkbox after the previous one with an
id
attribute value ofenergetic
. Thename
attribute should be the same as the previous checkbox.Also add a
label
element to the right of the new checkbox with textEnergetic
. Make sure to associate thelabel
element with the new checkbox.Like radio buttons, form data for selected checkboxes are
name
/value
attribute pairs. While thevalue
attribute is optional, it’s best practice to include it with any checkboxes or radio buttons on the page.Add a
value
attribute to each checkbox. For convenience, set each checkbox’svalue
attribute to the same value as itsid
attribute.In order to make a checkbox checked or radio button selected by default, you need to add the
checked
attribute to it. There’s no need to set a value to thechecked
attribute. Instead, just add the wordchecked
to theinput
element, making sure there is space between it and other attributes.Make the first radio button and the first checkbox selected by default.
Now you will add a footer section to the page.
After the
main
element, add afooter
element.Nest a
p
element with the textNo Copyright - freeCodeCamp.org
within thefooter
element.Make the text
freeCodeCamp.org
into a link by enclosing it in an anchor (a
) element. Thehref
attribute should be set tohttps://www.freecodecamp.org
.Notice that everything you’ve added to the page so far is inside the
body
element. All page content elements that should be rendered to the page go inside thebody
element. However, other important information goes inside thehead
element.Add a
head
element above thebody
element.The
title
element determines what browsers show in the title bar or tab for the page.Add a
title
element within thehead
element using the text below:CatPhotoApp
Notice that the entire contents of the page are nested within an
html
element. All other elements must be descendants of thishtml
element.Add the
lang
attribute with the valueen
to the openinghtml
tag to specify that the language of the page is English.All pages should begin with
<!DOCTYPE html>
. This special string is known as a declaration and ensures the browser tries to meet industry-wide specifications.Add this declaration as the first line of the code.
You can set browser behavior by adding self-closing
meta
elements in thehead
. Here’s an example:1
<meta attribute="value">
Tell the browser to parse the markup into multiple languages by creating a
meta
element as a child of thehead
element. Set itscharset
attribute toUTF-8
.
1 | <!DOCTYPE html> |
CatPhotoApp
Cat Photos
See more cat photos in our gallery.

Cat Lists
Things cats love:
- cat nip
- laser pointers
- lasagna

Top 3 things cats hate:
- flea treatment
- thunder
- other cats
