Css Manual
Css Manual
CSS MANUAL
INTRODUCTION TO CSS
What is CSS
CSS stands for Cascading Style Sheets. CSS is created and maintained through a
group of people within the W3C called the CSS Working Group. The CSS
Working Group creates documents called specifications. When a specification has
been discussed and officially ratified by the W3C members, it becomes a
recommendation.
It is used to style or design a web page. CSS describes how HTML elements are
to be displayed on screen , paper, or in other media view. CSS handles the look
and feel part of a web page.
Using CSS, you can control the color of the text, the style of fonts, the spacing
between paragraphs, how columns are sized and laid out, what background images
or colors are used, layout designs,variations in display for different devices and
screen sizes as well as a variety of other effects.
Advantages of CSS
CSS saves time − You can write CSS once and then reuse same sheet in
multiple HTML pages. You can define a style for each HTML element and
apply it to as many Web pages as you want.
Pages load faster − If you are using CSS, you do not need to write HTML
tag attributes every time. Just write one CSS rule of a tag and apply it to all
the occurrences of that tag. So less code means faster download times.
Global web standards − Now HTML attributes are being deprecated and
it is being recommended to use CSS. So its a good idea to start using CSS
in all the HTML pages to make them compatible to future browsers.
Anatomy of CSS
CSS consists of "style rules". Each style rule consists of a "selector" and
"declarations" of property-value pairs:
Types of CSS
There are three types of CSS which are given below:
Inline CSS
Internal or Embedded CSS
External CSS
Inline CSS: Inline CSS contains the CSS property in the body section attached
with element is known as inline CSS. This kind of style is specified within an
HTML tag using the style attribute.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Inline CSS</title>
</head>
<body>
<h1 style="color:brown; font-style: italic;">An example with inline
CSS</h1>
<p>
<p style="font-weight:bolder; color:blue;">Lorem ipsum dolor, sit
amet consectetur adipisicing elit!</p>
</p>
</body>
</html>
Result
<!DOCTYPE html>
<html lang="en">
<head>
<title>Internal or Embedded CSS</title>
<style>
h1{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
color: darkgreen;
}
p{
font-size:larger;
font-style:italic;
}
</style>
</head>
<body>
<h1>An example with embedded CSS</h1>
<p>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit!</p>
</p>
</body>
</html>
External CSS : External CSS contains separate CSS file which contains only style
property with the help of tag attributes (For example class, id, heading, … etc).
CSS property written in a separate file with .css extension and should be linked to
the HTML document using link tag. This means that for each element, style can be
set only once and that will be applied across web pages.
href
This specifies the path to the CSS file (which is often placed in a folder called css
or
styles).
rel
This specifies the relationship between the HTML page and the file it is linked to.
The
value should be stylesheet when linking to a CSS file.
Example: create a new file and save it with file extension .css , for example ,
here we’ll be using home.css as our css file .
HTML
<!DOCTYPE html>
<p>Here we are using external CSS, and the name of the file is
home.css. Let's check it out <br> above the HEAD tag , we linked a file there
representing the CSS file
</p>
</body>
</html>
CSS- home.css
h1{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
color: darkgreen;
}
p{
font-size:larger;
font-style:italic;
color:darkgoldenrod;
}
Result
CSS Selectors
There are different ways of selecting HTML elements while using CSS styles to
style the page.
Universal Selector
The Universal Selector is a way of targeting every single element in a page, using
the star selector (*)
<ul>
<li>Web Design</li>
<li>Web Development</li>
<li>Web Online and Digital Marketing</li>
<li>Computer Graphics</li>
</ul>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
eius ducimus sint enim aut quos odio. Explicabo,
</p>
*{
margin:0;
list-style-type:none;
color:red;
}
<p>Here we are using external CSS , and the name of the file is home.css.
Let's check it out <br>
above the HEAD tag, we linked a file there representing the css file
</p>
</body>
</html>
Class Selector
The use of Class Selector is a way of grouping elements with the same class
attribute value. To use the class selector in CSS , (.) is used to classify a class in
CSS. So it would be the .classname .
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Class Selector </title>
<style>
/* using the class attribute to style with CSS */
.first{
color: brown;
font-style: italic;
}
.second{
color: chocolate;
font-family: 'Courier New', Courier, monospace;
}
</style>
</head>
<body>
<h1 class="first">This is first class heading</h1>
<h1 class="second">This is second class heading</h1>
<h1 class="first">This is first class heading</h1>
<h1 class="second">This is second class heading</h1>
</body>
</html>
Result
</style>
</head>
<body>
<h1 id="first">This is first id heading</h1>
<p id="second">Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Nobis nulla maiores voluptatem,<br> unde at
</body>
</html>
Result
Child Selector
The Child Selector is way of targeting an element that is the direct child of
another element using greater than sign (>).
<ul class="menu">
<li>Home</li>
<li>About</li>
<li>Blog</li>
<li>Services
<ul>
<li>Web Design</li>
<li>Web Development</li>
<li>Online Marketing</li>
<li>Advancec Excel</li>
</ul>
</li>
<li>Contact</li>
</ul>
Descendant Selector
Matches an element that is a descendent of another specified element (not just a
direct child of that element).
Example
<ul class="menu">
<li>Home</li>
<li>About</li>
<li>Blog</li>
<li>Services
<ul>
<li>Web Design</li>
<li>Web Development</li>
<li>Online Marketing</li>
<li>Advancec Excel</li>
</ul>
</li>
<li>Contact</li>
</ul>
Result
<h1>Heading 1</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Distinctio
soluta explicabo officia
corrupti rerum facilis cumque ea iure architecto libero incidunt nulla
similique tempore aperiam, odio
possimus labore esse, aspernatur.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Distinctio
soluta explicabo officia
corrupti rerum facilis cumque ea iure architecto libero incidunt nulla
similique tempore aperiam,
odio possimus labore esse, aspernatur.
h1 + p{
color:red;
font-style:italic;
}
h1 ~ p{
color:red;
font-style:italic;
width:500px;
}
a[target]{
color:green;
text-transform: uppercase;
font-weight: bold;
}
input[type="text"]{
width:350px;
height: 30px;
}
CSS Rules
If there are two or more rules that applies to the same element, it is important to
know which would take precedence.
Last Rule
If the two selectors are identical, the latter of the two will take precedence.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Last Rule </title>
<style>
p{
background-color: darkblue;
}
p{
background-color: darkmagenta;
}
</style>
</head>
<body>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam,
incidunt! Quos deserunt tempore nam perspiciatis exercitationem nulla saepe
Result
Specificity
Specificity Selector is used specify an element having two selectors but the one
selector is more specific than the others , the one with more specific rule will
take precedence over more general ones.
Example
Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Specificity Rule </title>
<style type="text/css">
#content span{
color: orange;
}
.emphasis{
color: red;
}
</style>
</head>
</div>
</body>
</html>
Result
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.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Important Rule </title>
<style>
#content span{
color: orange;
}
.emphasis{
color: red !important;
}
</style>
</div>
</body>
</html>
Result
Initial Rule
The initial keyword is used to set a CSS property to its default value . It can be
used for any CSS property and any HTML element .
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Initial Rule </title>
<style>
p{
font-size: 20px;
}
p{
font-size: initial;
}
</style>
Result
Pseudo Classes
Pseudo class selectors are CSS selectors with a colon preceding them. A pseudo-
class is similar to a class in HTML, but it’s not specified explicitly in the markup.
Examples :link , :hover , :active , :focus , :firstchild , :lastchild etc .
:link
This allows you to set styles for links that have not yet been visited.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pseudo Classes </title>
<style>
li a:link{
color: crimson;
</body>
</html>
Result
:hover
This is applied when a user hovers over an element with a pointing device such as
a mouse. This has commonly been used to change the appearance of links and
buttons when a user places their cursor over them.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pseudo Classes </title>
<style>
li a:link{
color: crimson;
text-decoration:underline;
font-weight: bolder;
}
li a:hover{
</body>
</html>
Result
:active
This is applied when an element link is being activated by a user; for example,
when a button is being pressed or a link being clicked.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pseudo Classes </title>
<style>
li a:link{
</body>
</html>
Result
:nth-child
This has the ability to do things like select even/odd elements.
Code
<!DOCTYPE html>
<html lang="en">
<head>
</body>
</html>
Result
:focus
This is applied when an element has focus. Any element that you can interact
with, such as a link you can click on or any form control can have focus.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pseudo Classes </title>
<style>
</body>
</html>
Result
Font Weight
The font-weight CSS property sets the weight (or boldness) of the font. The
weights available depend on the font-family that is currently set.
The font weight can be specified with the following;
the keyword value – “bold”, “light”, “bolder”, “lighter”
the numeric value – 100 – 900
the global values – “initial” , “unset”, “inherit”, “revert”, “revert-layer”
Font Style
p{
text-decoration: line-through
}
p{
text-decoration:underline;
}
</style>
</head>
<body>
<h2>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam,
incidunt! Quos deserunt tempore nam persp
</h2>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti
fugiat optio iusto corrupti nemo consectetur.
</p>
</body>
</html>
Text Align
The text-align property is used to set the horizontal alignment of a text. A text
can be left or right aligned, centered, or justified. The property can take one of
four values: left , center , right , justify.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Text Align </title>
<style>
h2{
text-align: right;
}
p{
text-align: center;
}
</style>
</head>
<body>
<h2>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam,
incidunt! Quos deserunt tempore nam persp
</h2>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam,
incidunt! Quos deserunt tempore nam persp
</p>
</body>
Result
Text Transform
The text-transform property is used to specify uppercase and lowercase letters in
a text.
It can be used to turn everything into uppercase or lowercase letters, or
capitalize the first letter of each word.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Text Transform </title>
<style>
h2{
text-transform: uppercase;
}
p{
text-transform:lowercase;
}
</style>
</head>
<body>
<h2>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam,
incidunt! Quos deserunt tempore nam persp
</h2>
</body>
</html>
Result
Text Indent
The text-indent property allows you to indent the first line of text within an
element.
The value can be in em, px, %.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Text Indent </title>
<style>
h2{
text-indent: 10em;
}
p{
text-indent: 130px;
}
</style>
</head>
<body>
<h2>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam,
incidunt! Quos deserunt tempore nam persp
</h2>
<p>
</body>
</html>
Result
Result
Result
html
Table Styling
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pseudo Classes </title>
<style>
body{
background: black;
font-family: arial, verdana, sans-serif;
}
#container{
background: white;
width: 850px;
margin: auto;
padding-top: 20px;
}
Result
Font-family
font-family: family-name|generic-family|initial|inherit;
Example
font-family:'Courier New', Courier, monospace;
font-family: Verdana, Geneva, Tahoma, sans-serif;
Start with the font you want, and always end with a generic family, to let the
browser pick a similar font in the generic family, if no other fonts are available.
Note: Separate each value with a comma.
Note: If a font name contains white-space, it must be quoted. Single quotes must
be used when using the "style" attribute in HTML.
Result
Web Fonts
@font-face {
font-family: 'amaranthBold';
src: url('fonts/amaranth-regular-webfont.eot');
src: url('fonts/amaranth-regular-webfont.eot?#iefix')
format('embedded-opentype'),
url('fonts/amaranth-regular-webfont.woff2') format('woff2'),
url('fonts/amaranth-regular-webfont.woff') format('woff'),
url('fonts/amaranth-regular-webfont.ttf')
format('truetype'),
url('fonts/amaranth-regular-webfont.svg#amaranthregular')
format('svg');
font-weight: normal;
font-style: normal;
@font-face {
font-family:'amaranthregular';
src: url('fonts/amaranth-bold-webfont.eot');
src: url('fonts/amaranth-bold-webfont.eot?#iefix')
format('embedded-opentype'),
url('fonts/amaranth-bold-webfont.woff2') format('woff2'),
url('fonts/amaranth-bold-webfont.woff') format('woff'),
url('fonts/amaranth-bold-webfont.ttf') format('truetype'),
url('fonts/amaranth-bold-webfont.svg#amaranthregular')
format('svg');
font-weight: normal;
font-style: normal;
h1{
font-family: 'amaranthBold', sans-serif;
}
h2{
font-family: 'amaranthregular', sans-serif ;
}
</style>
</head>
<body>
<h1>Heading Fonts 1</h1>
<h2>Heading Fonts 2</h2>
</body>
</html>
Result
Result
WhatFont
WhatFont makes it so easy to inspect any font on a website, with just one click.
The extension is available for both Chrome and Safari, as well as a bookmarklet.
Once installed, all you need to do is click the extension’s icon in your browser’s
toolbar, and then click the font you wish to view. Simple. Awesome Screenshot:
For screenshoting stuff
ColorZilla
ColorZilla is a browser extension that allows you to find out the exact color of
any object within your web browser. This is especially handy if you want to match
elements your page to an image's color.
Awesome Screenshot
Awesome ScreenShot makes screen capture and annotation effortless! Click the
camera icon, and Awesome Screenshot takes a snap of the whole page on your
screen or any portion. You can also upload an image from your computer, drag
and drop, or paste from your clipboard to your account.
Color
Color The color property allows you to specify the color of text inside an
element . You can specify any color in CSS in one of three ways: RGB, Color
Names, Hex Codes
Color names
There are 147 predefined color names that are recognized by browsers.
For example: DarkCyan
.article {
color: darkcyan;
}
HEX CODES
These are six-digit codes that represent the amount of red, green and blue in a
color, preceded by a pound or hash # sign.
For example: #ee3e80
h1{
color: #ee3e80;
}
RGB VALUES
These express colors in terms of how much red, green and blue are used to
make it up. For example: rgb(100,50,100)
html:
<h1>Heading 1</h1>
<div class="article">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et
aliquid quisquam magnam, eveniet, reiciendis quas fugit eos
repellat vitae libero recusandae iure iste ab, quia illo modi
quo
quasi molestiae?
</div>
CSS:
Result
Opacity (rgba)
The CSS3 rgba property allows you to specify a color, just like you would with an
RGB value, but adds a fourth value to indicate opacity. This value is known as an
alpha value and is a number between 0.0 and 1.0 (so a value of 0.5 is 50% opacity
and 0.15 is 15% opacity). The rgba value will only affect the element on which it is
applied (not child elements).
html:
<div class="post">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Fugiat numquam atque, corrupti animi velit facilis consequatur,
eaque ad natus, modi corporis quasi repudiandae sapiente
pariatur, amet dolorem asperiores itaque adipisci.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Fugiat numquam atque, corrupti animi velit facilis consequatur,
eaque ad natus, modi corporis quasi repudiandae sapiente
pariatur, amet dolorem asperiores itaque adipisci.
</p>
</div>
<div class="article">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Fugiat numquam atque, corrupti animi velit facilis consequatur,
eaque ad natus, modi corporis quasi repudiandae sapiente
CSS
body{
background-image:url("images/back.jpg");
background-repeat: no-repeat;
background-size:cover;
}
.post{
width:600px;
margin:auto;
padding:10px;
background-color:rgb(4, 69, 107);
color:#fff;
margin-bottom: 20px;
}
.article{
width:600px;
margin:auto;
padding:10px;
background-color:rgba(4, 69, 107, 0.6);/*this is opacity
(rgba)*/
color:#fff;
}
Result
Backgrounds
The background property in CSS allows you to control the background of any
element (what paints underneath the content in that element). Background plays
an important role in the visual presentation of a web page.
CSS provide several properties for styling the background of an element, including
coloring the background, placing images in the background and managing their
positioning, etc. The background properties are background-color, background-
image, background-repeat, background-attachment and background-position.
Background-Color
The background-color property is used to set the background color of an
element.
The following example demonstrates how to set the background color of the
whole page.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Color</title>
<style>
p{
background-color:chocolate;
}
</style>
</head>
<body>
<p>
Lorem ipsum dolor, sit amet consectetur adipisicing elit.
Accusamus voluptatem est quasi? Possimus, quaerat veniam tenetur quibusdam
dolorum pariatur <br> fugit perspiciatis, assumenda dolore, necessitatibus
nihil. Hic exercitationem corrupti voluptatem voluptates. <br>
Lorem ipsum, dolor sit amet consectetur adipisicing elit.
Accusamus fugiat repellendus facere dolor harum nostrum iste, necessitatibus
neque nisi voluptates <br> modi quaerat voluptas placeat? Eius molestiae nemo
ratione quo distinctio!
</p>
Result
Background-Image
The background-image property set an image as a background of an HTML
element.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Image</title>
<style>
body{
background-image: url(images/tree.png);
font-size: 5rem;
}
</style>
</head>
<body>
<p>
A body with a background Image
</p>
</body>
</html>
Result
body{
background-image: url('images/back.jpg');
background-repeat: no-repeat;
background-size: cover;
</style>
</head>
<body>
<h2> Backgroung Size cover</h2>
</body>
</html>
Background-contain
Background size contain ensures that image fits inside a bus even though the
image is bigger than the box
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Background Size Contain</title>
<style type="text/css">
body{
background: #000;
}
#main{
width: 600px;
height: 700px;
background-image: url('images/back.jpg');
background-repeat: no-repeat;
background-size: contain;
background-position: center;
background-color: #fff;
margin: auto;
}
Result
Background Repeat
The background-repeat property sets if/how a background image will be repeated.
By default, a background-image is repeated both vertically and horizontally.
A background image can be repeated along the horizontal and vertical axes, or
not repeated at all.
Repeat
The background image is repeated both horizontally and vertically (the default
way it is shown if the background-repeat property isn’t used).
Example
<!DOCTYPE html>
<html>
<head>
Result
Repeat-x
The image is repeated horizontally only.
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Background Repeat X</title>
Result
Repeat-y
The image is repeated vertically only.
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Background Repeat Y</title>
<style type="text/css">
body{
background-image: url('images/tree.png');
}
</style>
</head>
<body>
<p>
A background image with repeats-y background
<p>
</body>
</html>
Result
No-repeat
The image is only shown once.
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Background No-Repeat</title>
Result
</style>
</head>
Result
</style>
</head>
<body>
<div id="main">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.
</p>
Result
Check your browser for the result
Background Position
The background-position property sets the starting position of a background
image.
By default, a background-image is placed at the top-left corner of an element, and
repeated both vertically and horizontally. The first represents the horizontal
position and the second represents the vertical.
left top, left center, left bottom, center top, center center , center
bottom, right top, right center, right bottom, 50% 50%, 20px 30px.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Position </title>
<style>
body{
background-image: url(images/tree.png);
background-repeat: no-repeat;
color:darkred;
background-position: center top;
</style>
</head>
<body>
<p>
welcome to the attachment view of a background , here we are using
the fixed attachment first <br>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officiis
temporibus reiciendis assumenda, id iure, laboriosam architecto dolore
quibusdam nam exercitationem accusantium perspiciatis vel dolores odio!
Voluptatibus sit expedita maiores dicta.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officiis
temporibus reiciendis assumenda, id iure, laboriosam architecto dolore
quibusdam nam exercitationem accusantium perspiciatis vel dolores odio!
Voluptatibus sit expedita maiores dicta.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officiis
temporibus reiciendis assumenda, id iure, laboriosam architecto dolore
quibusdam nam exercitationem accusantium perspiciatis vel dolores odio!
Voluptatibus sit expedita maiores dicta.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officiis
temporibus reiciendis assumenda, id iure, laboriosam architecto dolore
quibusdam nam exercitationem accusantium perspiciatis vel dolores odio!
Voluptatibus sit expedita maiores dicta.
</p>
</body>
</html>
Result
</style>
</head>
<body>
<p>
welcome to the attachment view of a background , here we are using
the fixed attachment first <br>
Write this
<!DOCTYPE html>
<html lang="en">
<head>
<title>Background Shorthand </title>
<style>
body{
color:darkred;
font-size: 70px;
background:url(images/tree.png) no-repeat fixed center top ;
}
</style>
</head>
<body>
<p>
welcome to the attachment view of a background , here we are using the
fixed attachment first <br>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officiis
temporibus reiciendis assumenda, id iure, laboriosam architecto dolore quibusdam nam
exercitationem accusantium perspiciatis vel dolores odio! Voluptatibus sit expedita
maiores dicta.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officiis
temporibus reiciendis assumenda, id iure, laboriosam architecto dolore quibusdam nam
exercitationem accusantium perspiciatis vel dolores odio! Voluptatibus sit expedita
maiores dicta.
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officiis
temporibus reiciendis assumenda, id iure, laboriosam architecto dolore quibusdam nam
</p>
</body>
</html>
Result
Display
The display CSS property sets whether an element is treated as a block or inline
element and the layout used for its children, such as flow layout, grid or flex. In
HTML, the default display property value is taken from the HTML specifications
or from the browser/user default style sheet.
We have different display values ;
1. Block
2. Inline
3. Inline-block
4. Flex
5. Grid
6. And many more .
</body>
</html>
Result
Inline element
An inline element does not start on a new line and only takes up as much width as
necessary.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Inline </title>
<style>
a{
background-color: blueviolet;
font-style: italic;
}
span{
text-transform: uppercase;
background-color: darkmagenta;
}
</style>
</body>
</html>
Result
Inline
This property is used to display an element as an inline element (like <span>). The
height and width properties are not effected on display:inline; property. It allows
only left and right side of margins, not top and bottom.
Block
This property means that the element is displayed as a block, as paragraphs and
headers have always been. A block has some whitespace above and below it and
tolerates no HTML elements next to it, except when ordered otherwise (by
adding a float declaration to another element, for instance).
Inline – block
This property is used to display an element as an inline-level block container. The
element itself is formatted as an inline element, but it can apply height and width
values. It is placed as an inline element (on the same line as adjacent content). It
looks like an inline element but it behaves as a block element and don’t force to
line break.
Position
The position property specifies the type of positioning method used for an
element (static, relative, fixed, absolute or sticky).
Position-relative
An element with position: relative; is positioned relative to its normal position.
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.
Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Position relative </title>
<style>
body{
background-color: deeppink;
}
.container{
background-color:#fff;
width:450px;
margin:auto;
}
#box1, #box2, #box3{
background-color:darkmagenta;
width:120px;
height:120px;
color:#fff;
border:1px solid #000;
text-transform: uppercase;
font-size:19px;
text-align: center;
line-height:120px
}
#box2{
position: relative;
top:30px;
left:70px;
</style>
</head>
<body>
<div class="container">
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
<div id="box3">Box 3</div>
</div>
<br>
</body>
</html>
Result
Position- absolute
If is positioned with absolute, that element is positioned in relation to the
browser window and not the parent element. Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Position Absolute </title>
<style>
body{
background-color: deeppink;
}
.container{
background-color:#fff;
background-color:darkmagenta;
width:120px;
height:120px;
color:#fff;
border:1px solid #000;
text-transform: uppercase;
font-size:19px;
text-align: center;
line-height:120px
}
#box1{
position: absolute;
top:30px;
left:70px;
}
</style>
</head>
<body>
<div class="container">
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
<div id="box3">Box 3</div>
</div>
</body>
</html>
</body>
</html>
Result
<!DOCTYPE html>
<html lang="en">
<head>
<title>Position relative </title>
<style>
body{
background-color: deeppink;
}
.container{
background-color:#fff;
width:450px;
margin:auto;
position: relative;
}
#box1, #box2, #box3{
background-color:darkmagenta;
width:120px;
height:120px;
color:#fff;
border:1px solid #000;
text-transform: uppercase;
font-size:19px;
text-align: center;
line-height:120px
}
#box2{
position: absolute;
top:50px;
right:70px;
}
</style>
</head>
<body>
<div class="container">
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
<div id="box3">Box 3</div>
</body>
</html>
Result
Fixed
If you want an element to remain fixed while a user scrolls down a long page,
simply the give the element a position of fixed.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Position Fixed </title>
<style>
body{
background-color: deeppink;
}
.container{
background-color:#fff;
width:450px;
margin:auto;
position: relative;
}
#box1, #box2, #box3{
background-color:darkmagenta;
width:120px;
height:120px;
color:#fff;
</style>
</head>
<body>
<div class="container">
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
<div id="box3">Box 3</div>
</div>
<br>
</body>
</html>
Result
Z-index
The z-index property specifies the stack order of an element. An element with
greater stack order is always in front of an element with a lower stack order.
Note: z-index only works on positioned elements (position:absolute,
position:relative, or position:fixed).
Code
<!DOCTYPE html>
background-color:darkmagenta;
width:120px;
height:120px;
color:#fff;
border:1px solid #000;
text-transform: uppercase;
font-size:19px;
text-align: center;
line-height:120px
}
#box1{
position:absolute;
z-index:2;
top:20px;
right:40px;
}
#box2{
position:absolute;
z-index:1;
top:100px;
right:50px;
}
</style>
</head>
<body>
<div class="container">
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
</html>
Result
FLOAT
The CSS float property specifies how an element should float. The float CSS
property places an element on the left or right side of its container, allowing text
and inline elements to wrap around it.
Before float
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Float</title>
<style type="text/css">
body{
background: #000;
margin: 0;
color: #fff;
}
#wrapper{
background: yellow;
#innerRapper{
overflow: hidden;
}
#mainContent{
background: red;
width: 650px;
/*float: left;*/
}
#aside{
background: green;
width: 400px;
/*float: right;*/
}
#footer{
background: blue;
height: 100px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="innerRapper">
<div id="mainContent">
<h2>Main Content</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
Result
After Float
It is obvious that the white containing element is missing in the below picture, this
can be solved by giving the containing element an overflow:hidden or
overflow:auto declaration.
Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
#wrapper{
background: yellow;
width: 1200px;
margin: auto;
}
#innerRapper{
overflow: hidden;
}
#mainContent{
background: red;
width: 650px;
/*float: left;*/
}
#mainContent, #aside{
float: left;
}
#aside{
background: green;
width: 400px;
/*float: right;*/
}
#footer{
background: blue;
height: 100px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="innerRapper">
<div id="mainContent">
<h2>Main Content</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam,
Result
Result
.div2 {
width: 300px;
height: 100px;
padding: 50px;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="div1">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
</p>
</div>
<br>
<div class="div2">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi
maxime dolores itaque,
Result
<!DOCTYPE html>
<html lang="en">
<head>
<title>Box Sizing</title>
<style>
.div1 {
width: 300px;
height: 100px;
border: 1px solid blue;
box-sizing: border-box;
}
.div2 {
width: 500px;
</style>
</head>
<body>
<div class="div1">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
</p>
</div>
<br>
<div class="div2">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi
maxime dolores itaque,
</p>
</div>
</body>
</html>
Result
Box shadow
The box-shadow property attaches one or more shadows to an element.
Example
<!DOCTYPE html>
<html lang="en">
Result
Box model
In CSS, the term "box model" is used when talking about design and layout.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Padding</title>
<style>
Result
After padding
Result
Padding Shorthand
Padding: 8px 20px
8px will affect the top and bottom padding and 20px will affect the left and right .
Padding padding: 15px
You use this shorthand when you want the sides whole of the box (padding-top,
padding-right, padding-bottom, padding-left) to have the same value for padding.
Padding: 8px 4px 10px 12px
Use this shorthand when you want different sides of a box to have different
values for padding. 8px 4px 10px 12px will affect the Top, Right, Bottom and Left
padding in that order.
To shorten the code, it is possible to specify all the padding properties in one
property.
The following individual padding properties:
padding-top
padding-right
padding-bottom
</body>
</html>
Result
Margin
Margins are used to create space around elements, outside of any defined
borders.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Margin</title>
<style>
.box {
border:6px dotted brown;
width: 500px;
}
</style>
</head>
<body>
<div class="box">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Perferendis placeat eaque
reiciendis exercitationem temporibus esse atque repellat ipsum quidem accusamus!
Corporis sed dicta commodi obcaecati, quisquam velit tenetur repudiandae amet?
</p>
</div>
</body>
</html>
Result
After Margin
Code
<!DOCTYPE html>
Result
Margin shorthand
Margin: 8px 20px
8px will affect the top and bottom margin and 20px will affect the left and right
margin
Margin: 15px
</body>
</html>
Borders
The CSS border properties allow you to specify the style, width, and color of an
element's border.
Example : here we are going to be working with the following properties of
borders
1. Border style
2. Border width
3. Border color
<!DOCTYPE html>
<html lang="en">
<head>
<title>border</title>
<style>
body{
background-color:grey;
}
#container{
background-color: #fff;
width:300px;
margin:auto;
}
section{
padding: 7px;
width: 200px;
border-width: 6px;
Result
Border shorthand
There are many properties to consider when dealing with borders. To shorten
the code, it is also possible to specify all the individual border properties in one
property.
The border property is a shorthand property for the following individual border
properties:
border-width
border-style (required)
.box1{
width:200px;
border-top:9px purple groove;
border-right:10px brown dotted;
border-left:9px palevioletred dashed;
border-bottom:9px purple groove;
}
</style>
</head>
<body>
<div class="box1">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Officiis
ullam dolor,
numquam sit exercitationem suscipit explicabo sequi quasi placeat
eligendi
vitae eveniet harum quibusdam aspernatur, hic minima? Cumque, molestiae
iste.
</div>
</body>
</html>
Result
<!DOCTYPE html>
<html lang="en">
<head>
<title>border</title>
<style>
div{
border: 10px dotted blue;
}
</p>
</div>
</body>
</html>
Result
Flex Box
The Flexible Box Layout Module, makes it easier to design flexible responsive
layout structure without using float or positioning.
Flex Box Element
To start using the Flexbox model, you need to first define a flex container.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flex Box</title>
<style>
.flex-container{
display: flex;
background-color: chartreuse;
}
.flex-container > div{
font-size: xx-large;
background-color: white;
padding:20px ;
margin: 10px;
}
</style>
</head>
<body>
<div class="flex-container">
<div>box 1</div>
<div>box 2</div>
<div>box 3</div>
</div>
</body>
</html>
Result
Justify content
This defines the alignment along the main axis. It helps distribute extra free space
leftover when either all the flex items on a line are inflexible, or are flexible but
have reached their maximum size. It also exerts some control over the alignment
of items when they overflow the line. It has values like ; center, flex-start, flex-
end, space-around, space space-between, space-evenly.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Justify Content </title>
<style>
.flex-container{
justify-content: center;
background-color: chartreuse;
display: flex;
}
.flex-container > div{
background-color: aliceblue;
margin: 10px;
padding: 20px;
font-size:x-large;
}
</style>
</head>
<body>
<div class="flex-container">
<div>box 1</div>
<div>box 2</div>
<div>box 3</div>
</body>
</html>
Result
Flex Direction
The flex-direction property defines in which direction the container wants to
stack the flex items. It has values like ; row , column , row-reverse, column-
reverse.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flex Direction </title>
<style>
.flex-container{
justify-content: center;
background-color: chartreuse;
display: flex;
flex-direction: row-reverse;
}
.flex-container > div{
background-color: aliceblue;
margin: 10px;
padding: 20px;
font-size:x-large;
}
</style>
</head>
<body>
<div class="flex-container">
<div>box 1</div>
Result
Flex Wrap
The flex-wrap property specifies whether the flex items should wrap or not.
By default, flex items will all try to fit onto one line. You can change that and
allow the items to wrap as needed with this property. It has values like ; wrap,
nowrap, wrap-reverse.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flex Wrap </title>
<style>
.flex-container{
background-color: chartreuse;
display: flex;
flex-wrap: wrap;
}
.flex-container > div{
background-color: aliceblue;
margin: 10px;
padding: 20px;
font-size:x-large;
}
</style>
</head>
<body>
<div class="flex-container">
<div>box 1</div>
</body>
</html>
Result
Flex Flow
The flex-flow property is a shorthand property for setting both the flex-direction
and flex-wrap properties.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Position Z-index </title>
<style>
</body>
</html>
Result
Align content
The align-content property is used to align the flex lines. It has values like ;
center, flex-start, flex-end, stretch , space-around, space-between.
</body>
</html>
Result
Order
The order property specifies the order of the flex items. The order value must be
a number, default value is 0.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flex Item Order </title>
<style>
.flex-container{
background-color: blue;
display: flex;
justify-content: center;
}
.flex-container > div{
background-color: aliceblue;
margin: 10px;
padding: 30px;
font-size:xx-large;
}
</style>
</head>
<body>
<div class="flex-container">
<div style="order:2 ;">3</div>
<div style="order:5;">1</div>
<div style="order:1;">4</div>
<div style="order:4;">2</div>
<div style="order:3;">5</div>
</div>
Result
Flex-grow
The flex-grow property specifies how much a flex item will grow relative to the
rest of the flex items.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flex Grow</title>
<style>
.flex-container{
background-color:darkgray;
display: flex;
justify-content: center;
}
.flex-container > div{
background-color:chocolate;
margin: 10px;
padding: 30px;
font-size:xx-large;
}
</style>
</head>
<body>
<div class="flex-container">
<div style="flex-grow: 2;">flex grow 2</div>
<div style="flex-grow: 5;"> flex grow 5</div>
<div style="flex-grow: 1;"> flex grow 1</div>
<div style="flex-grow: 4;">flex grow 4</div>
</div>
Result
Flex-shrink
The flex-shrink property specifies how much a flex item will shrink relative to the
rest of the flex items.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flex Shrink </title>
<style>
.flex-container{
background-color:darkgray;
display: flex;
align-items: stretch;
}
.flex-container > div{
background-color:chocolate;
width: 500px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
<div class="flex-container">
<div>1</div>
<div>2</div>
Result
Flex-basis
The flex-basis property specifies the initial length of a flex item.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flex Shrink </title>
<style>
.flex-container{
background-color:darkgray;
display: flex;
align-items: stretch;
}
.flex-container > div{
background-color:chocolate;
width: 500px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</style>
</head>
<body>
</div>
</body>
</html>
Result
Flex
The flex property is a shorthand property for the flex-grow, flex-shrink, and flex-
basis properties.
Example: in order of flex-grow, flex-shrink and flex-basis.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Flex Shrink </title>
<style>
.flex-container{
background-color:darkgray;
display: flex;
align-items: stretch;
}
.flex-container > div{
background-color:chocolate;
width: 100px;
margin: 10px;
text-align: center;
line-height: 75px;
font-size: 30px;
}
</div>
</body>
</html>
Result
Align-Self
The align-self property specifies the alignment for the selected item inside the
flexible container.
The align-self property overrides the default alignment set by the container's align-
items property.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Align self </title>
<style>
.flex-container{
background-color:darkgray;
display: flex;
height: 200px;
}
</style>
</head>
<body>
<h1>The align-self Property</h1>
<p>The "align-self: center;" aligns the selected flex item in the middle
of the container:</p>
<div class="flex-container">
<div>1</div>
<div>2</div>
<div style="align-self: center">3</div>
<div>4</div>
</div>
Result
It is called responsive web design when you use CSS and HTML to resize, hide,
shrink, enlarge, or move the content to make it look good on any screen.
Before tablets and mobile phones, web pages were designed only for computer
screens, and it was common for web pages to have a static design and a fixed size.
Then, when we started surfing the internet using tablets and mobile phones, fixed
size web pages were too large to fit the viewport. To fix this, browsers on those
devices scaled down the entire web page to fit the screen.
HTML5 introduced a method to let web designers take control over the
viewport, through the <meta> tag.
You should include the following <meta> viewport element in all your web pages:
This gives the browser instructions on how to control the page's dimensions and
scaling.
The width=device-width part sets the width of the page to follow the screen-width
of the device (which will vary depending on the device).
The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by
the browser.
Here is an example of a web page without the viewport meta tag, and the same
web page with the viewport meta tag:
Using a grid-view is very helpful when designing web pages. It makes it easier to
place elements on the page.
First ensure that all HTML elements have the box-sizing property set to border-
box. This makes sure that the padding and border are included in the total width
and height of the elements.
* {
box-sizing: border-box;
}
The following example shows a simple responsive web page, with two columns:
The example above is fine if the web page only contains two columns.
First we must calculate the percentage for one column: 100% / 12 columns =
8.33%.
Then we make one class for each of the 12 columns, class="col-" and a number
defining how many columns the section should span:
<div class="row">
<div class="col-3">...</div> <!-- 25% -->
<div class="col-9">...</div> <!-- 75% -->
</div>
It uses the @media rule to include a block of CSS properties only if a certain
condition is true.
If the browser window is 600px or smaller, the background color will be lightblue :
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
Add a Breakpoint
We use media query to make our website responsive by adding a breakpoint. We
can add a breakpoint where certain parts of the design will behave differently on
each side of the breakpoint.
Another Breakpoint
Note that the two sets of classes are almost identical, the only difference is the
name (col- and col-s-):
It might seem odd that we have two sets of identical classes, but it gives us the
opportunity in HTML, to decide what will happen with the columns at each
breakpoint:
HTML Example
For desktop:
The first and the third section will both span 3 columns each. The middle section
will span 6 columns.
For tablets:
The first section will span 3 columns, the second will span 9, and the third section
will be displayed below the first two sections, and it will span 12 columns:
<div class="row">
<div class="col-3 col-s-3">...</div>
<div class="col-6 col-s-9">...</div>
<div class="col-3 col-s-12">...</div>
</div>
/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {...}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {...}
Media queries can also be used to change layout of a page depending on the
orientation of the browser.
You can have a set of CSS properties that will only apply when the browser
window is wider than its height, a so called "Landscape" orientation:
The web page will have a lightblue background if the orientation is in landscape
mode:
If the width property is set to a percentage and the height property is set to
"auto", the image will be responsive and scale up and down:
img {
width: 100%;
height: auto;
}
If the max-width property is set to 100%, the image will scale down if it has to,
but never scale up to be larger than its original size:
Background Images
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-size: cover;
border: 1px solid red;
}
A large image can be perfect on a big computer screen, but useless on a small
device. Why load a large image when you have to scale it down anyway? To
Here is one large image and one smaller image that will be displayed on different
devices:
body {
background-image: url('img_smallflower.jpg');
}
video {
width: 100%;
height: auto;
}
Notice that in the example above, the video player can be scaled up to be larger
than its original size. A better solution, in many cases, will be to use the max-
width property instead.
If the max-width property is set to 100%, the video player will scale down if it has
to, but never scale up to be larger than its original size:
video {
max-width: 100%;
height: auto;
}
We want to add a video in our example web page. The video will be resized to
always take up all the available space:
video {
width: 100%;
height: auto;
}