IP Chapter 4 CSS
IP Chapter 4 CSS
CSS
Cascading Style Sheet
Why CSS?
Because it helps to separate content of an HTML
document from its presentation
example.html
style.css
CSS saves time:
You can write CSS once and then reuse same sheet in multiple HTML
pages.
Pages load faster :
If you are using CSS, you do not need to write HTML tag attributes every
time. less code means faster download times.
Easy maintenance:
To make a global change, simply change the style, and all elements in all
the web pages will be updated automatically.
Superior styles to HTML:
CSS has a much wider array of attributes than HTML
Multiple Device Compatibility:
Style sheets allow content to be optimized for more than one type of
device. By using the same HTML document, different versions of a
website can be presented for handheld devices such as PDAs and cell
phones or for printing.
Cascading Style Sheets, level 1 (CSS1) was came out of
W3C as a recommendation in December 1996. This
version describes the CSS language as well as a simple
visual formatting model for all the HTML tags.
Property
is a type of attribute of HTML tag
Value
Values are assigned to properties.
The Element Selector
used to select an HTML element by its tag name
p{color:green;}
The ID Selector
used to select an HTML element by its id
#paraone{color:green;}
The Class Selector
define generic styles that can be applied to different HTML elements
.firstclass{color:green;}
To select
s p.firstclass{color:red;}
The Universal Selectors
matches the name of any element type
*{color:green;}
input[type]{color:green;}
input[type="text"]{color:green;}
Grouping Selectors
to apply style to many selectors, just separate the
selectors with a comma
h1,h2,h3,#para,.classone{color:green;}
A combinator is something that explains the
descendant selector
child selector
Child Selector
Used to select all elements that are the immediate children of a
specified element.
To select all <p> elements that are immediate children of a <div>
element:
div > p {background-color: yellow; }
Adjacent Sibling Selector
Used to select all elements that are the adjacent siblings of a
specified element.
Sibling elements must have the same parent element, and
"adjacent"
To select all <p> elements that are placed immediately after
<div> elements:
div + p { background-color: yellow; }
Inline style
External style sheet
you can change the look of an entire website by changing just
one file!
Each page must include a reference to the external style sheet
file inside the <link> element
<link rel="stylesheet" type="text/css" href="mystyle.css">
body {
background-color: lightblue;
s
}
h1 { mystyle.css
color: navy;
margin-left: 20px;
}
Internal style sheet
element
To use inline styles, add the style attribute to the
relevant tag
<h1 style="color:blue;margin-left:30px;">
This is a heading.
</h1>
Which style will be used when there is more than one style
specified for an HTML element?
Cascading order
Rules of precedence application:
Inline styles
Internal styles
External styles
Browser default
Comments are ignored by browsers.
following methods:
a color name - like "red"
h1 {color: orange;}
p {color: rgb(255,125,0);}
When setting the style for several link states, there are some order rules:
a:hover MUST come after a:link and a:visited
a:active MUST come after a:hover
List Property Types
The list-style-type
used to control the shape or appearance of the marker
The list-style-image
to set an image as a marker rather than a bullet or number
The list-style-position
specifies whether a long point that wraps to a second line
should align with the first line or start underneath the start of
the marker.
The marker-offset
specifies the distance between a marker and the text in the list
The list-style
serves as shorthand for the preceding properties
Image Properties Example Possible
Values
list-style-type list-style-type:decimal; None, disc,
circle, square
list-style-type:square; decimal,
lower-alpha,
upper-alpha,
lower-roman,
upper-roman
list-style-image list-style-image:url(/https/www.scribd.com/images/bullet.gif)
list-style-position list-style-position : outside; inside, outside
marker-offset marker-offset:2em;
Shorthand property list-style: square inside
url(“bullet.jpg”);
1. Specify the role of HTML and also CSS in a given
HTML document.
borders,
padding, and
The border-color
specifies the color of a border
The border-style
specifies whether a border should be solid, dashed
line, double line, or one of the other possible values
The border-width
specifies the width of a border
Border Properties Example Possible
Values
border-color border-bottom-color:#009900;
OR
border-color: red;
border-color: red green;
border-color: red green yellow;
border-color: red green yellow blue;
border-style border-bottom-style: solid; dotted, solid,
OR double,
border-style: groove; dashed
Border-style: dotted solid;
Border-style: dotted solid groove;
border-width border-bottom-width:4px; thin, thick,
border-left-width: thin; medium,
border-right-width: medium; length or %
OR
border-width: thick medium;
Shorthand property border:4px solid red;
The padding property
used to specify how much space should appear between the
content of an element and its border
Relative Positioning
sets the position of an HTML element relative to where it normally appears
Fixed Positioning
to fix the position of an element to a particular spot on the page, regardless of
scrolling.
Specified coordinates will be relative to the browser window
Absolute Positioning
is positioned relative to the nearest positioned ancestor
A "positioned" element is one whose position is anything except static.
if an absolute positioned element has no positioned ancestors, it uses the
document body,
div.relative {
position: relative;
<body> width: 400px;
<img src=“bird3.png” id=“fixed”/> height: 200px;
<div class="relative"> border: 3px solid #8AC007;
relative div }
<div class="absolute"> div.absolute {
absolute div position: absolute;
<div> top: 80px;
</div> right: 0;
width: 200px;
</body> height: 100px;
border: 3px solid #8AC007;
}
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #8AC007;
}
When elements are positioned, they can overlap
other elements.
The z-index property
specifies the stack order of an element (which
element should be placed in front of, or behind, the
others).
option
visible
The box and its contents are shown to the user.
hidden
The box and its content are made invisible, although they still
affect the layout of the page.
collapse
This is for use only with dynamic table columns and row
effects.
Initial
Sets this property to its default value.
Inherit
Inherits this property from its parent element.
Display: inherit;
Display: inline-block;
This value causes an element to generate an inline-level block container. The inside
of an inline-block is formatted as a block box, and the element itself is formatted as
an atomic inline-level box.
Display: table;
Display: table-row;
The double colon replaced the single-colon notation for pseudo-elements in CSS3 to
distinguish pseudo-classes from pseudo-elements.
p::first-line { text-decoration: underline; }
p.noline::first-line { text-decoration: none; }
p::first-letter { font-size: 5em; }
p::before { content: url(/https/www.scribd.com/images/bullet.gif); }
p::after { content: “Thank you”; color:red; font-size:34px;}
<body>
<div>
<p>First paragraph in div</p>
<p class=“noline”>Second paragraph in div. </p>
</div>
</body>
The overflow property
element's box
CSS Syntax
overflow: visible|hidden|scroll|auto|initial|inherit;
visible
The overflow is not clipped. It renders outside the element's box. This
is default
hidden
The overflow is clipped, and the rest of the content will be invisible
scroll
The overflow is clipped, but a scroll-bar is added to see the rest of the
content
auto
If overflow is clipped, a scroll-bar should be added to see the rest of the
content
initial
Sets this property to its default value.
inherit
Inherits this property from its parent element.
The float Property
<style>
img {width:100px;
height:100px;
float: left;
margin: 5px;}
</style>
s
<body>
<img src="bird3.png"/>
<p>Lorem ipsum dolor sit
amet, ……p>
</body>
Aside from the simple example of wrapping text
s
Width: prevent it from stretching out to the edges
of its container
Margin: auto; centers the element within its
container
Problem: when the browser window is smaller
than the width of the element. The browser will
add a horizontal scrollbar to the page.
CSS Syntax
opacity: number|initial|inherit;
Property Values
rotate()
rotates an element clockwise or counter-clockwise according to a given degree.
Using negative values will rotate the element counter-clockwise.
scale()
increases or decreases the size of an element (according to the parameters given
for the width and height).
skew()
skews an element along the X and Y-axis by the given angles.
matrix()
combines all the 2D transform methods into one.
CSS3 allows you to format your elements using 3D
transformations
3D transformation methods:
rotate3d(x,y,z)
rotateX(), rotateY(), rotateZ()
scale3d(x,y,z)
scaleX(), scaleY(), scaleZ()
translate3d(x,y,z)
translateX(), translateY(), translateZ()
Perspective(n)
Set the perspective from where an element is viewed
perspective: length|none;
CSS3 transitions
A shorthand property for setting the four transition properties into a single
transition
property
transition-property Specifies the name of the CSS property the transition effect is for
Property
By default, the width and
height of an element is
calculated like this:
width + padding + border =
print
Used for printers
screen
Used for computer screens, tablets, smart-phones etc.
speech
Used for screen readers that "reads" the page out loud
Incorrect (or no) DOCTYPE
Browserling
CrossBrowserTesting.com
Browsershots
Incorrect (or no) DOCTYPE
So to make sure your code works in all browsers you’ll need to add the
unprefixed version alongside all prefixed one’s
Using CSS Resets
What Is A CSS Reset?
is a short, often compressed (minified) set of CSS rules that resets the
styling of all HTML elements to a consistent baseline.
Every browser comes with a different set of internal, basic CSS styles
which apply if the current website does not override them
For Example
Margin- and padding-related differences that occur across different
browsers
HTML5Reset