Unit2 CSS
Unit2 CSS
CSS stands for Cascading Style Sheets. It is a style sheet language which is used to describe the look and
formatting of a document written in markup language. It provides an additional feature to HTML. It is generally used with
HTML to change the style of web pages and user interfaces.
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.
•Easy maintenance - To make a global change, simply change the style, and all elements in all the web pages will be
updated automatically.
•Superior styles to HTML - CSS has a much wider array of attributes than HTML, so you can give a far better look to
your HTML page in comparison to HTML attributes.
•Multiple Device Compatibility - Style sheets allow content to be optimized for more than one type of device. By using
the same HTML document, different versions of a website can be presented for handheld devices such as PDAs and cell
phones or for printing.
•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.
CSS Syntax
A CSS rule set contains a selector and a declaration block.
Property: A Property is a type of attribute of HTML element. It could be color, border etc.
Value: Values are assigned to CSS properties. In the above example, value "yellow" is assigned to color property.
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.
• CSS Element Selector
• CSS Id Selector
• CSS Class Selector
• CSS Universal Selector
• CSS Group Selector
1) CSS Element Selector : The element selector selects the HTML element by name.
<html>
<head>
<style>
p{
text-align: center; color: blue; }
</style>
</head>
<body>
<p>This style will be applied on every paragraph.</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.
<html>
<head>
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<p id="para1">Hello Class</p>
<p>This paragraph will not be affected.</p>
</body>
</html>
3) CSS Class Selector : The class selector selects HTML elements with a specific class attribute. It is used with a period
character . (full stop symbol) followed by the class name.
<html>
<head>
<style>
.center {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1 class="center">This heading is blue and center-aligned.</h1>
<p class="center">This paragraph is blue and center-aligned.</p>
</body>
</html>
<html>
<head>
<style>
*{
color: green;
font-size: 20px;
}
</style>
</head>
<body>
<h2>This is heading</h2>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
5) CSS Group Selector : The grouping selector is used to select all the elements with the same style definitions. It is
used to minimize the code. Commas are used to separate each selector in grouping.
<html>
<head>
<style>
h2, p {
text-align: center;
color: blue;
}
</style>
</head>
<body>
<h1>Hello Javatpoint.com</h1>
<h2>Hello Javatpoint.com (In smaller font)</h2>
<p>This is a paragraph.</p>
</body>
</html>
How to add CSS
CSS is added to HTML pages to format the document according to information in the style sheet. There are three ways to
insert CSS in HTML documents.
1) Inline CSS : Inline CSS is used to apply CSS on a single line or element.
<h2 style="color:red;margin-left:40px;">Inline CSS is applied on this heading.</h2>
<p>This paragraph is not affected.</p>
2) Internal CSS : The internal style sheet is used to add a unique style for a single document. It is defined in <head>
section of the HTML page inside the <style> tag.
<html>
<head>
<style>
h1 { color: red; margin-left: 80px;
}
</style>
</head>
<body>
<h1>The internal style sheet is applied on this heading.</h1>
<p>
</body>
</html>
3) External CSS : The external style sheet is generally used when you want to make changes on multiple pages. It is
ideal for this condition because it facilitates you to change the look of the entire web site by changing just one file.
It uses the <link> tag on every pages and the <link> tag should be put inside the head section.
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
CSS Comments
CSS comments are generally written to explain your code. It is very helpful for the users who reads your code so that
they can easily understand the code. Comments are ignored by browsers.
Comments are single or multiple lines statement and written within /*............*/ .
CSS BOX MODEL
It is a container that contains multiple properties including borders, margin, padding, and the content itself. It is used to
create the design and layout of web pages. It can be used as a toolkit for customizing the layout of different elements.
The web browser renders every element as a rectangular box according to the CSS box model.
1) background-color: The background-color property is used to specify the background color of the element.
<style>
h2,p{
background-color: #b0d4de;
}
</style>
2) background-image: The background-image property is used to set an image as a background of an element. By
default the image covers the entire element.
<style>
body {
background-image: url("paper1.gif");
margin-left:100px;
}
</style>
3) background-repeat: By default, the background-image property repeats the background image horizontally and
vertically. Some images are repeated only horizontally(repeat-x;) or vertically(repeat-y;).
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
4) background-attachment: The background-attachment property is used to specify if the background image is fixed or
scroll with the rest of the page in browser window. If you set fixed the background image then the image will not move
during scrolling in the browser.
background: white url('bb.gif');
background-repeat: no-repeat;
background-attachment: fixed;
5) background-position: The background-position property is used to define the initial position of the background
image. By default, the background image is placed on the top-left of the webpage. Position: Center, top, bottom, left,
right.
background: white url('good-morning.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
CSS Border
CSS border properties are use to specify the style, color and size of the border of an element. The CSS border properties are
given below:
1) border-style: is used to specify the border type which you want none It doesn't define any border.
to display on the web page. There are some border style values dotted It is used to define a dotted border.
which are used with border-style property to define a border.
<style> dashed It is used to define a dashed border.
p{
border-style: none; solid It is used to define a solid border.
border-style: dotted; double It defines two borders wIth the same border-
border-style: dashed; width value.
border-style: solid; groove It defines a 3d grooved border. effect is
border-style: double; generated according to border-color value.
border-style: groove;
border-style: ridge; ridge It defines a 3d ridged border. effect is
border-style: inset; generated according to border-color value.
border-style: outset;
} inset It defines a 3d inset border. effect is
</style> generated according to border-color value.
3) font-variant: is used to create a small-caps effect. Possible values are normal and small-caps.
<p style = "font-variant: small-caps;"> This text will be rendered as small caps </p>
2) direction: is used to set the text direction. Possible values are ltr or rtl.
<p style = "direction: rtl;"> This text will be rendered from right to left </p>
3) letter-spacing: is used to add or subtract space between the letters that make up a word.
<p style = "letter-spacing: 5px;"> This text is having space between letters. </p>
4) word-spacing: property is used to add or subtract space between the words of a sentence.
<p style = "word-spacing: 5px;"> This text is having space between words. </p>
6) text-align: property is used to align the text of a document. Value can be left, right, center.
<p style = "text-align: right;"> This will be right aligned. </p>
7) text-decoration: property is used to underline, overline, and strikethrough text. Possible values are none, underline,
overline, line-through, blink.
<p style = "text-decoration: underline;"> This will be underlined </p>
8) text-transform: property is used to capitalize text or convert text to uppercase or lowercase letters. Possible values
are none, capitalize, uppercase, lowercase.
<p style = "text-transform: capitalize;"> This will be capitalized </p>
CSS Padding
The CSS padding properties are used to generate space around an element's content, inside of any defined borders. With
CSS, you have full control over the padding. There are properties for setting the padding for each side of an element
(top, right, bottom, and left).
If the padding property has four values: padding: 25px 50px 75px 100px;
Then the order of value will be: Top, Right, Bottom and left respectively.
CSS Margins
The CSS margin properties are used to create space around elements, outside of any defined borders. With CSS, you
have full control over the margins. There are properties for setting the margin for each side of an element (top, right,
bottom, and left).
CSS has properties for specifying the margin for each side of an element i.e. margin-top, margin-right, margin-bottom,
and margin-left.
Example- p{
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
Margin Shorthand Property - margin: 25px 50px 75px 100px;
If there are two values – margin: 25px 50px; Then, top and bottom margins are 25px; right and left margins are 50px.
If there is only one value - margin: 25px; then all four margins are 25px.
CSS Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.). In addition, links can be styled
differently depending on what state they are in.
The four links states are:
• a:link - a normal, unvisited link
• a:visited - a link the user has visited
• a:hover - a link when the user mouses over it
• a:active - a link the moment it is clicked
Example –
a:link {
color: red;
}
a:visited {
color: green;
}
a:hover {
color: yellow;
}
a:active {
color: blue;
}
CSS Tables
1) border-collapse: specifies whether the browser should control the appearance of the adjacent borders that touch each
other or whether each cell should maintain its style. Possible values are collapse and separate.
table
{
border-collapse: collapse;
border-collapse: separate;
}
2) border-spacing: specifies the width that should appear between table cells.
table
{ border-spacing:10px 50px;
}
3) caption-side: captions are presented in the <caption> element. You use the caption-side property to control the
placement of the table caption. This property can have one of the four values top, bottom, left or right.
caption
{
caption-side: top;
}
CSS Images
Images play an important role in any webpage. CSS plays a good role to control image display. You can set the following
image properties using CSS.
1) border: property is used to set the width of an image border. This property can have a value in length or in %. A
width of zero pixel means no border.
<img style = "border:3px dashed red;“ src = "/css/images/logo.png" />