Internet_Programming(IP)_Unit_04
Internet_Programming(IP)_Unit_04
What is CSS
CSS Rules
The id Selector
We can also define styles for HTML elements with the id selector. The id selector is defined
as a #. The style rule below will match the element that has an id attribute with a value of
"yellow":
#yellow {color:yellow}
Note: Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.
CSS Comments
Comments are used to explain our code, and may help us when we edit the source code at a
later date. A comment will be 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
}
CSS How To Use CSS
There are four ways of inserting a style sheet:
Inline style
Internal style sheet
External style sheet
Imported sytle Sheet
Inline Styles
An inline style loses many of the advantages of style sheets by mixing content with
presentation. To use inline styles we 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 CSS demo of Pragraph</p>
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!
<head>
<style type="text/css">
{
hr {color:sienna}
p {margin-left:20px}
body {background-image:url("images/back40.gif")
}
</style>
</head>
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
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:
body {background-color:#b0c4de}
In the example below, the h1, p, and div elements have different background colors:
h1 {background-color:#6495ed}
p {background-color:#e0ffff}
div {background-color:#b0c4de}
Example:
<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 W3Schools.com example.</p>
</body>
</html>
Background Image
body {background-image:url('bgdesert.jpg')}
<html>
<head>
<style type="text/css">
body {background-image:url('paper.gif')}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
By default, the background-image property repeats an image both horizontally and vertically.
<html>
<head>
<style type="text/css">
body
{
background-image:url('gradient2.png');
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
If the image is repeated only horizontally (repeat-x), the background will look better:
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
}
<html>
<head>
<style type="text/css">
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
When using a background image, use an image that does not disturb the text.
body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
}
body
{
background-image:url('img_tree.png');
background-repeat:no-repeat;
background-position:top right;
}
<html>
<head>
<style type="text/css">
body
{
Background-image:url('img.png');
Background-repeat: no-repeat;
Background-position: top right;
Margin-right: 200px;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>set background no-repeat, set postion example.</p>
<p>Now the background image is only show once, and positioned away from the text.</p>
<p>In this example we have also added a margin on the right side, so the background image will
never disturb the text.</p>
</body>
</html>
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or
CSS2).
CSS Text
The CSS text properties define the appearance of text:
text example
Text Color
The color property is used to set the color of the text. The color can be specified by:
body {color:blue}
h1 {color:#00ff00}
h2 {color:rgb(255,0,0)}
<html>
<head>
<style type="text/css">
body {color:red}
h1 {color:#00ff00}
p.ex {color:rgb(0,0,255)}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<p>This is an ordinary paragraph. Notice that this text is red. The default text-color for a page is
defined in the body selector.</p>
<p class="ex">This is a paragraph with class="ex". This text is blue.</p>
</body>
</html>
Text Alignment
The text-align property is used to set the horizontal alignment of a text.Text can be centered, or
aligned to the left or right, or justified.
When text-align is set to "justify", each line is stretched so that every line has equal width, and the
left and right margins are straight (like in magazines and newspapers).
h1 {text-align:center}
p.date {text-align:right}
p.main {text-align:justify}
<html>
<head>
<style type="text/css">
h1 {text-align:center}
p.date {text-align:right}
p.main {text-align:justify}
</style>
</head>
<body>
<h1>CSS text-align Example</h1>
<p class="date">May, 2009</p>
<p class="main">In my wenger and more vulnerable years my father gave me some advice that
I've been turning over in my mind ever since. 'Whenever we feel like criticizing anyone,' he told
me, just remember that all the people in this world haven't had the advantages that we've
had.'</p>
<p><b>Note:</b> Try to resize the browser window to see how justify works.</p>
</body>
</html>
Text Decoration
The text-decoration property is used to set or remove decorations from text.
The text-decoration property is mostly used to remove underlines from links for design purposes:
h1 {text-decoration:overline}
h2 {text-decoration:line-through}
h3 {text-decoration:underline}
h4 {text-decoration:blink}
<html>
<head>
<style type="text/css">
h1 {text-decoration:overline}
h2 {text-decoration:line-through}
h3 {text-decoration:underline}
h4 {text-decoration:blink}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
<h4>This is heading 4</h4>
<p><b>Note:</b> The "blink" value is not supported in IE, Chrome, or Safari.</p>
</body>
</html>
Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.
p.uppercase {text-transform:uppercase}
p.lowercase {text-transform:lowercase}
p.capitalize {text-transform:capitalize}
It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of
each word.
<html>
<head>
<style type="text/css">
p.uppercase {text-transform:uppercase}
p.lowercase {text-transform:lowercase}
p.capitalize {text-transform:capitalize}
</style>
</head>
<body>
<p class="uppercase">This is some text.</p>
<p class="lowercase">This is some text.</p>
<p class="capitalize">This is some text.</p>
</body>
</html>
Text Indentation
The text-indentation property is used to specify the indentation of the first line of a text.
p {text-indent:50px}
<html>
<head>
<style type="text/css">
p {text-indent:100px}
</style>
</head>
<body>
<p>In my younger and more vulnerable years my father gave me some advice that I've been
turning over in my mind ever since. 'Whenever we feel like criticizing anyone,' he told me, just
remember that all the people in this world haven't had the advantages that we've had.'</p>
</body>
</html>
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or
CSS2).
CSS Font
CSS font properties define the font family, boldness, size, and the style of a text.
<html>
<head>
<style type="text/css">
p.serif {font-family:"Times New Roman",Georgia,Serif;
font color=”red”;
font size=36px}
p.sansserif{font-family:Arial,Verdana,Sans-serif }
div.msc{ font-family:Arial,Verdana,Sans-serif}
</style>
</head>
<body>
<h1>CSS font-family</h1>
<p class="serif">This is a paragraph, shown in the Times New Roman font.</p>
<p class="sansserif">This is a paragraph, shown in the Arial font.</p>
</body>
</html>
Font Style
The font-style property is mostly used to specify italic text.This property has three values:
p.normal {font-style:normal}
p.italic {font-style:italic}
p.oblique {font-style:oblique}
<html>
<head> <style type="text/css">
p.normal {font-style:normal}
p.italic {font-style:italic}
p.oblique {font-style:oblique}
</style>
</head>
<body>
<p class="normal">This is a paragraph, normal.</p>
<p class="italic">This is a paragraph, italic.</p>
<p class="oblique">This is a paragraph, oblique.</p>
</body>
</html>
Font Size
Being able to manage the text size is important in web design. However, we 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.
Absolute size:
Relative size:
If we do not specify a font size, the default size for normal text, like paragraphs, is 16px
(16px=1em).
Setting the text size with pixels, gives we full control over the text size:
h1 {font-size:40px}f
h2 {font-size:30px}
p {font-size:14px}
<html>
<head>
<style>
h1 {font-size:40px}
h2 {font-size:30px}
p {font-size:14px}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
</body>
</html>
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or
CSS2).
All HTML elements can be considered as boxes. In CSS, the term "box model" is used when talking
about design and lawet.
The CSS box model is essentially a box that wraps around HTML elements, and it consists of:
margins, borders, padding, and the actual content.The box model allows us to place a border
around elements and space elements in relation to other elements.
Margin - Clears an area around the border. The margin does not have a background color,
and it is completely transparent
Border - A border that lies around the padding and content. The border is affected by the
background color of the box
Padding - Clears an area around the content. The padding is affected by the background
color of the box
Content - The content of the box, where text and images appear
In order to set the width and height of an element correctly in all browsers, we need to know how
the box model works.
CSS Border
Border Style
None of the other border properties will have any effect unless border-style is set.
border-style Values
The border-width property is used to set the width of the border.The width is set in pixels, or by
using one of the three pre-defined values: thin, medium, or thick.
Note: The "border-width" property does not work if it is used alone. Use the "border-style"
property to set the borders first.
p.one
{
border-style:solid;
border-width:5px;
}
p.two
{
border-style:solid;
border-width:medium;
}
<html>
<head>
<style type="text/css">
p.one
{
border-style:solid;
border-width:5px;
}
p.two
{
border-style:solid;
border-width:medium;
}
p.three
{
border-style:solid;
border-width:1px;
}
</style>
</head>
<body>
<p class="one">Some text.</p>
<p class="two">Some text.</p>
<p class="three">Some text.</p>
<p><b>Note:</b> The "border-width" property does not work if it is used alone. Use the "border-
style" property to set the borders first.</p>
</body>
</html>
Border Color
The border-color property is used to set the color of the border. The color can be set by:
<html>
<head>
<style type="text/css">
p.one
{
border-style:solid;
border-color:red;
}
p.two
{
border-style:solid;
border-color:#98bf21;
}
</style>
</head>
<body>
<p class="one">A solid red border</p>
<p class="two">A solid green border</p>
<p><b>Note:</b> The "border-color" property does not work if it is used alone. Use the "border-
style" property to set the borders first.</p>
</body>
</html>
Border - Individual sides
<html>
<head>
<style type="text/css">
p
{
border-top-style:dotted;
border-right-style:solid;
border-bottom-style:dotted;
border-left-style:solid;
}
</style>
</head>
<body>
<p>2 different border styles.</p>
</body>
</html>
border-style:dotted solid;
o top and bottom borders are dotted
o right and left borders are solid
border-style:dotted;
o all four borders are dotted
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or
CSS2).
CSS Margin
Margin
The margin clears an area around an element (outside the border). The margin does not have a
background color, and is completely transparent.
The top, right, bottom, and left margin can be changed independently using separate properties. A
shorthand margin property can also be used, to change all margins at once.
margin-top:100px;
margin-bottom:100px;
margin-right:50px;
margin-left:50px;
<html>
<head>
<style type="text/css">
p
{
background-color:yellow;
}
p.margin
{
margin-top:100px;
margin-bottom:100px;
margin-right:50px;
margin-left:50px;
}
</style>
</head>
Example :
<html>
<head>
<style type="text/css">
p
{
background-color:yellow;
}
p.margin
{
margin:100px 50px;
}
</style>
</head>
<body>
<p>This is a paragraph with no specified margins.</p>
<p class="margin">This is a paragraph with specified margins.</p>
</body>
</html>
margin:25px 50px;
o top and bottom margins are 25px
o right and left margins are 50px
margin:25px;
o all four margins are 25px
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or
CSS2).
The CSS padding properties define the space between the element border and the element
content.
Padding
The padding clears an area around the content (inside the border) of an element. The padding is
affected by the background color of the element.The top, right, bottom, and left padding can be
changed independently using separate properties. A shorthand padding property can also be used,
to change all paddings at once.
padding-top:25px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;
<html>
<head>
<style type="text/css">
p
{
background-color:yellow;
}
p.padding
{
padding-top:15px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;
}
</style>
</head>
<body>
<p>This is a paragraph with no specified padding.</p>
<p class="padding">This is a paragraph with specified paddings.</p>
</body>
</html>
Example:
<html>
<head>
<style type="text/css">
p
{
background-color:yellow;
}
p.padding
{
padding:50px 25px;
}
</style>
</head>
<body>
<p>This is a paragraph with no specified padding.</p>
<p class="padding">This is a paragraph with specified paddings.</p>
</body>
</html>
padding:25px 50px;
o top and bottom paddings are 25px
o right and left paddings are 50px
padding:25px;
o all four paddings are 25px
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or
CSS2).
CSS List
The CSS list properties allow us to place the list item marker, change between different list item
markers, or set an image as the list item marker.
List
unordered list - the list items are marked with bullets (typically circles or squares)
ordered list - the list items are marked with numbers or letters
With CSS, lists can be styled further, and images can be used as list item markers.
It is possible to specify the type of list item marker with the list-style-type property:
ul.circle {list-style-type:circle}
ul.square {list-style-type:square}
ol.upper-roman {list-style-type:upper-roman}
ol.lower-alpha {list-style-type:lower-alpha}
<html>
<head>
<style type="text/css">
ul.circle {list-style-type:circle}
ul.square {list-style-type:square}
ol.upper-roman {list-style-type:upper-roman}
ol.lower-alpha {list-style-type:lower-alpha}
</style>
</head>
<body>
<p>Type circle:</p>
<ul class="circle">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<p>Type square:</p>
<ul class="square">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<p>Type upper-roman:</p>
<ol class="upper-roman">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<p>Type lower-alpha:</p>
<ol class="lower-alpha">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
</body>
</html>
Some of the values are for unordered lists, and some for ordered lists.
Value Description
None No marker
Disc Default. The marker is a filled circle
Circle The marker is a circle
Square The marker is a square
Value Description
None No marker
Circle The marker is a circle
Disc The marker is a filled circle. This is default
Square The marker is a square
decimal-leading-zero The marker is a number padded by initial zeros (01, 02, 03, etc.)
Internet Explorer does not support all property values for ordered lists.
<html>
<head>
<style type="text/css">
ul
{
list-style-image:url('arrow.gif');
list-style-type:square;
}
</style>
</head>
<body>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
ul
{
list-style-type:none;
padding:0px;
margin:0px;
}
li
{
background-image:url(arrow.gif);
background-repeat:no-repeat;
background-position:0px 5px;
padding-left:14px;
}
Example explained:
For ul:
o Set the list-style-type to none to remove the list item marker
o Both padding and margin must be set to 0px for cross-browser compatibility
For li:
o Set the URL of the image, and show it only once (no-repeat)
o Use the background-position property to place the image where we want it (left 0px
and down 5px)
o Use the padding-left property to position the text in the list
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or
CSS2).