Chapter 2 Cascade Style Sheet CSS AAU Sem II 2014
Chapter 2 Cascade Style Sheet CSS AAU Sem II 2014
2
Cascading Style Sheets (CSS):
is a simple mechanism for adding style (e.g. fonts,
colors, layouts) to Web documents.
Styles provide powerful control over the
presentation of web pages.
4 Web Programming
CSS (cont’d)
CSS styles can be specified:
Inside a single HTML element (inline)
Inside the <head> element of an HTML document
(internal)
In an external CSS file (external)
CSS Syntax
selector {property: value;}
6 Web Programming
CSS Syntax …
Parts of a style rule (or statement)
CSS Syntax …
The selector points to the HTML element you
want to style.
The declaration block contains one or more
declarations separated by semicolons.
Each declaration includes a CSS property
name and a value, separated by a colon.
A CSS declaration always ends with a
semicolon, and declaration blocks are
surrounded by curly braces.
8 Web Programming
The selector is normally the HTML element you
want to style.
p {color:red;text-align:center;}
all <p> elements will be center-aligned, with a
red text color
To make the CSS more readable, you can put one
declaration on each line, like this:
p
{
color:red;
text-align:center;
Example Explained
p is}a selector in CSS (it points to the HTML element you want to style: <p>).
color is a property, and red is the property value
text-align is a property, and center is the property value
9 Web Programming
CSS Selectors
A CSS selector selects the HTML element(s) you want
to style.
CSS selectors are used to "find" (or select) HTML
elements based on their element name, id, class,
attribute
Types of selectors
HTML tag names (or element Selector )
Class selectors
Id selectors
Descendant Selector
Child Selector
pseudo-class Selector
Attribute Selectors
10 Web Programming
HTML tag names as selectors(or The
element Selector )
The element selector selects elements based on the
element name.
You can select all <p> elements on a page like this (in
this case, all <p> elements will be center-aligned, with
a red text color):
p {
text-align: center;
color: red;
}
used to override the default attributes of HTML tags
Format: tag_name {property : value}
Ex. a { color: red;
text-decoration: overline
}
11
Grouping Selectors
Selectors can be grouped (separated by comma)
Ex. p, div, table { align: center }
If you have elements with the same style definitions,
like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
12 Web Programming
Grouping Selectors …
In the example below we have grouped the
selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
13 Web Programming
The class selector
define generic styles that can be applied to
different HTML elements
applied to the class attribute of HTML elements
The class selector is used to specify a style for
a group of elements. Unlike the id selector, the
class selector is most often used on several
elements.
This allows you to set a particular style for
many HTML elements with the same class.
The class selector uses the HTML class
attribute, and is defined with a "."
14 Web Programming
Format:
1. Styles for a particular element
tag_name.style_name { property: value }
Ex. p.color { color: green }
<p class=“color”>some text</p>
2. Styles for all elements
.style_name { property: value }
Ex. .color { color: blue }
<div class=“color”>some text</div>
<table class=“color”>…</table>
15 Web Programming
In the example below, all HTML elements with
class="center" will be center-aligned:
.center {text-align:center;}
You can also specify that only specific HTML
elements should be affected by a class.
In the example below, all p elements with
class="center" will be center-aligned:
p.center {text-align:center;}
16 Web Programming
CSS (cont’d)
The Id selector
unlike the class selector, the Id selector always
applies to only one element
The id selector is used to specify a style for a
single, unique element.
The id selector uses the id attribute of the
HTML element, and is defined with a "#".
17 Web Programming
Format:
1. Styles for a particular element
tag_name#style_name { property: value }
Ex. p#color { color: green }
<p id=“color”>some text</p>
2. Styles for all elements
#style_name { property: value }
Ex. #color { color: blue }
<div id=“color”>some text</div>
<table id=“color”>…</table>
possible ???
18 Web Programming
CSS Opacity / Transparency
The opacity property specifies the opacity/transparency of an element.
Transparent Image
The opacity property can take a value from 0.0 - 1.0. The lower value, the
more transparent:
img {
opacity: 0.5;
}
The opacity property is often used together with the :hover selector to
change the opacity on mouse-over: img {
opacity: 0.5;
}
img:hover {
opacity: 1.0;
19 } Web Programming
Transparency using RGBA
div {
background: rgba(76, 175, 80, 0.3) /* Green
background with 30% opacity */
}
20 Web Programming
CSS (cont’d)
CSS comments
Format:
/* comment text */
/*This is a comment*/
p
{
text-align:center;
/*This is another comment*/
color:black;
font-family:arial;
}
21 Web Programming
Three Ways to Insert CSS
There are three ways of inserting a style
sheet:
External style sheet
Internal style sheet
Inline style
22 Web Programming
Inline Styles
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The
style attribute can contain any CSS property.
style information is directly attached to the HTML elements they
affect
higher cascade precedence than the other specification
methods
declaring an individual element’s format:
Attribute style
CSS (style) property
<tag_name style=“property:value; property:
Followed by a colon and a value
value;”> … </tag_name>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
24 Web Programming
Internal or Embedded Style Sheets
this method can only specify style information for the
current document:
1:1 relationship
However, the same document may have other style definitions
applied to it
1:M relationship
embedded style sheet rule will have higher precedence
than external style sheet rule, if there is a conflict
between styles
embedded style sheet rule will have lower precedence
than an inline style sheet rule
Internal / Embedded Style Sheets
Embed an entire CSS document in an XHTML
document’s head section inside a style element
Attribute type
Multipurpose Internet Mail Extensions (MIME) type
describes the type of the document’s content
text/css is the type for CSS document
27 Web Programming
Eg:
<head>
<style>
hr {color:sienna;}
p {margin-left:20px;}
body
{background-image:url("images/back40.
gif");}
</style>
</head>
28 Web Programming
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
29 Web Programming
External Style Sheet
a{
text-decoration:none;
}
a:hover{
text-decoration:underline;
color:red;
background-color:#ccffcc;
}
32 Web Programming
"mystyle.css"
body {
<!DOCTYPE html> background-color: lightblue;
<html> }
<head>
<link rel="stylesheet" href="mystyle.css"> h1 {
</head> color: navy;
<body> margin-left: 20px;
}
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
33 Web Programming
Cascading Order
What style will be used when there is more than one style
specified for an HTML element?
All the styles in a page will "cascade" into a new "virtual" style
sheet by the following rules, where number one has the
highest priority:
So, an inline style has the highest priority, and will override external
and internal styles and browser defaults.
34 Web Programming
Some common CSS properties
Background
CSS background properties are used to define the
background effects of an element
CSS properties used for background effects:
background-color: color
background-image: url(url)
Sets the background image for an element
36 Web Programming
Background Image
The background-image property specifies an
image to use as the background of an element.
By default, the image is repeated so it covers the
entire element.
The background image for a page can be set like
this:
body {background-
image:url('paper.gif');}
Background Image - Repeat Horizontally or
Vertically
By default, the background-image property repeats
an image both horizontally and vertically.
37 Web Programming
Example:
body
{
background-image:url('gradient2.png');
background-repeat:repeat-x;
}
39 Web Programming
The background-position property sets the
starting position of a background image.
Values of background-position
left top
left center
left bottom
right top
right center
right bottom
center top
center center
center bottom
40 Web Programming
Background Image - Fixed position
body {
background-
image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
41 Web Programming
Background - Shorthand property
To shorten the code, it is also possible to specify all the
properties in one single property. This is called a
shorthand property.
The shorthand property for background is simply
"background":
42 Web Programming
body {
background:#ffffff url('img_tree.png') no-repeat right
top;
}
When using the shorthand property the order of
the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
It does not matter if one of the property values is
missing, as long as the ones that are present are
in this order.
43 Web Programming
Text
color: color
The color property is used to set the color of the text.
The color can be specified by:
name - a color name, like "red"
RGB - an RGB value, like "rgb(255,0,0)"
Hex - a hex value, like "#ff0000"
44 Web Programming
The text-decoration property
Specifies the decoration added to text.
text-decoration: decoration {none, underline, overline, line-
through, blink}
Note: The color of the decoration should be set by the
"color" property.
The value text-decoration: none; is often used to remove
underlines from links:
a{
text-decoration: none;
}
Set the text decoration for h1, h2, h3, and h4 elements:
h1 {text-decoration:overline}
h2 {text-decoration:line-through}
h3 {text-decoration:underline}
h4 {text-decoration:blink}
45 Web Programming
direction: direction {ltr, rtl} browser
issue??
(writing direction)
letter-spacing: value in pixels(+ve or –
ve)
text-align: alignment {left, right, center,
justify}
white-space: Sets how white space
inside an element is handled
normal
pre
nowrap
46 Web Programming
Text Shadow
h1 {
text-shadow: 3px 2px red;
}
47 Web Programming
text-indent: value
(16px=1em).
48 Web Programming
text-transform: transform {none, capitalize,
uppercase, lowercase}
The text-transform property controls the
capitalization of text.
Capitalize: Transforms the first character of
each word to uppercase
Uppercase: Transforms all characters to uppercase
lowercase :Transforms all characters to lowercase
Example:
h1 {text-transform:uppercase;}
h2 {text-transform:capitalize;}
p {text-transform:lowercase;}
49 Web Programming
word-spacing: value
The word-spacing property increases or
decreases the white space between words.
Note: Negative values are allowed.
Example
Specify that the space between words in
paragraphs should be 30 pixels:
p
{
word-spacing:30px;
}
50 Web Programming
Line Height
p.small {
line-height: 0.8;
}
p.big {
line-height: 1.8;
}
51 Web Programming
CSS Font
54 Web Programming
Font Size
The font-size property sets the size of the
text.
Setting the text size with pixels, gives you full
control over the text size:
Eg:
h1{font-size:40px;}
h2 {font-size:30px;}
p {font-size:14px;}
(16px=1em)
55 Web Programming
An em is equal to the current font-size, for
instance, if the font-size of the document is 12pt,
1em is equal to 12pt.
Ems are scalable in nature, so 2em would equal
24pt, .5em would equal 6pt
56 Web Programming
Pixels (px): Pixels are fixed-size units that
are used in screen media (i.e. to be read on
the computer screen). One pixel is equal to
one dot on the computer screen (the smallest
division of your screen’s resolution). Many
web designers use pixel units in web
documents in order to produce a pixel-perfect
representation of their site as it is rendered in
the browser. One problem with the pixel unit
is that it does not scale upward for visually-
impaired readers or downward to fit mobile
devices.
57 Web Programming
Points (pt): Points are traditionally used in
print media (anything that is to be printed on
paper, etc.). One point is equal to 1/72 of an
inch. Points are much like pixels, in that they
are fixed-size units and cannot scale in size
58 Web Programming
So, What’s the Difference?
It’s easy to understand the difference
between font-size units when you see them in
action. Generally,1em = 12pt = 16px =
100%. When using these font-sizes, let’s see
what happens when you increase the base
font size (using the body CSS selector) from
100% to 120%.
59 Web Programming
As you can see, both the em and percent units
get larger as the base font-size increases, but
pixels and points do not. It can be easy to set
an absolute size for your text, but it’s much
easier on your visitors to use scalable text
that can display on any device or any
machine. For this reason, the em and percent
units are preferred for web document text.
60 Web Programming
font-style: style {normal, italic, oblique}
font-weight: weight {normal, bold, bolder,
lighter, 100, 200, …}
font-size: size
font-family: font_list (in order of precedence,
separated by comma)
61 Web Programming
Font Variant
p.small {
font-variant: small-caps;
}
62 Web Programming
Borders
Box model
The CSS box model is essentially a box that
wraps around every HTML element. It consists
of: margins, borders, padding, and the
actual content. The image below illustrates
the box model:
63 Web Programming
Margin - Clears an area around the border.
The margin does not have a background color,
it is completely transparent
Border - A border that goes 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
64 Web Programming
Width and Height of an Element
Important: When you set the width and
height properties of an element with CSS, you
just set the width and height of the content
area. To calculate the full size of an element,
you must also add the padding, borders and
margins.
65 Web Programming
The total width of the element in the example
below is 300px
width:250px;
padding:10px;
border:5px solid gray;
margin:10
The total width of an element should 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 be calculated like this:
Total element height = height + top padding + bottom
padding + top border + bottom border + top margin +
bottom margin
66 Web Programming
Margin properties:
margin-top
margin-right
margin-bottom
margin-left
margin
These properties set the top, right, bottom,
and left margin of a box.
Eg: H1 { margin-top: 2em }
67 Web Programming
The margin property is a shorthand property for
setting margin-top, margin-right, margin-
bottom and margin-left at the same place in the
style sheet.
(top->right->bottom->left)
If there is only one value, it applies to all sides.
If there are two values, the top and bottom margins
are set to the first value and the right and left
margins are set to the second.
If there are three values, the top is set to
the first value, the left and right are set to
the second, and the bottom is set to the
third.
68 Web Programming
If there are four values, they apply to the top,
right, bottom, and left, respectively.
Eg:
1)body { margin: 2em }
/* all margins set to 2em */
2) body { margin: 1em 2em }
/* top & bottom = 1em, right & left =
2em */
3) body { margin: 1em 2em 3em }
/* top=1em, right=2em, bottom=3em,
left=2em */
69 Web Programming
The last rule of the example above is
equivalent to the example below:
body {
margin-top: 1em;
margin-right: 2em;
margin-bottom: 3em;
margin-left: 2em; /* copied from opposite side
(right) */
}
70 Web Programming
Border properties
border-top-width
={thin,thick,medium,length}
border-right-width
border-bottom-width
border-left-width
border-width
These properties set the width of the top,
right, bottom, and left border of a box
71 Web Programming
The border's thickness has an explicit value. Explicit
border widths cannot be negative
Eg:
H1 { border-width: thin } /* thin thin
thin thin */
H1 { border-width: thin thick } /* thin
thick thin thick */
H1 { border-width: thin thick medium }
72 Web Programming
border-top-color
border-right-color
border-bottom-color
border-left-color
border-color
73 Web Programming
border-style
The border-style property specifies what kind of
border to display
None of the border properties will have ANY effect
unless the border-style property 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
74 Web Programming
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
75 Web Programming
76 Web Programming
Border - Individual sides
In CSS it is possible to specify different
borders for different sides:
p
{
border-top-style:dotted;
border-right-style:solid;
border-bottom-style:dotted;
border-left-style:solid;
}
77
The example above can also be set with a
single property:
Example
border-style:dotted solid;
78
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
79
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.
80
Border - Shorthand property
To shorten the code, it is also possible to
specify all the individual border properties in
one property. This is called a shorthand
property.
The border property is a shorthand for the
following individual border properties:
border:<border-width>|<border-style>|<color>
The 'border' property is a shorthand property for
setting the same width, color, and style for all
four borders of a box.
border-width
border-style (required)
border-color
Example
border:5px solid red;
81 Web Programming
Unlike the shorthand 'margin' and 'padding'
properties, the 'border' property cannot set
different values on the four borders. To do so, one
or more of the other border properties must be
used.
For example, the first rule below is equivalent to
the set of four rules shown after it:
P { border: solid red }
P{
border-top: solid red;
border-right: solid red;
border-bottom: solid red;
border-left: solid red
}
82 Web Programming
border-bottom Property
The border-bottom property is a shorthand
property for (in the following order):
border-bottom-width
border-bottom-style
h1 {border-bottom-color
border-bottom: 5px solid red;
}
h2 {
border-bottom: 4px dotted blue;
}
div {
border-bottom: double;
}
83 Web Programming
border-bottom-color Property
84 Web Programming
CSS Rounded Borders
The border-radius property is used to add rounded borders to an element:
p{
border: 2px solid red;
border-radius: 5px;
}
An outline is a line drawn outside the
element's border.
85 Web Programming
CSS Outline
p{
border: 2px solid black;
outline: #4CAF50 solid 10px;
margin: auto;
padding: 20px;
text-align: center;
}
86 Web Programming
CSS has the following outline properties:
outline-style
outline-color
outline-width
outline-offset
outline
87 Web Programming
CSS Outline Offset
The outline-offset property adds space
between an outline and the edge/border of
an element. The space between an
element and its outline is transparent.
p{
margin: 30px;
border: 1px solid black;
outline: 1px solid red;
outline-offset: 15px;
}
88 Web Programming
CSS Outline - Shorthand property
The outline property is a shorthand property for setting the following
individual outline properties:
outline-width
outline-style (required)
outline-color
The outline property is specified as one, two, or three values from the
list above. The order of the values does not matter.
89 Web Programming
Other Selectors
Descendant Selector
The descendant selector matches all elements that are
descendants of a specified element.
The following example selects all <p> elements inside <div>
elements:
div p {
background-color: yellow;
}
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<section><p>Paragraph 3 in the div.</p></section>
</div>
<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>
90
Child Selector (>)
The child selector selects all elements that are the
children of a specified element.
The following example selects all <p> elements that are
children of a <div> element:
div > p {
background-color: yellow;
}
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<section><p>Paragraph 3 in the div.</p></section> <!-- not Child but Descendant -->
<p>Paragraph 4 in the div.</p>
</div>
91
Pseudo-class Selector
What are Pseudo-classes?
A pseudo-class is used to define a special state of an
element.
For example, it can be used to:
Style an element when a user mouses over it
Style visited and unvisited links differently
Style an element when it gets focus
Pseudo-classes are keywords that start with a colon:
Syntax
The syntax of pseudo-classes:
:pseudo-class-name
selector:pseudo-class {
property: value;
}
92
Anchor Pseudo-classes
Links can be displayed in different ways:
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited link */
a:visited {
color: #00FF00;
}
/* selected link */
a:active {
color: #0000FF;
}
93 Web Programming
Pseudo-classes and CSS Classes
Pseudo-classes can be combined with CSS classes:
When you hover over the link in the example, it
will change color:
a.highlight:hover {
color: #ff0000;
}
Hover on <div>
An example of using the :hover pseudo-class on a <div> element:
div:hover {
background-color: blue;
}
94 Web Programming
:focus
The :focus CSS pseudo-class represents an element (such as a form input) that
has received focus. It is generally triggered when the user clicks or taps on an
element or selects it with the keyboard's Tab key.
.red-input:focus {
background: yellow;
color: red;
}
.blue-input:focus {
background: yellow;
95
color: blue;
The :first-child Pseudo-class
The :first-child pseudo-class matches a specified element that is the first child
of another element.
p:first-child {
color: blue;
}
96 Web Programming
:last-child
p:last-child Selects every <p> elements that is the last child of its
parent
p:last-child {
background: #ff0000;
}
97 Web Programming
Pseudo-elements
What are Pseudo-Elements?
A CSS pseudo-element is used to style specified
parts of an element.
For example, it can be used to:
Style the first letter, or line, of an element
Insert content before, or after, the content of an
element
Syntax
98 Web Programming
The ::first-line Pseudo-element
The ::first-line pseudo-element is used to add a special style to the first line of
a text.
The following example formats the first line of the text in all <p> elements:
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
99 Web Programming
Note: The ::first-line pseudo-element can only be applied to block-level
elements.
• font properties
• color properties
• background properties
• word-spacing
• letter-spacing
• text-decoration
• vertical-align
• text-transform
• line-height
• clear
The following example formats the first letter of the text in all <p> elements:
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
font properties
color properties
background properties
margin properties
padding properties
border properties
text-decoration
vertical-align (only if "float" is "none")
text-transform
line-height
float
clear
The following example inserts an image before the content of each <h1>
element:
h1::before {
content: url(smiley.gif);
}
p::before {
content: "Read this: ";
}
::marker {
color: red;
font-size: 23px;
}
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<ol>
<li>First</li>
<li>Second</li>
<li>Third</li>
104
</ol> Web Programming
The ::selection Pseudo-element
The ::selection pseudo-element matches the portion of an element that is
selected by a user.
The following example makes the selected text red on a yellow background:
Example
::selection {
color: red;
background: yellow;
}
The following example selects all <a> elements with a target attribute:
a[target] {
background-color: yellow;
}
<a href="https://www.w3schools.com">w3schools.com</a>
<a href="http://www.disney.com" target="_blank">disney.com</a>
<a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>
<a href="https://www.w3schools.com">w3schools.com</a>
<a href="http://www.disney.com"
target="_blank">disney.com</a>
<a href="http://www.wikipedia.org"
target="_top">wikipedia.org</a>
display property
The display property defines how a certain
HTML element should be displayed.
Every HTML element has a default display
value depending on what type of element it is.
The default display value for most elements
is block or inline.
li {
display: inline;
}
h1.hidden {
display: none;
}
span.b {
display: inline-block;
width: 100px;
height: 100px;
padding: 5px;
border: 1px solid blue;
background-color: yellow;
}
span.c {
display: block;
width: 100px;
height: 100px;
118 Web Programming
padding: 5px;
Using inline-block to Create Navigation Links
•One common use for display: inline-block is to display list items
horizontally instead of vertically. The following example creates
horizontal navigation links:
.nav {
background-color: yellow; <ul class="nav">
list-style-type: none; <li><a href="#home">Home</a></li>
text-align: center; <li><a href="#about">About
padding: 0; Us</a></li>
margin: 0; <li><a href="#clients">Our
} Clients</a></li>
<li><a href="#contact">Contact
.nav li { Us</a></li>
display: inline-block; </ul>
font-size: 20px;
padding: 20px;
}
Eg:
.relative1 {
position: relative;
}
.relative2 { position: relative; top: -20px; left: 20px;
background-color: white; width: 500px;
124 Web Programming
}
<div class="relative1">relative behaves the same as static unless you
add some extra properties.
</div>
<div class="relative2">Setting the top, right, bottom, and left properties
of a relatively-positioned element will cause it to be adjusted away from
its normal position. Other content will not be adjusted to fit into any gap
left by the element.
</div>
.center {
position: absolute;
top: 50%; <div class="container">
width: 100%; <img src="img_5terre_wide.jpg"
text-align: center; alt="Cinque Terre" width="1000"
font-size: 18px; height="300">
} <div class="center">Centered</div>
</div>
img {
width: 100%;
height: auto;
opacity: 0.3;
}
Note: The overflow property only works for block elements with a specified height.
div {
overflow-x: hidden; /* Hide horizontal
scrollbar */
overflow-y: scroll; /* Add vertical
scrollbar */
}
box-sizing: border-box;
}
Ul2{
list-style-type:circle;
list-style-position:outside;
}140 Web Programming
Indents the marker
and the text. The
side
bullets appear inside
the content flow
li a:hover:not(.active) {
background-color: #555;
color: white;
} 143 Web Programming
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li {
text-align: center;
border-bottom: 1px solid #555;
}
li:last-child {
border-bottom: none;
}
144 Web Programming
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
}
148 Web Programming
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
li a.active {
background-color: #4CAF50;
color: white;
li a:hover:not(.active) {
background-color: #555;
color: white;
}
149 Web Programming
<ul>
<li><a class="active"
href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
a{
display: block;
padding: 8px;
background-color: #dddddd;
}
152 Web Programming
Example explained:
float: left; - use float to get block elements to slide next to each other
display: block; - Allows us to specify padding (and height, width, margins,
etc. if you want)
padding: 8px; - Since block elements take up the full width available, they
cannot float next to each other. Therefore, specify some padding to make
them look good
background-color: #dddddd; - Add a gray background-color to each a
element
li {
float: left;
}
154 Web Programming
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
155 Web Programming
<ul>
<li><a class="active"
href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<div class="header">
<h1>Header</h1>
</div>
</body>
body {
margin: 0;
}