Step 31
Moving on to the final
fieldset
. What if you wanted to allow a user to upload a profile picture?Well, the
input
typefile
allows just that. Add alabel
with the textUpload a profile picture:
, and add aninput
accepting a file upload.<fieldset> <label>Upload a profile picture:<input type="file"></label> </fieldset>
Step 32
Add another
label
after the first, with the textInput your age (years):
. Then, nest aninput
with thetype
ofnumber
.Next, add a
min
attribute to theinput
with a value of13
because users under the age of 13 should not register. Also, users probably will not be over the age of 120; add amax
attribute with a value of120
.Now, if someone tries to submit the form with values outside of the range, a warning will appear, and the form will not submit. Give it a try.
<fieldset> <label>Upload a profile picture: <input type="file" /></label> <label>Input your age (years): <input type="number" min="13" max="120"></label> </fieldset>
Step 33
Adding a dropdown to the form is easy with the
select
element. Theselect
element is a container for a group ofoption
elements, and theoption
element acts as a label for each dropdown option. Both elements require closing tags.Start, by adding a
select
element below the twolabel
elements. Then, nest 5option
elements within theselect
element.<fieldset> <label>Upload a profile picture: <input type="file" /></label> <label>Input your age (years): <input type="number" min="13" max="120" /></label> <select> <option></option> <option></option> <option></option> <option></option> <option></option> </select> </fieldset>
Step 34
Nest the
select
element (with itsoption
elements) within alabel
element with the textHow did you hear about us?
. The text should come before theselect
element.<fieldset> <label>Upload a profile picture: <input type="file" /></label> <label>Input your age (years): <input type="number" min="13" max="120" /></label> <label>How did you hear about us? <select> <option></option> <option></option> <option></option> <option></option> <option></option> </select> </label> </fieldset>
Step 35
The dropdown options are currently empty. To give them content, add the following text to each subsequent
option
element:(select one) freeCodeCamp News freeCodeCamp YouTube Channel freeCodeCamp Forum Other
<fieldset> <label>Upload a profile picture: <input type="file" /></label> <label>Input your age (years): <input type="number" min="13" max="120" /></label> <label>How did you hear about us? <select> <option>(select one)</option> <option>freeCodeCamp News</option> <option>freeCodeCamp YouTube Channel</option> <option>freeCodeCamp Forum</option> <option>Other</option> </select> </label> </fieldset>
Step 36
Submitting the form with an option selected would not send a useful value to the server. As such, each
option
needs to be given avalue
attribute. Without which, the text content of theoption
will be submitted to the server.Give the first
option
avalue
of""
, and the subsequentoption
elementsvalue
attributes from1
to4
.<fieldset> <label>Upload a profile picture: <input type="file" /></label> <label>Input your age (years): <input type="number" min="13" max="120" /></label> <label>How did you hear about us? <select> <option value="">(select one)</option> <option value=1>freeCodeCamp News</option> <option value=2>freeCodeCamp YouTube Channel</option> <option value=3>freeCodeCamp Forum</option> <option value=4>Other</option> </select> </label> </fieldset>
Step 37
The
textarea
element acts like aninput
element of typetext
, but comes with the added benefit of being able to receive multi-line text, and an initial number of text rows and columns.Users will be able to register with a bio. Add a
label
with the textProvide a bio:
at the end of thefieldset
. Add atextarea
element inside thelabel
element. Note that thetextarea
requires a closing tag.<fieldset> <label>Upload a profile picture: <input type="file" /></label> <label>Input your age (years): <input type="number" min="13" max="120" /></label> <label>How did you hear about us? <select> <option value="">(select one)</option> <option value="1">freeCodeCamp News</option> <option value="2">freeCodeCamp YouTube Channel</option> <option value="3">freeCodeCamp Forum</option> <option value="4">Other</option> </select> </label> <label>Provide a bio: <textarea></textarea> </label> </fieldset>
Step 38
Link the applicable form elements and their
label
elements together.Use
profile-picture
,age
,referrer
, andbio
as values for the respectiveid
attributes.<fieldset> <label for="profile-picture">Upload a profile picture: <input type="file" id="profile-picture" /></label> <label for="age">Input your age (years): <input type="number" min="13" max="120" id="age" /></label> <label for="referrer">How did you hear about us? <select id="referrer"> <option value="">(select one)</option> <option value="1">freeCodeCamp News</option> <option value="2">freeCodeCamp YouTube Channel</option> <option value="3">freeCodeCamp Forum</option> <option value="4">Other</option> </select> </label> <label for="bio">Provide a bio: <textarea id="bio"></textarea> </label> </fieldset>
Step 39
The
textarea
appears too small. To give it an initial size, you can add therows
andcols
attributes.Add an initial size of
3
rows and30
columns.<fieldset> <label for="profile-picture">Upload a profile picture: <input id="profile-picture" type="file" /></label> <label for="age">Input your age (years): <input id="age" type="number" min="13" max="120" /></label> <label for="referrer">How did you hear about us? <select id="referrer"> <option value="">(select one)</option> <option value="1">freeCodeCamp News</option> <option value="2">freeCodeCamp YouTube Channel</option> <option value="3">freeCodeCamp Forum</option> <option value="4">Other</option> </select> </label> <label for="bio">Provide a bio: <textarea id="bio" rows="3" cols="30"></textarea> </label> </fieldset>
Step 40
To give Campers an idea of what to put in their bio, the
placeholder
attribute is used. Theplaceholder
accepts a text value, which is displayed until the user starts typing.Give the
textarea
aplaceholder
ofI like coding on the beach...
.<fieldset> <label for="profile-picture">Upload a profile picture: <input id="profile-picture" type="file" /></label> <label for="age">Input your age (years): <input id="age" type="number" min="13" max="120" /></label> <label for="referrer">How did you hear about us? <select id="referrer"> <option value="">(select one)</option> <option value="1">freeCodeCamp News</option> <option value="2">freeCodeCamp YouTube Channel</option> <option value="3">freeCodeCamp Forum</option> <option value="4">Other</option> </select> </label> <label for="bio">Provide a bio: <textarea id="bio" rows="3" cols="30" placeholder="I like coding on the beach..."></textarea> </label> </fieldset>
定义和用法
placeholder 属性规定可描述输入字段预期值的简短的提示信息(比如:一个样本值或者预期格式的短描述)
该提示会在用户输入值之前显示在输入字段中
注意:placeholder 属性适用于下面的 input 类型:text、search、url、tel、email 和 password
语法
<input placeholder="text">
属性值
值 描述 text 规定可描述输入字段预期值的简短的提示信息。
Step 41
With form submissions, it is useful, and good practice, to provide each submittable element with a
name
attribute. This attribute is used to identify the element in the form submission.Give each submittable element a unique
name
attribute of your choosing, except for the tworadio
inputs.<fieldset> <label for="first-name">Enter Your First Name: <input id="first-name" name="first-name" type="text" required /></label> <label for="last-name">Enter Your Last Name: <input id="last-name" name="last-name" type="text" required /></label> <label for="email">Enter Your Email: <input id="email" name="email" type="email" required /></label> <label for="new-password">Create a New Password: <input id="new-password" name="new-password" type="password" pattern="[a-z0-5]{8,}" required /></label> </fieldset> <fieldset> <label for="personal-account"><input id="personal-account" type="radio" name="account-type" /> Personal Account</label> <label for="business-account"><input id="business-account" type="radio" name="account-type" /> Business Account</label> <label for="terms-and-conditions" name="terms-and-conditions"> <input id="terms-and-conditions" type="checkbox" required name="terms-and-conditions" /> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a> </label> </fieldset> <fieldset> <label for="profile-picture">Upload a profile picture: <input id="profile-picture" type="file" name="picture"/></label> <label for="age">Input your age (years): <input id="age" type="number" min="13" max="120" name="personalAge/></label> <label for="referrer">How did you hear about us? <select id="referrer" name="referrer"> <option value="">(select one)</option> <option value="1">freeCodeCamp News</option> <option value="2">freeCodeCamp YouTube Channel</option> <option value="3">freeCodeCamp Forum</option> <option value="4">Other</option> </select> </label> <label for="bio">Provide a bio: <textarea id ="bio" rows="3" cols="30" placeholder="I like coding on the beach..." name="personalBio"></textarea> </label> </fieldset>
Step 42
The HTML for the registration form is finished. Now, you can spruce it up a bit.
Start by changing the font to
Tahoma
, and the font size to16px
in thebody
.body { width: 100%; height: 100vh; margin: 0; background-color: #1b1b32; color: #f5f6f7; font-family:Tahoma; font-size:16px; }
Step 43
Center the
h1
andp
elements by giving them amargin
of1em auto
. Then, align their text in thecenter
as well.h1,p{ margin:1em auto; text-align:center; }
Step 44
Center the
form
element, by giving it amargin
of0 auto
. Then, fix its size to a maximum width of500px
, and a minimum width of300px
. In between that range, allow it to have awidth
of60vw
.form{ margin:0 auto; max-width:500px; min-width:300px; width:60vw; }
Step 45
During development, it is useful to see the
fieldset
default borders. However, they make the content appear too separated.Remove the
border
, and add2rem
of padding only to the top and bottom of eachfieldset
. Be sure to remove thepadding
from the left and right.fieldset{ border:none; padding:2rem 0; }
Step 46
To give the
fieldset
elements a bit of separation, select them and give them aborder-bottom
of3px solid #3b3b4f
.fieldset { border: none; padding: 2rem 0; border-bottom:3px solid #3b3b4f }
Step 47
The border of the last
fieldset
element looks a little out of place. You can select the last element of a specific type using thelast-of-type
CSS pseudo-class, like this:p:last-of-type { }
That will select the last
p
element. Create a new selector that targets the lastfieldset
element and set itsborder-bottom
tonone
.fieldset:last-of-type{ border-bottom:none; }
选择器 示例 示例说明 CSS .class .intro 选择所有class="intro"的元素 1 #id #firstname 选择所有id="firstname"的元素 1 * * 选择所有元素 2 element p 选择所有<p>元素 1 element,element div,p 选择所有<div>元素和<p>元素 1 element element div p 选择<div>元素内的所有<p>元素 1 element>element div>p 选择所有父级是 <div> 元素的 <p> 元素 2 element+element div+p 选择所有紧跟在 <div> 元素之后的第一个 <p> 元素 2 [attribute] [target] 选择所有带有target属性元素 2 [attribute=value] [target=-blank] 选择所有使用target="-blank"的元素 2 [attribute~=value] [title~=flower] 选择标题属性包含单词"flower"的所有元素 2 [attribute|=language] [lang|=en] 选择 lang 属性等于 en,或者以 en- 为开头的所有元素 2 :link a:link 选择所有未访问链接 1 :visited a:visited 选择所有访问过的链接 1 :active a:active 选择活动链接 1 :hover a:hover 选择鼠标在链接上面时 1 :focus input:focus 选择具有焦点的输入元素 2 :first-letter p:first-letter 选择每一个<p>元素的第一个字母 1 :first-line p:first-line 选择每一个<p>元素的第一行 1 :first-child p:first-child 指定只有当<p>元素是其父级的第一个子级的样式。 2 :before p:before 在每个<p>元素之前插入内容 2 :after p:after 在每个<p>元素之后插入内容 2 :lang(language) p:lang(it) 选择一个lang属性的起始值="it"的所有<p>元素 2 element1~element2 p~ul 选择p元素之后的每一个ul元素 3 [attribute^=value] a[src^="https"] 选择每一个src属性的值以"https"开头的元素 3 [attribute$=value] a[src$=".pdf"] 选择每一个src属性的值以".pdf"结尾的元素 3 [attribute*=value] a[src*="runoob"] 选择每一个src属性的值包含子字符串"runoob"的元素 3 :first-of-type p:first-of-type 选择每个p元素是其父级的第一个p元素 3 :last-of-type p:last-of-type 选择每个p元素是其父级的最后一个p元素 3 :only-of-type p:only-of-type 选择每个p元素是其父级的唯一p元素 3 :only-child p:only-child 选择每个p元素是其父级的唯一子元素 3 :nth-child(n) p:nth-child(2) 选择每个p元素是其父级的第二个子元素 3 :nth-last-child(n) p:nth-last-child(2) 选择每个p元素的是其父级的第二个子元素,从最后一个子项计数 3 :nth-of-type(n) p:nth-of-type(2) 选择每个p元素是其父级的第二个p元素 3 :nth-last-of-type(n) p:nth-last-of-type(2) 选择每个p元素的是其父级的第二个p元素,从最后一个子项计数 3 :last-child p:last-child 选择每个p元素是其父级的最后一个子级。 3 :root :root 选择文档的根元素 3 :empty p:empty 选择每个没有任何子级的p元素(包括文本节点) 3 :target #news:target 选择当前活动的#news元素(包含该锚名称的点击的URL) 3 :enabled input:enabled 选择每一个已启用的输入元素 3 :disabled input:disabled 选择每一个禁用的输入元素 3 :checked input:checked 选择每个选中的输入元素 3 :not(selector) :not(p) 选择每个并非p元素的元素 3 ::selection ::selection 匹配元素中被用户选中或处于高亮状态的部分 3 :out-of-range :out-of-range 匹配值在指定区间之外的input元素 3 :in-range :in-range 匹配值在指定区间之内的input元素 3 :read-write :read-write 用于匹配可读及可写的元素 3 :read-only :read-only 用于匹配设置 "readonly"(只读) 属性的元素 3 :optional :optional 用于匹配可选的输入元素 3 :required :required 用于匹配设置了 "required" 属性的元素 3 :valid :valid 用于匹配输入值为合法的元素 3 :invalid :invalid 用于匹配输入值为非法的元素 3
Step 48
It would be nicer to have the
label
text appear above the form elements.Select all
input
,textarea
, andselect
elements, and make them take up the full width of their parent elements.Also, add
10px
ofmargin
to the top of the selected elements. Set the other margins to0
.input,textarea,select{ width:100%; margin:10px 0 0 0 ; }
Step 49
For the second
fieldset
, you want theinput
andlabel
text to appear on the same line.Start, by giving the
input
elements in the secondfieldset
a class ofinline
.<fieldset> <label for="personal-account"><input id="personal-account" type="radio" name="account-type" class="inline" /> Personal Account</label> <label for="business-account"><input id="business-account" type="radio" name="account-type" class="inline"/> Business Account</label> <label for="terms-and-conditions" name="terms-and-conditions"> <input id="terms-and-conditions" type="checkbox" required name="terms-and-conditions" class="inline"/> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a> </label> </fieldset>
Step 50
Select only the
.inline
elements, and give themwidth
ofunset
. This will remove the earlier rule which set all theinput
elements towidth: 100%
..inline{ width:unset; }
Step 51
Add some space between the
.inline
elements and thelabel
text, by giving a rightmargin
of0.5em
. Also, set all the other margin to0
..inline { width: unset; margin:0 0.5em 0 0; }
Step 52
If you look close enough, you will notice the
.inline
elements are too high on the line.To combat this, set the
vertical-align
property tomiddle
..inline { width: unset; margin: 0 0.5em 0 0; vertical-align:middle; }
属性定义及使用说明
vertical-align 属性设置一个元素的垂直对齐方式
该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐。允许指定负长度值和百分比值。这会使元素降低而不是升高。在表单元格中,这个属性会设置单元格框中的单元格内容的对齐方式
值 描述 baseline 默认。元素放置在父元素的基线上。 sub 垂直对齐文本的下标。 super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text-top 把元素的顶端与父元素字体的顶端对齐 middle 把此元素放置在父元素的中部。 bottom 使元素及其后代元素的底部与整行的底部对齐。 text-bottom 把元素的底端与父元素字体的底端对齐。 length 将元素升高或降低指定的高度,可以是负数。 % 使用 "line-height" 属性的百分比值来排列此元素。允许使用负值。 inherit 规定应该从父元素继承 vertical-align 属性的值。
Step 53
To make the
input
andtextarea
elements blend in with the background theme, set theirbackground-color
to#0a0a23
. Then, give them a1px
,solid
border with a color of#0a0a23
.input,textarea{ background-color:#0a0a23; border:1px solid #0a0a23; }
Step 54
Currently, if you type in the
input
ortextarea
elements, you will not be able to see the text. Also, their height is too small to be easy to use.Fix this, by setting the
color
to#ffffff
, and setting theirmin-height
to2em
.input, textarea { background-color: #0a0a23; border: 1px solid #0a0a23; color:#ffffff; min-height:2em; }
Step 55
You want the
select
element to remain with a white background, but now it is not getting the samemin-height
as theinput
andtextarea
elements.Move the
min-height
property and value so that all three element types have the samemin-height
value, and theselect
element still has a white background.input, textarea, select { margin: 10px 0 0 0; width: 100%; min-height: 2em; } input, textarea { background-color: #0a0a23; border: 1px solid #0a0a23; color: #ffffff; }
Step 56
To style the submit button, you can use an attribute selector, which selects an element based on the given attribute value. Here is an example:
input[name="password"]
The above selects
input
elements with aname
attribute value ofpassword
.Now, use the attribute selector to style the submit button with a
display
ofblock
, and awidth
of60%
.input[type="submit"]{ display:block; width:60%; }
Step 57
With a
display
ofblock
the submit button sits flush against the left edge of its parent.Use the same technique used to center the
form
to center the submit button.input[type="submit"] { display: block; width: 60%; margin:0 auto; }
Step 58
To make the submit button look more in line with the rest of the form, give it the same
height
as the other fields (2em
). Also, increase thefont-size
to1.1rem
.input[type="submit"] { display: block; width: 60%; margin: 0 auto; height:2em; font-size:1.1rem; }
Step 59
To make the submit button appear more distinct, give it a
background-color
of#3b3b4f
, and aborder-color
ofwhite
.input[type="submit"] { display: block; width: 60%; margin: 0 auto; height: 2em; font-size: 1.1rem; background-color:#3b3b4f; border-color:white; }
Step 60
Lastly, for the submit button, you want to separate it from the
fieldset
above, and adjust its width to never be below300px
.Change the
margin
property to include1em
on the top and bottom, while leaving the right and left margins set toauto
. Then set the width as described above.input[type="submit"] { display: block; width: 60%; margin: 1em auto; height: 2em; font-size: 1.1rem; background-color: #3b3b4f; border-color: white; min-width:300px; }
Step 61
Most browsers inject their own default CSS properties and values for different elements. If you look closely, you might be able to notice the file
input
is smaller than the other textinput
elements. By default, apadding
of1px 2px
is given toinput
elements you can type in.Using another attribute selector, style the
input
with atype
offile
to be the same padding as the otherinput
elements.input[type="file"]{ padding:1px 2px; }
Step 62
Speaking of
padding
, the submit button is sitting at the bottom of theform
element. Add2em
ofpadding
only to the bottom of theform
.form { width: 60vw; max-width: 500px; min-width: 300px; margin: 0 auto; padding-bottom:2em; }
Step 63
Last, but not least, change the text color of the
terms and conditions
link to#dfdfe2
.Well done! You have completed the final part of the Registration Form practice project.
a{ color:#dfdfe2; }