+ All Categories
Home > Mobile > Basics of css3

Basics of css3

Date post: 02-Jul-2015
Category:
Upload: rakesh-jha
View: 172 times
Download: 0 times
Share this document with a friend
Description:
Introduction to CSS3 Text Formatting Selectors Box Model Links, Backgrounds Lists, Tables Positioning & Layout New features of CSS3 - transition, tranform, etc. Introduction to jQueryMobile Hands-on exercises
101
CSS 3 Rakesh Kumar Jha M. Tech, MBA
Transcript
Page 1: Basics of css3

CSS 3

Rakesh Kumar Jha

M. Tech, MBA

Page 2: Basics of css3

Contents Introduction to CSS3

Text Formatting

Selectors

Box Model

Links, Backgrounds

Lists, Tables

Positioning & Layout

New features of CSS3 - transition, tranform, etc.

Introduction to jQueryMobile

Hands-on exercises

Page 3: Basics of css3

• Save a lot of work with CSS! • how to use CSS to control the style and layout of multiple Web pages all at once. <html> <head> <style type="text/css"> body { background-color:#d0e4fe; } h1 { color:orange; text-align:center; } p { font-family:"Times New Roman"; font-size:20px; } </style> </head> <body> <h1>CSS example!</h1> <p>This is a paragraph.</p> </body> </html>

Page 4: Basics of css3

CSS Introduction

• Before you continue you should have a basic understanding of the following:

– HTML / XHTML

Page 5: Basics of css3

What is CSS ?

What is CSS?

• CSS stands for Cascading Style Sheets

• Styles define how to display HTML elements

• Styles were added to HTML 4.0 to solve a problem

• External Style Sheets can save a lot of work

Page 6: Basics of css3

Styles Solved a Big Problem

• HTML was never intended to contain tags for formatting a document.

• HTML was intended to define the content of a document, like: – <h1>This is a heading</h1> – <p>This is a paragraph.</p>

• When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web developers. Development of large web sites, where fonts and color information were added to every single page, became a long and expensive process.

• To solve this problem, the World Wide Web Consortium (W3C) created CSS.

Page 7: Basics of css3

Styles Solved a Big Problem

• In HTML 4.0, all formatting could be removed from the HTML document, and stored in a separate CSS file.

• All browsers support CSS today.

Page 8: Basics of css3

CSS Saves a Lot of Work!

• Styles are normally saved in external .css files.

• External style sheets enable you to change the appearance and layout of all the pages in a Web site, just by editing one single file

Page 9: Basics of css3

CSS Syntax

• A CSS rule has two main parts: a selector, and one or more declarations:

• The selector is normally the HTML element you want to style.

• Each declaration consists of a property and a value.

• The property is the style attribute you want to change. Each property has a value.

Page 10: Basics of css3

Some CSS Example

• A CSS declaration always ends with a semicolon, and declaration groups are surrounded by curly brackets

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

• To make the CSS more readable, you can put one declaration on each line, like this:

P

{

color:red;

text-align:center;

}

Page 11: Basics of css3

CSS Comments

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

• A CSS comment begins with "/*", and ends with "*/", like this:

/*This is a comment*/ p { text-align:center;

/*This is another comment*/

color:black; font-family:arial; }

Page 12: Basics of css3

CSS Id and Class

The id and class Selectors

• In addition to setting a style for a HTML element, CSS allows you to specify your own selectors called "id" and "class".

Page 13: Basics of css3

The id Selector

• The id selector is used to specify a style for a single, unique element. • The id selector uses the id attribute of the HTML element, and is defined with a

"#". • The style rule below will be applied to the element with id="para1":

<html> <head> <style type="text/css"> #para1 { text-align:center; color:red; } </style> </head> <body> <p id="para1">Hello World!</p> <p>This paragraph is not affected by the style.</p> </body> </html> Note - Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.

Page 14: Basics of css3

The class Selector

• The class selector is used to specify a style for a group of elements.

• Unlike the id selector, the class selector is most often used on several elements.

• This allows you to set a particular style for many HTML elements with the same class.

• The class selector uses the HTML class attribute, and is defined with a "."

• In the example below, all HTML elements with class="center" will be center-aligned:

Page 15: Basics of css3

The class Selector

<html> <head> <style type="text/css"> .center { text-align:center; } </style> </head>

<body>

<h1 class="center">Center-aligned heading</h1> <p class="center">Center-aligned paragraph.</p>

</body>

</html>

Page 16: Basics of css3

The class Selector

• You can also specify that only specific HTML elements should be affected by a class.

• In the example below, all p elements with class="center" will be center-aligned: <html> <head>

<style type="text/css"> p.center { text-align:center; } </style>

</head> <body>

<h1 class="center">This heading will not be affected</h1> <p class="center">This paragraph will be center-aligned.</p>

</body> </html>

Do NOT start a class name with a number! This is only supported in Internet Explorer.

Page 17: Basics of css3

Three Ways to Insert CSS

There are three ways of inserting a style sheet:

– External style sheet

– Internal style sheet

– Inline style

Page 18: Basics of css3

External Style Sheet

• An external style sheet is ideal when the style is applied to many pages.

• With an external style sheet, you can change the look of an entire Web site by changing one file.

• Each page must link to the style sheet using the <link> tag.

• The <link> tag goes inside the head section: <head>

<link rel="stylesheet" type="text/css" href="mystyle.css" />

</head>

Page 19: Basics of css3

External Style Sheet

• An external style sheet can be written in any text editor.

• The file should not contain any html tags.

• style sheet should be saved with a .css extension.

• Example hr {color:sienna;}

p {margin-left:20px;} body {background-image:url("images/back40.gif");}

Do not leave spaces between the property value and the units! "margin-left:20 px" (instead of "margin-left:20px") will work in IE, but not in Firefox or Opera.

Page 20: Basics of css3

Internal Style Sheet

• An internal style sheet should be used when a single document has a unique style.

• You define internal styles in the head section of an HTML page, by using the <style> tag, like this:

<head> <style type="text/css"> hr {color:sienna;} p {margin-left:20px;} body {background-image:url("images/back40.gif");} </style> </head>

Page 21: Basics of css3

Inline Styles

• An inline style loses many of the advantages of style sheets by mixing content with presentation.

• Use this method sparingly

• To use inline styles you use the style attribute in the relevant tag.

• The style attribute can contain any CSS property.

• The example shows how to change the color and the left margin of a paragraph:

– <p style="color:sienna;margin-left:20px">This is a paragraph.</p>

Page 22: Basics of css3

Multiple Style Sheets

• If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet.

• For example, an external style sheet has these properties for the h3 selector:

h3 { color:red; text-align:left; font-size:8pt; }

Page 23: Basics of css3

Multiple Style Sheets

• And an internal style sheet has these properties for the h3 selector:

h3 { text-align:right; font-size:20pt; }

If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:

h3 { color:red; text-align:right; font-size:20pt; }

The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet

Page 24: Basics of css3

Multiple Styles Will Cascade into One

Styles can be specified: • inside an HTML element

• inside the head section of an HTML page

• in an external CSS file

• Tip: Even multiple external style sheets can be referenced inside a single HTML document.

Page 25: Basics of css3

Cascading order

• What style will be used when there is more than one style specified for an HTML element?

• Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority:

• Browser default • External style sheet • Internal style sheet (in the head section) • Inline style (inside an HTML element)

• So, an inline style (inside an HTML element) has the highest priority, • Note: If the link to the external style sheet is placed after the

internal style sheet in HTML <head>, the external style sheet will override the internal style sheet

Page 26: Basics of css3

CSS Background

CSS background properties are used to define the background effects of an element.

CSS properties used for background effects: • background-color

• background-image

• background-repeat

• background-attachment

• background-position

Page 27: Basics of css3

Background Color

• The background-color property specifies the background color of an element. • The background color of a page is defined in the body selector:

<html> <head>

<style type="text/css"> body { background-color:#b0c4de; } </style>

</head> <body>

<h1>My CSS web page!</h1> <p>Hello world! This is a www.rkj.com example.</p>

</body> </html>

Page 28: Basics of css3

Background Color

With CSS, a color is most often specified by: • a HEX value - like "#ff0000"

• an RGB value - like "rgb(255,0,0)"

• a color name - like "red“

h1 {background-color:#6495ed;}

p {background-color:#e0ffff;}

div {background-color:#b0c4de;}

Page 29: Basics of css3

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. • The background image for a page can be set like this:

<html> <head>

<style type="text/css"> body {background-image:url('paper.gif');} </style>

</head> <body> <h1>Hello World!</h1> </body> </html>

Page 30: Basics of css3

Background Image - Repeat Horizontally or Vertically

• By default, the background-image property repeats an image both horizontally and vertically.

• Some images should be repeated only horizontally or vertically, if images is small

• If the image is repeated only horizontally (repeat-x), the background will look better:

body { background-image:url('gradient2.png'); background-repeat:repeat-x; }

Page 31: Basics of css3

Background Image - Set position and no-repeat

• When using a background image, use an image that does not disturb the text.

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

background-image:url('img_tree.png'); background-repeat:no-repeat;

The position of the image is specified by the background-position property

background-image:url('img_tree.png'); background-repeat:no-repeat; background-position:right top;

Page 32: Basics of css3

Background - Shorthand property

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

• This is called a shorthand property.

• The shorthand property for background is simply "background": – body {background:#ffffff url('img_tree.png') no-repeat right top;}

Page 33: Basics of css3

Background - Shorthand property

When using the shorthand property the order of the property values are:

• background-color

• background-image

• background-repeat

• background-attachment

• background-position

Page 35: Basics of css3

How to set a fixed background image

• how to set a fixed background image. The image will not scroll with the rest of the page.

body

{

background-image:url('smiley.gif');

background-repeat:no-repeat;

background-attachment:fixed;

}

Page 36: Basics of css3

CSS Text

<html> <head> <style type="text/css"> h1 { text-align: center; text-transform: uppercase; color: #A7C942; } p { text-indent: 50px; text-align:justify; letter-spacing:3px; } a { text-decoration:none; } </style> </head> <body> <h1>text formatting</h1> <p>This text is styled with some of the text formatting properties. The heading uses the text-align, text-transform, and color properties. The paragraph is indented, aligned, and the space between characters is specified. The underline is removed from the <a target="_blank" href="tryit.asp?filename=trycss_text">"Try it yourself"</a> link.</p> </body> </html>

Page 37: Basics of css3

Text Color

<html> <head> <style type="text/css"> h1 { text-align: center; text-transform: uppercase; color: #A7C942; } p { text-indent: 50px; text-align:justify; letter-spacing:3px; } a { text-decoration:none; } </style> </head> <body> <h1>text formatting</h1> <p>This text is styled with some of the text formatting properties. The heading uses the text-align, text-transform, and color properties. The paragraph is indented, aligned, and the space between characters is specified. The underline is removed from the <a target="_blank" href="tryit.asp?filename=trycss_text">"Try it yourself"</a> link.</p> </body> </html>

Page 38: Basics of css3

Selectors

Page 39: Basics of css3

Selector

• CSS selectors allow you to select and manipulate HTML element(s)

• CSS selectors are used to "find" (or select) HTML elements based on their id, classes, types, attributes, values of attributes and much more.

Page 40: Basics of css3

The element Selector

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

Page 41: Basics of css3

The element Selector <!DOCTYPE html>

<html>

<head>

<style>

p {

text-align: center;

color: red;

}

</style>

</head>

<body>

<p>Every paragraph will be affected by the style.</p>

<p id="para1">Me too!</p>

<p>And me!</p>

</body>

</html>

Page 42: Basics of css3

The id Selector

• The id selector uses the id attribute of an HTML tag to find the specific element.

• An id should be unique within a page, so you should use the id selector when you want to find a single, unique element.

Page 43: Basics of css3

The id Selector

<!DOCTYPE html> <html> <head> <style> #para1 { text-align: center; color: red; } </style> </head> <body> <p id="para1">Hello World!</p> <p>This paragraph is not affected by the style.</p> </body> </html>

Page 44: Basics of css3

The class Selector

• The class selector finds elements with the specific class.

• The class selector uses the HTML class attribute.

Page 45: Basics of css3

The class Selector

<!DOCTYPE html> <html> <head> <style> .center { text-align: center; color: red; } </style> </head> <body> <h1 class="center">Red and center-aligned heading</h1> <p class="center">Red and center-aligned paragraph.</p> </body> </html>

Page 46: Basics of css3

The class Selector

You can also specify that only specific HTML elements should be affected by a class <!DOCTYPE html> <html> <head> <style> p.center { text-align: center; color: red; } </style> </head> <body> <h1 class="center">This heading will not be affected</h1> <p class="center">This paragraph will be red and center-aligned.</p> </body> </html>

Page 47: Basics of css3

Grouping Selectors

• In style sheets there are often elements with the same style:

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

Page 48: Basics of css3

Grouping Selectors

• To minimize the code, you can group selectors.

• To group selectors, separate each selector with a comma.

Page 49: Basics of css3

Grouping Selectors

<!DOCTYPE html> <html> <head> <style> h1, h2, p { text-align: center; color: red; } </style> </head> <body> <h1>Hello World!</h1> <h2>Smaller heading!</h2> <p>This is a paragraph.</p> </body> </html>

Page 50: Basics of css3

Box Model

Page 51: Basics of css3

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 HTML elements, and it consists of: margins, borders, padding, and the actual content.

Page 52: Basics of css3

Box Model

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

Page 53: Basics of css3

Box Model

Explanation of the different parts:

– Content - The content of the box, where text and images appear

– Padding - Clears an area around the content. The padding is transparent

– Border - A border that goes around the padding and content

– Margin - Clears an area outside the border. The margin is transparent

Page 54: Basics of css3

Box Model

<!DOCTYPE html> <html> <head> <style> div { background-color: lightgrey; width: 300px; padding: 25px; border: 25px solid navy; margin: 25px; } </style> </head> <body> <div>Good Morning, My Self Rakesh Jha. Welcome to HP Phonegap Training session</div> </body> </html>

Page 55: Basics of css3

Width and Height of an Element

• In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.

Page 56: Basics of css3

Width and Height of an Element

<!DOCTYPE html> <html> <head> <style> div { width: 320px; padding: 10px; border: 5px solid gray; margin: 0; } </style> </head> <body> <img src="klematis4_big.jpg" width="350" height="263" alt="Klematis"> <div>The picture above is 350px wide. The total width of this element is also 350px.</div> </body> </html>

Page 57: Basics of css3

Links, Backgrounds

Page 58: Basics of css3

Backgrounds

• CSS background properties are used to define the background effects of an element.

• CSS properties used for background effects:

– background-color

– background-image

– background-repeat

– background-attachment

– background-position

Page 59: Basics of css3

Background Color

CSS background <!DOCTYPE html> <html> <head> <style> body { background-color: #FF0000; } </style> </head> <body> <h1>My CSS web page!</h1> <p>Hello world! This is a W3Schools.com example.</p> </body> </html> properties are used to define the background effects of an element. CSS properties used for background effects:

background-color background-image background-repeat background-attachment background-position

Page 60: Basics of css3

Background Color

<!DOCTYPE html> <html> <head> <style> h1 { background-color: #6495ed; } p { background-color: #e0ffff; } div { background-color: #b0c4de; } </style> </head> <body> <h1>CSS background-color example!</h1> <div> This is a text inside a div element. <p>This paragraph has its own background color.</p> We are still in the div element. </div> </body> </html>

Page 61: Basics of css3

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.

Page 62: Basics of css3

Background Image

<!DOCTYPE html> <html> <head> <style> body { background-image: url("paper.gif"); } </style> </head> <body> <h1>Hello World!</h1> </body> </html>

Page 63: Basics of css3

Background Image - Repeat Horizontally or Vertically

<!DOCTYPE html> <html> <head> <style> body { background-image: url("gradient_bg.png"); } </style> </head> <body> <h1>Hello World!</h1> </body> </html>

Page 64: Basics of css3

Background Image - Repeat Horizontally or Vertically

<!DOCTYPE html> <html> <head> <style> body { background-image: url("gradient_bg.png"); background-repeat: repeat-x; } </style> </head> <body> <h1>Hello World!</h1> </body> </html>

Page 65: Basics of css3

Background Image - Set position and no-repeat

<!DOCTYPE html> <html> <head> <style> body { background-image: url("img_tree.png"); background-repeat: no-repeat; } </style> </head> <body> <h1>Hello World!</h1> <p>W3Schools background image example.</p> <p>The background image is only showing once, but it is disturbing the reader!</p> </body> </html>

Page 66: Basics of css3

Background Image - Set position and no-repeat

<!DOCTYPE html> <html> <head> <style> body { background-image: url("img_tree.png"); background-repeat: no-repeat; background-position: right top; margin-right: 200px; } </style> </head> <body> <h1>Hello World!</h1> <p>background no-repeat, set position example.</p> <p>Now the background image is only shown once, </p> <p>n this example we have also added a margin on the right side, so the background image will never disturb the text.</p> </body> </html>

Page 67: Basics of css3

Background - Shorthand property

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

Page 68: Basics of css3

Background - Shorthand property

<!DOCTYPE html> <html> <head> <style> body { background: #ff0000 url("img_tree.png") no-repeat right top; margin-right: 200px; } </style> </head> <body> <h1>Hello World!</h1> <p>Now the background image is only shown once, and it is also positioned away from the text.</p> <p>In this example we have also added a margin on the right side, so that the background image will not disturb the text.</p> </body> </html>

Page 69: Basics of css3

Links

• Links can be styled in different ways.

Page 70: Basics of css3

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: – a:link - a normal, unvisited link

– a:visited - a link the user has visited

– a:hover - a link when the user mouses over it

– a:active - a link the moment it is clicked

Page 71: Basics of css3

Styling Links

• 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

Page 72: Basics of css3

Styling Links <!DOCTYPE html>

<html>

<head>

<style>

/* unvisited link */

a:link {

color: #FF0000;

}

/* visited link */

a:visited {

color: #00FF00;

}

/* mouse over link */

a:hover {

color: #FF00FF;

}

/* selected link */

a:active {

color: #0000FF;

}

</style>

</head>

<body>

<p><b><a href="default.asp" target="_blank">This is a link</a></b></p>

<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p>

<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>

</body>

</html>

Page 73: Basics of css3

Common Link Styles

<!DOCTYPE html> <html> <head> <style> a:link { text-decoration: none; } a:visited { text-decoration: none; } a:hover { text-decoration: underline; } a:active { text-decoration: underline; } </style> </head> <body> <p><b><a href="default.asp" target="_blank">This is a link</a></b></p> <p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p> <p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p> </body> </html>

Page 74: Basics of css3

Background Color For Link

<!DOCTYPE html> <html> <head> <style> a:link { background-color: #B2FF99; } a:visited { background-color: #FFFF85; } a:hover { background-color: #FF704D; } a:active { background-color: #FF704D; } </style> </head> <body> <p><b><a href="default.asp" target="_blank">This is a link</a></b></p> <p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.</p> <p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p> </body> </html>

Page 75: Basics of css3

Lists & Tables

Page 76: Basics of css3

Lists

The CSS list properties allow you to:

– Set different list item markers for ordered lists

– Set different list item markers for unordered lists

– Set an image as the list item marker

Page 77: Basics of css3

Lists

In HTML, there are two types of lists:

– unordered lists - the list items are marked with bullets

– ordered lists - the list items are marked with numbers or letters

• With CSS, lists can be styled further, and images can be used as the list item marker.

Page 78: Basics of css3

Different List Item Markers

• The type of list item marker is specified with the list-style-type property

• Some of the values are for unordered lists, and some for ordered lists.

Page 79: Basics of css3

<!DOCTYPE html> <html> <head> <style> ul.a { list-style-type: circle; } ul.b { list-style-type: square; } ol.c { list-style-type: upper-roman; } ol.d { list-style-type: lower-alpha; } </style> </head> <body> <p>Example of unordered lists:</p> <ul class="a"> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ul> <ul class="b"> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ul> <p>Example of ordered lists:</p> <ol class="c"> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ol> <ol class="d"> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ol> </body> </html>

Page 80: Basics of css3

An Image as The List Item Marker

• The type of list item marker is specified with the list-style-type property:

Page 81: Basics of css3

An Image as The List Item Marker

<!DOCTYPE html> <html> <head> <style> ul { list-style-image: url('sqpurple.gif'); } </style> </head> <body> <ul> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ul> </body> </html>

Page 82: Basics of css3

Crossbrowser Solution

<!DOCTYPE html> <html> <head> <style> ul { list-style-type: none; padding: 0px; margin: 0px; } ul li { background-image: url(sqpurple.gif); background-repeat: no-repeat; background-position: 0px 5px; padding-left: 14px; } </style> </head> <body> <ul> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ul> </body> </html>

Page 83: Basics of css3

List - Shorthand property

• It is also possible to specify all the list properties in one, single property.

• This is called a shorthand property.

• The shorthand property used for lists, is the list-style property:

Page 84: Basics of css3

List - Shorthand property

<!DOCTYPE html> <html> <head> <style> ul { list-style: square url("sqpurple.gif"); } </style> </head> <body> <ul> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ul> </body> </html>

Page 85: Basics of css3

CSS List Properties

Property Description

list-style Sets all the properties for a list in one declaration

list-style-image Specifies an image as the list-item marker

list-style-position Specifies if the list-item markers should appear inside or outside the content flow

list-style-type Specifies the type of list-item marker

Page 86: Basics of css3

List - Shorthand property

Page 87: Basics of css3

Tables

• The look of an HTML table can be greatly improved with CSS

Page 88: Basics of css3

Table Borders

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

• The example below specifies a black border for table, th, and td elements:

Page 89: Basics of css3

Table Borders

<!DOCTYPE html> <html> <head> <style> table, th, td { border: 1px solid black; } </style> </head> <body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> </tr> </table> </body> </html>

Page 90: Basics of css3

Collapse Borders <!DOCTYPE html> <html> <head> <style> table { border-collapse: collapse; } table, td, th { border: 1px solid black; } </style> </head> <body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> </tr> </table> <p><b>Note:</b> If a !DOCTYPE is not specified, the border-collapse property can produce unexpected results in IE8 and earlier versions.</p> </body> </html>

Page 91: Basics of css3

Collapse Borders <!DOCTYPE html> <html> <head> <style> table { border-collapse: collapse; } table, td, th { border: 1px solid black; } </style> </head> <body> <table> <tr> <th>Firstname</th> <th>Lastname</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> </tr> </table> <p><b>Note:</b> If a !DOCTYPE is not specified, the border-collapse property can produce unexpected results in IE8 and earlier versions.</p> </body> </html>

Page 92: Basics of css3

Table Width and Height

• Width and height of a table is defined by the width and height properties.

• The example below sets the width of the table to 100%, and the height of the th elements to 50px:

Page 93: Basics of css3

<!DOCTYPE html> <html> <head> <style> table, td, th { border: 1px solid black; } table { width: 100%; } th { height: 50px; } </style> </head> <body> <table> <tr> <th>Firstname</th> <th>Lastname</th> <th>Savings</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> <td>$100</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> <td>$150</td> </tr> <tr> <td>Joe</td> <td>Swanson</td> <td>$300</td> </tr> <tr> <td>Cleveland</td> <td>Brown</td> <td>$250</td> </tr> </table> </body> </html>

Page 94: Basics of css3

Table Text Alignment

• The text in a table is aligned with the text-align and vertical-align properties.

• The text-align property sets the horizontal alignment, like left, right, or center:

Page 95: Basics of css3

<!DOCTYPE html> <html> <head> <style> table, td, th { border: 1px solid black; } td { text-align: right; } </style> </head> <body> <table> <tr> <th>Firstname</th> <th>Lastname</th> <th>Savings</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> <td>$100</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> <td>$150</td> </tr> <tr> <td>Joe</td> <td>Swanson</td> <td>$300</td> </tr> <tr> <td>Cleveland</td> <td>Brown</td> <td>$250</td> </tr> </table> </body> </html>

Page 96: Basics of css3

Table Padding

• To control the space between the border and content in a table, use the padding property on td and th elements:

Page 97: Basics of css3

<!DOCTYPE html> <html> <head> <style> table, td, th { border: 1px solid black; } td { padding: 15px; } </style> </head> <body> <table> <tr> <th>Firstname</th> <th>Lastname</th> <th>Savings</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> <td>$100</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> <td>$150</td> </tr> <tr> <td>Joe</td> <td>Swanson</td> <td>$300</td> </tr> <tr> <td>Cleveland</td> <td>Brown</td> <td>$250</td> </tr> </table> </body> </html>

Page 98: Basics of css3

Table Color

• The example below specifies the color of the borders, and the text and background color of th elements:

Page 99: Basics of css3

<!DOCTYPE html> <html> <head> <style> table, td, th { border: 1px solid green; } th { background-color: green; color: white; } </style> </head> <body> <table> <tr> <th>Firstname</th> <th>Lastname</th> <th>Savings</th> </tr> <tr> <td>Peter</td> <td>Griffin</td> <td>$100</td> </tr> <tr> <td>Lois</td> <td>Griffin</td> <td>$150</td> </tr> <tr> <td>Joe</td> <td>Swanson</td> <td>$300</td> </tr> <tr> <td>Cleveland</td> <td>Brown</td> <td>$250</td> </tr> </table> </body> </html>

Page 100: Basics of css3

Table Color

• The example below specifies the color of the borders, and the text and background color of th elements:

Page 101: Basics of css3

Positioning & Layout


Recommended