Responsive Web Dedign(一)
freeCodeCamp —- Responsive Web Dedign
Learn HTML by Building a Cat Photo App
The
h1throughh6heading elements are used to signify the importance of content below them. The lower the number, the higher the importance, soh2elements have less importance thanh1elements. Only use oneh1element per page and place lower importance headings below higher importance headings.The
pelement is used to create a paragraph of text on websites. Create apelement below yourh2element and give it the following text:See more cat photos in our galleryCommenting 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 theh1element, and a</main>closing tag after thepelement.In the previous step, you put the
h1,h2, comment, andpelements inside themainelement. 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
imgelement.imgelements 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
srcattribute in animgelement specifies the image’s URL (where the image is located).Here is an example of an
imgelement with asrcattribute pointing to the freeCodeCamp logo:1
<img src="https://cdn.freecodecamp.org/platform/universal/fcc_secondary.svg">
All
imgelements should have analtattribute. Thealtattribute’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 analtattribute 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 picturesinto 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 apelement.In the previous step you turned the words
link to cat picturesinto 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 apelement.Add a
targetattribute with the value_blankto 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.comas the anchor’shrefattribute value.Before adding any new content, you should make use of a
sectionelement to separate the cat photos content from the future content.Take your
h2, comment,p, and anchor (a) elements and nest them in asectionelement.It is time to add a new section. Add a second
sectionelement below the existingsectionelement.Within the second
sectionelement, add a newh2element 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
h2element of the secondsectionelement, add anh3element with this text:Things cats love:After the
h3element 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
srcattribute value set to:https://cdn.freecodecamp.org/curriculum/cat-photo-app/lasagna.jpgAnd its
altattribute value to:A slice of lasagna on a plate.The
figureelement represents self-contained content and will allow you to associate an image with a caption.Nest the image you just added within afigureelement.A figure caption (
figcaption) element is used to add a caption to describe the image contained within thefigureelement. For example,<figcaption>A cute cat</figcaption>adds the captionA cute cat.After the image nested in the
figureelement, add afigcaptionelement with text set to:Cats love lasagna.Emphasize the word
lovein thefigcaptionelement by wrapping it in an emphasisemelement.After the
figureelement, add anotherh3element 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
sectionelement’s lasth3element, add an ordered list with these three list items:flea treatmentthunderother catsAfter the ordered list, add another
figureelement.Inside the
figureelement you just added, nest animgelement with asrcattribute set tohttps://cdn.freecodecamp.org/curriculum/cat-photo-app/cats.jpg.To improve accessibility of the image you added, add an
altattribute with the text:Five cats looking around a field.After the last
imgelement, add afigcaptionelement with the textCats hate other cats.The
strongelement is used to indicate that some text is of strong importance or urgent.In thefigcaptionyou just added, indicate thathateis of strong importance by wrapping it in astrongelement.It is time to add a new section. Add a third
sectionelement below the secondsectionelement.Inside the third
sectionelement, add anh2element with the text:Cat FormNow you will add a web form to collect information from users.
After the
Cat Formheading, add aformelement.The
actionattribute 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
actionattribute with the valuehttps://freecatphotoapp.com/submit-cat-phototo theformelement.The
inputelement allows you several ways to collect data from a web form. Likeimgelements,inputelements are self-closing and do not need closing tags.Nest aninputelement in theformelement.There are many kinds of inputs you can create using the
typeattribute. 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
typeattribute with the valuetextto theinputelement.In order for a form’s data to be accessed by the location specified in the
actionattribute, you must give the text field anameattribute 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
nameattribute with the valuecatphotourlto 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 URLto yourinputelement.To prevent a user from submitting your form when required information is missing, you need to add the
requiredattribute to aninputelement. There’s no need to set a value to therequiredattribute. Instead, just add the wordrequiredto theinputelement, making sure there is space between it and other attributes.Use the
buttonelement to create a clickable button. For example,<button>Click Here</button>creates a button with the textClick Here.Add a
buttonelement with the textSubmitbelow theinputelement. The default behavior of clicking a form button without any attributes submits the form to the location specified in the form’sactionattribute.Even though you added your button below the text input, they appear next to each other on the page. That’s because both
inputandbuttonelements 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
typeattribute with the valuesubmitto thebuttonto 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 thatinputelements are self-closing.Before the text input, add a radio button with the option set as:
Indoorlabelelements are used to help associate the text for aninputelement with theinputelement itself (especially for assistive technologies like screen readers). For example,<label><input type="radio"> cat</label>makes it so clicking the wordcatalso selects the corresponding radio button.Nest your
radiobutton inside alabelelement.The
idattribute is used to identify specific HTML elements. Eachidattribute’s value must be unique from all otheridvalues for the entire page.Add an
idattribute with the valueindoorto 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
labelelement withOutdooras thelabeltext. Give the radio button anidattribute withoutdooras 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
nameattribute with the same value.Add the
nameattribute with the valueindoor-outdoorto both radio buttons.If you select the
Indoorradio button and submit the form, the form data for the button is based on itsnameandvalueattributes. Since your radio buttons do not have avalueattribute, the form data will includeindoor-outdoor=on, which is not useful when you have multiple buttons.Add a
valueattribute to both radio buttons. For convenience, set the button’svalueattribute to the same value as itsidattribute.The
fieldsetelement is used to group related inputs and labels together in a web form.fieldsetelements are block-level elements, meaning that they appear on a new line.Nest the
IndoorandOutdoorradio buttons within afieldsetelement, and don’t forget to indent the radio buttons.The
legendelement acts as a caption for the content in thefieldsetelement. It gives users context about what they should enter into that part of the form.Add a
legendelement 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
inputelements, so add anotherfieldsetelement directly below the currentfieldsetelement.Add a
legendelement with the textWhat's your cat's personality?inside the secondfieldsetelement.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
legendelement you just added, add aninputwith itstypeattribute set tocheckboxand give it the option of:LovingAdd an
idattribute with the valuelovingto the checkbox input.There’s another way to associate an
inputelement’s text with the element itself. You can nest the text within alabelelement and add aforattribute with the same value as theinputelement’sidattribute.Associate the text
Lovingwith the checkbox by nesting only the textLovingin alabelelement and giving it an appropriateforattribute.Add the
nameattribute with the valuepersonalityto the checkboxinputelement.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
idattribute value should belazyand thenameattribute value should be the same as the last checkbox.Also add a
labelelement to the right of the new checkbox with the textLazy. Make sure to associate thelabelelement with the new checkbox using theforattribute.Add a final checkbox after the previous one with an
idattribute value ofenergetic. Thenameattribute should be the same as the previous checkbox.Also add a
labelelement to the right of the new checkbox with textEnergetic. Make sure to associate thelabelelement with the new checkbox.Like radio buttons, form data for selected checkboxes are
name/valueattribute pairs. While thevalueattribute is optional, it’s best practice to include it with any checkboxes or radio buttons on the page.Add a
valueattribute to each checkbox. For convenience, set each checkbox’svalueattribute to the same value as itsidattribute.In order to make a checkbox checked or radio button selected by default, you need to add the
checkedattribute to it. There’s no need to set a value to thecheckedattribute. Instead, just add the wordcheckedto theinputelement, 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
mainelement, add afooterelement.Nest a
pelement with the textNo Copyright - freeCodeCamp.orgwithin thefooterelement.Make the text
freeCodeCamp.orginto a link by enclosing it in an anchor (a) element. Thehrefattribute should be set tohttps://www.freecodecamp.org.Notice that everything you’ve added to the page so far is inside the
bodyelement. All page content elements that should be rendered to the page go inside thebodyelement. However, other important information goes inside theheadelement.Add a
headelement above thebodyelement.The
titleelement determines what browsers show in the title bar or tab for the page.Add a
titleelement within theheadelement using the text below:CatPhotoAppNotice that the entire contents of the page are nested within an
htmlelement. All other elements must be descendants of thishtmlelement.Add the
langattribute with the valueento the openinghtmltag 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
metaelements in thehead. Here’s an example:1
<meta attribute="value">
Tell the browser to parse the markup into multiple languages by creating a
metaelement as a child of theheadelement. Set itscharsetattribute 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