CSS Cascading Style Sheet
CSS Cascading Style Sheet
CSS or Cascading style sheet is the style sheet language of web, used to style or formatting of an HTML
document.
Before we had CSS, all of the styling of HTML was embedded directly in the html document- like, attributes
width or bgcolor (background color) or in the form of presentational tags like font, center, strike etc.
Using separate style sheet for an entire site, a developer can apply styles across a whole site while updating a
single css file.
Css was Developed by Hakon Wium Lie of MIT in 1995. In 1996, level 1 of CSS was published, level 2
comes in 1998 and level 3 ( CSS3) in 2009 with HTML5.
With HTML5, CSS has become the W3C standard for controlling visual presentation of web pages. W3C
is celebrating 20th anniversary of css in 2016 as level 1 of css was published 20 years ago on 17 Dec 1996.
CSS Syntax
Type of CSS
Type Explanation
Internal CSS CSS Code is written inside <style> tag in head. Recommended for single page only.
CSS code is written in separate file with .css extension and <link> element is used to
External CSS
attach external css with html document. Used for multipage websites.
Inline CSS CSS code is written inside html tag directly, in style attribute.
Internal CSS
Use internal stylesheet when you are working on single page website. An internal stylesheet is defined using the
<style> tag and goes in the head section of an HTML document.
The <style> tag specifies the content type of a stylesheet with its type attribute which should be set to "text/css". But
in HTML5, type is optional.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Internal CSS</title>
<meta charset="UTF-8">
<style >
p{color: green}
h1{ text-align:center; color:red}
</style>
</head>
<body>
<h1>Heading 1</h1>
<p>The text in this paragraph will be green.</p>
<p>This paragraph is also green.</p>
</body>
</html>
External CSS
Use an external stylesheet when you want to apply one style to many pages. If you make one change in an
external stylesheet, the change is universal on all the pages where the stylesheet is used.
An external stylesheet is declared in an external file with a .css extension. It is called by pages whose
interface it will affect. External stylesheets are called using the <link> tag which should be placed in the
head section of an HTML document. This tag includes three attributes.
• rel - When using an external stylesheet on a webpage, this attribute takes the value "stylesheet"
• type - When using an external stylesheet on a webpage, this attribute takes the value "text/css".
• href - Denotes the name and location of the external stylesheet to be used.
<!DOCTYPE html>
<html>
<head>
<title>External CSS</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css" >
</head>
<body>
<p>
The text in this paragraph will be blue.
</p>
</body>
</html>
Inline CSS
Use inline css when you want to apply a style to a single occurrence of an element.
Inline stylesheets are declared within individual tags and affect those tags only. Inline stylesheets are declared with
the style attribute and value inside.
CSS Media Atttribute specifies the device media where css will run. Default value of media attribute is all.
Others values for media attribute are screen and print. Here are some examples for media attribute
<style media="screen">
body{
font-family:sans-serif;
}
</style>
<style media="print">
body{
font-family:monospace;
}
</style>
or
<link href="css/style.css" rel="stylesheet" media="screen">
<link href="css/style-print.css" rel="stylesheet" media="print">
OUTPUT:
ID Selectors
ID Selector is used to call an HTML Element by its unique id name. Id is always unique in a single web
page. We can not give same ID to two different HTML Elements in a webpage.
Id is basically an attribute used in opening or start tag. Inside double quotation, the value of ID is given. ID
value is single, means no white space separation
In css code, Id is always starts with HASH ( #), than id value.
CSS Id Selector
<style>
#head1{ color:green}
#para{ color:red; background:yellow}
p{ color:blue}
</style>
OUTPUT:
Class Selectors
Class Selector in css is used to call all html Elements with same class group.
Class represents a group of different or same elements. We can give same Class name to two or more
different HTML Elements.
Class is basically an Attribute used in Opening or Start Tag. Inside double Quotation, the value of Class is
given.
In css code, class always starts with DOT ( .), than class value.
OUTPUT:
Grouping Selector
Grouping is used to call a group of HTML Elements by tagname, classname or id.
Unlike Class selector, we don't need to create attribute first.
We can group multiple tags, IDs and Classes. We have to use COMMA (,) to separate Selectors in grouping.
Nesting in CSS
Nesting is used to call a particular child of parent Element. If we are calling a P tag, all Para Tags will be
selected. We can Nest a particular tag of parent using nesting. We use single space bar to relate child of that
particular element.
OUTPUT:
In Example above, all paragraphs are assigned red color, but para inside header class is assigned color white and
background gray.
Universal Selector *
Universal Selector or * is used to call all html elements in css.
OUTPUT:
In CSS, font color is given by using css "color" Property, background color is given using "background-color" property
and border color is given using "border-color" property. Basically there are three major colors, Red, Green, and Blue.
Rest all colors are a combination of these three colors. RGB Colors are basically Screen Colors. However for printing
purpose, we use CMYK
1. Color Name
2. Hexadecimal Color code
3. RGB Colors
0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f.
#RRGGBB;
There are total six digits. First two represent red, next two represents green and last two represents blue
color.
Text-align Value
left align text to the left
center align text to the center
right align text to the right
justify Justified text
<style>
#text1{text-align:left;}
#text2{text-align:center;}
#text3{text-align:right;}
</style>
<style>
#text4{text-align:justify;}
</style>
<style>
#text4{text-indent:100px; color:red}
#text5{text-indent:200px; color:blue}
#text6{text-indent:-50px; color:green}
</style>
Text Decoration
CSS text Decoration add or remove decoration of text, like underline, overline, line-through and none.
<style>
#text5{text-decoration:overline;}
#text6{text-decoration:underline;}
#text7{text-decoration:line-through;}
#text8{text-decoration:none;}
</style>
<h4 id="text5">TECH ALTUM</h4>
<h4 id="text6">TECH ALTUM</h4>
<h4 id="text7">TECH ALTUM</h4>
<h4 id="text8">TECH ALTUM</h4>
Text Transform
CSS Text Transform is used to transform text from lowercase to uppercase, lowercase to capitalize and
uppercase to lowercase.
<style>
#text9{text-transform:capitalize;}
#text10{text-transform:lowercase;}
#text11{text-transform:uppercase;}
#text12{text-transform:none;}
</style>
Word Spacing
<style>
#text1{ word-spacing:25px;}
#text2{ word-spacing:50px;}
</style>
Letter Spacing
CSS can change Font Properties of HTML elements, like font size, line height, font family, font weight, font style,
font stretch, font variant, Font Family etc. Using these properties, we can change look of any html element. In this
article, i will explain all CSS Font Properties one by one.
Font Size
In CSS, Font size property can change the font size of a font. Some famous values for font size are xx-small, x-small,
small, smaller, medium, large, larger, x-large, xx-large. We can give value of font-size using 5 different units.
• Pixels (px)
• Em(em)
• Points (pt)
• percentage (%)
• rem (rem)
"em" and "%" are relative units, whereas px, pt and rem are fixed
Line-Height
Css Line-height property defines the actual height of a line. It can be used in px or n
Line-height in px is fixed, whereas n is relative. 1 means 100% of font-size, 2 means 200% of font-size.
<div>
<p style="line-height:normal">Line Height normal </p>
<p style="line-height:1">Line Height 1 </p>
<p style="line-height:2">Line Height 2 </p>
<p style="line-height:16px">Line Height 16px </p>
<p style="line-height:20px">Line Height 20px </p>
<p style="line-height:24px">Line Height 24px </p>
<p style="line-height:30px">Line Height 30px </p>
</div>
Always prefer line height in numbers as numbers are relative. Line height is pixel is
fixed.
Font Weight
In CSS, Font weight is used to give Bold or Bolder appearance to font. Various value of font-weight are
• Normal
• Bold
• Bolder → same like bold
• 100 → same like normal
• 200
• 300
• 400
• 500
• 600 → same as bold
• 700
• 800 → slightly bolder than bold
• 900 → maximum allowed value
<p style="font-weight:bold"> Font weight bold </p>
Font Style
In CSS, Font style is used to gives italics appearance. Various value of font-style are
• Italic
• Normal
• Oblique
Font Variant
CSS Font varient is used to set variant of font we are using. Font variant small caps set all text in smaller
size and all capitals. Various values of font variant are
• Normal
• Small-caps
• normal
• condensed
• semi-condensed
• ultra-condensed
• extra-condensed
• expanded
• semi-expanded
• ultra-expanded
• extra-expanded
CODE:
OUTPUT:
Font Family
In CSS, Font family specify the Font Typeface or Font Family we are using. These fonts are already
embedded in our operating system. But in CSS3, we can embedded external fonts too.
Web Safe Fonts
• Serif
• Sans-Serif
• Arial
• Helvetica
• Times new roman
• Times
• Georgia
• Verdana
• Courier
• Open Sans
• Monospace
• Cursive
• Fantasy
• Algerian
We can also use two different fonts together in a group. This tell the browser that if one font is unavailable,
use another one.
Font Property
CSS Font Property is used to group all font properties in one single property. Properties like font size, line
height and font family are compulsory to add, rest all are optional.
CSS Font Property
Font with size 16px, line height 18px and arial font.
Font with size 16px, line height 18px and arial font.
Font with size 18px, line height 1.4, bold, italic, small caps, condensed and arial font.
<p style="font:16px/18px arial"> Font with size 16px, line height 18px and arial
font.</p>
<p style="font:14px/18px arial"> Font with size 14px, line height 18px and arial
font.</p>
<p style="bold font:18px/1.4 cursive"> Font with size 14px, line height 18px and arial
font.</p>
<p style="font:small-caps condensed bold italic 18px/1.4 sans-serif"> Font with size
18px, line height 1.4, bold, italic, small caps, condensed and arial font.</p>
CSS Box Model is a box inside HTML Elements including Padding, Margin, & Border.
CSS Padding
Padding is used to give space inside an element. Padding inherit background color of content. As shown in
example above, padding occupy space inside.
padding Example
<img src="1.png" style="border:solid; padding:20px;" >
CSS Margin
Margin is used to give space outside an element. Margin background color is transparent. Margin value can
be positive and negative.
Margin units are px, %, em and auto. margin auto is used to align a block element in middle.
CSS Border
Border is used to give visible outline around an element. Border comes between Margin and Padding. Border
can have different styles, color and width. There are three type of border Properties:
1. Border-Style
2. Border-Width
3. Border-Color
Border-style
Border-width
A border can have width in px in em. Normally we use border width in pixels. Default border width is 3px
and color is inherited from font color.
To write more than one property of border, we can also write border short-cut:
Border Shortcuts
Long property shortcut
border-style:solid;
border-width:2px; border:solid 2px red
border-color:red
border-style:solid;
border-width:4px;
border-top-color:red; border:solid 2px;
border-right-color:blue; border-color:red blue
border-bottom-color:red;
border-left-color:blue
border-style:solid;
border-width:2px;
border-top-color:red; border:solid 2px;
border-right-color:blue; border-color:red blue green yellow
border-bottom-color:green;
border-left-color:yellow
margin-top:40px;
margin-right:30px
margin: 40px 30px 20px 10px
margin-bottom:20px
margin-left:10px
margin-top:40px;
margin-right:20px
margin:40px 20px
margin-bottom:40px
margin-left:20px
padding-top:50px;
padding-right:40px
padding: 50px 40px 30px 20px
padding-bottom:30px
padding-left:20px
padding-top:40px;
padding-right:20px
padding:40px 20px
padding-bottom:40px
padding-left:20px
NOTE: Border, Margin and padding will always occupy space, do remember to calculate the complete box-model
before writing CSS.
CSS Float
CSS Float property is used to flow an element ( usually images, div, li ) to the left or right corner of parent element
and the next element wrap around it. A floating element doesn't occupy space on DOM. Thus we have to use clear
both after last floating element to avoid wrapping.
• None
• Left
• Right
Float Left
CSS Float Left pull an element to the left corner of parent element, and other elements wrap around it. A
floating element doesn't occupy space in flow.
<style>
img{ float:left}
</style>
<style>
.list2 li{ float:left}}
</style>
<ul>
<li>List 1</l>
<li>List 2</li>>
<li>List 3</l>
<li>List 4</li>
</ul>
<ul class="list2">
<li>List 1</l>
<li>List 2</li>>
<li>List 3</l>
<li>List 4</li>
</ul>
<style>
*{ margin:0; padding:0}
.wrap{width:800px; height:auto; border:1px solid}
.aside1{width:200px; height:200px; background:aqua}
.section1{width:600px; height:200px; background:pink}
.aside2{width:200px; height:200px; background:aqua; float:left}
.section2{width:600px; height:200px; background:pink; float:left}
</style>
<div class="wrap">
<div class="aside1"></div>
<div class="section1"></div>
</div>
<div class="wrap">
<div class="aside2"></div>
<div class="section2"></div>
</div>
<style>
</style>
<div class="wrap">
<div class="aside1"></div>
<div class="section1"></div>
</div>
<div class="wrap">
<div class="aside2"></div>
<div class="section2"></div>
</div>
(Div without float)
CSS Clear
CSS Clear property is used to stop an element to wrap around the adjacent floating element.
• None( By Default)
• Left
• Right
• Both
<style>
.wrap{width:800px; height:auto; border:1px solid}
.aside2{width:200px; height:200px; background:aqua; float:left}
.section2{width:600px; height:200px; background:pink; float:left}
</style>
<div class="wrap">
<div class="aside2"></div>
<div class="section2"></div>
<p>This Paragraph will wrap around Adjacent floating Divs</p>>
</div>
<style>
.wrap{width:800px; height:auto; border:1px solid}
.aside2{width:200px; height:200px; background:aqua; float:left}
.section2{width:600px; height:200px; background:pink; float:left}
.clear{ clear:both}
}
</style>
<div class="wrap">
<div class="aside2"></div>
<div class="section2"></div>
<div class="clear"></div>
<p>This Paragraph will not wrap around Adjacent floating Divs as it is
clear
from both sides</p>>
</div>
Clear Both
CSS Clear Both Property does not allow any element to wrap around any adjacent Floating element in
both Left and Right Directions.
Background Color
<html>
<head>
<title>Background color</title>
<style>
.wrap1{width:90%; height:200px;
background-color:#FF0000;}
</style>
</head>
<body>
<div class="wrap1">
</div>
</body>
</html>
Background Image
<html>
<head>
<title>Background Image</title>
<style>
.wrap2{width:90%; height:200px;
background-image:url(images/favicon.ico);}
</style>
</head>
<body>
<div class="wrap2">
</div>
</body>
</html>
Background Repeat
Values of Background-Repeat:
no repeat
<html>
<head>
<title>Background Repeat</title>
<style>
.wrap3{width:90%; height:200px;
background-image:url(images/favicon.ico);
background-repeat:no-repeat;}
</style>
</head>
<body>
<div class="wrap3">
</div>
</body>
</html>
repeat-x
(How to write a CSS in HTML code)
<html>
<head>
<title>Background Repeat</title>
<style>
.wrap4{width:90%; height:200px;
background-image:url(images/favicon.ico);
background-repeat:repeat-x;}
</style>
</head>
<body>
<div class="wrap4">
</div>
</body>
</html>
repeat-y
(How to write a CSS in HTML code)
<html>
<head>
<title>Background Repeat</title>
<style>
.wrap5{width:90%; height:400px;
background-image:url(images/favicon.ico);
background-repeat:repeat-y;}
</style>
</head>
<body>
<div class="wrap5">
</div>
</body>
</html>
Background Position
Background Position Specify position of background image. By Default the pposition is left top.
top
<html>
<head>
<title>Background Position</title>
<style>
.wrap7{width:90%; height:200px;
background-image:url(images/favicon.ico);
background-repeat:no-repeat;
background-position:top left;}
</style>
</head>
<body>
<div class="wrap7">
</div>
</body>
</html>
bottom
<html>
<head>
<title>Background Position</title>
<style>
.wrap8{width:90%; height:200px;
background-image:url(images/favicon.ico);
background-repeat:no-repeat;
background-position:bottom;}
</style>
</head>
<body>
<div class="wrap8">
</div>
</body>
</html>
left
right
<html>
<head>
<title>Background Position</title>
<style>
.wrap10{width:90%; height:200px;
background-image:url(images/favicon.ico);
background-repeat:no-repeat;
background-position:right;}
</style>
</head>
<body>
<div class="wrap10">
</div>
</body>
</html>
center
<html>
<head>
<title>Background Position</title>
<style>
.wrap11{width:90%; height:200px;
background-image:url(images/favicon.ico);
background-repeat:no-repeat;
background-position:center;}
</style>
</head>
<body>
<div class="wrap11">
</div>
</body>
</html>
Background Attachment
Background Attachment tells weather a background image scrolls or remain fixed when we scroll window.
By Default it is scroll.
Two Values of Background Attachment
• Scroll (Default)
• Fixed
<html>
<head>
<title>Background Position</title>
<style>
.wrap11{width:90%; height:200px;
background:url(images/box-model.png) no-repeat;
background-attachment:fixed}
</style>
</head>
<body>
<div class="wrap11">
</div>
</body>
</html>
Background Shorthand
We Can write all background properties in one by using property background. Here is an example
<html>
<head>
<title>Background Position</title>
<style>
.wrap11{width:90%; height:200px;
background:url(images/box-model.png) no-repeat left top aqua;;}
</style>
</head>
<body>
<div class="wrap11">
</div>
</body>
</html>
CSS Pseudo Class and Pseudo Elements
Pseudo Class change properties of an element on particular event, like mouseover, focus etc.
Pseudo Elements are used to call particular child of parent, like first-line, first-letter etc.
Pseudo Classes
• :link
• :visited
• :active
• :focus
• :hover
Pseudo Elements
• :first-line
• :first-letter
• :first-child
• :before
• :after
a:link.
:link pseudo class is used to change properties of an unvisited link. once clicked, the link pseudo property
will not show as link is visited. Default link color is blue. link pseudo class was introduced in css level 1.
<style>
a:link{ color:red}
</style>
<a href="#">Link</a>
:visited
:visited pseudo class works only when an hyperlink is visited. Default color of visited hyperlinks is purple.
visited pseudo classcan change color and properties of visited links. visited pseudo class was also
introduced in css level 1.
<style>
a:link{ color:red} /* Default color of a link*/
a:visited{ color:yellow} /* color of link after visit*/
<a href="#">Link</a>
:active
:active class works only when user mouse left key is clicked, as soon as he remove.
<style>
a:link{ color:red} /* Default color of a link*/
a:visited{ color:yellow} /* Changed color of a link after visited*/
a:active{ color:gray} /* Changed color of a link on being click*/
</style>
<a href="#">Link</a>
:hover
:hover class works when user mouseover the element. On mouseout, it comes back to its original form.
<style>
a:link{ color:blue} /* Default color of hyperlink*/
a:visited{ color:yellow} /* hyperlink color after visit*/
a:active{ color:gray} /* hyperlink color on being click*/
a:hover{ color:red} /* Changed color on mouseover*/
</style>
<a href="#">Link</a>
:focus
:focus class change style of an element only when that element is focused. All hyperlinks, and form controls
support focus. Both mouse and tab key can focus an element.
<style>
#text1:focus{ outline:none; border:2px solid red}
a:focus{ }
</style>
<style>
p{ color:blue} /* For Entire Para tag*/
p:first-line{ color:red} /* For First line of the Para tag*/
</style>
<p>Inteligula congue id elis donec sce sagittis intes id laoreet aenean leo sem
massawisi condisse leo sem ac. Tincidunt nibh quis dui fauctor et.</p>
:first-child
:first-child of the parent only. Means only first child of parent will follow this class.
<style>
ul li{ color:blue} /*For all list
Items*/
ul li:first-child{ color:yellow} /*For First listed
Item*/
</style>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
:first-letter
<style>
p{ color:blue; font-size:16px}
p:first-letter{ font-size:24px; color:red}
</style>
<p>This is a paragraph</p>
:before
:before is used to add some content before element through css without affecting the Markup. before was
introduced in css 2.1 and is supported on Internet Explorer 8 and above. before content is treated as child of
same element. :before is inline.
<style>
.list li:before{ content:"Q No: " }
</style>
<style>
button:before{
content:"";
border-style:6px solid;
border-color:white transparent transparent transparent;
}
</style>
<button>Dropdown<button>
<p>This is a Quote<p>
:after
:after is used to add some content after an html tag, without any change in HTML Markup. After was also
introduced in css 2.1 with before.:after is also inline.
<style>
.list li:after{ content:"." }
</style>
<ul>
<li>List 1<li>
<li>List 2<li>
<li>List 3<li>
<li>List 4<li>
<li>List 5<li>
<ul>
NAVIGATION Menu is used to give various links inside a web-page. Here we'll learn to create a simple
Navigation menu using HTML and CSS Only.
<div class="wrap">
<div class="nav">
<ul>
<li><a href="#">HOME</a></li>
<li><a href="#">ABOUT</a></li>
<li><a href="#">SERVICES</a></li>
<li><a href="#">SOLUTIONS</a></li>
<li><a href="#">CONTACT</a></li>
</ul>
</div>
</div>
<style>
.nav ul{ list-style-type:none;}
</style>
Give Background Color and Align text
(How to write a CSS in HTML code)
<style>
.nav ul{ list-style-type:none;}
.nav ul li{ float:left; padding:0px 5px; background:#000; text-align:center;}
</style>
<style>
.nav ul{ list-style:none;}
.nav ul li{ float:left; padding:0px 5%; background:#000; text-align:center;}
.nav ul li a{ color:#ccc;}
</style>
<style>
.nav ul{ list-style:none;}
.nav ul li{ float:left; padding:0px 5%; background:#000; text-align:center;}
.nav ul li a{ color:#ccc; display:block; padding:10px}
.nav ul li:hover a{ color:#000;}
.nav ul li:hover{ background:#ccc;}
</style>
Insert Header
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>my webpage</title>
<style>
*{
margin:0px;
padding:0px;
}
.wrap{
width:960px;
height:auto;
border:1px solid red;
}
.header{
height:150px;
background:#333;
}
.nav{
height:50px;
background:#000;
}
</style>
<head>
<body>
<div class="wrap">
<div class="header"></div>
<div class="nav"></div>
</div>
</div>
</body>
</html>
Inserting Container
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>my webpage</title>
<style>
*{
margin:0px;
padding:0px;
}
.wrap{
width:960px;
height:auto;
border:1px solid red;
}
.header{
height:150px;
background:#333;
}
.nav{
height:50px;
background:#000;
}
.container{
height:768px;
background:#ccc;
}
</style>
<head>
<body>
<div class="wrap">
<div class="header"></div>
<div class="nav"></div>
<div class="container"></div>
</div>
</div>
</body>
</html>
Inserting Footer
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>my webpage</title>
<style>
*{
margin:0px;
padding:0px;
}
.wrap{
width:960px;
height:auto;
border:1px solid red;
}
.header{
height:150px;
background:#333;
}
.nav{
height:50px;
background:#000;
}
.container{
height:768px;
background:#ccc;
}
.footer{
height:80px;
background:#ccc;
}
</style>
<head>
<body>
<div class="wrap">
<div class="header"></div>
<div class="nav"></div>
<div class="container"></div>
<div class="footer"></div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>my webpage</title>
<style>
*{
margin:0px;
padding:0px;
}
.wrap{
width:960px;
height:auto;
border:1px solid red;
}
.header{
height:150px;
background:#333;
}
.nav{
height:50px;
background:#000;
}
.container{
background:#ccc;
}
.left{
width:30%;
height:200px;
float:left;
background:#333;
}
.right{
width:70%;
height:200px;
float:left;
}
.clear{
clear:both; // Clear Both will clear floating from both sides.
}
.footer{
height:80px;
background:#ccc;
}
</style>
<head>
<body>
<div class="wrap">
<div class="header"></div>
<div class="nav"></div>
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="clear"></div>
</div>
<div class="footer"></div>
</div>
</div>
</body>
</html>
CSS Display
CSS Display property shows how and html element behaves. Display property can also change display of
an HTML Element, like from block to inline and inline to block etc.
By default, the initial value of display for all html elements is inline. User Agent stylesheet ( CSS given by
browser ) change the behavior form inline to block, table, inline-block, none etc and thus all html behaves
different.
Display Inline
Display inline is default display property of <a>, <img>, <span>, <b>, <i>, <s>, <etc>. These Elements
occupy space of content only and does not break line. Inline elements of text type(a, span, b, i, strong, em,
small) doesn't support width. But image and iframe supports.
<style>
span,a{ background:blue;}
div,p{ background:yellow}
</style>
See the default display property of <span>, <a> is inline, and for <p>, and <div> is block.
<p>Para 1</p>
<p>Para 2</p>
<p>Para 3</p>
Display NONE
Display none hide an html element from user. Thus it doesn't occupy any space. We can change display of
these elements on hover of parent element.
Width of wrap in Pixels ( 960px, 1200px). In Percentage or auto, for exp 80%
Device Compatibility Remain Same, but compress. Remain Same. Do not compress
Fixed Layout
Fixed Layout is a layout in which the width of main container is fixed ( in pixels).
<style>
*{
margin:0px;
padding:0px;
}
.wrap{
width:960px; /* Width will remain in pixels*/
height:auto;
margin:auto; /* To set content in center*/
}
.header{
height:150px;
background:aqua;
}
.nav{
height:50px;
background:gray;
}
.container{
background:white;
}
.aside{
height:400px;
background:pink;
float:left;
}
.section{
height:560px;
background:yellow;
float:left;
}
.footer{
height:100px;
background:#333;
}
.clear{
clear:both;
}
</style>
<div class="wrap">
<div class="header">This is header</div>
<div class="nav">This is navigation menu</div>
<div class="container">
<div class="aside">this is left section</div>
<div class="section">this is right section</div>
<div class="clear"></div>
</div>
<div class="footer">this is footer</div>
</div>
</div>
Example:
http://iu.ac.bd/fixed_layout.html
Liquid Layout:
Liquid Layout is a layout in which the width of main container is flexible( in percentage). Whatever the
screen-size is, the layout will remain same.
• Width is in percentage.
• Text scroll down when zoom in or zoom out
• Dependent of screen size.
• Horizontal Scroll will Never come on any screen unless any fixed content is inside.
<style>
*{
margin:0px;
padding:0px;
}
.wrap{
width:90%; /* Width will remain in percentage*/
height:auto;
margin:auto; /* To set content in center*/
}
.header{
height:auto;
background:aqua;
}
.nav{
height:50px;
background:gray;
}
.container{
background:white;
}
.aside{
width:25%; /* Aside will be 25% of parent*/
background:pink;
float:left;
}
.section{
width:75%; /* Section will be 75% of parent*/
background:yellow;
float:left;
}
.footer{
height:100px;
background:#333;
}
.clear{
clear:both;
}
</style>
<div class="wrap">
<div class="header">This is header</div>
<div class="nav">This is navigation menu</div>
<div class="container">
<div class="aside">this is left section</div>
<div class="section">this is right section</div>
<div class="clear"></div>
</div>
<div class="footer">this is footer</div>
</div>
</div>
Example:
http://iu.ac.bd/liquid_layout.html
CSS Positions
CSS Position means where an HTML element should be placed. Positions are used to specify the location of
an HTML element on a page layout. By default, all elements in html are positioned staticly, means according
to page flow. But using css positions, they can be moved from their position relatively or absolutely.
Static Position
Static position is the default position of all HTML elements. Static means position according to the HTML
Order or position taken by an element by its own.
Note: CSS Properties top, left, bottom, right and z-index doesn't work with static position.
<div class="box">
<p> Div with Static Position</p>
</div>
Position Relative
Relative Position means position of an HTML element with respect to its actual position. Relative position
can overlap over another elements, relative to its position. Relative elements moves, but their space remains.
By default, relative elements are above static elements on z axis. But of two or more elements are relative
and we move them, the last relative element will come on the top(z axis)
Note: We can use top, left, bottom, right and z-index after giving position relative to an HTML element.
<style>
.box1{
width:200px;
height:200px;
background:#333;
}
.box2{
width:200px;
height:200px;
background:#999;
position:relative;
left:100px;
bottom:50px
}
</style>
Position Absolute
Absolute Position means position of an HTML element in a specific location outside of normal document
flow. Position Absolute remove the element from normal flow and shows all of its own. An element with
position absolute can be placed anywhere on the page using properties top, left, right and bottom. Absolute
Element can also overlap other elements by using z-index property.
Note: We can use top, left, bottom, right and z-index after giving position absolute to an HTML element.
<style>
.box1{
width:200px;
height:200px;
background:#333;
}
.box2{
width:200px;
height:50px;
background:#999;
position:absolute;
}
</style>
Note: We can use top, left, bottom, right and z-index after giving position fixed to an HTML element.
CSS Z-Index
CSS Z-Index is used to place an non static element above another element. Value of z-index is a number.
Note: z-index can work only if the position of the element is relative, absolute, or fixed. It Doesn't work on
position static.
<style>
.box1{
width:200px;
height:200px;
background:#333;
position:relative;
z-index:1;
}
.box2{
width:200px;
height:200px;
background:#999;
position:relative;
z-index:2;
}
.box3{
width:200px;
height:200px;
background:#ccc;
position:relative;
z-index:3;
}
</style>
<style>
*{ margin:0px; padding:0px}
ul{ list-style:none}
a{ text-decoration:none}
.wrap{ width:960px; margin:auto}
.navtop { background:#333; height:40px; color:#fff}
.navtop > ul{ float:left; position:relative; z-index:1}
</div>
</body>
</html>