04 CSS
04 CSS
(CSS)
Dr- AlaaEddin Almabhouh
Web Programming
OUTLINE
• Motivation of CSS
• CSS Syntax
• Three Ways to Insert CSS
• CSS Colors
• CSS Properties
• CSS Layout
WHAT IS CSS?
p{
text-align: center;
color: red;
}
THE ELEMENT SELECTOR - EXAMPLE
<!DOCTYPE html>
<html>
<head>
<style>
h1 {text-align: center;}
p {color: gray;}
</style>
</head>
<body>
<h1>My Heading</h1>
<p>This is a simple paragraph</p>
</body>
</html>
THE ID SELECTOR
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be unique within a page, so the id selector is used to select
one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of
the element.
Example:
p.center {
You can also specify that only
text-align: center;
specific HTML elements should color: red;
be affected by a class. }
If you have elements with the same style definitions, it will be better to
group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
h1, h2, p {
text-align: center;
color: red;
}
CSS COMMENTS
p{
Comments are used to explain the color: red;
code, and may help when you edit /* This is a single-line comment */
the source code at a later date. text-align: center;
}
Comments are ignored by browsers.
A CSS comment starts with /* and /* This is
ends with */. Comments can also span a multi-line
comment */
multiple lines
THREE WAYS TO INSERT CSS
In HTML, colors can also be specified using RGB values, HEX
values, and HSL values:
‒ rgb(255, 99, 71)
‒ #ff6347
‒ hsl(9, 100%, 64%)
RGB VALUE
In HTML, a color can be specified using hue, saturation, and lightness (HSL)
in the form:
hsl(hue, saturation, lightness)
‒ Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and 240
is blue.
‒ Saturation ( )عبشتلاis a percentage value, 0% means a shade of gray, and 100% is
the full color.
‒ Lightness ( )ةءاضإis also a percentage, 0% is black, 50% is neither light or dark,
100% is white
CSS Properties
CSS BACKGROUNDS
The CSS background properties are used to define the background effects for elements.
CSS background properties:
Property Description
background Sets all the background properties in one declaration
background- Sets whether a background image is fixed or scrolls with
attachment the rest of the page
background-color Sets the background color of an element
background-image Sets the background image for an element
background-position Sets the starting position of a background image
background-repeat Sets how a background image will be repeated
BACKGROUND IMAGE
p{
border: 2px solid red;
border-radius: 5px;
}
CSS MARGINS
The CSS margin properties are used to create space around elements,
outside of any defined borders.
With CSS, you have full control over the margins. CSS has properties for
specifying the margin for each side of an element:
‒ margin-top
‒ margin-right
‒ margin-bottom
‒ margin-left
CSS MARGINS (CONT.)
p{
margin: 25px 50px 75px 100px;
}
CSS PADDING
The CSS padding properties are used to generate space around an element's
content, inside of any defined borders.
CSS has properties for specifying the padding for each side of an element:
‒ padding-top
‒ padding-right
‒ padding-bottom
‒ padding-left
BOX MODEL
unicode-bidi Used together with the direction property to set or return whether the text should be overridden to
support multiple languages in the same document
vertical-align Sets the vertical alignment of an element
white-space Specifies how white-space inside an element is handled
word-spacing Increases or decreases the space between words in a text
CSS FONTS
Property Description
font Sets all the font properties in one declaration
font-family Specifies the font family for text
font-size Specifies the font size of text
font-style Specifies the font style for text
font-variant Specifies whether or not a text should be displayed in a
small-caps font
font-weight Specifies the weight of a font
CSS ICONS
To use the Font Awesome icons, add the following line inside the <head>
section of your HTML page:
The position property specifies the type of positioning method used for an
element.
• The position property:
static: normal position
relative: relative to the normal position
the left and top properties specify offsets from the normal position
absolute: relative to first non-static parent (or html)
The element's position can be specified with the left,
top, right, and bottomproperties
fixed: relative to the browser window
POSITIONING TIPS
Property Description
bottom Sets the bottom margin edge for a positioned box
clip Clips an absolutely positioned element
left Sets the left margin edge for a positioned box
position Specifies the type of positioning for an element
right Sets the right margin edge for a positioned box
top Sets the top margin edge for a positioned box
z-index Sets the stack order of an element
CSS LAYOUT - OVERFLOW
The CSS overflow property controls what happens to content that is too big
to fit into an area.
Property Description
overflow Specifies what happens if content overflows an element's
box
overflow-x Specifies what to do with the left/right edges of the
content if it overflows the element's content area
overflow-y Specifies what to do with the top/bottom edges of the
content if it overflows the element's content area
CSS LAYOUT - FLOAT AND CLEAR
img {
float: right;
}
CSS OPACITY / TRANSPARENCY
img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
CSS NAVIGATION BAR
With CSS you can transform boring HTML menus into good-looking navigation
bars.
A navigation bar needs standard HTML as a base.
A navigation bar is basically a list of links, so using the <ul> and <li>
elements makes perfect sense.
CSS WEBSITE LAYOUT
• W3Schools
http://www.w3schools.com/css
• Tutorials Point
https://www.tutorialspoint.com/css