JavaScript / Web pages

span, buttons and borders

Style part of a line with span, add input fields and buttons, change their look on mouse-over, and draw borders around elements.

A class on <p> colours the whole paragraph. What if you want to colour one word only? Wrap it in a <span> tag. On its own it changes nothing, but it gives CSS a "handle" on that part.

CSS
.sea {
  color: blue;
}
.mountain {
  color: green;
}
Browser

Input fields and buttons

The <input> tag makes something the visitor can interact with, and has no end tag. Its type attribute sets what it is: text makes a typing field, button makes a button, and the value attribute is the text on the button.

HTML
<h1>School Survey</h1>
<p>Your opinion</p>
<input type="text">
<input type="button" value="Submit">
Browser

hover and transition

Writing :hover after a selector applies the style only while the mouse pointer is over the element. The transition property makes the change gradual over a time in seconds, like 2s. Hover over both buttons:

CSS
.btn1 {
  background-color: aqua;
}
.btn2:hover {
  background-color: yellow;
  transition: 2s;
}
Browser
No space between the selector and :hover: write .btn2:hover, not .btn2 :hover. The first button keeps its colour all the time because its rule has no :hover.

Borders

The border property draws a frame around an element. It takes three values separated by spaces: the thickness in pixels, the type (solid for one line, double for two), and the colour.

CSS
h1 {
  border: 10px double blue;
}
li {
  font-weight: 900;
}
Browser

Practice

1Edit the programCurriculum practice

The CSS is ready. Wrap Apple in a span with class apple, Giraffe in a span with class giraffe, and Light blue sneakers in a span with class sneaker.

HTML Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
<p>My favorite fruit is Apple</p>
<p>My favorite animal is Giraffe</p>
<p>Today's shoes are Light blue sneakers</p>
Browser
2Edit the programCurriculum practice

Below the paragraph, add a text field <input type="text">, then a button (type="button") that says Submit.

HintWrite type first, then value.
HTML Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
<h1>School Survey</h1>
<p>Your opinion</p>
Browser
3Edit the programCurriculum practice

After the existing rule, add a rule that makes the Zoo button (class btn2) background yellow only on hover.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
.btn1 {
  background-color: aqua;
}
Browser
4Fill in the blankCurriculum practice

Make the colour change of the Zoo button gradual over 5 seconds.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
.btn1:hover {
  background-color: lime;
  transition: 2s;
}
.btn2:hover {
  background-color: yellow;
  ___: ___;
}
Browser
5Write the programCurriculum practice

Give the h1 a border: thickness 5px, type solid, colour red (in that order).

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
h1 {
  
}
Browser
6Edit the programExtra practice

The CSS is ready. In the exam notice, wrap Sunday in a span with class day, and Room 5 in a span with class room.

HTML Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
<h2>Exam Notice</h2>
<p>The physics exam is on Sunday in Room 5.</p>
Browser
7Write the programExtra practice

Write two rules in this order: (1) h1 with border 3px double green. (2) .join:hover with background colour lime, then a transition of 1s.

CSS Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Browser

Key points

  • <span> wraps part of a line so you can style it alone.
  • <input type="text"> is a text field; <input type="button" value="..."> is a button.
  • .x:hover styles on mouse-over; transition: 2s; makes it gradual.
  • border: 10px double blue; = thickness, then type, then colour.