Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Monday, 1 August 2011

Part 10 - Positioning | CSS

CSS Position

With the knowledge of CSS Positioning you will be able to manipulate the exact position of your HTML elements. Designs that previously required the use of JavaScript or HTML image maps may now be done entirely in CSS. Not only is it easier to code, but it also loads much quicker!

Position Relative

Relative positioning changes the position of the HTML element relative to where it normally appears. If we had a header that appears at the top of our page, we could use relative positioning to move it a bit to the right and down a couple of pixels. Below is an example.

CSS Code:

h3 { 
  position: relative; 
 top: 15px;
 left: 150px;
}
p { 
  position: relative; 
 left: -10px;
}
You probably noticed that you define the four possible directions (left, right, up, and down) using only two (left and top). Here's a quick reference when moving HTML elements in CSS.
  • Move Left - Use a negative value for left.
  • Move Right - Use a positive value for left.
  • Move Up - Use a negative value for top.
  • Move Down - Use a positive value for top.

Display:

Before CSS After Positioning

Position Absolute

With absolute positioning, you define the exact pixel value where the specified HTML element will appear. The point of origin is the top-left of the browser's viewable area, so be sure you are measuring from that point.
Note: Firefox does not currently interpret absolute positioning correctly. However both IE 6.0+ and Opera 8.0+ do.

CSS Code:

h3 { 
 position: absolute; 
 top: 50px;
 left: 45px;
}
p{ 
 position: absolute; 
 top: 75px;
 left: 75px;
}

Display:

Before CSS
After Positioning
Specifying a direction with absolute positioning is the same as relative positioning.

All CSS Positioning Properties

The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
Property Description Values CSS
bottom Sets the bottom margin edge for a positioned box auto
length
%
inherit
2
clip Clips an absolutely positioned element shape
auto
inherit
2
left Sets the left margin edge for a positioned box auto
length
%
inherit
2
overflow
Specifies what happens if content overflows an element's box auto
hidden
scroll
visible
inherit
2
position Specifies the type of positioning for an element absolute
fixed
relative
static
inherit
2
right Sets the right margin edge for a positioned box auto
length
%
inherit
2
top Sets the top margin edge for a positioned box auto
length
%
inherit
2
z-index Sets the stack order of an element number
auto
inherit
2

Part 9 - Classes & ID | CSS

Classes & ID

There is often confusion about when it is appropriate to use CSS IDs and when CSS Classes should be used instead. This lesson is geared to display the differences, as well as provide more information about CSS IDs.

What is an ID Selector?

CSS IDs are similar to classes in that they define a special case for an element. Below is an example of a CSS ID.

CSS Code:

p#exampleID1 { background-color: white; } 
p#exampleID2 { text-transform: uppercase; } 

HTML Code:

<p id="ExampleID1">This paragraph has an ID name of
 "exampleID1" and has a white CSS defined background</p>

<p id="ExampleID2">This paragraph has an ID name of
 "exampleID2" and has had its text transformed to uppercase letters. </p>

Display:

This paragraph has an ID name of "exampleID1" and has a white CSS defined background.
This paragraph has an ID name of "exampleID2" and has had its text transformed to uppercase letters.
Notice that an ID's CSS is an HTML element, followed by a "#", and finally ID's name. The end result looks something like "element#idname". Also, be sure to absorb the fact that when an ID is used in HTML, we must use "id=name" instead of "class=name" to reference it!

Why Did They Choose Those Names?

  • ID = A person's Identification (ID) is unique to one person.
  • Class = There are many people in a class.

ID for Layout and Uniqueness

Standards specify that any given ID name can only be referenced once within a page or document. From our experience, IDs are most commonly used correctly in CSS layouts. This makes sense because there are usually only one menu per page, one banner, and usually only one content pane.
In Tizag.com CSS Layout Examples we have used IDs for the unique items mentioned above. View the CSS Code for our first layout example. Below are the unique IDs in our code.
  • Menu - div#menuPane
  • Content - div#content

Part 8 - Lists & Tables | CSS

CSS Lists

Lists come in two basic flavors: unordered and ordered. However, CSS allows for more list customization than HTML -- to the extent that even images can be used as bullet points for unordered lists!.

CSS List Style Type

If you want to use something different from the default numbering of ordered lists, or the bullets/discs of unordered lists, then all you have to do is choose a different style for your lists. CSS allows you to select from a wide variety of different list item shapes.
  • Unordered list styles: square, circle, disc (default), and none
  • Ordered list styles: upper-alpha, lower-alpha, upper-roman, lower-roman, decimal (default), and none

CSS Code:

ol { list-style-type: upper-roman; }
ul { list-style-type: circle; } 

Display:

Here is an ordered list:
  • This list is
  • using roman
  • numerals
  • with CSS!
and now an unordered list:
  1. This list is
  2. using circle types
  3. with CSS!

CSS Lists With Images

As we stated in the introduction, CSS lists allow you to insert an image in place of the normal bullets. A good choice for a bullet image would one that is smaller than the height of your text and is a relatively simple/plain graphic.

CSS Code:

ul { list-style-image: url("listArrow.gif"); } 
ol { list-style-image: url('/listArrow2.gif'); }

Display:

Here is an unordered list:
  • This list is
  • using a picture with CSS!
and now an ordered list:
  1. This list is
  2. using a picture
  3. with CSS!
As you can see, it does not matter if the lists are <ul> or <ol> when using images. Nevertheless, it is a good coding practice to only use images for an unordered list. Ordered lists usually have an incremented (growing) value that appears next to each list item and you can't do this with just one image.

CSS List Position

With CSS, it is possible to alter the indentation that takes place with your list items. See the example below for the trick of indenting your lists.

CSS Code:

ul { list-style-position: inside; } 
ol { list-style-position: outside; }

Display:

  • This list is
  • using inside
  • positioning with CSS!
and now an ordered list:
  1. This list is
  2. using outside
  3. positioning with CSS!
Note: "Outside" is actually the default setting for indentation.

List: All in One

It is possible to combine the above CSS techniques into one line of CSS. This is useful if you would like to have a list-style-type take the place of your list image, if the image is not able to be loaded.

CSS Code:

ul { list-style: upper-roman inside url("http://www.example.com/notHere.gif");}

Display:

  • This list's image did
  • NOT load correctly
  • But because we chose to include
  • A CSS list type, we avoided a bland list!

CSS Table Properties

The CSS table properties allow you to set the layout of a table.
Browser support: IE: Internet Explorer, M: Mac IE only, F: Firefox, N: Netscape.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Property Description Values IE F N W3C
border-collapse Sets whether the table borders are collapsed into a single border or detached as in standard HTML collapse
separate
5 1 7 2
border-spacing Sets the distance that separates cell borders (only for the "separated borders" model) length length 5M 1 6 2
caption-side Sets the position of the table caption top
bottom
left
right
5M 1 6 2
empty-cells Sets whether or not to show empty cells in a table (only for the "separated borders" model)  show
hide
5M 1 6 2
table-layout Sets the algorithm used to display the table cells, rows, and columns auto
fixed
5 1 6 2
 Example:
Peter Griffin
Lois Griffin

Cleveland Brown
Glenn Quagmire
Note: The border-spacing property does not work in Internet Explorer.

Part 7 - Borders & Outline | CSS

The CSS border properties define the borders around an element:

CSS Border Properties

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


Border Style

The border-style property specifies what kind of border to display.
Remark None of the other border properties will have any effect unless border-style is set.

border-style Values

none: Defines no border
dotted: Defines a dotted border
dashed: Defines a dashed border
solid: Defines a solid border
double: Defines two borders. The width of the two borders are the same as the border-width value
groove: Defines a 3D grooved border. The effect depends on the border-color value
ridge: Defines a 3D ridged border. The effect depends on the border-color value
inset: Defines a 3D inset border. The effect depends on the border-color value
outset: Defines a 3D outset border. The effect depends on the border-color value

Border Width

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.

Example

p.one
 {
 border-style:solid;
 border-width:5px;
 }
 p.two
 {
 border-style:solid;
 border-width:medium;
 } 



Border Color

The border-color property is used to set the color of the border. The color can be set by:
  • name - specify a color name, like "red"
  • RGB - specify a RGB value, like "rgb(255,0,0)"
  • Hex - specify a hex value, like "#ff0000"
You can also set the border color to "transparent".
Note: The "border-color" property does not work if it is used alone. Use the "border-style" property to set the borders first.

Example

 p.one
 {
 border-style:solid;
 border-color:red;
 }
 p.two
 {
 border-style:solid;
 border-color:#98bf21;
 } 
 

Border - Individual sides

In CSS it is possible to specify different borders for different sides:

Example

p
 {
 border-top-style:dotted;
 border-right-style:solid;
 border-bottom-style:dotted;
 border-left-style:solid;
 }

The example above can also be set with a single property:

Example

 border-style:dotted solid;

The border-style property can have from one to four values.
  • border-style:dotted solid double dashed;
    • top border is dotted
    • right border is solid
    • bottom border is double
    • left border is dashed
  • border-style:dotted solid double;
    • top border is dotted
    • right and left borders are solid
    • bottom border is double
  • border-style:dotted solid;
    • top and bottom borders are dotted
    • right and left borders are solid
  • border-style:dotted;
    • all four borders are dotted
The border-style property is used in the example above. However, it also works with border-width and border-color.

Border - Shorthand property

As you can see from the examples above, there are many properties to consider when dealing with borders.
To shorten the code, it is also possible to specify all the border properties in one property. This is called a shorthand property.
The shorthand property for the border properties is "border":

Example

 border:5px solid red;

When using the border property, the order of the values are:
  • border-width
  • border-style
  • border-color
It does not matter if one of the values above are missing (although, border-style is required), as long as the rest are in the specified order.

CSS Outline Properties

The outline properties specifies the style, color, and width of an outline.
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
Property Description Values CSS
outline A shorthand property for setting all the outline properties outline-color
outline-style
outline-width
2
outline-color Sets the color of the outline around an element color
invert
2
outline-style Sets the style of the outline around an element none
dotted
dashed
solid
double
groove
ridge
inset
outset
2
outline-width Sets the width of the outline around an element thin
medium
thick
length

Part 6 - Margin & Padding | CSS

Margin

CSS Margins are nearly identical to the CSS Padding attribute except for one important difference: a margin defines the white space around an HTML element's border, while padding refers to the white space within the border. Setting the actual value of margin is just the same as with padding, so you can probably zip right through this lesson.
Please see the example below for a visual representation. Note: A border has been added to each element so you that you may see the effects of the margin attribute.

CSS Code:

p {margin: 5px; border: 1px solid black; }  
h5{margin: 0px; border: 1px solid red;} 

Display:

This is a paragraph that has a margin of 5 pixels on every side: top, right, bottom, and left.
This header has no margin. It is only spaced away from the paragraph because the paragraph has a margin of 5 pixels!
There are several ways to go about defining the CSS Margin attribute. We will show you every possible way and let you know which methods are the best.

Margin: 1 Value

As you saw in the example above, margin can be uniform outside an element. Specifying one value will create a uniform margin on all sides: top, right, bottom, left. In addition to using exact values, you may also define the margin with the use of percentages.

CSS Code:

p {margin: 2%; border: 1px solid black; } 
h5{margin: 0px; border: 1px solid red;} 

Display:

This is a paragraph that has a margin of 2% on every side: top, right, bottom, and left.
This header has a margin of 5 pixels.

Margin: margin-(direction):

Each HTML element actually has four different margins: top, right, bottom, and left. It is possible to define these individual margins simply by adding a direction suffix to the margin attribute. Example form: margin-(direction). Defining only one direction will leave the other 3 margins untouched.

CSS Code:

p { margin-left: 5px; border: 1px solid black; } 
h5{ margin-top: 0px;
margin-right: 2px;
margin-bottom: 13px;
margin-left: 21px;
border: 1px solid red; } 

Display:

This paragraph had one margin specified (left), using directional declaration. Note that there is still a margin below and above this paragraph. This is because our CSS only replaced one of the four margins, which didn't effect the top and bottom margins.
This header had each margin specified separately, using directional declaration.

Margin: 4 Values

Four margin values can be declared at once by either specifying two or four values. When only using two values, the first will define the margin on the top and bottom, while the second value will define the margin on the left and right.
When using the four value margin specification, the corresponding directions are: top, right, bottom, left. To help you remember what the order is, just remember that it starts at the top and then moves clockwise until it reaches the left. The examples below show partial (2) and complete (4) margin usage.

CSS Code:

p {margin: 5px 15px; 
border: 1px solid black; }  
h5{margin: 0px 5px 10px 3px;
 border: 1px solid red;} 

Display:

This paragraph has a top and bottom margin of 5 pixels and a right and left margin of 15 pixels.
This header has a top margin of 0 pixels, a right margin of 5 pixels, a bottom margin of 10 pixels, and a left margin of 3 pixels.

Padding

With CSS Padding you will be able to change the default padding that appears inside various HTML elements (paragraphs, tables, etc). But first, let us make sure we understand the definition of padding. A padding is the space between an element's border and the content within it.

Please see the example below for a visual representation. Note: The border has been made visible, for each element, so you may more readily see the effects of padding.

CSS Code:

p {padding: 15px; border: 1px solid black; }  
h5{padding: 0px; border: 1px solid red;} 

Display:

This is a paragraph that has a padding of 15 pixels on every side: top, right, bottom, and left.
This header has no padding, which places the text right against the border!
There are several ways to go about defining the CSS Padding attribute. I will show you every possible way and let you know which ways are the best.

CSS Padding: 1 Value

As you saw in the example above, padding can be uniform inside an element. Specifying one value will create a uniform padding on all sides: top, right, bottom, left. In addition to using exact values, you may also define the padding with the use of percentages.

CSS Code:

p {padding: 2%; border: 1px solid black; } 
h5{padding: 0px; border: 1px solid red;} 

Display:

This is a paragraph that has a padding of 5 pixels on every side: left, top, right, and bottom.
This header has no padding. It is only spaced away from the paragraph because the paragraph has a padding of 5 pixels!

CSS Padding: padding-(direction):

Each HTML element actually has 4 different paddings: top, right, bottom, and left. It is possible to define these individual paddings simply by adding a direction suffix to the padding attribute. Example form: padding-(direction). Defining only one direction will leave the other 3 default paddings untouched.

CSS Code:

p { padding-left: 5px; border: 1px solid black; } 
h5{ 
    padding-top: 0px;
    padding-right: 2px;
    padding-bottom: 13px;
    padding-left: 21px;
    border: 1px solid red; 
} 

Display:

This paragraph had one padding specified(left), using directional declaration.
This header had each padding specified separately, using directional declaration.

CSS Padding: 2 & 4 Values

Four padding values can be declared at once by either specifying two or four values. When only using two values, the first will define the padding on the top and bottom, while the second will define the padding on the left and right.
When using the four value padding specification, the corresponding directions are: top, right, bottom, left. To help you remember what the order is, just remember that it starts at the top and then moves clockwise until it reaches the left. The examples below shows partial (2) and complete (4) padding usage.

CSS Code:

p {
    padding: 5px 15px; 
    border: 1px solid black; 
}  
h5{
    padding: 0px 5px 10px 3px; 
    border: 1px solid red;
} 

Display:

This paragraph has a top and bottom padding of 5 pixels and a right and left padding of 15 pixels.
This header has a top padding of 0 pixels, a right padding of 5 pixels, a bottom padding of 10 pixels, and a left padding of 3 pixels.

Part 5 - Box Model | CSS

Box Model in CSS

All HTML elements can be considered as boxes. In CSS, the term "box model" is used when talking about design and layout.
In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.
The box model illustrates how the CSS properties: margin, border, and padding, affects the width and height of an element.

The Box Model

The image below illustrates the box model:

CSS Explanation of the different parts:
  • 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

Width and Height of an Element

Important: When you specify the width and height properties of an element with CSS, you are just setting the width and height of the content area. To know the full size of the element, you must also add the padding, border and margin.
The total width of the element in the example below is 300px:
width:250px;
padding:10px;
border:5px solid gray;
margin:10px;
Let's do the math:
250px (width)
+ 20px (left and right padding)
+ 10px (left and right border)
+ 20px (left and right margin)
= 300px
Imagine that you only had 250px of space. Let's make an element with a total width of 250px:

Example

width:220px;
 padding:10px;
 border:5px solid gray;
 margin:0px; 

The total width of an element should always be calculated like this:
Total element width = width + left padding + right padding + left border + right border + left margin + right margin
The total height of an element should always be calculated like this:
Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin

Browsers Compatibility Issue

If you tested the previous example in Internet Explorer, you saw that the total width was not exactly 250px.
IE includes padding and border in the width, when the width property is set, unless a DOCTYPE is declared.
To fix this problem, just add a DOCTYPE to the code:

Example

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html>
 <head>
 <style type="text/css">
 div.ex
 {
 width:220px;
 padding:10px;
 border:5px solid gray;
 margin:0px;
 }
 </style>
 </head> 

Part 4 - Text & Font | CSS

The CSS text properties define the appearance of text:

text example

This example includes some text formatting properties. The heading uses the text-align, text-transform, and color properties. The paragraph is indented and aligned, and the underline is removed from the "Try it yourself" link.


Text Color

The color property is used to set the color of the text. The color can be set by:
  • name - specify a color name, like "red"
  • RGB - specify an RGB value, like "rgb(255,0,0)"
  • Hex - specify a hex value, like "#ff0000"
The default color for a page is defined in the body selector.

Example

 body {color:blue}
 h1 {color:#00ff00}
 h2 {color:rgb(255,0,0)}

javascript:tinyMCE.execInstanceCommand('mce_editor_0',%20'forecolor',%20false,%20'#0000FF');
Remark For W3C compliant CSS: If you define the color property, you must also define the background-color property.

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).

Example

h1 {text-align:center}
 p.date {text-align:right}
 p.main {text-align:justify} 



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:

Example

 a {text-decoration:none} 

It can also be used to decorate text:

Example

h1 {text-decoration:overline}
 h2 {text-decoration:line-through}
 h3 {text-decoration:underline}
 h4 {text-decoration:blink}

Remark It is not recommended to underline text that is not a link, as this often confuse users.

Text Transformation

The text-transform property is used to specify uppercase and lowercase letters in a text.
It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of each word.

Example

p.uppercase {text-transform:uppercase}
 p.lowercase {text-transform:lowercase}
 p.capitalize {text-transform:capitalize}



Text Indentation

The text-indentation property is used to specify the indentation of the first line of a text.

Example

  p {text-indent:50px} 

CSS Font

CSS gives you great control over the way your text is displayed. You can change the text size, color, style, and more. You probably already knew how to make text bold or underlined, but did you know you could resize your font using percentages? Let us begin the lesson with an easy and important font attribute, color!

CSS Font Color

Although the color of the text seems like it would be part of CSS Font, it actually is a standalone attribute in CSS. This could be for many reasons, including the fact that it will be used a great deal, so why make the coder type out "font-color", when they could just type out "color" instead? Here's an example of changing the color of your font.

CSS Code:

h4 { color: red; }

h5 { color: #9000A1; } 

h6 { color: rgb(0, 220, 98); } 

Display:

This is a red h4 header.

This is a hexadecimal #9000A1 h5 header.
This is an rgb(0, 220, 98) h6 header.
In the above example we used three different formats for defining a color: a color name, hexadecimal values, and RGB. Check out the list of supported color names. Hexadecimal form is a pound sign (#) followed by at most 6 hex values (0-F). RGB defines the individual values for Red, Green, and Blue.
Example form: rgb(Red, Green, Blue); with the range of 0-255 for each value.

CSS Font Family

Font families can be divided into two groups: serif and sans-serif. A sans-serif font does not include the small lines at the end of characters, while a serif font does include these small lines. When choosing which kind you prefer, remember that studies have shown that sans-serif fonts are much easier to read on a computer monitor than serif fonts.

CSS Code:

h4 { font-family: sans-serif; }

h5 { font-family: serif; } 

h6 { font-family: arial; } 

Display:

This is a header with sans-serif font

This is a header with a serif font
This is a header with an arial font
As you probably noticed throughout Tizag.com, we do not use serif fonts, except in special cases, like for the titles of the Code and Display boxes.

CSS Font Size

You can manipulate the size of your fonts by using values, percentages, or key terms. Using values are useful if you do not want the user to be able to increase the size of the font because your site will look incorrect if they did so. Percentages are great when you want to change the default font, but do not want to set a static value.

CSS Code:

p { font-size: 120%; } 

ol{ font-size: 10px; } 

ul{ font-size: x-large; }

Display:

This is a font size of 120%
  1. This is a font size of 10px
  • This is a font size of "x-large"
Though key terms are not very useful, the common terms are: xx-large, x-large, large, medium, small, x-small, and xx-small.

CSS Font Style

CSS Font-Style is where you define if your font will be italic or not. Possible key terms are the following: italic, oblique, and normal.

CSS Code:

p { font-style: italic; }

h4{ font-style: oblique; }

Display:

This is an italic font

This is an oblique font

CSS Font Weight

If you want to control the weight of your font (its thickness), using font weight is the best way to go about it. We suggest that you only use font-weight in multiples of 100 (e.g. 200, 300, etc) because any less and you probably will not see any difference. The values range from 100 (thin)-900 (thick).

CSS Code:

p { font-weight: 100; } 

ul{ font-weight: bolder; }

Display:

This is a font with a weight of 100
  • This is a font with
  • a "bolder" weight
Available key terms for font-weight: bold, bolder, and normal.

CSS Font Variant

CSS Font Variant allows you to convert your font to all small caps. Note: not every font supports CSS Font Variant, so be sure to test before you publish.

CSS Code:

p { font-variant: small-caps; }
 
google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);

Part 3 - Colors & Backgrounds | CSS

CSS Background

The background of your website is very important, so please spend some time with this tutorial. If you are aiming for a professional website, a good rule of thumb is to use a light background with dark text. However, if you're just making a website for pleasure, then any kind of color combination is acceptable.
With CSS, you are able to set the background color or image of any CSS element. In addition, you have control over how the background image is displayed. You may choose to have it repeat horizontally, vertically, or in neither direction. You may also choose to have the background remain in a fixed position, or have it scroll as it does normally. The following examples will show you how to implement all of these options.

CSS Background Color

As you have seen throughout Tizag Tutorials, many different background colors are present. These varying backgrounds were obtained without using tables! Below are a couple examples of CSS backgrounds.

CSS Code:

h4 { background-color: white; } 
p  { background-color: #1078E1; } 
ul { background-color: rgb( 149, 206, 145); } 

Display:

This is a <h4> with a white background

This is a <p> with a background using the hexadecimal value of #1078E1
  • This is an unordered list
  • with an RGB value of 149, 206, 145
In the above example we used three different formats for defining a color: a color name, hexadecimal values, and RGB. Check out the the list of supported color names. Hexadecimal form is a pound sign (#) followed by, at most, 6 hex values (0-F). RGB defines the individual values for Red, Green, and Blue. Example form: rgb(Red, Green, Blue); with the range of 0-255 for each value.

CSS Background Image

Need an image to repeat left-to-right, like the gradient background that appears at the top of Tizag.com? Or maybe you would like to have an image that remains fixed when the user scrolls down your page. This can be done quite easily with CSS and more, including:
  • choosing if a background will repeat and which directions to repeat in.
  • precision positioning
  • scrolling/static images
Let's begin with a default CSS background image.

CSS Code:

p { background-image: url(smallPic.jpg); }
h4{ background-image: url(http://www.tizag.com/pics/cssT/smallPic.jpg); }

Display:

This <p> has a background image using a local path to the picture.

This <h4> has a background image using the complete url path.

Background Image Repeat

You can have a background image repeat vertically (y-axis), horizontally (x-axis), in both directions, or in neither direction.

CSS Code:

p {  
 background-image: url(smallPic.jpg); 
 background-repeat: repeat; }
h4 {  
 background-image: url(smallPic.jpg);
 background-repeat: repeat-y;}
ol {  
 background-image: url(smallPic.jpg); 
 background-repeat: repeat-x;}
ul {  
 background-image: url(smallPic.jpg); 
 background-repeat: no-repeat;}

Display:

This <p> has a background image repeating in both directions (default repeat). If you use this option, make sure that your image was designed to be repeated.

This <h4> has a background image repeating vertically (y). You could this to add a style to the side of your element.

  1. This is an ordered list
  2. With a background that repeats
  3. Horizontally (x)
  • This is an unordered list
  • With a background that repeats
  • in neither direction.

CSS Fixed Background Image

You may choose to have your background scroll naturally, or to have it in a fixed position. Note: This feature does not work properly in most browsers when placed within a textarea, except Internet Explorer 6.0, which displays it correctly.

CSS Code:

textarea.noScroll { 
 background-image: url(smallPic.jpg); 
 background-attachment: fixed;
}
textarea { 
 background-image: url(smallPic.jpg); 
 background-attachment: scroll;}

Display:

This <p> has a background image that is stationary. So you can scroll as much as you want, but you won't be able to budge this background! This <h4> has a background image that scrolls (default setting). Experiment with fixed and scrolling background to find the one that works for you.

CSS Background Image Positioning

If you would like to define where exactly an image appears within an HTML element, you may use CSS's background-position. Please take note that there are three different ways of defining position: length, percentages, and keywords. We recommending using lengths -- specifically, pixels.

CSS Code:

p { 
 background-image: url(smallPic.jpg); 
 background-position: 20px 10px;
}
h4 { 
 background-image: url(smallPic.jpg); 
 background-position: 30% 30%;
}
ol { 
 background-image: url('/smallPic.jpg'); 
 background-position: top center;
}

Display:

This <p> has a background image positioned with pixel values.

This <h4> has a background image positioned with percentages.

  1. This is an ordered list
  2. With a background that was positioned
  3. using keywords.
Note: When using pixels, the location of the image will be (A)px from the left of the screen and (B)px from the top of the screen, where A and B are integers. Note: When using percentages, the location of the image will be (A)% from the left of the screen and (B)% from the top of the screen, where A and B are integers. Note: Available positioning keywords are: top, right, bottom, left, and center.

CSS Gradient Background

If you would like to create a gradient background like the one that appears at the top of Tizag.com, you must first create an image inside a painting program (Photoshop, Draw, etc) like the one you see below.

Necessary Image:

Notice that the image is very slim. We are going to be tiling the image horizontally, so you can make the image skinny as possible. As long as the image is 1 pixel or wider, you will be fine.
Using the repeat attribute, we set the value to repeat-x which causes the image to span left to right across the specified element. This example adds a gradient background to the paragraph element.

CSS Code:

p {
 background-image: url(http://www.example.com/gradient.gif);
 background-repeat: repeat-x; 
}

Display:

This paragraph has a gradient background. The gradient background was first made in a painting program and then repeated horizontally across the paragraph element. It makes for a neat effect that also loads quickly! Because the image is very skinny, the file size is also very small. You could also create a gradient that changes color left to right and repeat it in the vertical direction to have a gradient on the side of your web page.

Part 2 - Syntax | CSS

Syntax

The CSS syntax is made up of three parts: a selector, a property and a value:
selector {property:value}
The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon, and surrounded by curly braces:
body {color:black}
Note: If  the value is multiple words, put quotes around the value:
p {font-family:"sans serif"}
Note: If you want to specify more than one property, you must separate each property with a semicolon. The example below shows how to define a center aligned paragraph, with a red text color:
p {text-align:center;color:red}
To make the style definitions more readable, you can describe one property on each line, like this:
p
{
text-align:center;
color:black;
font-family:arial
}


Grouping

You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. All header elements will be displayed in green text color:
h1,h2,h3,h4,h5,h6
{
color:green
}


The class Selector

With the class selector you can define different styles for the same type of HTML element.
Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:
p.right {text-align:right}
p.center {text-align:center}
You have to use the class attribute in your HTML document:
<p class="right">This paragraph will be right-aligned.</p>
<p class="center">This paragraph will be center-aligned.</p>
Note: To apply more than one class per given element, the syntax is:
<p class="center bold">This is a paragraph.</p>
The paragraph above will be styled by the class "center" AND the class "bold".
You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:
.center {text-align:center}
In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector:
<h1 class="center">This heading will be center-aligned</h1>
<p class="center">This paragraph will also be center-aligned.</p>
Remark Do NOT start a class name with a number! It will not work in Mozilla/Firefox.

Add Styles to Elements with Particular Attributes

You can also apply styles to HTML elements with particular attributes.
The style rule below will match all input elements that have a type attribute with a value of "text":
input[type="text"] {background-color:blue}


The id Selector

You 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 "green":
#green {color:green}
The style rule below will match the p element that has an id with a value of "para1":
p#para1
{
text-align:center;
color:red
}
Remark Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.

CSS Comments

Comments are used to explain your code, and may help you when you 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
 }

How to Insert a Style Sheet

When a browser reads a style sheet, it will format the document according to it. There are three ways of inserting a style sheet:

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>
The browser will read the style definitions from the file mystyle.css, and format the document according to it.
An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:
hr {color:sienna}
p {margin-left:20px}
body {background-image:url("images/back40.gif")}
Remark Do not leave spaces between the property value and the units! "margin-left:20 px" (instead of "margin-left:20px") will only work in IE6, but it will not work in Firefox or Opera.

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 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>
The browser will now read the style definitions, and format the document according to it.
Note: A browser normally ignores unknown tags. This means that an old browser that does not support styles, will ignore the <style> tag, but the content of the <style> tag will be displayed on the page. It is possible to prevent an old browser from displaying the content by hiding it in the HTML comment element:
<head>
<style type="text/css">
<!--
hr {color:sienna}
p {margin-left:20px}
body {background-image: url('/images/back40.gif')}
-->
</style>
</head>

Inline Styles

An inline style loses many of the advantages of style sheets by mixing content with presentation. Use this method sparingly, such as when a style is to be applied to a single occurrence of an element.
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>


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
}
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:
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.

Part 1 - Introduction | CSS

What is CSS?

  • CSS stands for Cascading Style Sheets
  • Styles define how to display HTML elements
  • Styles are normally stored in Style Sheets
  • Styles were added to HTML 4.0 to solve a problem
  • External Style Sheets can save a lot of work
  • External Style Sheets are stored in CSS files
  • Multiple style definitions will cascade into one

Styles solved a big problem

The original HTML was never intended to contain tags for formatting a document. HTML tags were intended to define the content of a document, like:
<p>This is a paragraph.</p>
 <h1>This is a heading</h1>
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 had to be added to every single Web page, became a long, expensive and unduly painful process.
To solve this problem, the World Wide Web Consortium (W3C) - responsible for standardizing HTML - created CSS in addition to HTML 4.0. 
With HTML 4.0, all formatting can be removed from the HTML document and stored in a separate CSS file.
All browsers support CSS today.

Styles save a lot of work

Styles sheets define HOW HTML elements are to be displayed.
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 CSS document!

Multiple styles will cascade into one

Style sheets allow style information to be specified in many ways.
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.

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:
  1. Browser default
  2. External style sheet
  3. Internal style sheet (in the head section)
  4. Inline style (inside an HTML element)
So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the <head> tag, or in an external style sheet, or in a browser (a default value).
Remark 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!
 

E-Learning Copyright © 2011 Powered by Blogger