Lab 5 & 6 - CSS
Lab 5 & 6 - CSS
Lab 5 & 6 - CSS
Three ways of appliying CSS: As the heading says, there are three ways of applying CSS.
1. Inline
2. Internal
3. External
I don’t want to comment which one is better; it depends on your personal preference.
So you can use any one of them. But gradually, you will be able to figure out, which one
suits your style of coding.
In-line
Exercise 1:
Html Code without CSS Code with CSS
<HTML> <HTML>
<HEAD> <HEAD>
</HEAD>
</HEAD>
<BODY>
<BODY> <p style="color:red"> This is the first
<p > This is the first paragraph of this page, paragraph of this page, and it should
appear in red</p>
and it should appear in red</p>
</BODY> </BODY>
</HTML> </HTML>
Internal
Exercise 2: Starting with easiest one,
</BODY>
</HTML>
Let me explain, what I have done. I associated a “property: value (color: red)” pair with a
HTML Tag. But p inside the <style> block is known as selector, as explained above. This
also means, wherever in my page <p> tag is used, text will appear in red.
External
Exercise 3:
This one is quite different and is most popular among professionals. How it works, Write
the style in a file, and save the file with “.css” extension. Just look at the example below.
We have two files “pstyle.css” and “style_external.html”.
Below is pstyle.css
Note:
Using CSS this way is so popular among professionals. The advantage is to define all the
styles in one .css file. Include the css file in every web page of your web site and use
which ever style you want to use.
Exercise 4: CSS Selectors
CSS selectors are used to "find" (or select) HTML elements based on their element
name, id and class.
Selector Class
With Class Selector, you can define different type of ‘styles’ for the same ‘selector’. Say,
that you want to have two types of paragraphs in your document, one paragraph is left
aligned and another paragraph is right aligned. Here is how you can do this
<HTML>
<HEAD>
<style type="text/css">
p.class1
{
text-align:left;
}
p.class2
{
text-align:right;
}
</style>
</HEAD>
<BODY>
<p class="class1">This is first paragraph, Is the test left aligned</p>
<p class="class2">This is second paragraph, Is the test right aligned</p>
</BODY>
</HTML>
In <body> section,
<h1 class=”center”> It looks good in the center</h1>
<p class=”center”> This paragraph is center aligned</p>
</BODY>
</HTML>
And the output is as follows
Exercise 5:
Like Class Selector, there is one more Selector, ID Selector. It is very much similar to
Class Selector with a slight difference, even syntax is almost same.
h1#myheader
{
Background: green
}
So, you got the difference, the difference is, “#” instead of “.” And now, “myheader” is
known as ID.
Take an example
<HTML>
<HEAD>
<style type="text/css">
h1#myheader
{
color: blue;
background: green
}
</style>
</HEAD>
<BODY>
<h1 id="myheader"> This is h1 header </h1>
</BODY>
</HTML>
The output is
This is h1 header
If you have elements with the same style definitions, like this:
h1 { text-align:center;
color: red;
}
h2 { text-align: center;
color: red;
}
p { text-align: center;
color: red;
}
It will be better to group the selectors, to minimize the code. To group selectors,
separate each selector with a comma.
In the example below we have grouped the selectors from the code above:
Comments are used to explain the code, and may help when you edit the source code at
a later date. Comments are ignored by browsers.
A CSS comment starts with /* and ends with */. Comments can also span multiple lines:
Example
p { color: red;
/* This is a single-line comment */
text-align: center; }
/* This is a multi-line comment */ L
Cascading Order
What style will be used when there is more than one style specified for an HTML
element?
Generally speaking we can say that all the styles will "cascade" into a new "virtual" style
sheet by the following rules, where number one has the highest priority:
3. Browser default
So, an inline style (inside a specific HTML element) has the highest priority, which means
that it will override a style defined inside the <head> tag, or in an external style sheet, or
a browser default value.
Text Properties:
color: blue;
text-align: center/left/rigth/justify;
text-decoration: overline/line-through/underline;
text-transform: uppercase/lowercase/capitalize;
text-indent: 10px/-10px;
letter-spacing: 5px/-5px;
line-height: 0.8;
direction: rtl;
word-spacing: 10px;
text-shadow: 3px 2px red;
Exercise: Coding for All Text Properties:
<html>
<head>
<style type="text/css">
p.color {
color: blue;
}
p.align {
text-align: right;
}
p.decoration {
text-decoration: line-through;
}
p.indent {
text-indent: 50px;
}
p.lspace {
letter-spacing: 5px;
}
p.shadow {
text-shadow: 3px 2px red;
}
p.height {
line-height: 5.5;
}
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
</style>
</head>
<body>
<p class="color">This is some text in blue color.</p>
<p class="align">This is some text aligned right.</p>
<p class="decoration">This is some text line through</p>
<p class="indent">This is some text with 50px indent</p>
<p class="lspace">This is some text with line spacing</p>
<p class="shadow">This is some shadow text</p>
<p class="height">This is some text with line height</p>
<p class="uppercase">This is some uppercase text.</p>
<p class="lowercase">This is some lowercase text.</p>
<p class="capitalize">This is some capitalized text.</p>
</body>
</html>
Exercise 6:
Font Properties:
1. font-family: arial;
2. font-style: normal/italic/oblique;
3. font-size: 30px;
4. font-weight: normal/bold;
5. font-variant: normal/small-caps;
<!DOCTYPE >
<html>
<head>
<style>
p.fmly {
font-family: arial;
}
p.fmly1 {
font-family: symbol;
}
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
h1 {
font-size: 40px;
}
h2 {
font-size: 30px;
}
p{
font-size: 14px;
}
p.normal {
font-weight: normal;
}
p.light {
font-weight: lighter;
}
p.thick {
font-weight: bold;
}
p.thicker {
font-weight: 900;
}
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
}
</style>
</head>
<body>
<h1>CSS font-family</h1>
<p class="fmly">This is a paragraph</p>
<p class="fmly1">This is a paragraph</p>
<p class="normal">This is a paragraph in normal style.</p>
<p class="italic">This is a paragraph in italic style.</p>
<p class="oblique">This is a paragraph in oblique style.</p>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p class="normal">This is a paragraph.</p>
<p class="light">This is a paragraph.</p>
<p class="thick">This is a paragraph.</p>
<p class="thicker">This is a paragraph.</p>
<p class="normal">this is normal text</p>
<p class="small">Capitalised Small text.</p>
</body>
</html>
Exercise 7: The Box Model
Margin, padding and border, they all work together. In the center you have the element,
surrounding it is padding, and surrounding padding is margin box. Margin box is
surrounded by border box.
Let us see how to use borders.
1. border-style, the possible values are solid, dashed, dotted, double, groove, ridge,
inset and outset.
<HTML>
<HEAD>
<style type="text/css">
h2 {
border-style: solid;
border-width: 3px;
border-left-width: 20px;
border-right-width: 10px;
border-top-width: 50px;
border-bottom-width:100px;
border-color: red;
}
</style>
</HEAD>
<BODY>
<h2> Hey margin is 5em, and <br> padding is 10em</h2>
</BODY>
</HTML>
background-color: red;
background-image: url("image_path");
background-repeat: repeat-x/repeat-y/round/no-repeat;
background-attachment: fixed/scroll/local;
background-size: 20px 30px;
background-position: right top/right bottom/left top/left bottom;
or 30px 20px;
Margin Properties:
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
Link Properties:
a:link - unvisited link
a:hover – when mouse is on it
a:active – when mouse click on it
a:visited – after mouse clicked
Example:
<!DOCTYPE >
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
}
/* mouse over link */
a:hover {
color: green;
text-decoration:none;
}
/* selected link */
a:active {
color: blue;
}
/* visited link */
a:visited {
color: brown;
}
</style>
</head>
<body>
<a href="text_css.html" target="_blank">This is a link</a>
</body>
</html>
Exercise 8: Layouts:
HTML Layout Elements
Try Out:
<!DOCTYPE>
<html>
<head>
<style>
body {
height: 100%;
}
div.container {
width:100%;
border: 1px solid red;
}
header, footer {
padding: 1em;
color: white;
background-color: black;
text-align: center;
}
nav {
float: left;
max-width: 10px;
margin: 0;
padding: 1em;
}
nav ul {
list-style-type: none;
padding: 0;
}
nav ul a {
text-decoration: none;
}
article {
margin-left: 170px;
border-left: 1px solid gray;
padding: 1em;
overflow: hidden;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>LBRCE, Mylavaram</h1>
</header>
<nav>
<ul>
<li><a href="layout.html" target="blank">CSE</a></li>
<li><a href="#">ECE</a></li>
<li><a href="#">EEE</a></li>
</ul>
</nav>
<article id="content">
<h1>CSE</h1>
<p>welcome to cse dept.</p>
<p>this is located at block1
</p>
</article>
</div>
</body>
</html>
Output:
Exercise 9: More Border Properties
Let us take an example.
<HTML>
<HEAD>
<style type="text/css">
body
{
color: yellow;
background-color:black;
font-family: "Times New Roman";
font-size: 14;
}
a
{
text-decoration: line-through;
font-family: "Arial";
text-transform:uppercase;
font-size: 20;
}
</style>
</HEAD>
<BODY>
The back ground of this page is black and text color is yellow <br>
My name is <a> Harshit </a>
</BODY>
</HTML>
Exercise 10:
The attribute “letter-spacing” is used for spacing between letters. The value of
the attribute is length in %, or em, or pt.
The attribute “word-spacing” is used for spacing between words. The value of
the attribute is length in %, or em, or pt.
The attribute “text-align” is used for aligning the text. The possible value for the
attribute are center, left, right, justify.
The attribute “line-height” sets the height of the lines in a paragraph.
<HTML>
<HEAD>
<style type="text/css">
body
{
color: yellow;
background-color:black;
font-family: "Times New Roman";
font-size: 14;
}
a
{
text-decoration: line-through;
font-family: "Arial";
text-transform: uppercase;
font-size: 20;
}
p
{
letter-spacing: 0.3em;
word-spacing: 2em;
line-height: 1.5em;
text-align: left;
}
</style>
</HEAD>
<BODY>
The back ground of this page is black and text color is yellow <br>
My name is <a> Ahmed </a>
<p>
This is my first paragraph, <br> trying to notice How much is <br>letter
spacing which is 0.5 em <br> word spacing 2m<br>line height 1.5em <br>text
alignment, you tell me
</p>
</BODY>
</HTML>
Exercise 11:
</style>
</HEAD>
<BODY>
<h2> Hey margin is 5em, and <br> padding is 10em</h2>
</BODY>
</HTML>
In the output, the distance between the text and where the pink box end, is the
padding.
The distance between any boundaries of the browser to the point where the pink box
starts.