CSS notes
CSS notes
es in the form of
property/value pairs for determining how elements in a web page should be displayed.
They were developed with the aim to make the structure of the HTML web page ,and look and feel of the web page
and the elements present on the web page ,handled separately.
The aim of developing CSS was to make HTML deal only with the structure of web pages and to make all style –
related aspects of web pages be handled in CSS.
W3C has developed some specifications(or rules) for creating and using style sheets.these specifications are called
Cascading style sheets (CSS)specification.
Css syntax
Selector {property1:property-value; property2:property-value; property3:property-value;……….}
Selector is the element that the rule defines ,
property1, property2 and property3 are the properties(attributes)defined for that element ,and property1:property-
value; property2:property-value; property3:property-value; are values assigned to these properties.
The portion of the syntax enclosed within the curly braces is termed as declaration.
Eg:
body{background-color:#f0f8ff;font-family:arial}
shorthand property is a CSS property ,which enables you to set two or more properties in one declaration
eg: body{background:#0000ff url(c:\Image.jpg) repeat-x}
working with styles
we can create style in three ways:
External Style Sheets
Embedded Style Sheets
Inline Styles
externalstylesheet.html
<html>
<head>
<title>External style Sheets</title>
<link rel="stylesheet" href="style.css" type=”text/css” >
</head>
<body>
<h1>External Style Sheet Example</h1>
<a href="page1.html" target="_blank">
<h2>Page 1</h2></a>
<a href="page2.html" target="_blank">
<h2>Page 2</h2></a>
</body>
</html>
Advantages of External CSS Style Sheets
The same style sheet can be reused by all of the web pages in your site. This saves you
from including the stylistic markup in each individual document.
You can change the appearance of several pages by altering just the style sheet rather
than each individual page.
Because the source document does not contain the style rules, different style sheets can
be attached to the same document. So you can use the same XHTML document with one
style sheet when the viewer is on a desktop computer, another style sheet when the user
has a handheld device, another style sheet when the page is being printed, and another
style sheet when the page is being viewed on a TV, and so on. You reuse the same
document with different style sheets for different visitors’ needs.
A style sheet can import and use styles from other style sheets, making for modular
development and good reuse.
<td style="background-color:#808800">22-01-2000</td>
<td style="background-color:#808800">flat no22,shipra
</tr>
<tr>
<td style=background-color:pink;font-family:courier;padding:10px;border-style:solid;border-width:3px;border-
color:#000000;>rinu</td>
<td style="background-color:#808800">26-11-2002</td>
<td style="background-color:#808800">flat no23,shipra
</tr>
</table>
</body>
</html>
CSS Selector
CSS selectors are used to select the content you want to style. Selectors are the part of CSS rule set. CSS selectors select HTML
elements according to its id, class, type, attribute etc.
There are several different types of selectors in CSS.
1. CSS Element Selector
2. CSS Id Selector
3. CSS Class Selector
4. CSS Universal Selector
5. CSS Group Selector
1) CSS Element Selector
The element selector selects the HTML element by name.
Eg
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
2) CSS Id Selector
The id selector selects the id attribute of an HTML element to select a specific element. An id is always unique within the page so it is
chosen to select a single, unique element.It is written with the hash character (#), followed by the id of the element.
Eg
<html>
<head>
<title>Embedded style Sheets</title>
<style type="text/css">
#para1 {text-align:center;color:blue;}
</style>
</head>
<body>
<p id="para1">Hello world!
<p>this paragraph is not affected by the style
</body>
</html>
Styling background
CSS backround properties are used to define the background effects of an element.
Property Background
Background Is a shorthand property for background-
color,background-image,background-
repeat,background-attachment and background –
position.
It sets all these background properties in one
declaration.
background-attachment Specifies whether a background image is fixed or
scrolls when the user scrolls through the rest of the
page
background-color Specifies the background color of an element
background-image Specifies the background image of an element.
background –position. Specifies the initial position of a background
image.(top left/center/right,center left/center/right,
bottom left/center/right,x% y%,x position
yposition)
background-repeat Specifies how a background image is
repeated(repeat-x,repeat–y,no-repeat)
Eg:
<html>
<head>
<title>Embedded style Sheets</title>
<style type="text/css">
body{background-color:00ffbb;background-image:url('flower.jpg');
background-repeat:no-repeat;background-position:right top;
background-attachment:fixed;}
p{color:rgb(0,0,255);}
</style>
</head>
<body>
<h1>Hello world!</h1>
sets a background color
<p>sets a font color
<br>
<br>
</body>
</html>
Styling Text
The following are the css text properties used for styling the text:
Property Description
Color Sets the colour of the text.
With css,a colour is most often specified by:
a HEX value-like “#ff00000”
an RGB value-like “rgb(255,0,0)”
a colour name-like “red”
Direction Specifies the text direction/writing direction
Letter-spacing Increases or decreases the space between characters in a
text.
Line-height Sets the line height
Text-align Specifies the horizontal alignment of text.
Text can be left/right/center/justified
Text-decoration Specifies the decoration added to text.
This is mosly used to remove underlines from links
for design purposes.
It can also used to decorate text.{text-
decoration:overline/linethrough/underline/blink;}
<html>
<head>
<title>styling font</title>
<style type="text/css">
body{color:green;}
h1{color:red;text-align:center;}
p.col{color:rgb(0,0,255);text-indent:50px;}
p.date{text-align:right}
p.upper{text-transform:uppercase;}
a{text-decoration:none;}
h2{text-decoration:overline;}
h3{text-decoration:line-through;}
h4{text-decoration:underline;}
</style>
</head>
<body>
<p class="date">30 Aug 2018</P>
<h1>Hello world!</h1>
<p class="col">this is an ordinary paragraph
<p>link to:<a href="http://www.google.com">google</a></p>
<h2>heading 2</h2>
<h3>heading 3</h3>
<h4>heading 4</h4>
<p class="upper">this text is in uppercase</p>
</body>
</html>
Styling font
Css font properties define the font family,boldness,size and the style of a text.
Property Description
Font Sets all the font properties in one declaration
Font-family Specifies the font family for the 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
Font family
In CSS, there are two types of font family names:
generic family - a group of font families with a similar look (like "Serif" or"Monospace")
font family - a specific font family (like "Times New Roman" or "Arial")
Generic Family Font family Description
Serif Times New Roman Serif fonts have small lines at the
ends on some characters
Georgia
Sans-serif Arial "Sans" means without - these fonts
do not have the lines at the ends of
Verdana characters
Monospace Courier New Lucida All monospace characters have the
same width
Console
Styling Lists
The CSS list properties allow you to:
Set different list item markers for ordered lists
Set different list item markers for unordered lists
Set an image as the list item marker
Add background colors to lists and list items
In HTML, there are two main types of lists:
unordered lists (<ul>) - the list items are marked with bullets
ordered lists (<ol>) - the list items are marked with numbers or letters
<html>
<style>
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
</style>
<body>
<ul class=”a”>
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ul class=“b”>
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol class=“c”>
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
<ol style=“d”>
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
</body>
</html>
Eg 2
<html>
<head>
</head>
<body>
<ul style = "list-style-type:circle;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ul style = "list-style-type:square;">
<li>Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<body>
<ul>
<li style = "list-style-image: url(/https/www.scribd.com/images/bullet.gif);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ul>
<ol>
<li style = "list-style-image: url(/https/www.scribd.com/images/bullet.gif);">Maths</li>
<li>Social Science</li>
<li>Physics</li>
</ol>
</body>
</html>
ul.b {
list-style-position: inside;
}
</style>
</head>
<body>
<h2>list-style-position: inside:</h2>
<ul class="b">
<li>Coffee - A brewed drink prepared from roasted coffee beans, which are the seeds of berries from the Coffea
plant</li>
<li>Tea - An aromatic beverage commonly prepared by pouring hot or boiling water over cured leaves of the
Camellia sinensis, an evergreen shrub (bush) native to Asia</li>
<li>Coca Cola - A carbonated soft drink produced by The Coca-Cola Company. The drink's name refers to two
of its original ingredients, which were kola nuts (a source of caffeine) and coca leaves</li>
</ul>
</body>
</html>
Css tables
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a black border for <table>, <th>, and <td> elements:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
Notice that the table in the example above has double borders. This is because both the table and the <th> and <td>
elements have separate borders.
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
If you only want a border around the table, only specify the border property for <table>:
table {
border: 1px solid black;
}
Eg:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: 1px solid black;
}
table {
border-collapse: collapse;
width: 100%;
}
th {
height: 50px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or center) of the content in <th> or <td>.
By default, the content of <th> elements are center-aligned and the content of <td>
elements are left-aligned.
Eg:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: 1px solid black;
}
table {
border-collapse: collapse;
width: 100%;
}
th {
text-align: left;
}
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or
<td>.
By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements).
Eg
<style>
table, td, th {
border: 1px solid black;
}
table {
border-collapse: collapse;
width: 100%;
}
td {
height: 50px;
vertical-align: bottom;
}
</style>
Table Padding
To control the space between the border and the content in a table, use the padding
property on <td> and <th> elements:
<style>
table, td, th {
border: 1px solid #ddd;
text-align: left;
}
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 15px;
}
</style>
Table Color
The example below specifies the background color and text color of <th> elements:
th {
background-color: #4CAF50;
color: white;
}
Horizontal Dividers
First Name Last Name Savings
Add the border-bottom property to <th> and <td> for horizontal dividers:
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
</style>
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
tr:hover {background-color:#f5f5f5;}
</style>
Striped Tables
First Name Last Name Savings
Peter Griffin $100
th, td {
text-align: left;
padding: 8px;
}
CSS Floating
With CSS float,an element can be pushed to the left or right, allowing other elements to wrap around it.
Float is very often used for images ,but it is also useful when working with layouts.
Css float properties and possible value
Property Description Values
Clear Specifies which sides of an element where other floating Left,right,both,none,inherit
elements are not allowed.
Float Specifies whether or not a box should float Left,right,none,inherit
Elements are floated horizontally, this means that an element can only be floated left or right..
The element after floating element will flow around it.
The element before floating element will not be affected.
Eg:-
Img
{
float:left;
}
<html>
<head>
<style>
div.gallery {
margin: 5px;
border: 1px solid #ccc;
float: left;
width: 180px;
}
div.gallery:hover {
border: 1px solid #777;
}
div.gallery img {
width: 100%;
height: auto;
}
div.desc {
padding: 15px;
text-align: center;
}
</style>
</head>
<body>
<div class="gallery">
<a target="_blank" href="fjords.jpg">
<img src="5terre.jpg" alt="Cinque Terre" width="300" height="200">
</a>
<div class="desc">Add a description of the image here</div>
</div>
<div class="gallery">
<a target="_blank" href="forest.jpg">
<img src="forest.jpg" alt="Forest" width="300" height="200">
</a>
<div class="desc">Add a description of the image here</div>
</div>
<div class="gallery">
<a target="_blank" href="lights.jpg">
<img src="lights.jpg" alt="Northern Lights" width="300" height="200">
</a>
<div class="desc">Add a description of the image here</div>
</div>
<div class="gallery">
<a target="_blank" href="mountains.jpg">
<img src="mountains.jpg" alt="Mountains" width="300" height="200">
</a>
<div class="desc">Add a description of the image here</div>
</div></body>
</html>
Transparent Image
The opacity property can take a value from 0.0 - 1.0. The lower value, the more transparent:
img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property specifies the transparency of an element. The lower the value, the more transparent:</p>
<p>Image with 50% opacity:</p>
<img src="img_forest.jpg" alt="Forest" width="170" height="100">
</body>
</html>
img:hover {
opacity: 1.0;
filter: alpha(opacity=100); /* For IE8 and earlier */
}
eg
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
img:hover {
opacity: 1.0;
filter: alpha(opacity=100); /* For IE8 and earlier */
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property is often used together with the :hover selector to change the opacity on mouse-over:</p>
<img src="img_forest.jpg" alt="Forest" width="170" height="100">
<img src="img_mountains.jpg" alt="Mountains" width="170" height="100">
<img src="img_5terre.jpg" alt="Italy" width="170" height="100">
</body>
</html>
Transparent Box
When using the opacity property to add transparency to the background of an element, all of its child elements
inherit the same transparency. This can make the text inside a fully transparent element hard to read:
div {
opacity: 0.3;
filter: alpha(opacity=30); /* For IE8 and earlier */
}
eg
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #4CAF50;
padding: 10px;
}
div.first {
opacity: 0.1;
}
div.second {
opacity: 0.3;
}
div.third {
opacity: 0.6;
}
</style>
</head>
<body>
<h1>Transparent Box</h1>
<p>When using the opacity property to add transparency to the background of an element, all of its child elements become
transparent as well. This can make the text inside a fully transparent element hard to read:</p>
<div class="first"><p>opacity 0.1</p></div>
<div class="second"><p>opacity 0.3</p></div>
<div class="third"><p>opacity 0.6</p></div>
<div><p>opacity 1 (default)</p></div>
</body>
</html>
An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0
(fully transparent) and 1.0 (fully opaque).
div {
background: rgba(76, 175, 80, 0.3) /* Green background with 30% opacity */
}
eg
<!DOCTYPE html>
<html>
<head>
<style>
div {
background: rgb(76, 175, 80);
padding: 10px;
}
div.first {
background: rgba(76, 175, 80, 0.1);
}
div.second {
background: rgba(76, 175, 80, 0.3);
}
div.third {
background: rgba(76, 175, 80, 0.6);
}
</style>
</head>
<body>
<h1>Transparent Box</h1>
<p>With opacity:</p>
<div style="opacity:0.1;"><p>10% opacity</p></div>
<div style="opacity:0.3;"><p>30% opacity</p></div>
<div style="opacity:0.6;"><p>60% opacity</p></div>
<div><p>opacity 1</p></div>
<p>With RGBA color values:</p>
<div class="first"><p>10% opacity</p></div>
<div class="second"><p>30% opacity</p></div>
<div class="third"><p>60% opacity</p></div>
<div><p>default</p></div>
<p>Notice how the text gets transparent as well as the background color when using the opacity property.</p>
</body>
</html>
<html>
<head>
<style>
div.background {
background: url(klematis.jpg) repeat;
border: 2px solid black;
}
div.transbox {
margin: 30px;
background-color: #ffffff;
border: 1px solid black;
opacity: 0.6;
filter: alpha(opacity=60); /* For IE8 and earlier */
}
div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
}
</style>
</head>
<body>
<div class="background">
<div class="transbox">
<p>This is some text that is placed in the transparent box.</p>
</div>
</div>
</body>
</html>
CSS Navigation Bar
Navigation Bars
Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation bars.
Navigation Bar = List of Links
A navigation bar needs standard HTML as a base.
In our examples we will build the navigation bar from a standard HTML list.
A navigation bar is basically a list of links, so using the <ul> and <li> elements makes perfect sense:
<!DOCTYPE html>
<html>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Output
Home
News
Contact
About
Vertical Navigation Bar
To build a vertical navigation bar, you can style the <a> elements inside the list, in addition to the code above:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li a {
display: block;
width: 60px;
background-color: #dddddd;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>A background color is added to the links to show the link area.</p>
<p>Notice that the whole link area is clickable, not just the text.</p>
</body>
</html>
display: block; - Displaying the links as block elements makes the whole link area clickable (not just the
text), and it allows us to specify the width (and padding, margin, height, etc. if you want)
width: 60px; - Block elements take up the full width available by default. We want to specify a 60 pixels
width
You can also set the width of <ul>, and remove the width of <a>, as they will take up the full width available when
displayed as block elements.
li {
display: inline;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
display: inline; - By default, <li> elements are block elements. Here, we remove the line breaks before and after
each list item, to display them on one line
li {
float: left;
}
li a {
display: block;
padding: 8px;
background-color: #dddddd;
}
</style>
float: left; - use float to get block elements to slide next to each other
display: block; - Displaying the links as block elements makes the whole link area clickable (not just the
text), and it allows us to specify padding (and height, width, margins, etc. if you want)
padding: 8px; - Since block elements take up the full width available, they cannot float next to each other.
Therefore, specify some padding to make them look good
background-color: #dddddd; - Add a gray background-color to each a element
Tip: Add the background-color to <ul> instead of each <a> element if you want a full-width background color:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #dddddd;
}