CSS
Advantage of using CSS
This reduces the complexity and
redundancy of code in the Web page and
saves our time, as we do not need to write
the same code again and again.
A style sheet
A style sheet is made up of one or more style instructions
(called rules or rule sets) that describe how an element
or group of elements should be displayed.
* Each rule selects an element and declares how it should
look.
syntax for h1
h1 { color: green; }
h1 { border: 1px solid blue; }
synatx for p
p { font-size: small; font-family: sans-serif; }
main sections/parts of a rule are
– the selector that identifies the element or
elements to be affected
– the declaration that provides the rendering
instructions
* The declaration consists of
– Property - value pair
– a property (such as color) and its value (green) are
separated by a colon and a space.
There are three ways that style information can be
applied to an HTML document.
– External style sheets
– Embedded style sheets
– Inline style
External style sheets
– An external style sheet is a separate, text-only document that
contains a number of style rules.
– It must be named with the .css suffix.
– The .css document is then linked to or imported into one or
more HTML documents.
– All the files in a website may share the same style sheet.
– This is the most powerful and preferred method for
attaching style sheets to content.
Embedded style sheets
– It is placed in a document using the style element,
and its rules apply only to that document.
– The style element must be placed in the head of
the document.
Inline styles
– We can apply properties and values to a single element using
the style attribute in the element itself, as shown below:
* <h1 style="color: red">Introduction</h1>
– To add multiple properties, just separate them with
semicolons, like this:
* <h1 style="color: red; margin-top: 2em">Introduction</h1>
– Inline styles apply only to the particular element in which they
appear.
– Inline styles should be avoided, unless it is absolutely
necessary to override styles from an embedded or external
style sheet.
Parents and children
– All the elements contained within a given
element are said to be its descendants.
– For example, the h1, h2, p, em, and img
elements in the document are all descendants of
the body element.
– The elements have parent-child relationship.
Document structure
( DIAGRAM IN PRESENTATION)
Grouped selectors
h1, h2, p, em, img { border: 1px solid blue; }
Floating
Floating an element moves it to the left or
right, and allows the following text to wrap
around it.
Positioning
Positioning is a way to specify the location of
an element anywhere on the page with pixel
precision.
float example
img {
float: right;
margin: 10px;
}
p {
padding: 15px;
background-color: #FFF799;
border: 2px solid #6C4788;
}
paragraph float
p.float1 {
float: left;
width: 12%;
margin: 0px;
background: #CCC;
}
p.float2 {
float: left;
width: 75%;
margin: 0px;
text-align: center;
background: #CCC;
}
p.float3 {
float: left;
width: 12%;
margin: 0px;
background: #CCC;
}
p
{border: 1px solid red;
}
CSS provides several methods for positioning
elements on the page
– They can be positioned relative to where they
would normally appear in the flow.
– They can be removed from the flow altogether
and placed at a particular spot on the page.
position property
Values: static | relative | absolute | fixed |
inherit