Professional Documents
Culture Documents
Topics: Cascading Style Sheet 22 February 2021
Topics: Cascading Style Sheet 22 February 2021
CSS Basic
CSS Introduction
CSS Syntax
CSS Id & Class
Topics
CSS How to insert
CSS Styling
CSS Styling Backgrounds
Styling Text
Styling Fonts
Styling Links
Styling Lists https://www.youtube.com/watch?v=0R4_qWPLyuY
Dr. MareeswariV Styling Tables
CSS Box Model
Assistant Professsor (Sr) CSS Box Model
CSS Border
School of Information Technology and Engineering CSS Margin
CSS Padding
VIT, Vellore CSS Advanced
Cabin No:SJT 210-A30 CSS Grouping/Nesting
CSS Positioning
<span>
CSS Demo
Inline Styles
An inline style loses many of the advantages of style sheets by mixing <html> <body>
content with presentation. <head> <h1>CSS example!</h1>
<style type="text/css"> <p>This is a paragraph.</p>
To use inline styles you use the style attribute in the relevant tag. The
body </body>
style attribute can contain any CSS property. { background-color:#d0e4fe; } </html>
<p style="color:red;margin-left:20px">This is a paragraph.</p> h1
{ color:orange;
text-align:center; }
p
{ font-family:"Times New
Roman";
font-size:20px; }
</style> </head>
The id Selector
The id selector is used to specify a style for a single, unique element. The class Selector
<html> <body> It is used to specify a style for a <html>
group of elements. <head>
<head> <p id="para1">Hello World!</p>
It uses the HTML class attribute, <style type="text/css">
<style type="text/css"> <p>This paragraph is not affected by the and is defined with a ".“ .redcenter
#para1 style.</p> Eg: .center {text-align:center;} { text-align:center;
{ text-align:center; <p id="para2"> The id selector uses the You can also specify that only color:red; }
id attribute of the HTML element, and specific HTML elements should be
color:red; } affected by a class. </style></head>
is defined with a "#".The style rule
#para2 Eg: p.center {text-align:center;} <body>
below will be applied to the element
{ text-align:right; <h1 class="redcenter">Center-
with id="para2" </p> aligned heading</h1>
color:blue; } </body> <p class="redcenter">Center-
</style> </head> </html> aligned paragraph.</p>
</body></html>
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE
Difference
ID's are unique Classes are NOT unique Multiple Style Sheets
Each element can have only one You can use the same class on If some properties have been set for the same selector in different style
ID multiple elements. sheets, the values will be inherited from the more specific style sheet.
Each page can have only one You can use multiple classes on For example, properties for the h3 selector:
element with that ID the same element.
External style sheet Internal style sheet internal style sheet
ID is absolutely the fastest. Part of the reason is that ID is supposed to be h3 h3
unique, so the API stops searching after the ID is found in the DOM. If you also links to the
{ { external style sheet
must use a class or attribute selector, you can improve performance by color:red; text-align:right;
specifying the optional context parameter. as final:
text-align:left; font-size:20pt; color:red;
Information that is reusable should be kept in a class and font-size:8pt; } text-align:right;
} font-size:20pt;
information that is totally unique should be kept in an ID.
<p class="para3" id="para4"> Both an ID and a Class applied on
a single element</p>
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE
background-color background-image
body {background-color:#b0c4de;} By default, the image is repeated so it covers the entire element
Background-attachment background-position
You may choose to have your background scroll naturally, or to have it If you would like to define where exactly an image appears within an
in a fixed position. HTML element, you may use CSS's background-position.
body { Three different ways of defining position:
length, percentages, and keywords.
background-image: url(smallPic.jpg);
p{ background-image: url(smallPic.jpg);
background-attachment: fixed; }
background-position: 20px 10px; }
body {
h4 { background-image: url(smallPic.jpg);
background-image: url(smallPic.jpg); background-position: 30% 30%; }
background-attachment: scroll;} ol { background-image: url(smallPic.jpg);
background-position: top center; }
The location of the image will be (A)px from the left of the screen and
(B)px from the top of the screen.
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE
Text Transformation
Text Decoration It can be used to turn everything into uppercase or lowercase letters, or
capitalize the first letter of each word.
The text-decoration property is used to set or remove decorations from
text. p {text-transform:uppercase;} lowercase or capitalize
h1 {text-decoration:overline;} Text Indentation
h2 {text-decoration:line-through;} It is used to specify the indentation of the first line of a text.
h3 {text-decoration:underline;} p {text-indent:50px;}
h4 {text-decoration:blink;} Word Spacing
a {text-decoration:none;} remove underlines from links It is used to specify the exact value of the spacing between your words.
Note:The "blink" value is not supported in IE, Chrome, or Safari. p { word-spacing: 10px; }
Letter Spacing
It is used to specify the exact value of the spacing between your letters.
p { letter-spacing: 3px; }
Emoji
font-variant: normal|small-caps|initial|inherit; <!DOCTYPE html><html><head>
<meta charset="UTF-8">
</head><body>
<h1>Sized Emojis</h1>
<p style="font-size:48px">
😀 😄 😍 💗
</p></body></html>
Link Properties
The four links states are: Text Decoration
1. a:link - a normal, unvisited link a:link {text-decoration:none;}
2. a:visited - a link the user has visited a:visited {text-decoration:none;}
3. a:hover - a link when the user mouses over it a:hover {text-decoration:underline;}
4. a:active - a link the moment it is clicked a:active {text-decoration:underline;}
a:link {color:#FF0000;} /* unvisited link */ Background Color
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;} /* mouse over link */ The background-color property specifies the background color for links:
a:active {color:#0000FF;} /* selected link */ a:link {background-color:#B2FF99;}
When setting the style for several link states, there are some order
rules:
a:hover MUST come after a:link and a:visited
a:active MUST come after a:hover
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE
<html> <head>
a.five:visited {color:#0000ff; <body>
<style type="text/css">
text-decoration:none;} <p>Mouse over the links to see them change layout.</p>
a.one:link {color:#ff0000;}
a.five:hover{text-decoration:underline;} <p><b><a class="one" href="default.asp" target="_blank">This link
a.one:visited {color:#0000ff;}
a.six:link,a.six:visited changes color</a></b></p>
a.one:hover {color:#ffcc00;}
{ display:block; <p><b><a class="two" href="default.asp" target="_blank">This link
a.two:link {color:#ff0000;} changes font-size</a></b></p>
font-weight:bold;
a.two:visited {color:#0000ff;}
color:#FFFFFF; <p><b><a class="three" href="default.asp" target="_blank">This link
a.two:hover {font-size:150%;} changes background-color</a></b></p>
background-color:#98bf21;
a.three:link {color:#ff0000;} <p><b><a class="four" href="default.asp" target="_blank">This link
width:120px;
a.three:visited {color:#0000ff;} changes font-family</a></b></p>
text-align:center;
a.three:hover {background:#66ff66;} <p><b><a class="five" href="default.asp" target="_blank">This link
padding:4px;
a.four:link {color:#ff0000;}
text-decoration:none;} changes text-decoration</a></b></p>
a.four:visited {color:#0000ff;}
a.six:hover,a.six:active <p><b><a class="six" href="../tutorial.html" target="_blank">This
a.four:hover {font-family:monospace;}
{ background-color:#7A991A;
link changes Box Color</a></b></p>
a.five:link { color:#ff0000;
text-decoration:underline;}
</body></html>
text-decoration:none;}
Dr. MAREESWARI V/ AP(Sr) / SITE </style> </head> Dr. MAREESWARI V/ AP(Sr) / SITE
Table Padding
To control the space between the border and content in a table, use the
padding property on td and th elements:
Table Text Alignment td
The text in a table is aligned with the text-align and vertical-align properties. {
The text-align property sets the horizontal alignment, like left, right, or padding:15px;
center }
td Table Color
{ The example below specifies the color of the borders, and the text and
text-align:right; background color of th elements:
} table, td, th
The vertical-align property sets the vertical alignment, like top, bottom, or {
middle: border:1px solid green;
td }
{ th
height:50px; {
vertical-align:bottom; background-color:green;
} color:white;
}
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE
<body>
<html> <head>
<table> BOX MODEL
<style type="text/css">
<tr> In CSS, the term "box model" is used when talking about design and
table layout. It is essentially a box that wraps around HTML elements.
<th>Student Name</th>
{ border-collapse:collapse;
<th>Seminar Topic</th> Margin - Clears an area around the border. The margin does not have
width:50%; }
</tr> a background color, it is completely transparent .
th <tr>
{ height:50px; Border - A border that goes around the padding and content. The
<td>Madhusuthanan P</td> border is affected by the background color of the box.
vertical-align:center; } <td>Internet, Intranet and
td WWW</td> Padding - Clears an area around the content. The padding is affected
{ text-align:right; } </tr> by the background color of the box.
table,th,td <tr> Content -The content of the box, where text and images appear.
{ <td>Roopa S</td>
border:1px solid red; <td>Internet Protocols</td>
} </tr>
</table> </body> </html>
</style> </head>
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE
inherit <body>
The inherit keyword specifies that a property should inherit its <div> Here is <span>a span element</span> which is
value from its parent element. blue, as span elements are set to be. </div>
The inherit keyword can be used for any CSS property, and on any <div class="extra" style="color:green">
HTML element.
Here is <span>a span element</span> which is green,
<!DOCTYPE html> because it inherits from its parent.</div>
<html> <head> <style>
<div style="color:red"> Here is <span>a span
span {
element</span>
color: blue; border: 1px solid black; }
which is blue, as span elements are set to be. </div>
.extra span {
color: inherit;} </body ></html>
</style> </head>
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE
Positioning
The CSS positioning properties allow you to position an element. It can
also place an element behind another, and specify what should happen
when an element's content is too big.
Elements can be positioned using the top, bottom, left, and right
properties. However, these properties will not work unless the position
property is set first. They also work differently depending on the
positioning method.
There are four different positioning methods.
1. Static
2. Fixed
3. Relative
4. Absolute
Fixed Positioning
Static Positioning An element with fixed position is positioned relative to the browser
HTML elements are positioned static by default. A static positioned window.
element is always positioned according to the normal flow of the page. It will not move even if the window is scrolled:
Static positioned elements are not affected by the top, bottom, left, and p.pos_fixed {
right properties. position:fixed;
top:30px;
right:5px;}
Note: IE7 and IE8 support the fixed value only if a !DOCTYPE is
specified.
Fixed positioned elements are removed from the normal flow. The
document and other elements behave like the fixed positioned element
does not exist.
Fixed positioned elements can overlap other elements.
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE
Relative Positioning
A relative positioned element is positioned relative to its normal
position.
h2.pos_left {
position:relative;
left:-20px; }
The content of relatively positioned elements can be moved and
overlap other elements, but the reserved space for the element is
still preserved in the normal flow.
h2.pos_top{
position:relative;
top:-50px;}
Relatively positioned elements are often used as container blocks
for absolutely positioned elements.
Dr. MAREESWARI V/ AP(Sr) / SITE Dr. MAREESWARI V/ AP(Sr) / SITE