Professional Documents
Culture Documents
CHAPTER Three - Cascading Style Sheets (CSS)
CHAPTER Three - Cascading Style Sheets (CSS)
<link> tells the browser where to find the CSS file used
to style the page.
Rel specifies the relationship between the HTML page Stylesheet, script
and the file it is linked to
Type specifies the type of document being linked to. “text/css” , “text/javascript”
Href specifies the path to the CSS file (which is often default.css or
placed in a folder called css or styles) /css/default.css
CSS Selectors:
There are many different types of CSS selectors that allow
you to target rules to specific element in an HTML document.
Universal Selector (just select everything!). Applies to all
elements in the document. We use *{}
<html>
<head>
<style type="text/css">
*{
color: red;
}
</style>
</head>
<body>
<p>This paragraph includes <em>descendant </em> selector </p>
This line not include<em>descendant</em> selector
</body>
</html>
Type Selector. The CSS type selector matches elements by
node name. In other words, it selects all elements of the
given type within a document.
<html>
<head>
<style type="text/css">
p{
color: red;
}
</style>
</head>
<body>
<p>This paragraph includes <em>descendant </em> selector </p>
This line not include<em>descendant</em> selector
</body>
</html>
Class Selector. Styles can be created using classes.
Classes mark certain elements so that you can refer to
them in your style sheet.
✓ A class can be applied to multiple section, whereas an
ID uniquely identifies a specific selection within a
document.
✓ Creating Class Style
.className
{ E.g., <style >
attribute: value <li class="new">Spraying
} Techniques for Fruit Trees
.new { Color: red}
</li>
</style>
Id Selector. Ids work the same way, except that you can
apply them only once per document.
✓ define the ID in the <style> area, preceding the
ID name with a hash symbol (#), as shown below.
# idName
{ E.g.
attribute: value <style >
} #special {color: red}
</style>
<html>
<head>
<style type="text/css">
div ol > li strong {color: red;}
</style>
</head>
<body>
<ol>
<li>The strong text here is <strong>not</strong> change the color. </li>
</ol>
<div>
<ol>
<li>next is direct child ..
<ul>
<li>The strong text here <strong>is changed</strong> the color. </li>
</ul>
</li>
</ol> </div> </body> </html>
Adjacent Sibling Selector. Selects an element’s next
sibling, that is it allows you to select an element that is
directly after another specific element.
✓ The special character used in Adjacent Sibling
selector is the + (plus) character.
✓ Specified as div + p {color: red;}
<html>
<head>
<style type="text/css">
div + p {color: red;}
</style>
</head>
<body>
<h1>Heading</h1>
<p>Next Paragraph</p>
<div> Div section </div>
<p> Paragraph after Div</p>
</body>
</html>
General Sibling Selector. Almost similar to the
adjacent sibling selector. The difference is that it selects any
element that follows another element, it doesn’t need to be
directly preceding element, can appear anywhere after it.
character used in Adjacent Sibling selector is ~ (tilde)
character.
✓ Created as: p ~ h1 {color: red;}
<html>
<head>
<style type="text/css">
p ~ h1 {color: red;}
</style>
</head>
<body>
<h1>Heading </h1>
<p>Paragraph after heading 1</p>
<h1>Heading 2</h1>
<p>Paragraph after heading 2</p>
<b>This is to test the general sibling</b><br>
<i>This is to test the general sibling</i>
<h1>Heading 2</h1>
</body>
Multiple Style Sheets 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:
✓ Inline Style (inside an HTML elements)
✓ Internal and External style sheets (inside
head section).
✓ Browser Default
So, an inline style has the highest priority, and will
override external and internal styles and browser defaults.
If there are two or more rules that apply to the same element,
it is important to understand which will take precedence.
✓ Last rule: If the two selectors are identical, the latter of
the two will take precedence.
✓ Specificity: If one selector is more specific than the
others, the more specific rule will take precedence over
more general ones.
✓ Important: You can add !important after any property
value to indicate that it should be considered more
important than other rules that apply to the same
element.
CSS Inheritance:
If you specify the font-family or color properties on the
<body> element, they will apply to most child elements.
This is because the value of the font-family property is
inherited by child elements.
It saves you from having to apply these properties to as
many elements (and results in simpler style sheets).
You can compare this with the background-color or border
properties; they are not inherited by child elements. If
these were inherited by all child elements then the page
could look quite messy.
To enforce properties to inherit values from their parent
elements, use “inherit” word for the value of the
properties.
CSS Color
As we have discussed, each HTML elements has invisible box
and content. Hello World
Box
Content
So, you can set color for both the box and the content with
background-color and color properties respectively.
The value can be specified using RGB values, hex codes
and color names.
Color name: e.g., background-color: red; CSS has 147
predefined color names.
RGB values: each and every colors are a combination of
RGB (i.e. RED, GREEN and BLUE) colors
E.g., Color: rgb (0,0,0) //black color
Each value be from 0 to 255
Color: rgb (255,255,255) is white color
Hex codes: Color property can be set with
hexadecimal value. Each color has 6-digit hexadecimal
value
E.g., color: #000000 //black color
background: #ff0000 // red color
Opacity: The value for opacity is a number between 0
(completely transparent) and 1(completely opaque).
The opacity setting applies to the entire element both
the foreground and the background.
Note: If you want to affect just one or the other, use an
RGBa color value instead.
CSS3: HSL Colors
CSS hsl() function can be used to provide a color value when
using CSS.
It allows you to specify a color value by specifying the hue,
saturation, and light components of the color.
HSL (which stands for Hue Saturation Lightness) is a hue-based
representation of the RGB color space of computer graphics.
Color: hsl(180, 0.5 ,0.8)
The hue component: represents an angle of the color circle. You can
specify the value as an angle in degree (e.g. 180deg) or simply as a
number (e.g. 180)
Saturation: it is expressed as a percentage. For e.g. 100% is fully
saturated (more colorful and intense), while 0 is fully-unsaturated gray.
Lightness: it represents the amount of light in the color. For lightness,
50% is the “normal” setting, while 100% is white and 0% is black.
CSS Box Model:
When laying out a document, the browser's rendering engine
represents each element as a rectangular box according to
the standard CSS basic box model.
CSS determines the size, position, and properties (color,
background, border size, etc.) of these boxes.
border: solid ;
Margin area:
✓ This property can be used to set a margin on all four sides
of an element. Margins create extra space around an
element, unlike padding, which creates extra space within an
element.
✓ This property(margin) is a shorthand for the following CSS
properties:
Margin , margin-top, margin-right, margin-bottom, margin-left
✓ Margin may be specified using one, two, three, or four
values. Each value is a <length>, a <percentage>, or the
keyword auto. Negative values draw the element closer to
its neighbors than it would be by default.
❖When one value is specified, it applies the same margin to all four sides.
❖When two values are specified, the first margin applies to the top and
bottom, the second to the left and right.
❖When three values are specified, the first margin applies to the top, the
second to the right and left, the third to the bottom.
❖When four values are specified, the margins apply to the top, right,
bottom, and left in that order (clockwise).
<p>Analog synthesizers are often said to have a "warmer" sound
than their digital counterparts. </p>
<p class="example">Analog synthesizers are often said to have a
"warmer" sound than their digital counterparts. </p>
CSS
p{
width: 200px;
border: 2px solid #0088dd;
padding: 10px;}
p.example {
margin: 20px;}
CSS Typography
❖ Is a way that text is arranged and presented.
❖ So, there are a lot of property for text including the font
of the text.
✓ Font
❖ Font-size: 2em Font-weight: 700/bold Typeface(font
family): Times Font Style: italic Font-variant: small-caps
✓ Text-decoration
❖ text-decoration: underline/overline/line-through
✓ Text-transformation
❖ Text-transform: capitalize/uppercase/lowercase
✓ Text-align
❖ Text-align: right Text-indent: 2em
Generic Font names
In this example the h1 inherits the default 16px size from the html element and
applying the 150% value multiplies that inherited value, resulting h1 that is 24 pixels.
font: The font shorthand property can compile all the font-related property into
one rule.
The font property must include a font-size value and a font-family value, in that order.
Omitting one or putting them in the wrong order cause the entire rule to be invalid. This is
an example of minimal font property value:
Leading(line-height): is used for the vertical space between lines of text.
Indent: The text-indent property indents the first line of text by a specified amount. Length
measurement or percentage value for text-indent.
Horizontal Text Alignment: aligning text for webpage is possible like word processing or
desktop publishing program with the text-align property.
Letter and word spacing: add space between the letters of the text or words in a line,
respectively.
Text Shadow: adds a shadow below the text that makes it seem to hover above the
page. Text shadows are behind the text but in front of the background and border if
there is one.
The value contains two or three measurements (a horizontal offset, vertical offset and an
optional blur radius).
CSS Lists
✓ Browsers automatically insert
bullets before unordered list items,
and numbers before items in order
list.
✓ Authors can choose the type and
position of the marker, or turn them
off entirely.
✓ List properties are the followings:
❖ list-style-type: upper-roman
❖ list-style-position: inside/outside
❖ list-style-image: url(location)
Layout and Positioning
✓ If one block-level element sits inside ❖ The <body> element is the containing
another block-level elements then the element for these three <div>
outer box is known as the containing elements.
or parent elements. ❖ The second <div> element is the
containing element for two
✓ It is common to group a number of paragraphs of Latin text and images
elements together inside a <div> or (represented by crossed squares).
<section> elements. Let us see the
following figure in detail
❖ A box may be nested inside several
other block-level elements. The
containing element is always the direct
parent of that element.
❖ The red lines in this diagram represent
<div> elements.
❖ The header (containing the logo and
navigation) are in one <div> element,
the main content of the page is in
another, and the footer is in a third.
✓ Floating and positioning are used for breaking out the normal flow and
arranging elements on the page. Floating an element moves it to the left or
right and allows the following text to wrap around it. Positioning is a way to
specify the location of an element anywhere on the page with pixel precision.
Normal Flow
✓ In normal flow, each block level element sits on top of the next one. Inline
elements and text characters line up next to one another to fill the block
elements.
✓ When the window or containing element resizes, the block elements expand
or contract to the new width, and the inline content reflows.
✓ It is default way in which browsers treat HTML elements.
Floating
✓ float property moves an element as far possible to the left or right, allowing
the following content to wrap around it.
✓ The next example shows how the paragraph and the contained image are
rendered by default and how it looks when the float property is applied
How can we add a space between the image element and the surrounding text? We can
use Margin.
Important notes
❖ The floated element removed from its position in the normal flow but it continues to
influence the surrounding content. (Like island of a stream).
❖ Float stay in the content area of the containing element.
❖ Margins are maintained.
Floating inline elements
❖ It is possible to float an inline text (non-replaced) element. The following example
shows floating a span of text.
Important points (non-replaced inline element floating)
✓ It is necessary to specify a width for floated text element.
✓ Floated inline elements behave as block elements.
✓ Margin on floated elements do not collapse.
Floating block elements
✓ It is necessary to provide a width for floated block elements.
✓ Elements do not float higher than their reference in the source.
✓ Non-floated elements maintain the normal flow.
Clear property: is used to turn the text wrapping off and get back to normal flow
as usual. This can be done by clearing the element that you want to start below the
float. The left ,right, both can be used as a value.
N.B. You apply clear property to the element you want to start below the floated
element, not the floated element itself.
In the example below the clear property is used to make h2 element start below left-
floated element.
Positioning
Static
❖ This is the normal positioning scheme in
which elements are positioned as they
occur in the normal document flow.
❖ Relative: moves an element in relation to where it would have been in normal flow.
When an element is relatively positioned, the space it once occupied is preserved
and continues to influence the layout of surrounding content.
✓ You can indicate that an element should be relatively positioned using the
position property with a value of relative.
✓ It is used with offset properties (top or bottom and right or left) to indicate how
far to move the elements from where it would have been in normal flow.
❖ Absolute: When the position property is given a value of absolute, the box is taken
out of normal flow and no longer affects the position of other elements on the page.
When an element is absolutely positioned, the space it once occupied is closed up.
❖ Fixed: Fixed positioning is a type of absolute positioning that requires the position
property to have a value of fixed.
✓ It positions the element always in relation to the browser window. Therefore,
when a user scrolls down the page, it stays in the exact same place.
In this example the h1 positioned
as fixed so it can placed exactly
the same place when the user
scrolls.
❖ Sticky: Sticky positioning is a combination of relative and fixed in that it behaves
as though it is relatively positioned, until it is scrolled into a specified position relative
to the viewport, at which point it remains fixed.
❖ Z-index: When you use relative, fixed, or absolute positioning, boxes can overlap.
✓ If boxes do overlap, the elements that appear later in the HTML code sit on top
of those that are earlier in the page.
✓ If you want to control which element sits on top, you can use the z-index property.
✓ Its value is a number, and the higher the number the closer that element is to the
front.
Display Types
❖ The display property defines the type of element box an element generates in the
layout.
❖ In addition to the inline and block display types, we can make elements display as
list items, flex or grid or various parts of a table.
❖ It is common practice to make li elements (which usually display the characteristics of block
elements) display as inline elements to turn a list into a horizontal navigation bar. a(anchor)
element display as a block in order to give it a specific width and height.
❖ Another useful value for the display property is none, which removes the content
from the normal flow entirely. Unlike visibility: hidden, which just makes the element
invisible but keeps the space it would have occupied blank, display: none removes
the content, and the space it would have occupied is closed up.
N.B Display type assignment is useful for achieving layout effects while keeping
the semantics of the HTML source.
CSS Flexbox
❖ The Flexible Box Module, usually referred to as flexbox, was designed as a one-
dimensional layout model, and as a method that could offer space distribution between
items in an interface and powerful alignment capability.
❖ When we describe flexbox as being one dimensional, we are describing the fact that
flexbox deals with layout in one dimension at a time — either as a row or as a column.
This can be contrasted with the two-dimensional model of CSS Grid Layout, which
controls columns and rows together.
❖ When working with flexbox you need to think in terms of two axes — the main axis
and the cross axis. The main axis is defined by the flex-direction property, and the
cross axis runs perpendicular to it.
❖ Main Axis
The main axis is defined by flex-direction, which has four possible values:
row, row-reverse, column or column reverse
1) When we choose row or row-reverse, the
main axis will run along the row in the
inline direction
2) When we choose column or column-
reverse the main axis will run from the
top of the page to the bottom – in the
block direction
CSS Grid
❖ Grid Layout used at dividing a page into major regions or defining the
relationship in terms of size, position, and layer, between parts of a control
built from HTML primitives.
❖ Like tables, grid layout enables an author to align elements into columns and
rows. However, many more layouts are either possible or easier with CSS
grid than they were with tables.
Grid layout properties
○ display ○ grid-column-start
○ grid-template-columns ○ grid-row-end
○ grid-template-rows ○ grid-column-end
○ grid-template-areas ○ grid-row
○ grid-template ○ grid-column
○ grid-auto-columns ○ grid-area
○ grid-auto-rows ○ row-gap
○ grid-auto-flow ○ column-gap
○ grid ○ gap
○ grid-row-start
HTML5 Layout Elements
❖ For a long time, web page authors used <div> elements to group together related
elements on the page (such as the elements that form a header, an article, footer or
sidebar).
❖ Authors used class or id attributes to indicate the role of the <div> element in the
structure of the page.
Traditional way HTML5
❖ Header/footer: The <header> and <footer> elements can be used to create
the main header or footer that appears at the top or bottom of every page on
the site.
❖ Nav: The <nav> element is used to contain the major navigational blocks on the
site such as the primary site navigation
❖ Article: The <article> element acts as a container for any section of a page that
could stand alone and potentially be syndicated
❖ Aside: The <aside> element state additional information about the page or
other things
❖ Section: The <section> element groups related content together, and typically
each section would have its own heading.
❖ Hgroup: The purpose of the <hgroup> element is to group together a set of one
or more <h1> through <h6> elements so that they are treated as one single
heading.
❖ Figure and Figcaption: Used to insert image and figure caption.