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.
Note: The <td>
elements are the data containers of the table. They can contain all sorts of HTML elements; text, images, lists, other tables, etc.
HTML Table - Add a Border
To add a border to a table, use the CSS border
property:
HTML Table - Collapsed Borders
To let the borders collapse into one border, add the CSS border-collapse
property:
HTML Table - Add Cell Padding
Cell padding specifies the space between the cell content and its borders.
If you do not specify a padding, the table cells will be displayed without padding.
To set the padding, use the CSS padding
property:
HTML Table - Left-align Headings
By default, table headings are bold and centered.
To left-align the table headings, use the CSS text-align
property:
HTML Table - Add Border Spacing
Border spacing specifies the space between the cells.
To set the border spacing for a table, use the CSS border-spacing
property:
HTML Table - Cell that Spans Many Columns
To make a cell span more than one column, use the colspan
attribute:
HTML Table - Cell that Spans Many Rows
To make a cell span more than one row, use the rowspan
attribute:
A Special Style for One Table
To define a special style for one particular table, add an id
attribute to the table:
What is CSS?
-
CSS stands for Cascading Style Sheets
-
CSS describes how HTML elements are to be displayed on screen, paper, or in other media
-
CSS saves a lot of work. It can control the layout of multiple web pages all at once
-
External stylesheets are stored in CSS files
Why Use CSS?
CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.
CSS Syntax
The selector points to the HTML element you want to style.
The declaration block contains one or more declarations separated by semicolons.
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded by curly braces.
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 */
:
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
-
Simple selectors (select elements based on name, id, class)
-
Combinator selectors (select elements based on a specific relationship between them)
-
Pseudo-class/ˈsuːdoʊ/ 假的 selectors (select elements based on a certain state)
-
Pseudo-elements selectors (select and style a part of an element)
-
Attribute selectors (select elements based on an attribute or attribute value)
The CSS element Selector
The element selector selects HTML elements based on the element name.
The CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the element.
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.
The CSS Universal Selector
The universal selector (*) selects all HTML elements on the page.
The CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.
Summary
Selector | Example | Example description |
---|---|---|
#firstname | Selects the element with id="firstname" | |
.intro | Selects all elements with class="intro" | |
p.intro | Selects only <p> elements with class="intro" | |
* | Selects all elements | |
p | Selects all <p> elements | |
div, p | Selects all <div> elements and all <p> |