Chapter 4 - Layout Elements and Properties, Forms and Inputs
Chapter 4 - Layout Elements and Properties, Forms and Inputs
Development &
Application
Block-level Elements
A block-level element always starts on a new line, and the browsers automatically add some space (a margin)
before and after the element.
A block-level element always takes up the full width available (stretches out to the left and right as far as it
can).
Example:
<p>This is an inline span <span style="border: 1px solid black">Hello World</span>
element inside a paragraph.</p>
Lorem Ipsum <div style="border: 1px solid black">I am a div</div> dolor sit amet.
<p>The border is added to demonstrate the footprint of the DIV element.</p>
The <div> element has no required attributes, but style, class and id are common.
<div> as a container
The <div> element is often used to group sections of a web page together.
In CSS, the term "box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists
of: content, padding, borders and margins. The image below illustrates the box model:
Width and Height of an
Element
In order to set the width and height of an element correctly in all browsers,
you need to know how the box model works.
When you set the width and height properties of an element with CSS, you just set the
width and height of the content area. To calculate the total width and height of an
element, you must also include the padding and borders.
div {
width: 320px;
height: 50px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
CSS Outline
An outline is a line drawn outside the element's border.
CSS Outline-Offset
The outline-offset property adds space between an outline and the edge/border
of an element. The space between an element and its outline is transparent.
p {
margin: 30px;
border: 1px solid black;
outline: 1px solid red;
outline-offset: 15px;
}
HTML Layout Elements
Websites often display content in multiple columns (like a magazine or a newspaper).
HTML Layout Elements
HTML has several semantic elements that define the different parts of a web page:
There are four different techniques to create multicolumn layouts. Each technique has its
pros and cons:
• CSS framework
• CSS float property
• CSS flexbox
• CSS grid
CSS Frameworks
If you want to create your layout fast, you can use a CSS framework, like W3.CSS or
Bootstrap.
the CSS float property. Float is easy to learn - you just need to remember how the float and clear properties work. Disadvantages: Floating elements are tied to the doc
Disadvantages: Floating elements are tied to the document flow, which may harm the
flexibility.
CSS Layout – Float and Clear
The CSS float property specifies how an element should float.
The CSS clear property specifies what elements can float beside the cleared element and on
which side.
img {
float: right;
width:170px;
height:170px;
margin-left:15px;
}
<p>
<img src="pineapple.jpg" alt="Pineapple" style="">
Some place holder text here!
</p>
CSS Layout – Float and Clear
Example: float: left;
img {
float: left;
width:170px;
height:170px;
margin-left:15px;
}
<p>
<img src="pineapple.jpg" alt="Pineapple" style="">
Some place holder text here!
</p>
CSS Layout – Float and Clear
The clear property
When we use the float property, and we want the next element below (not on right or
left), we will have to use the clear property.
The clear property specifies what should happen with the element that is next to a
floating element.
• none - The element is not pushed below left or right floated elements. This is default
• left - The element is pushed below left floated elements
• right - The element is pushed below right floated elements
• both - The element is pushed below both left and right floated elements
• inherit - The element inherits the clear value from its parent
When clearing floats, you should match the clear to the float: If an element is floated
to the left, then you should clear to the left. Your floated element will continue to
float, but the cleared element will appear below it on the web page.
CSS Layout – Float and Clear
When clearing floats, you should match the clear to the float: If an element is floated to the
left, then you should clear to the left. Your floated element will continue to float, but the
cleared element will appear below it on the web page.
.div1 {
float: left;
padding: 10px;
border: 3px solid #73AD21;
}
.div2 {
padding: 10px;
border: 3px solid red;
}
.div3 {
float: left;
padding: 10px;
border: 3px solid #73AD21;
}
.div4 {
padding: 10px;
border: 3px solid red;
clear: left;
}
CSS Layout – Flex Box
To start using the Flexbox model, you need to first define a flex container.
<style>
.flex-container {
display: flex;
background-color: DodgerBlue;
}
.flex-container > div {
background-color: #f1f1f1;
margin: 10px;
padding: 20px;
font-size: 30px;
}
</style>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
CSS Layout – Flex Box
The flex-direction Property
The flex-direction property defines in which direction the container wants to stack the flex
items.
flex-direction values:
• row
• row-reverse
• column
• Column-reverse
The column value stacks the flex items vertically (from top to bottom):
.flex-container {
display: flex;
flex-direction: column;
}
CSS Layout – Flex Box
The flex-wrap Property
The flex-wrap property specifies whether the flex items should wrap or not.
flex-wrap values:
• nowrap (default)
• wrap
• wrap-reverse
CSS Layout – Flex Box
The flex-flow Property
The flex-flow property is a shorthand property for setting both the flex-direction and flex-wrap
properties.
justify-content values:
• center
• flex-start
• flex-end
• space-between
• space-around
CSS Layout – Flex Box
The align-content Property
The align-content property is used to align the flex lines. (vertical)
align-content values:
• space-between
• space-around
• stretch
• flex-start
• flex-end
Perfect Centering
SOLUTION: Set both the justify-content and align-items properties to center, and the flex item will
be perfectly centered:
.flex-container{
display: flex;
justify-content: center;
align-items: center;
CSS Layout – Flex Items
Child Elements (Items)
The direct child elements of a flex container automatically becomes flexible (flex) items.
<div class="flex-container">
<div style="order: 3">1</div>
<div style="order: 2">2</div>
<div style="order: 4">3</div>
<div style="order: 1">4</div>
</div>
CSS Layout – Flex Items
The flex-grow Property
The flex-grow property specifies how much a flex item will grow relative to the rest of the flex items.
<div class="flex-container">
<div style="flex-grow: 1">1</div>
<div style="flex-grow: 1">2</div>
<div style="flex-grow: 8">3</div>
</div>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-shrink: 0">3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>
CSS Layout – Flex Items
The flex-basis Property
The flex-basis property specifies the initial length of a flex item.
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="flex-basis: 200px">3</div>
<div>4</div>
</div>
The <form> element is a container for different types of input elements, such
as: text fields, checkboxes, radio buttons, submit buttons, etc.
Forms
The <input> Element
The HTML <input> element is the most used form element.
An <input> element can be displayed in many ways, depending on the type attribute.
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe">
</form>
<form>
<input type="radio" id="html" name="fav_language" value="HTML">
<label for="html">HTML</label><br>
<input type="radio" id="css" name="fav_language" value="CSS">
<label for="css">CSS</label><br>
<input type="radio" id="javascript" name="fav_language" value="JavaScript">
<label for="javascript">JavaScript</label>
</form>
Checkboxes
The <input type="checkbox"> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
The Submit Button
The <input type="submit"> defines a button for submitting the form data to a form-handler.
The form-handler is typically a file on the server with a script for processing input
data.
The form-handler is specified in the form's action attribute.
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
HTML Form Attributes
The Action Attribute
The action attribute defines the action to be performed when the form is submitted.
Usually, the form data is sent to a file on the server when the user clicks on the submit
button.
In the example below, the form data is sent to a file called "action_page.php". This file
contains a server-side script that handles the form data:
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
HTML Form Attributes
The Target Attribute
The target attribute specifies where to display the response that is received after
submitting the form.
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction
(with method="post").
• <input>
• <label>
• <select>
• <textarea>
• <button>
• <fieldset>
• <legend>
• <datalist>
• <output>
• <option>
• <optgroup>
HTML Form Elements
The <select> Element
The <select> element defines a drop-down list:
The rows attribute specifies the visible number of lines in a text area.
The cols attribute specifies the visible width of a text area.
<form action="/action_page.php">
<fieldset>
<legend>Personalia:</legend>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
HTML Form Elements
The <datalist> Element
The <datalist> element specifies a list of pre-defined options for an <input> element.
Users will see a drop-down list of the pre-defined options as they input data.
The list attribute of the <input> element, must refer to the id attribute of the
<datalist> element.
<form action="/action_page.php">
<input list="browsers">
<datalist id="browsers">
<option value="Edge">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
</form>
HTML Form Elements
The <output> Element
The <output> element represents the result of a calculation (like one performed by a
script).
<form action="/action_page.php"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
0 <input type="range" id="a" name="a" value="50"> 100 +
<input type="number" id="b" name="b" value="50"> =
<output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>
HTML Input Types
Here are the different input types you can use in HTML:
<label for="pin">PIN:</label><br>
<input type="text" id="pin" name="pin" maxlength="4" size="4">
HTML Input Attributes
The min and max Attribute
The input min and max attributes specify the minimum and maximum values for an input field.
The min and max attributes work with the following input types: number, range, date, datetime-
local, month, time and week.
Tip: Use the max and min attributes together to create a range of legal values.
<label for="quantity">Range:</label>
<input type=“range" id="quantity" name="quantity" min="1" max="5">
The pattern attribute works with the following input types: text, date, search, url, tel,
email, and password.
Tip: Use the global title attribute to describe the pattern to help the user.
The short hint is displayed in the input field before the user enters a value.
More Attributes:
• step
• autofocus
• height
• width
• list
• autocomplete
• form
• formaction
• formenctype
• formmethod
• formtarget
• formnovalidate