Styling a Web Page with CSS
CSS defines the look of a page: colour, font and layout. With style properties we turn a plain HTML page into an attractive one.
All topics (11 of 11)
Chapter 1. Computer networks
Chapter 2. Information security
Chapter 3. Creating web pages
HTML defines the structure of a page, and CSS (Cascading Style Sheets) defines how it looks. Separating content from design makes a page easier to manage: a single CSS file can change the colour and font of a whole site. A CSS rule has two parts: a selector saying which element it applies to and property: value; pairs.
The simplest way to style an element is the style attribute: <p style="color: blue; font-size: 18px;">Salom</p>. Properties are separated by semicolons. Commonly used properties are: color for text colour, background-color for background colour, font-size for font size, font-family for the typeface and text-align for alignment (left, center, right).
To give one style to many elements, styles are moved to a separate place. They are written in a <style> tag inside <head> or in a separate style.css file connected with <link rel="stylesheet" href="style.css">. A selector can be a tag name (all p elements), a class (.card for elements with class="card") or an identifier (#menu for the one element with id="menu"). Classes are used most often.
Every element is pictured as a “box”: the content, padding (inner space) around it, a border (line), and margin (outer space) outside. A colour can be given by name (red), by HEX code (#1e88e5) or as rgb(30, 136, 229). In design, limit the number of colours and leave enough contrast between text and background so the page is easy to read.
Key takeaways
- HTML is structure; CSS is appearance.
- A CSS rule consists of a selector and
property: value;pairs. - Styles are given by the
styleattribute, a<style>tag or an external.cssfile. - A selector can be a tag, a class (
.name) or an identifier (#name); an element’s box hasmargin,borderandpadding.
Practice tasks
- Use the
styleattribute to change the colour of<h1>, the font size of<p>and the background colour of the page from your previous work. - Move the styles to a separate
style.cssfile and connect it with<link>; create one class (.card) and apply it to two blocks. - Try three pairs of text and background colours and discuss with classmates which one reads best.
Review questions
Test yourself: think first, then reveal the answer.
1How do the roles of HTML and CSS differ?Show answerHide answer
HTML defines the content and structure of a page, while CSS defines its colours, fonts and layout.2How does a class selector (`.card`) differ from an identifier selector (`#menu`)?Show answerHide answer
A class can be given to many elements, while an identifier should belong to only one element on the page.
0 of 11 topics completed
Sign in to mark