Web Applications

HTML Form and CSS

Objectives

  • HTML Form
  • HTML Form validation
  • CSS

Exercise: HTML Form & Validation

Create a new HTML file named form.html with HTML form. The form must contain:

  • an input for the user's name (full_name),
  • an input for the user's age (age),
  • an input for the user's email (email),
  • an invisible input (type) with value "html-form-and-validation",
  • a visible submit button.

The form must be submitted to: https://webik.ms.mff.cuni.cz/nswi142/services/02-handle-form.php using GET method.

Check Network tab to see how are data send.

Exercise: Validation with HTML5 Forms

Add validation using HTML5 Forms and make sure your page is valid. You can upload the HTML directly using "text input" as a value for "Check by".

  • Make name a required value.
  • Ensure name contains first name and surname separated with space.
  • Ensure email address is valid.
  • Ensure age is a number between 0 and 200.
Warning

Spoilers Ahead

Form elements

Behind the scene

Question: What is happening and where?

  • Browser opens a website with a form from a local file.
  • A user fills in the form in the web browser.
  • User submits the form.
  • Browser request URL with GET as a way of form submission.
  • Server (webik) process the request and returns response.
  • Browser shows response to the user.

Question:
Is the client side validation enough?
Can you send age equal to 300?

Demonstration: GET vs. POST

Change the method to POST, open Network tab and examine the request.

What happens when you refresh the page?

CSS Introduction

Cascading Style Sheets (CSS) is a stylesheet language used to describe the presentation of a document written in HTML. Example of complete design change is CSS Zen Garden.

Did you know, that this presentation is a single webpage?

There are different CSS levels and modules. Levels and modules determine which properties are available. You should be aware of what subset of CSS you can use.

You can utilize a W3C CSS validator to check your CSS.

ReCodEx is limited to CSS Level 3!

CSS and Specificity

What is the CSS selector specificity of the following selectors and why does it matter?

  • li.sidebar a.external
  • div#navigation a[href^="http"]:hover

Specificity Calculator seems to work.

Shorthand properties

Shorthand properties are CSS properties that let you set the values of multiple other CSS properties simultaneously.


.border {
  border-width: 1px;
  border-style: solid;
  border-color: black;
}
/* vs */
.border {
  border: 1px solid black;
}
    


.one   { border-width: 1px; }
.two   { border-width: 1px 2px; }
.three { border-width: 1px 2px; }
.four  { border-width: 1px 2px 3px 4px; }
    

Exercise: CSS Selectors

Download this HTML document and fix TODOs in the source code. The expected result should look like this.

Warning

Spoilers Ahead

You may find it useful ...

Cheat sheet

Property 'display'

The display CSS property sets whether an element is treated as a block or inline box and the layout used for its children, such as flow layout, grid or flex.

Use display: none; to remove element from page without leaving space for it.

Sometime there are alternatives you need to choose from. For example inline-block vs flex (wait for next practical).

Inline elements are affected by text-align property.

Float

The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow.

Rules:

  • Displayed as inline-block, implies one should explicitly set width (unless implicit).
  • Shifted to the left/right of the current line until its edge touches the containing block edge or edge of another float.
  • Positioned vertically as it would be within the normal flow, aligned with the top of the current line box.
  • If there is not enough horizontal room for the float, it is shifted downward until either it fits or there are no more floats present.
  • Other boxes laid out as if the float didn't exist, except for the current and subsequent line boxes shortened as necessary to make room for it. Implies that parent block does not increase size to encompass the float and the float may overflow

Demonstration: Float - clear

First div
float:right
clear:none
Second div
float:right
clear:none
Third div
float:right
clear:both
Specifies which sides of an element's box(es) may not be adjacent to an earlier floating box. Can be applied both to non-floated block elements and floated elements. The elements on the right are declared before this text. As they are declared to flow, the text just wrap around them.

Clear values: left, right, both, none.

Exercise: Using Display

Download this HTML document and fix TODOs in the source code.

Positioning

Combination of position, left, left, right, top, bottom, and z-index.

By appropriate combination of display modes and positioning we can create layouts.

Reminder

Selected values for CSS position property.

Relative - positioned element moved relative to the original position according to left/right/top/bottom. Rest of the layout as if not affected. Move is after positions are determined. We can move by 0 to make element positioned.

Absolute - positioned element moved relative to the closest positioned ancestor. Rest of layout as if the positioned element is not present. This implies that parent block does not increase size to encompass the element and the element may overflow.

Fixed - Element moved relative to the viewport and it doesn't move when scrolling. Rest of layout as if the positioned element is not present.

Recommended resources

Additional resources related to CSS.

Exercise "old school" layout

Let's try the old way of creating layouts. You are NOT allowed to use flex or grid. In addition, do not change display mode, or use positioning for float layout and vice versa.


Positioning-based

Download this HTML document and fix TODOs in the source code. The result should look like this.


Float-based layout

Download this HTML document and fix TODOs in the source code. The result should look like this.
Hint: margin property is often useful when creating a layout.

How are we doing?

  • HTML Form
  • HTML Form validation
  • CSS
Questions, ideas, or any other feedback?

Please feel free to use the anonymous feedback form.