Docsity

Css notes, Study notes of Web Design and Development

Toggle sidebar
bg1
Department of Information Technology
Web Engineering-I
CASCADING STYLE SHEETS (CSS) INTRODUCTION
Cascading style sheet is a style language that is used to format the layout of Web pages. CSS can
be used to define text styles, table sizes, and other aspects of Web pages. A style sheet provides a
set of rules describing how a document should appear. A CSS file allows you to separate your web
sites HTML content from its style. CSS was first developed in 1997, as a way for Web developers
to define the look and feel of their Web pages. They are used to control the look and feel of your
HTML documents in an organized and efficient manner. With CSS you will be able to:
•Add new looks to your old HTML
•Completely restyle a web site with only a few changes to your CSS code
•Control layout of many documents from one single style sheet.
•Apply different layout to different media-types (screen, print, etc.).
Style sheets let you place things exactly where you want them to be on the page, using the distance
in pixels from the top and the left of the browser window.
What is the difference between CSS and HTML?
HTML is used to structure content. CSS is used for formatting structured content.
Types of Style Sheets
There are three ways you can apply CSS to an HTML document.
1. Inline
2. Internal
3. External
1. Inline Style Sheet
Inline style sheets are defined right in the HTML file alongside the element you want to style.
Example:
<p style="color: #ff0000;">Some red text</p>
Inline styles will NOT allow the user to change styles of elements or text formatted this way
2. Internal Style Sheet
With this way you are simply placing the CSS code within the <head></head> tags of each HTML
file you want to style with the CSS.
Example:
<html>
<head>
<title> </title>
<style type="text/css">
CSS Content Goes Here
</style>
</head>
<body>
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
Discount

On special offer

Partial preview of the text

Download Css notes and more Study notes Web Design and Development in PDF only on Docsity!

Department of Information Technology

Web Engineering-I

CASCADING STYLE SHEETS (CSS) INTRODUCTION

Cascading style sheet is a style language that is used to format the layout of Web pages. CSS can

be used to define text styles, table sizes, and other aspects of Web pages. A style sheet provides a set of rules describing how a document should appear. A CSS file allows you to separate your web sites HTML content from its style. CSS was first developed in 1997, as a way for Web developers to define the look and feel of their Web pages. They are used to control the look and feel of your HTML documents in an organized and efficient manner. With CSS you will be able to:

  • Add new looks to your old HTML
  • Completely restyle a web site with only a few changes to your CSS code
  • Control layout of many documents from one single style sheet.
  • Apply different layout to different media-types (screen, print, etc.).

Style sheets let you place things exactly where you want them to be on the page, using the distance in pixels from the top and the left of the browser window.

What is the difference between CSS and HTML?

HTML is used to structure content. CSS is used for formatting structured content.

Types of Style Sheets There are three ways you can apply CSS to an HTML document.

  1. Inline
  2. Internal
  3. External 1. Inline Style Sheet

Inline style sheets are defined right in the HTML file alongside the element you want to style.

Example:

Some red text

Inline styles will NOT allow the user to change styles of elements or text formatted this way

2. Internal Style Sheet

With this way you are simply placing the CSS code within the tags of each HTML file you want to style with the CSS.

Example:

With this method each HTML file contains the CSS code needed to style the page. Meaning that any changes you want to make to one page will have to be made to all. This method can be good if you need to style only one page, or if you want different pages to have different styles.

External Style Sheet

An external CSS file can be created with any text or HTML editor such as "Notepad" or "Dreamweaver". A CSS file contains no HTML, only CSS. You simply save it with the .css file extension. You can link to the file externally by placing one of the following links in the head section of every HTML file you want to style with the CSS file.

Or you can also use the @import method as shown below

Either of these methods is achieved by placing one or the other in the head section.

Example:

OR

<title> <style type="text/css"> @import url(Path To stylesheet.css) </style> </head> <body>

</body> </html>

By using an external style sheet, your entire HTML files link to one CSS files in order to style the pages. This means, that if you need to alter the design of all your pages, you only need to edit one .css file to make global changes to your entire website.

Here are a few benefits of external style sheet.

  • Easier Maintenance
  • Reduced File Size
  • Reduced Bandwidth

Now all tags within the file will be set to Georgia and all

tags are set to Tahoma, leaving text within other elements unchanged from the body declaration of Verdana. There are instances where nested elements do not inherit the containing elements properties. For example, if the body margin is set to 20 pixels, the other elements within the file will not inherit the body margin by default.

body {margin: 20px;}

Combining Selectors

You can combine elements within one selector in the following way.

h1, h2, h3, h4, h5, h6 { color: #009900; font-family: Georgia, sans-serif; }

Here all the header elements are grouped into one selector. Each one is separated by a comma. The final result of the above code sets all headers to green and to the specified font. If the user does not have the first font as declared it will go to another sans-serif font the user has installed on their computer.

Selectors can also be combined in the following way:

h1, .link, #top-link {font-weight: bold}

This will match

tags, elements with class link, and element with id top-link.

CSS universal selectors

CSS universal selectors select any type of elements in an HTML page. It matches a single element. An asterisk ( i.e. "*" ) is used to denote a CSS universal selector.

  • { CSS-Property: value; }

CSS descendant selectors CSS descendant selectors select an element which is a descendant of another element. If, x, y and z are three HTML elements and z resides within start and end tag of y, and y resides within start and end tag of x; then both y and z are called as descendants of x. While writing descendant selectors, selectors must be separated with "white space" combinator.

Selector1 Selector { CSS-Property: value; }

CSS child selectors

CSS child selectors select an element which is a child of another element. If, x, y and z are three HTML elements and z resides within start and end tag of y, and y resides within start and end tag of x; then y is called as child of x; and z is called as child of y. While writing child selectors, selectors must be separated with ">" combinator.

Selector1 > Selector {

CSS-Property: value; }

CSS adjacent sibling selectors

CSS adjacent sibling selectors come as a pair of selectors and select the second one, if it immediately follows the first one in order of appearance in an HTML page. While writing adjacent sibling selectors, selectors must be separated with "+" combinator.

Selector1 + Selector { CSS-Property: value; }

CSS attribute selectors

For all of the CSS attribute selectors, enclosing the selector with "[" and "]" is must.

[name_of_the_attribute] { CSS-Property: value; }

E.g [title] { color: red; /* sets color to red */ }

[name_of_the_attribute="value_of_the_attribute"] { CSS-Property: value; }

E.g [title="w3resource"] { color: red; /* sets color to red */ }

[name_of_the_attribute=~"value"]

{

CSS-Property: value; }

Where, value of the attribute of the selected element is a white space separated list of words, and one of the words matches value.

E.g:

[title~="w3resource"] { color: red; /* sets color to red */ }

CSS IDs

IDs are similar to classes, except once a specific id has been declared it cannot be used again within the same HTML file. IDs are generally used to style the layout elements of a page that will only be needed once, whereas the classes are used to style text and such that may it be declared multiple times. The main container for this page is defined by the following.

Everything within this document is inside this division.

Here the id selector is used for the "container" division over a class, because this is only need to be used one time within this file. The CSS file is as the following:

#container{ width: 80%; margin: auto; padding: 20px; border: 1px solid #666; background: #ffffff; }

The id selector begins with a (#) number sign instead of a (.) period, as the class selector does. Styles in a CSS file are supposed to “cascade”. So, theoretically, this should allow styles declared later in your stylesheet to override styles that are declared earlier. But because of more specific selectors (like IDs), this doesn’t happen as often.

And the CSS file is: #element { background: blue; }

body div.element { background: green; }

Even though the body div.element selector appears after the #element ID selector, and despite the fact that the “body” element is included as part of the selector, the background of the element will be blue — not green — because the ID selector is more specific than the second selector.

Nesting

If the CSS is structured well, there shouldn't be a need to use many class or ID selectors. This is because you can specify properties to selectors within other selectors.

For Example:

#top { background-color: #ccc; padding: 1em

#top h1 { color: #ff0; }

#top p { color: red; font-weight: bold; }

Removes the need for classes or ID's if it is applied to HTML that looks something like this:

Pakistan

Pakistan is our homeland country.

I Love my country Pakistan

This is because, by separating selectors with spaces, we are saying 'h1 inside ID top is color #ff0' and 'p inside ID top is red and bold'.

Division

Division is a block level HTML element used to define sections of an HTML file. A division can contain all the parts that make up the website, including additional divisions, spans, images, text and so on. The division is defined within a HTML file by placing the following between the tags:

Site contents go here

Though most likely you will want to add some style to it. You can do that in the following way:

Site contents go here

The CSS file contains this:

#container{ width: 70%; margin: auto; padding: 20px; border: 1px solid #666; background: #ffffff; }

Now everything within that division will be styled by the "container" style rule. A division creates a linebreak by default. You can use both classes and IDs with a division tag to style sections of your website.

CSS Spans

Text Indent You can indent the first line of text in an HTML element with the following:

text-indent: value;

Possible values are:

  • length
  • percentage

Text Transform You can control the size of letters in an HTML element with the following:

text-transform: value;

Possible values are:

  • none
  • capitalize
  • lowercase
  • uppercase

White Space You can control the whitespace in an HTML element with the following:

white-space: value;

Possible values are:

  • normal
  • pre
  • nowrap

Word Spacing You can adjust the space between words in the following manner. You can use negative values.

word-spacing: value;

Possible values are:

  • normal
  • length

CSS Font Properties

Font The font property can set the style, weight, variant, size, line height and font:

font: italic bold normal small/1.4em Verdana, sans-serif;

The above would set the text of an element to an italic style a bold weight a normal variant a relative size a line height of 1.4em and the font to Verdana or another sans-serif typeface.

Font -Family You can set what font will be displayed in an element with the font-family property. There are 2 choices for values:

  • family-name
  • generic family

If you set a family name it is best to also add the generic family at the end. As this is a prioritized list. So if the user does not have the specified font name it will use the same generic family.

font-family: Verdana, sans-serif;

Font Size You can set the size of the text used in an element by using the font-size property.

font-size: value;

There are alot of choices for values:

  • xx-large
  • x-large
  • larger
  • large
  • medium
  • small
  • smaller
  • x-small
  • xx-small
  • length
  • % (percent)

Font Style You can set the style of text in an element with the font-style property.

font-style: value;

Possible values are:

  • normal
  • italic
  • oblique

Font Variant You can set the variant of text within an element with the font-variant property

font-variant: value;

Possible values are:

  • normal
  • small-caps

Font Weight You can control the weight of text in an element with the font-weight property: font-weight: value;

Possible values are:

  • (^) lighter
  • normal
  • 100
  • 200
  • (^300)
  • 400
  • 500

You can position an image used for the background of an element using the background-position property.

background-position: value;

Values:

  • top left
  • top center
  • (^) top right
  • center left
  • center center
  • center right
  • (^) bottom left
  • bottom center
  • bottom right
  • x-% y-%
  • (^) x-pos y-pos

Background Repeat You can set if an image set as a background of an element is to repeat (across=x and/or down=y) the screen using the background-repeat property.

background-repeat: value;

Values:

  • repeat
  • repeat-x
  • repeat-y
  • no-repeat

CSS Margins

The margin property declares the margin between an HTML element and the elements around it. The margin property can be set for the top, left, right and bottom of an element.

margin-top: length percentage or auto; margin-left: length percentage or auto; margin-right: length percentage or auto; margin-bottom: length percentage or auto;

There are 3 choices of values for the margin property

  • length
  • percentage
  • auto

You can also declare all the margins of an element in a single property as follows:

margin: 10px 10px 10px 10px;

If you declare all 4 values as above, then the order is as follows:

  1. top
  2. right
  3. bottom
  4. left

If only one value is declared, it sets the margin on all sides.

margin: 10px;

If you only declare two or three values, the undeclared values are taken from the opposing side.

margin: 10px 10px; /* 2 values / margin: 10px 10px 10px; / 3 values */

You can set the margin property to negative values. If you do not declare the margin value of an element, the margin is 0 (zero). Elements like paragraphs have default margins in some browsers, to combat this set the margin to 0 (zero).

p {margin: 0;}

You do not have to add px (pixels) if the value is 0 (zero).

CSS Padding

Padding is the distance between the border of an HTML element and the content within it. Most of the rules for margins also apply to padding, except there is no "auto" value, and negative values cannot be declared for padding.

padding-top: length percentage; padding-left: length percentage; padding-right: length percentage; padding-bottom: length percentage;

There are 2 choices of values for the padding property.

  • Length
  • Percentage

You can also declare all the padding of an element in a single property as follows:

padding: 10px 10px 10px 10px;

If you declare all 4 values as above, the order is as follows:

  1. top
  2. right
  3. bottom
  4. left

If only one value is declared, it sets the padding on all sides.

padding: 10px;

If you only declare two or three values, the undeclared values are taken from the opposing side.

padding: 10px 10px; /* 2 values / padding: 10px 10px 10px; / 3 values */

If you do not declare the padding value of an element, the padding is 0 (zero). You do not have to add px (pixels) the value is 0 (zero).

CSS Borders

border-bottom: 1px solid #333333;

Values:

  • color
  • style
  • width

Or you can set each value individually.

Border Bottom Color You can set the color of the bottom border around an element with the border-bottom-color property.

border-bottom-color: value;

Border Bottom Style

You can set the style of the bottom border around an element with the border-bottom-style property.

border-bottom-style: value;

Border Bottom Width You can set the width of the bottom border around an element with the border-bottom-width property.

border-bottom-width: value;

Border Left You can set the color, style and width of the left border around an element with the border-left property.

border-left: 1px solid #333333;

Values:

  • color
  • style
  • width

Or you can set each value individually.

Border Left Color You can set the color of the left border around an element with the border-left-color property.

border-left-color: value;

Border Left Style You can set the style of the left border around an element with the border-left-style property.

border-left-style: value;

Border Left Width You can set the width of the left border around an element with the border-left-width property.

border-left-width: value;

Border Right

You can set the color, style and width of the right border around an element in one declaration with the border-right property.

border-right: 1px solid #333333;

Values:

  • color
  • style
  • (^) width Or you can set each value individually.

Border Right Color You can set the color of the right border around an element with the border-right-color property.

border-right-color: value;

Border Right Style You can set the style of the right border around an element with the border-right-style property.

border-right-style: value;

Border Right Width You can set the width of the right border around an element with the border-right-width property.

border-right-width: value;

Border Top You can set the color, style and width of the top border around an element in one declaration with the border-top property.

border-top: 1px solid #333333;

Values:

  • color
  • style
  • width

Or you can set each value individually.

Border Top Color You can set the color of the top border around an element with the border-top-color property.

border-top-color:value;

Border Top Style You can set the style of the top border around an element with the border-top-style property.

border-top-style: value;

Border Top Width You can set the width of the top border around an element with the border-top-width property.

border-top-width: value;

CSS Ordered & Unordered Lists

List Style

line-height: value;

Values:

  • normal
  • number
  • length
  • percentage

Max Height You can control the maximum height of an element with the max-height property.

max-height: value;

Values:

  • none
  • length
  • percentage

Min Height You can control the minimum height of an element with the min-height property.

min-height: value;

Values:

  • length
  • percentage

Width You can control the width of an element with the width property.

width: value;

Values:

  • auto
  • length
  • percentage

Max Width You can control the maximum width of an element with the max-width property.

max-width: value;

Values:

  • none
  • length
  • (^) percentage

Min Width You can control the minimum width of an element with the min-width property.

min-width: value;

Values:

  • length
  • percentage

CSS Classification

Clear You can control if an element allows floated elements to its sides with the clear property.

clear: value;

Values:

  • none
  • both
  • left
  • right

None This is the default setting. Floated elements can appear on either side of the element set to clear: none; Both Setting the value to both, causes no floated elements to appear on either side of the element set to clear: both; Left Setting the value to left, causes no floated elements to appear to the left side of the element set to clear: left; Right Setting the value to right, causes no floated elements to appear to the right side of the element set to clear: right;

Clip You can control how much of an element is visible with the clip property.

clip: value;

Values:

  • auto
  • shape

Currently the only shape recognized by the clip property is rect (rectangle).

clip: rect(10px, 10px, 10px, 10px);

Cursor You can control the style of cursor to be used in an element with the cursor property.

cursor: value;

Values:

  • auto
  • crosshair
  • default
  • help
  • move
  • pointer
  • text
  • url
  • wait