Download HTML Forms and CSS Selectors: Understanding Input Controls and Labels in Bootstrap and more Summaries Computer Science in PDF only on Docsity!
Chapter 1
HTML
1.1 Introduction
In this chapter, various component of HTML are discussed to design a web page.
The basic structure for an HTML page is shown below.
- Entries inside the /<... /> are known as tags. Most of the tags has an opening and closing e.g.
(opening head) and (closing head). Some of the tags do not have closing tags e.g. and
. We need to write the HTML codes inside the tags.
- The comments are written between ‘’.
- Here Line 1 gives the details of the ‘HTML version’ to the web-browser. The ‘html’ tells it is version 5.
- The ‘head’ tag (Lines 3-5) contains the header related tags e.g. ‘title for the page’ and ‘links for the css files’
etc.
- The ‘body’ tag (7-11) contains the actual HTML code which is displayed on the web-browser. Also, we add
all the JavaScript related codes just before the closing body tag ().
1
2
3 4 5 6 7 8 9 10 11 12
1.2 First code
In below code, the message “Hello World” is displayed on the HTML page. The Fig. 1.1 is the resultant HTML
page.
- The title (Line 4) appears on the top of the browser.
- The tag is called ‘header’ tag, which has the larger size than the normal text (see the size of ‘Hello
World!’).
(continues on next page)
(continued from previous page)
HTML Tutorial
Hello World!
This is the first HTML code
Fig. 1.1: First code
1.3 Basic tags
- The Table 1.1 shows the list of tags which are required for writing the basic ‘HTML’ codes i.e. without any
style e.g. bold, italics and numbering etc.
Table 1.1: List of basic tags
Tag Description Example
h1,... , h6 Header tag h1 to h6 Hi
p paragraphs (Line changes at the end)
Hi
span No line change after span Hi Bye.
div make division between contents ...
a hyperlink see Section 1.
center Move content to center Hi
br Line break (no closing tag)
or
hr horizontal line (no closing tag) or
pre preserve formatting ....
table insert table see Section 1.
- Let’s see the example of each of these tags,
Note: All the new codes are added below the previous codes in the ‘body’ tag. Therefore only newly added
codes are shown in the tutorial.
Heading 2 Heading 6
(continues on next page)
1.3. Basic tags 2
1.4 Attributes
In Fig. 1.2, we saw an example of attribute (i.e. style) which changed the color of all the elements to ‘blue’ inside
the ‘div’ tag.
1.4.1 Attribute ‘name’ and ‘value’
- Attribute is defined inside the opening part of a ‘tag’. For example, in the below code, the attribute ‘style’
is defined inside the ‘div’ tag.
- An attribute has two parts i.e. ‘name’ and ‘value’. For example, in the above code, name and value of
the attribute are ‘style’ and ‘blue’ respectively.
1.4.2 Core attributes
Below are the three core attributes which are used frequently in web design.
- id : The ‘id’ is the unique name which can be given to any tag. This is very useful in distinguishing the
element with other elements.
This is paragraph with id 'para1'
This is paragraph with id 'para2'
- class : The attribute ‘class’ can be used with multiple tags. This is very useful in making groups in HTML
design.
This is paragraph with class 'blue'
This is span with class 'blue'
- style : We already see the example of style attribute, which can be used to change the formatting of the
text in HTML design. We can specify various styles which are discussed in Chapter 2.
Style attribute is used to bold and color
Note: Above three attributes are used with ‘CSS (cascading style sheet)’ and JavaScript/jQuery, which are the
very handy tools to enhance the look and functionalities of the web-page respectively. The CSS is discussed in
Chapter 2, whereas JavaScript and jQuery are discussed in Chapter 4 and Chapter 5 respectively.
- Also we can define multiple attributes for one tag as shown below,
Multiple attributes
- The other useful attributes are listed in Table 1.
Table 1.2: List of attributes
Name Values Description
id user defined names
Hi
class user defined names
Hi
style CSS styles
Hi
align left, right, center horizontal alignment
width numeric value or % value width of images and tables etc.
height numeric value height of images and tables etc.
1.4. Attributes 4
1.5 Tables
In this section, we will learn to draw tables along with some attributes which are discussed in Table 1.2. Table 1.
shows the list of tags available to create the table, which are used in Listing 1.1.
Table 1.3: Tags and attributes for creating tables
Tag Description
table beginning and end of table
tr row of table
th header cell
td data cell
Attributes
rowspan number of rows to merge
colspan number of columns to merge
border width of border
cellpadding width of whitespace between two border
cellspacing width of whitespace within a border
bgcolor background color
bordercolor color of border
width width of table (numeric or %)
height height of table (numeric)
caption caption for table
- Some of the attributes of Table 1.3 are used in below example,
Listing 1.1: Table with border and color
1 2 3 Table 1 : Various tags of table 4
5 Column 1 6 Column 2 7 Column 3 8 9 10
11 Data 1 12 Data 2 13 Data 3 14 15 16
17 New Data 1 18 New Data 2 19 20 21 22 23 24 25 26 Table 2 : Width is 80% 27 28 Column 1 29 Column 2 30 Column 3 31 32 (continues on next page)
1.5. Tables 5
Fig. 1.4: Text formatting
- alt : is the ‘alternate text’ which is displayed if image is not found. For example, in Line 6, the name of the
image is incorrectly written i.e. ‘logoa’ (instead of ‘logo’), therefore the value of ‘alt’ i.e. ‘Missing Logo.jpg’
will be displayed as shown in Fig. 1.5.
1 2 3 4
5 6
Fig. 1.5: Images
Note: We can use other attributes as well e.g. ‘height’, ‘align’ and ‘border’ etc.
1.7. Images 7
1.8 Lists
There are three type of lists in HTML,
- Unordered list : bullet are used in it (see Lines 2 and 9)
- Ordered list : numbers are used in it (see Lines 15, 22 and 28)
- Definition list : This can be used for writing definitions in HTML (see Line 35)
1 2 3 Unordered List 4 Pen 5 Pencil 6 Eraser 7 8 9 Change bullets : 'square', 'circle' or 'disc' 10 Pen 11 Pencil 12 Eraser 13 14 15 16 Ordered List 17 Pen 18 Pencil 19 Eraser 20 21 22 Change style : 'i', 'I', ' 1 ', 'a' or 'A' 23 Pen 24 Pencil 25 Eraser 26 27 28 Start from^ 'v' 29 Pen 30 Pencil 31 Eraser 32 33 34 35 36 HTML Definition List 37 HTML is easy 38 HTML is good 39
The outputs of above codes are shown in Fig. 1.6,
1.9 Links
Go to paragraph with id='para1'
PythonDSP
JavaScript Tutorial in same window.
JavaScript Tutorial in new Window.
(continues on next page)
1.8. Lists 8
(continued from previous page)
Download PDF, DOC or Zip Files
Email me
Feeback email
Note: We can change the color of the links using ‘alink (active link)’, ‘link’ and ‘vlink (visited link’, by defining
these attributes in the ‘body tag’ as shown below,
1.10 Forms
Forms can have different types of controls to collect the input-data from users, which are listed below and shown
in Table 1.5,
- Text input
- Text area
- Radio button
- Checkbox
- Select box
- File select
- Buttons
- Submit and reset buttons
- Hidden input
1.10. Forms 10
Table 1.5: List of control inputs and their attributes
Control Attributes Values Description
Input : text type text, password
value user-defined initial value in the area
name user-defined name send to server
size numeric value width of the text area
maxlength numeric value maximum number of characters
Input : radio type radio
name user-defined name send to server
value user-defined value value of the button if selected
checked check the button by default
Input : check box type checkbox
name user-defined name send to server
value user-defined value value of the box if selected
checked check the box by default
Input : button type button trigger client side script
submit submit the form and run ‘action’
reset reset form
image create image button
method get, post get or post method
action user-defined action to perform on submit
Input : hidden type hidden will not display on html, but can be used
for sending information to server
Selection box name user-defined name send to server
size numeric value enables scroll (default dropdown)
multiple numeric value select multiple items
value user-defined value value of the item if selected
selected select item by default
Text area rows, cols numeric value number of rows and cols
name user-defined name send to server
- Below are the exmaple of the control inputs described in Table 1.
Text input Name :
Password :
Radio button: name should be same Male Female Infant
Check box : name should be different Male Female Infant
Select box : drop-down
Select box : scroll (continues on next page)
1.10. Forms 11
Fig. 1.7: Various control inputs for creating form
1.10. Forms 13
Fig. 1.8: Various buttons and hidden-input in the form
1.10. Forms 14
- CSS has three parts,
- Selectors e.g. p, h3.h3_blue
- Properties e.g. color
- Values of properties e.g. red
1 2 3 4 5 6 CSS Tutorial 7 8 17 18 19 20 21 Heading 1 22 Heading 3 23 Heading 3 24 25 26 Heading 1 27 Heading 3 28 Heading 3 29 30 31
- Below is the output of above code,
2.1.3 External CSS
We can write the ‘CSS’ code in different file and then import the file into ‘html’ document as shown in this section.
In this way, we can manage the files easily.
- The ‘CSS’ code is saved in the file ‘my_css.css’ which is saved inside the folder ‘asset/css’.
/* asset/css/my_css.css */
h3.h3_blue{ color: blue; }
h3.h3_red{ color:red; }
- Next, we need to import the CSS file into the ‘html’ file as shown in Line 7.
1 2 (continues on next page)
2.1. Introduction 16
Fig. 2.1: Embedded CSS
(continued from previous page) 3 4 5 6 CSS Tutorial 7 8 9 10 11 Heading 1 12 Heading 3 13 Heading 3 14 15 16 Heading 1 17 Heading 3 18 Heading 3 19 20 21
2.2 Basic CSS Selectors
There are three types of selectors in CSS,
- Element : can be selected using it’s name e.g. ‘p’, ‘div’ and ‘h1’ etc.
- Class : can be selected using ‘.className’ operator e.g. ‘.h3_blue’.
- ID : can be selected using ‘#idName’ e.g. ‘#my_para’.
We will use following HTML for understanding the selectors,
2.2. Basic CSS Selectors 17
- Priority level :
- ID (highest priority)
- Class
- Element
- If two CSS has same priority, then CSS rule at the last will be applicable.
- Below is the html code with following tags,
- ‘p’ tag
- ‘p’ tag with class ‘c_head’
- ‘p’ tag with class ‘c_head’ and id ‘i_head’
1 2 3 4 5 6 CSS Selectors 7 8 9 10
Paragraph
11 12
Paragraph with class 'c_head'
13
Paragraph with class 'c_head' and id 'i_head'
14 15 16
Below is the CSS code. Let’s understand the formatting of all three ‘p’ tags individually. The results are shown
in Fig. 2.3.
- ‘p’ tag at Line 13 of html : Since, ‘id’ has highest priority, therefore CSS rule for #i_head’ (Line 12) will
not be overridden by Line 24; hence the color is red. Line 13 has ‘p’ tag, therefore ‘font-variant’ rule will be
applied by Line 17. Also, this tag has class ‘c_head’, therefore ‘font’ will be set to ‘cursive’. Hence, the line
is “all-caps with font-cursive in red color”.
- ‘p’ tag at Line 12 of html : Similarly, the ‘head’ tag has higher priority than ‘element’ therefore color of
this line is oranage and font-family is ‘cursive’. Also, Line 17 will make it all caps
- ‘p’ tag at Line 10 of html : Color defined at Line 18 will be overridden by Line 24; hence the color will
be blue. Also, Line 17 will make it all caps.
1 /* asset/css/my_css.css / 2 3 /class selection/ 4 .c_head{ 5 font-family: cursive; 6 color: orange; /override the blue color/ 7 } 8 9 10 /id selection/ 11 #i_head{ 12 color: red; 13 } 14 15 /element selection*/ 16 p { 17 font-variant: small-caps; 18 color: blue; 19 } 20 21 (continues on next page)
2.3. Hierarchy 19
(continued from previous page)
22 /element selection/ 23 p { 24 color: green; 25 }
Fig. 2.3: Priority level for CSS rule
2.4 More selectors
Table 2.1 shows the combinations of selectors to target the various elements of the HTML. Also, some of the
example of ‘Attribute selector’ is shown in this section.
Table 2.1: List of selectors
Selectors Description
h1, p, span etc. element selector
.className class selector
#idName id selector
* Universal selector (selects everything)
h1.className select h1 with class ‘className’
h1#idName select h1 with id ‘idName’
p span descendant selector (select span which is inside p)
p > span child selector (‘span’ which is direct descendant of ‘p’)
h1, h2, p group selection (select h1, h2 and p)
span[my_id] select ‘span’ with attribute ‘my_id’
span[my_id=m_span] select ‘span’ with attribute ‘my_id=m_span’
2.4.1 Attribute selector
- Add below code at the end of the html file. In these lines ‘custom attributes’ are added (i.e. my_id).
Span with attribute 'my_id' with value 'm_span'
Span with attribute 'my_id' with value 'm_span2'
- These custom attributes can be selected as below,
/attribute selection/ span[my_id] { /* select 'span' with attribute 'my_id' */ color: green; font-weight: bold (continues on next page)
2.4. More selectors 20