Introduction To Cascading Stylesheet Part II
Introduction To Cascading Stylesheet Part II
Part II
Foreground Color
The foreground of an element consists of its text and border (if one is
specified)
Default: transparent
Background Color
A background color fills the canvas behind the element that includes the
content area, and any padding (extra space) added around the content,
extending behind the border out to its outer edge
Default: border-box
The opacity setting applies to the entire element — both the foreground and
the background
opacity
Values: number (0 to 1)
Default: 1
Make the background of the entire page a light green (R:210, G:220, B:157, or
#d2dc9d)
Make the background of the header white with 50% transparency (R:255,
G:255, B:255, .5)
Selector Summary
Element Type Selector E {property: value;}
li:first-child
Link Pseudo-Classes
:link Applies a style to unclicked (unvisited) links
By default, browsers typically display linked text as blue and links that have
been clicked as purple
User Action Pseudo-Classes
:focus Applies when the element is selected and ready for input
When the mouse is placed over links, make the text a brighter purple
(#c700f2) and add a white background color (#fff)
Use these same style rules for when the links are in focus
As the mouse is being clicked (or tapped on a touch device), add a white
background color and make the text turn a vibrant purple (#ff00ff)
Other CSS3 Pseudo-Classes
:root , :empty, :first-child, :last-child, :only-child,
:first-of-type,:last-of-type, :only-of-type, :nth-child(),
:nth-last-child(), :nth-of-type(), :nth-last-of-type(),
:enabled, :disabled, :checked, :target (fragment
identifier), :lang(), :not()
Pseudo Element Selectors
Pseudo-elements are indicated by a double colon (::)
Pseudo Element Selectors
::first-line
This selector applies style rule for the following properties to the first
line of the specified element
This selector applies style rule for the following properties to the first
letter of the specified element
Using the ::before and ::after pseudo-elements you can tell browsers to
generate content before or after any element
Pseudo Element Selectors
The following example shows how to inserts an image by using the url()
function before the paragraph and “Thank you.” at the end of the
paragraph
p.warning::before { p.warning::after {
content: url(exclamation.png); content: " Thank you.";
margin-right: 6px; color: red;
} }
Pseudo Element Selectors
The following example shows how to inserts an image by using the url()
function before the paragraph and “Thank you.” at the end of the
paragraph
element[attribute]
element[attribute="exact value"]
element[attribute~="value"]
element[attribute|="value"]
Default: none
Background Image
body {
background-image: url(star.png);
}
blockquote {
background-image: url(dot.png);
padding: 2em;
border: 4px dashed;
}
Exercise
Add background image to your page
Background Repeat
background-repeat
Default: repeat
body {
Background Repeat background-image: url(star.png);
background-repeat: repeat-y;
}
body {
background-image: url(star.png);
background-repeat: no-repeat;
}
body {
background-image: url(star.png);
background-repeat: repeat-x;
}
Background Repeat
space and round
Exercise
Experiment background-repeat property
Example header {
margin-top: 0;
padding: 3em 1em 2em 1em;
text-align: center;
background-color: rgba(255, 255, 255, 0.5);
background-image: url(images/purpledot.png);
background-repeat: repeat-x;
}
Background Position
background-position
Default: padding-box
Background Position Origin
Background Attachment
background-attachment
Default: scroll
The Box Model
The Box Model
Every element in a document generates a box to which properties such as
width, height, padding, borders, and margins can be applied
Default: auto
height
Default: auto
Specifying Box Dimention
You can specify the width and height only for block-level elements and
non-text inline elements such as images
Specifying Box Dimention
There are two ways to specify the size of an element
overflow
Default: visible
Padding
Padding is the space between the content area and the border
padding
Default: 0
Padding
Padding is the space between the content area and the border
Default: 0
Padding
blockquote {
padding-top: 2em;
padding-right: 4em;
padding-bottom: 2em;
padding-left: 4em;
background-color: #d098d4;
}
Padding
The Shorthand padding Property
padding: top right bottom left; // clockwise direction
blockquote {
padding: 2em 4em 2em 4em;
background-color: #d098d4;
}
The Shorthand padding Property
If the left and right padding are the same, you can shorten it by supplying only
three values
blockquote {
padding: 2em 4em 2em;
background-color: #d098d4;
}
The Shorthand padding Property
If the top and bottem padding are the same and right and left padding are the
same, you can shorten it by supplying only two values
blockquote {
padding: 2em 4em;
background-color: #d098d4;
}
The Shorthand padding Property
If all sides padding are the same, you can shorten it by supplying only one
values
padding: top/bottom/right/left;
blockquote {
padding: 2em;
background-color: #d098d4;
}
Exercise
Create the page shown
<header>
<main>
<section>
<aside>
<footer>
Exercise
Set the box-sizing model to
border-box for all the elements in
the document
html {
box-sizing: border-box;
}
* {
box-sizing: inherit;
}
Exercise
Add 1em of padding on all sides of
main element
main {
padding: 1em;
}
Exercise
Add 1em of padding on all sides of
aside element except the left side
and add 45 pixels of padding on
the left side
Borders
A border is a line drawn around the content area and its (optional) padding
border-style
Default: none
Borders
A border is a line drawn around the content area and its (optional) padding
Default: none
Borders
If you do not specify a width, the default medium width will be used
If there is no color specified, the border uses the foreground color of the
element (same as the text)
Borders Example div#silly {
border-top-style: solid;
border-right-style: dashed;
border-bottom-style: double;
border-left-style: dotted;
width: 300px;
height: 100px;
}
div#silly {
border-style: solid dashed double dotted;
}
Borders Example
Border Width (Thickness)
border-width
Default: medium
Border Width (Thickness)
border-top-width, border-right-width,border-bottom-width,
border-left-width
Default: medium
div#silly {
Border Width Example border-top-width: thin;
border-right-width: medium;
border-bottom-width: thick;
border-left-width: 12px;
border-style: solid;
width: 300px;
height: 100px;
}
div#silly {
border-width: thin medium thick 12px;
border-style: solid;
Border Width Example
Border Color
border-color
border
Default: 0
Rounded Corners with border-radius
border-top-left-radius, border-top-right-radius,
border-bottom-right-radius, border-bottom-left-radius
Default: 0
Rounded Corners with border-radius
Elliptical corners
You can make a corner elliptical by specifying two values:
padding: 1em;
border: double 4px #eaddc4;
Exercise
Add rounded corners to the main and aside elements
border-radius: 25px;
Exercise
Adds a 1-pixel solid border on the top of the headline
Add another border that adds a 3-pixel solid border on the left
text-decoration: none;
border-bottom: 1px dotted;
Margins
A margin is an optional amount of space that you can add on the outside of
the border
Margins keep elements from bumping into one another or the edge of the
browser window or viewport
The shorthand margin property works the same as the padding shorthand
Margins
margin
Default: auto
Margins
margin-top, margin-right, margin-bottom, margin-left
Default: auto
Margins
p#A {
p#B {
margin: 4em;
border: 2px solid red; margin-top: 2em;
background : #e2f3f5; margin-right: 250px;
} margin-bottom: 1em;
margin-left: 4em;
body {
border: 2px solid red;
margin: 0 20%;
background: #e2f3f5;
border: 3px solid red;
}
background-color: #e2f3f5;
}
Margins
Collapsing margins
The top and bottom margins of neighboring elements collapse
The only time top and bottom margins don’t collapse is for floated or
absolutely positioned elements
When you apply left and right margins to inline text elements,
margin space will be held clear before and after the text in the flow of the
element, even if that element breaks over several lines
body {
margin: 0;
}
nav ul {
margin: 0;
}
Exercise
Add a margin-top to h1
h1 {
margin-top: 1.5em;
}
Exercise
Give the main element a 2.5% margin on all sides
For the aside element put 1em on the top, 2.5% on the right side, 0 on
the bottom, and 10% margin on the left side
Assigning Display Types
The display property defines the type of element box an element generates in
the layout
Display type assignment is useful for achieving layout effects while keeping
the semantics of the HTML source intact
You may also make an otherwise inline a (anchor) element display as a block
in order to give it a specific width and height:
Default: none
Box Drop Shadow
box-shadow: 6px 6px gray; /* A */
box-shadow: 6px 6px 5px gray; /* B */
box-shadow: 6px 6px 5px 10px gray; /* C */