HTML SECTION

HTML Fundamental

What is HTML?

Example:

HTML structure

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>

    <h1>My First Heading</h1>
    <p>My first paragraph.</p>

</body>
</html>

Explanation

What is an HTML Element?

An HTML element is defined by a start tag, some content, and an end tag:

<tagname>Content goes here...</tagname>

The HTML element is everything from the start tag to the end tag:

<h1>My First Heading</h1>
<p>My first paragraph.</p>
Start tagElement contentEnd tag
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<br>nonenone

HTML Page Structure

<html>
  <head>
    <title>Page title</title>
  </head>
  <body>
    <h1>This is a heading</h1>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
  </body>
</html>

I. HTML Elements

Here below are some example of Element

1. Empty HTML Elements

HTML elements with no content are called empty elements.

The <br> tag defines a line break, and is an empty element without a closing tag:

The <hr> tag defines a thematic break in an HTML page, and is most often displayed as a horizontal rule.

2. HTML Headings

HTML headings are defined with the <h1> to <h6> tags.

<h1> defines the most important heading. <h6> defines the least important heading:

<h1>Heading 1</h1>
<h2>Heading 2</h2>
<h3>Heading 3</h3>
<h4>Heading 4</h4>
<h5>Heading 5</h5>
<h6>Heading 6</h6>

3. HTML Paragraphs

HTML paragraphs are defined with the <p> tag:

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

4. HTML Links

HTML links are defined with the <a> tag:

<a href="https://www.w3schools.com">This is a link</a>

5. HTML Images

HTML images are defined with the <img> tag.

The source file (src), alternative text (alt), width, and height are provided as attributes:

<img src="w3schools.jpg" alt="W3Schools.com" width="104" height="142">

II. HTML Attributes

1. The href Attribute

The <a> tag defines a hyperlink. The href attribute specifies the URL of the page the link goes to:

<a href="https://www.google.com">Visit google</a>

2. The src Attribute

The <img> tag is used to embed an image in an HTML page. The src attribute specifies the path to the image to be displayed:

<img src="img_girl.jpg">

3. The width and height Attributes

The <img> tag should also contain the width and height attributes, which specifies the width and height of the image (in pixels):

<img src="img_girl.jpg" width="500" height="600">

4. The alt Attribute

The required alt attribute for the <img> tag specifies an alternate text for an image, if the image for some reason cannot be displayed. This can be due to slow connection, or an error in the src attribute, or if the user uses a screen reader.

<img src="img_girl.jpg" alt="Girl with a jacket">

III. HTML Styles

The HTML style attribute is used to add styles to an element, such as color, font, size, and more.

1. The style Attribute

The style attribute is used to add styles to an element, such as color, font, size, and more.

2. Background Color

The CSS background-color property defines the background color for an HTML element.

<h1 style="background-color:powderblue;">This is a heading</h1>
<p style="background-color:tomato;">This is a paragraph.</p>

3. Text Color

The CSS color property defines the text color for an HTML element:

<h1 style="color:blue;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>

4. Fonts

The CSS font-family property defines the font to be used for an HTML element:

<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>

5. Text Size

The CSS font-size property defines the text size for an HTML element:

<h1 style="font-size:300%;">This is a heading</h1>
<p style="font-size:160%;">This is a paragraph.</p>

6. Text Alignment

The CSS text-align property defines the horizontal text alignment for an HTML element:

<h1 style="text-align:center;">Centered Heading</h1>
<p style="text-align:center;">Centered paragraph.</p>

IV. HTML Formatting Elements

Formatting elements were designed to display special types of text:

1. HTML <b> Elements

The HTML <b> element defines bold text, without any extra importance.

<b>This text is bold</b>

2. HTML <strong> Elements

The HTML <strong> element defines text with strong importance. The content inside is typically displayed in bold.

<strong>This text is important!</strong>

V. HTML Colors

HTML colors are specified with predefined color names, or with RGB, HEX, HSL, RGBA, or HSLA values.

1. Color Names

In HTML, a color can be specified by using a color name (e.g. Tomato, Orange, DodgerBlue, MediumSeaGreen, Gray, SlateBlue, Violet, LightGray).

2. Background Color

You can set the background color for HTML elements.

3. Text Color

You can set the color of text.

4. Border Color

You can set the color of borders.

5. Color Values

In HTML, colors can also be specified using RGB values, HEX values, HSL values, RGBA values, and HSLA values.

The following three <div> elements have their background color set with RGB, HEX, and HSL values:

rgb(255, 99, 71)
#ff6347
hsl(9, 100%, 64%)

The following two <div> elements have their background color set with RGBA and HSLA values, which adds an Alpha channel to the color (here we have 50% transparency):

rgba(255, 99, 71, 0.5)
hsla(9, 100%, 64%, 0.5)

VI. HTML Tables

HTML tables allow web developers to arrange data into rows and columns.

Define an HTML Table

The <table> tag defines an HTML table.

Each table row is defined with a <tr> tag. Each table header is defined with a <th> tag. Each table data/cell is defined with a <td> tag.

By default, the text in <th> elements are bold and centered.

By default, the text in <td> elements are regular and left-aligned.

<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>

VII. HTML class Attribute

The HTML class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class.

1. Using The class Attribute

The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name.

2. The Syntax For Class

To create a class; write a period (.) character, followed by a class name. Then, define the CSS properties within curly braces {}.

VIII. HTML File Paths

A file path describes the location of a file in a web site's folder structure.

PathDescription
<img src="picture.jpg">The "picture.jpg" file is located in the same folder as the current page
<img src="images/picture.jpg">The "picture.jpg" file is located in the images folder in the current folder
<img src="/images/picture.jpg">The "picture.jpg" file is located in the images folder at the root of the current web
<img src="../picture.jpg">The "picture.jpg" file is located in the folder one level up from the current folder

1. Absolute File Paths

An absolute file path is the full URL to a file:

<img src="https://www.google.com/img/picture.jpg" alt="Mountain">

2. Relative File Paths

A relative file path points to a file relative to the current page.

In the following example, the file path points to a file in the images folder located at the root of the current web:

<img src="/images/picture.jpg" alt="Mountain">

CSS SECTION

Overall, of CSS

CSS saves a lot of work. It can control the layout of multiple web pages all at once.

Example 1:

Example 2:

Here we will show one HTML page displayed with four different stylesheets.

What is CSS?

Cascading Style Sheets (CSS) is used to format the layout of a webpage.

With CSS, you can control the color, font, the size of text, the spacing between elements, how elements are positioned and laid out, what background images or background colors are to be used, different displays for different devices and screen sizes, and much more!

Using CSS

CSS can be added to HTML documents in 3 ways:

  1. Inline - by using the style attribute inside HTML elements
  2. Internal - by link to internal section project
  3. External – by link to an external CSS file out of the project

The most common way to add CSS, is to keep the styles in external CSS files. However, in this tutorial we will use inline and internal styles, because this is easier to demonstrate, and easier for you to try it yourself.

Inline CSS

An inline CSS is used to apply a unique style to a single HTML element.

An inline CSS uses the style attribute of an HTML element.

The following example sets the text color of the <h1> element to blue, and the text color of the <p> element to red:

<h1 style="color:blue;">A Blue Heading</h1>

<p style="color:red;">A red paragraph.</p>

Internal CSS

An internal CSS is used to define a style for a single CSS file with the correct class name.

.message {
  color: green;
}

CSS Selector

A CSS rule-set consists of a selector and a declaration block:

p {
  color: red;
  text-align: center;
}

The CSS element Selector

The element selector selects HTML elements based on the element name.

Example: Here, all <p> elements on the page will be center-aligned, with a red text color:

p {
  text-align: center;
  color: red;
}

The CSS class Selector

The class selector selects HTML elements with a specific class attribute.

To select elements with a specific class, write a period (.) character, followed by the class name.

Example 1: In this example all HTML elements with class="center" will be red and center-aligned:

<p class="center">This paragraph refers to two classes.</p>

.center {
  text-align: center;
  color: red;
}

Note: you can style multiple class

Example 2: In this example the <p> element will be styled according to class="center" and to class="large":

<p class="center large">This paragraph refers to two classes.</p>

The CSS Universal Selector

The universal selector (*) selects all HTML elements on the page.

Example: The CSS rule below will affect every HTML element on the page:

* {
  text-align: center;
  color: blue;
}

The CSS Grouping Selector

The grouping selector selects all the HTML elements with the same style definitions.

Look at the following CSS code (the h1, h2, and p elements have the same style definitions):

h1, h2, p {
  text-align: center;
  color: red;
}

CSS Comments

Comments are used to explain the code, and may help when you edit the source code at a later date.

Comments are ignored by browsers. A CSS comment is placed inside the <style> element, and starts with /* and ends with */:

/* This is a single-line comment */
p {
  color: red;
}

Example 2: You can add comments wherever you want in the code:

p {
  color: red; /* Set text color to red */
}

Example 3: Comments can also span multiple lines:

/* This is
a multi-line
comment */
p {
  color: red;
}

CSS Backgrounds

CSS background-color

The background-color property specifies the background color of an element.

body {
  background-color: lightblue;
}

With CSS, a color is most often specified by:

Opacity / Transparency

The opacity property specifies the opacity/transparency of an element. It can take a value from 0.0 - 1.0. The lower value, the more transparent:

.className {
  background-color: green;
  opacity: 0.3;
}

Transparency using RGBA

If you do not want to apply opacity to child elements, like in our example above, use RGBA color values. The following example sets the opacity for the background color and not the text:

.claseName {
  background: rgba(0, 128, 0, 0.3) /* Green background with 30% opacity */
}

CSS Background Image

CSS background-image

The background-image property specifies an image to use as the background of an element. By default, the image is repeated so it covers the entire element.

body {
  background-image: url("paper.gif");
}

CSS background-repeat

By default, the background-image property repeats an image both horizontally and vertically. Some images should be repeated only horizontally or vertically like this:

body {
  background-image: url("gradient_bg.png");
  background-repeat: repeat-x;
}

CSS background-repeat: no-repeat

Showing the background image only once is also specified by the background-repeat property:

body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
}

Note: In the example above, the background image is placed in the same place as the text. We want to change the position of the image, so that it does not disturb the text too much.

CSS background-position

The background-position property is used to specify the position of the background image.

body {
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
}

CSS background - Shorthand property

To shorten the code, it is also possible to specify all the background properties in one single property. This is called a shorthand property. Instead of writing:

body {
  background-color: #ffffff;
  background-image: url("img_tree.png");
  background-repeat: no-repeat;
  background-position: right top;
}

You can use the shorthand property background:

body {
  background: #ffffff url("img_tree.png") no-repeat right top;
}

CSS Borders

CSS Border Properties

The CSS border properties allow you to specify the style, width, and color of an element's border.

CSS Border Style

The border-style property specifies what kind of border to display. The following values are allowed:

The border-style property can have from one to four values (for the top border, right border, bottom border, and the left border).

CSS Border - Shorthand Property

The border property is a shorthand property for the following individual border properties:

Example 1:

p {
  border: 5px solid red;
}

Example 2:

p {
  border-left: 6px solid red;
  background-color: lightgrey;
}

Example 3:

p {
  border-bottom: 6px solid red;
  background-color: lightgrey;
}

CSS Rounded Borders

The border-radius property is used to add rounded borders to an element:

p {
  border: 2px solid red;
  border-radius: 5px;
}

CSS Margins

The CSS margin properties are used to create space around elements, outside of any defined borders.

With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).

Margin - Individual Sides

CSS has properties for specifying the margin for each side of an element:

All the margin properties can have the following values:

Tip: Negative values are allowed.

p {
  margin-top: 100px;
  margin-bottom: 100px;
  margin-right: 150px;
  margin-left: 80px;
}

Margin - Shorthand Property

To shorten the code, it is possible to specify all the margin properties in one property. The margin property is a shorthand property for: margin-top, margin-right, margin-bottom, margin-left.

If the margin property has four values: margin: 25px 50px 75px 100px;

p {
  margin: 25px 50px 75px 100px;
}

If the margin property has three values: margin: 25px 50px 75px; (top 25px; right and left 50px; bottom 75px)

p {
  margin: 25px 50px 75px;
}

If the margin property has two values: margin: 25px 50px; (top and bottom 25px; right and left 50px)

p {
  margin: 25px 50px;
}

If the margin property has one value: margin: 25px; (all four margins are 25px)

p {
  margin: 25px;
}

The auto Value

You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins.

div {
  width: 300px;
  margin: auto;
  border: 1px solid red;
}

CSS Padding

The CSS padding properties are used to generate space around an element's content, inside of any defined borders.

With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left).

Padding - Individual Sides

CSS has properties for specifying the padding for each side of an element:

All the padding properties can have the following values:

Note: Negative values are not allowed.

div {
  padding-top: 50px;
  padding-right: 30px;
  padding-bottom: 50px;
  padding-left: 80px;
}

Padding - Shorthand Property

To shorten the code, it is possible to specify all the padding properties in one property.

If the padding property has four values: padding: 25px 50px 75px 100px; (top 25px; right 50px; bottom 75px; left 100px)

div {
  padding: 25px 50px 75px 100px;
}

If the padding property has three values: padding: 25px 50px 75px; (top 25px; right and left 50px; bottom 75px)

div {
  padding: 25px 50px 75px;
}

If the padding property has two values: padding: 25px 50px; (top and bottom 25px; right and left 50px)

div {
  padding: 25px 50px;
}

If the padding property has one value: padding: 25px; (all four paddings are 25px)

div {
  padding: 25px;
}

Padding and Element Width

The CSS width property specifies the width of the element's content area. The content area is the portion inside the padding, border, and margin of an element (the box model).

So, if an element has a specified width, the padding added to that element will be added to the total width of the element. This is often an undesirable result.

div {
  width: 300px;
  padding: 25px;
}

To keep the width at 300px, no matter the amount of padding, you can use the box-sizing property. This causes the element to maintain its width; if you increase the padding, the available content space will decrease.

div {
  width: 300px;
  padding: 25px;
  box-sizing: border-box;
}

CSS Height and Width

CSS Setting height and width

The height and width properties are used to set the height and width of an element.

The height and width properties do not include padding, borders, or margins. It sets the height/width of the area inside the padding, border, and margin of the element.

CSS height and width Values

The height and width properties may have the following values:

Example 1:

div {
  height: 200px;
  width: 50%;
  background-color: powderblue;
}

Example 2:

div {
  height: 100px;
  width: 500px;
  background-color: powderblue;
}

Setting max-width

The max-width property is used to set the maximum width of an element.

The max-width can be specified in length values, like px, cm, etc., or in percent (%) of the containing block, or set to none (this is default. Means that there is no maximum width).

The problem with the <div> above occurs when the browser window is smaller than the width of the element (500px). The browser then adds a horizontal scrollbar to the page. Using max-width instead, in this situation, will improve the browser's handling of small windows.

Note: The value of the max-width property overrides width.

div {
  max-width: 500px;
  height: 100px;
  background-color: powderblue;
}

All CSS Dimension Properties

PropertyDescription
heightSets the height of an element
max-heightSets the maximum height of an element
max-widthSets the maximum width of an element
min-heightSets the minimum height of an element
min-widthSets the minimum width of an element
widthSets the width of an element

CSS Box Model

All HTML elements can be considered as boxes. 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: margins, borders, padding, and the actual content.

Explanation of the different parts:

The box model allows us to add a border around elements, and to define space between elements.

div {
  width: 300px;
  border: 15px solid green;
  padding: 50px;
  margin: 20px;
}

CSS Outline

An outline is a line that is drawn around elements, OUTSIDE the borders, to make the element "stand out".

CSS has the following outline properties:

CSS Outline Style

The outline-style property specifies the style of the outline, and can have one of the following values:

.classname {
  border: 1px solid black;
  outline-style: solid;
  outline-color: red;
  outline-width: thin;
}

CSS Outline - Shorthand property

The outline property is a shorthand property for setting the following individual outline properties:

classname {outline: dashed;}
classname {outline: dotted red;}
classname {outline: 5px solid yellow;}
classname {outline: thick ridge pink;}

CSS Text

I strongly recommend to use flexbox for text alignment.

Example for practical https://flexboxfroggy.com/

The justify-content property, which aligns items horizontally and accepts the following values:

The align-items property aligns items vertically and accepts the following values:

The flex-direction property defines the direction items are placed in the container, and accepts the following values:

Another property you can apply to individual items is align-self. This property accepts the same values as align-items and its value for the specific item.

The flex-wrap property, which accepts the following values:

The two properties flex-direction and flex-wrap are used so often together that the shorthand property flex-flow was created to combine them. This shorthand property accepts the value of one of the two properties separated by a space. For example, you can use flex-flow: row wrap to set rows and wrap them.

The align-content to set how multiple lines are spaced apart from each other. This property takes the following values:

CSS Fonts

Choosing the right font for your website is important!

Font Selection is Important

Choosing the right font has a huge impact on how the readers experience a website. The right font can create a strong identity for your brand.

Using a font that is easy to read are important. The font adds value to your text. It is also important to choose the correct color and text size for the font.

Some Font Examples

Generic Font FamilyExamples of Font Names
SerifTimes New Roman, Georgia, Garamond
Sans-serifArial, Verdana, Helvetica
MonospaceCourier New, Lucida Console, Monaco
CursiveBrush Script MT, Lucida Handwriting
FantasyCOPPERPLATE, Papyrus

The CSS font-family Property

In CSS, we use the font-family property to specify the font of a text.

.classname {
  font-family: "Lucida Console", "Courier New", monospace;
}

Font Style

The font-style property is mostly used to specify italic text. This property has three values:

.classname1 {
  font-style: normal;
}
.classname2 {
  font-style: italic;
}
.classname3 {
  font-style: oblique;
}

Font Weight

The font-weight property specifies the weight of a font:

.classname1 {
  font-weight: normal;
}
.classname2 {
  font-weight: bold;
}

Font Size

The font-size property sets the size of the text.

Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.

Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for paragraphs.

The font-size value can be an absolute, or relative size.

Absolute size:

Relative size:

Set Font Size With Pixels

Setting the text size with pixels gives you full control over the text size:

h1 {
  font-size: 40px;
}
h2 {
  font-size: 30px;
}
p {
  font-size: 14px;
}

Set Font Size With Em

To allow users to resize the text (in the browser menu), many developers use em instead of pixels.

1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is 16px.

The size can be calculated from pixels to em using this formula: pixels/16=em

h1 {
  font-size: 2.5em; /* 40px/16=2.5em */
}
h2 {
  font-size: 1.875em; /* 30px/16=1.875em */
}
p {
  font-size: 0.875em; /* 14px/16=0.875em */
}

Responsive Font Size

The text size can be set with a vw unit, which means the "viewport width". That way the text size will follow the size of the browser window:

<h1 style="font-size:10vw">Hello World</h1>
<h2>Hello World</h2>

CSS Links

With CSS, links can be styled in different ways.

Styling Links

Links can be styled with any CSS property (e.g. color, font-family, background, etc.).

In addition, links can be styled differently depending on what state they are in.

The four links states are:

/* unvisited link */
a:link {
  color: red;
}
/* visited link */
a:visited {
  color: green;
}
/* mouse over link */
a:hover {
  color: hotpink;
}
/* selected link */
a:active {
  color: blue;
}

Note: When setting the style for several link states, there are some order rules:
• a:hover MUST come after a:link and a:visited
• a:active MUST come after a:hover

Text Decoration

The text-decoration property is mostly used to remove underlines from links:

a:link {
  text-decoration: none;
}
a:visited {
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
a:active {
  text-decoration: underline;
}

CSS Layout - The position Property

The position property specifies the type of positioning method used for an element (static, relative, fixed, absolute or sticky).

There are five different position values:

Elements are then positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the position value.

position: static;

HTML elements are positioned static by default. Static positioned elements are not affected by the top, bottom, left, and right properties.

.static {
  position: static;
  border: 3px solid #73AD21;
}

position: relative;

An element with position: relative; is positioned relative to its normal position. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position.

.relative {
  position: relative;
  left: 30px;
  border: 3px solid #73AD21;
}

position: fixed;

An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. A fixed element does not leave a gap in the page where it would normally have been located.

.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 300px;
  border: 3px solid #73AD21;
}

position: absolute;

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).

However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Note: A "positioned" element is one whose position is anything except static.

.relative {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid #73AD21;
}
.absolute {
  position: absolute;
  top: 80px;
  right: 0;
  width: 200px;
  height: 100px;
  border: 3px solid #73AD21;
}

position: sticky;

An element with position: sticky; is positioned based on the user's scroll position.

A sticky element toggles between relative and fixed, depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).

.sticky {
  position: -webkit-sticky; /* Safari */
  position: sticky;
  top: 0;
  background-color: green;
  border: 2px solid #4CAF50;
}

Overlapping Elements

When elements are positioned, they can overlap other elements.

The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others). An element can have a positive or negative stack order:

.overlap {
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: -1;
}

CSS Layout - Overflow

The CSS overflow property controls what happens to content that is too big to fit into an area.

The overflow property specifies whether to clip the content or to add scrollbars when the content of an element is too big to fit in the specified area. The overflow property has the following values:

overflow: visible

By default, the overflow is visible, meaning that it is not clipped and it renders outside the element's box:

.classname {
  width: 200px;
  height: 50px;
  background-color: #eee;
  overflow: visible;
}

overflow: hidden

With the hidden value, the overflow is clipped, and the rest of the content is hidden:

.classname {
  width: 200px;
  height: 50px;
  background-color: #eee;
  overflow: hidden;
}

overflow: scroll

Setting the value to scroll, the overflow is clipped and a scrollbar is added to scroll inside the box. Note that this will add a scrollbar both horizontally and vertically (even if you do not need it):

.classname {
  width: 200px;
  height: 50px;
  background-color: #eee;
  overflow: scroll;
}

overflow: auto

The auto value is similar to scroll, but it adds scrollbars only when necessary.

overflow-x and overflow-y

The overflow-x and overflow-y properties specifies whether to change the overflow of content just horizontally or vertically (or both):

overflow-x specifies what to do with the left/right edges of the content.
overflow-y specifies what to do with the top/bottom edges of the content.

.classname {
  width: 200px;
  height: 50px;
  background-color: #eee;
  overflow-x: scroll;
  overflow-y: scroll;
}

CSS Combinators

A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator.

There are four different combinators in CSS:

Descendant Selector

The descendant selector matches all elements that are descendants of a specified element.

The following example selects all <p> elements inside <div> elements:

CSS

div p {
  background-color: yellow;
}

HTML

<h2>Descendant Selector</h2>
<p>The descendant selector matches all elements that are descendants of a specified element.</p>

<div>
  <p>Paragraph 1 in the div.</p>
  <p>Paragraph 2 in the div.</p>
  <section><p>Paragraph 3 in the div.</p></section>
</div>

<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>

Child Selector (>)

The child selector selects all elements that are the children of a specified element.

The following example selects all <p> elements that are children of a <div> element:

CSS

div > p {
  background-color: yellow;
}

HTML

<h2>Child Selector</h2>
<p>The child selector (>) selects all elements that are the children of a specified element.</p>

<div>
  <p>Paragraph 1 in the div.</p>
  <p>Paragraph 2 in the div.</p>
  <section><p>Paragraph 3 in the div.</p></section> <!-- not Child but Descendant -->
  <p>Paragraph 4 in the div.</p>
</div>

CSS Opacity / Transparency

The opacity property specifies the opacity/transparency of an element.

Transparent Image

The opacity property can take a value from 0.0 - 1.0. The lower value, the more transparent.

Transparent Hover Effect

The opacity property is often used together with the :hover selector to change the opacity on mouse-over:

CSS

img {
  opacity: 0.5;
}
img:hover {
  opacity: 1.0;
}

HTML

<h1>Image Transparency</h1>
<p>The opacity property is often used together with the :hover selector to change the opacity on mouse-over:</p>
<img src="img_forest.jpg" alt="Forest" width="170" height="100">
<img src="img_mountains.jpg" alt="Mountains" width="170" height="100">
<img src="img_5terre.jpg" alt="Italy" width="170" height="100">

Text in Transparent Box

CSS

.background {
  background: url(klematis.jpg) repeat;
  border: 2px solid black;
}
.transbox {
  margin: 30px;
  background-color: #ffffff;
  border: 1px solid black;
  opacity: 0.6;
}
.transbox p {
  margin: 5%;
  font-weight: bold;
  color: #000000;
}

HTML

<div className="background">
  <div className="transbox">
    <p>This is some text that is placed in the transparent box.</p>
  </div>
</div>

CSS Tables

To specify table borders in CSS, use the border property.

To specifies a black border for <table>, <th>, <tr> and <td> elements.

CSS Forms

Forms can be styled with CSS to control inputs, selects, text areas, and submit buttons (e.g. First Name, Last Name, Country, Subject, Submit).

Website Layout

A website is often divided into headers, menus, content and a footer:

CSS Units

CSS has several different units for expressing a length.

Many CSS properties take "length" values, such as width, margin, padding, font-size, etc.

Length is a number followed by a length unit, such as 10px, 2em, etc.

There are two types of length units: absolute and relative.

Absolute Lengths

The absolute length units are fixed and a length expressed in any of these will appear as exactly that size.

Absolute length units are not recommended for use on screen, because screen sizes vary so much. However, they can be used if the output medium is known, such as for print layout.

UnitDescription
cmcentimeters
mmmillimeters
ininches (1in = 96px = 2.54cm)
px *pixels (1px = 1/96th of 1in)
ptpoints (1pt = 1/72 of 1in)
pcpicas (1pc = 12 pt)

Relative Lengths

Relative length units specify a length relative to another length property. Relative length units scales better between different rendering mediums.

UnitDescription
emRelative to the font-size of the element (2em means 2 times the size of the current font)
exRelative to the x-height of the current font (rarely used)
chRelative to width of the "0" (zero)
remRelative to font-size of the root element
vwRelative to 1% of the width of the viewport*
vhRelative to 1% of the height of the viewport*
vminRelative to 1% of viewport's* smaller dimension
vmaxRelative to 1% of viewport's* larger dimension
%Relative to the parent element

CSS Rounded Corners

With the CSS border-radius property, you can give any element "rounded corners".