UNIT - 4 Ip
UNIT - 4 Ip
Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to
simplify the process of making web pages presentable.
CSS handles the look and feel part of a web page. Using CSS, you can control the color of the
text, the style of fonts, the spacing between paragraphs, how columns are sized and laid out,
what background images or colors are used, layout designs,variations in display for different
devices and screen sizes as well as a variety of other effects.
CSS is easy to learn and understand but it provides powerful control over the presentation of
an HTML document. Most commonly, CSS is combined with the markup languages HTML or
XHTML.
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.
Cascading Style Sheet(CSS) is used to set the style in web pages that
contain HTML elements. It sets the background color, font-size, font-family,
color, … etc property of elements on a web page. There are three types of
CSS which are given below:
• Inline CSS
• Internal or Embedded CSS
• External CSS
Inline CSS: Inline CSS contains the CSS property in the body section
attached with element is known as inline CSS. This kind of style is specified
within an HTML tag using the style attribute.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Inline CSS</title>
</head>
<body>
<p style = "color:#009900; font-size:50px;
font-style:italic; text-align:center;">
GeeksForGeeks
</p>
</body>
</html>
External CSS: External CSS contains separate CSS file which contains only
style property with the help of tag attributes (For example class, id, heading,
… etc). CSS property written in a separate file with .css extension and should
be linked to the HTML document using link tag. This means that for each
element, style can be set only once and that will be applied across web pages.
Example: The file given below contains CSS property. This file save with
.css extension. For Ex: geeks.css
body {
background-color:powderblue;
} .main
{
text-align:center;
} .GFG {
color:#009900;
font-size:50px;
font-weight:bold;
} #geeks { font-
style:bold;
font-size:20px;
}
Below is the HTML file that is making use of the created external style sheet
• link tag is used to link the external style sheet with the html
webpage.
• href attribute is used to specify the location of the external style
sheet file.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="geeks.css"/>
</head>
<body>
<div class = "main">
<div class ="GFG">GeeksForGeeks</div>
<div id ="geeks">
A computer science portal for geeks
</div>
</div>
</body>
</html>
To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
<!DOCTYPE html>
<html>
<head>
<style> #para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
*output
Hello World!
To select elements with a specific class, write a period (.) character, followed
by the class name.
Example
In this example all HTML elements with class="center" will be red and center-
aligned:
<!DOCTYPE html>
<html>
<head>
<style> .center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
*output
CSS Background
CSS background property is used to define the background effects on element. There
are 5 CSS background properties that affects the HTML elements:
1. background-color
2. background-image
3. background-repeat
4. background-attachment
5. background-position
1) CSS background-color
The background-color property is used to specify the background color of the element.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. h2,p{
6. background-color: #b0d4de;
7. }
8. </style>
9. </head>
10. <body>
11. <h2>My first CSS page.</h2>
12. <p>Hello Javatpoint. This is an example of CSS background-color.</p>
13. </body>
14. </html>
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. background-image: url("paper1.gif");
7. margin-left:100px;
8. }
9. </style>
10. </head>
11. <body>
12. <h1>Hello Javatpoint.com</h1>
13. </body>
14. </html>
3) CSS background-repeat
By default, the background-image property repeats the background image
horizontally and vertically. Some images are repeated only horizontally or vertically.
background-repeat: repeat-x;
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. body {
6. background-image: url("gradient_bg.png");
7. background-repeat: repeat-x;
8. }
9. </style>
10. </head>
11. <body>
12. <h1>Hello Javatpoint.com</h1>
13. </body>
14. </html>
4) CSS 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. Let?s
take an example with fixed background image.
5) CSS 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.
1. center
2. top
3. bottom
4. left
5. right
background: white url('good-morning.jpg');
background-repeat: no-repeat; background-
attachment: fixed; background-position: center;
A CSS box model is a compartment that includes numerous assets, such as edge,
border, padding and material. It is used to develop the design and structure of a web
page. It can be used as a set of tools to personalize the layout of different components.
According to the CSS box model, the web browser supplies each element as a square
prism.
The CSS box model contains the different properties in CSS. These are listed
below.
o Border o
Margin o
Padding o
Content
Border Field
It is a region between the padding-box and the margin. Its proportions are determined
by the width and height of the boundary.
Margin Field
This segment consists of the area between the boundary and the edge of the border.
The proportion of the margin region is equal to the margin-box width and height. It is
better to separate the product from its neighbor nodes.
Padding Field
This field requires the padding of the component. In essence, this area is the space
around the subject area and inside the border-box. The height and the width of the
padding box decide its proportions.
Content Field
Material such as text, photographs, or other digital media is included in this area.
It is constrained by the information edge, and its proportions are dictated by the width
and height of the content enclosure.
The specific area that an element box may occupy on a web page is measured as
follows-
Example 1
Here, to explain the CSS box model, we have an instance.
Size of the box Properties of CSS
1. <!DOCTYPE html>
2. <head>
3. <title>CSS Box Model</title>
4. <style>
5. .main
6. {
7. font-size:30px;
8. font-weight:bold;
9. Text-align:center;
10. }
11. .gfg
12. {
13. margin-left:50px;
14. border:50px solid Purple;
15. width:300px;
16. height:200px;
17. text-align:center;
18. padding:50px;
19. }
20. .gfg1
21. {
22. font-size:40px;
23. font-weight:bold;
24. color:black;
25. margin-top:60px;
26. background-color:purple;
27. }
28. .gfg2
29. {
30. font-size:20px;
31. font-weight:bold;
32. background-color:white;
33. }
34. </style>
35. </head>
36. <body>
37. <div class = "main">CSS Box-Model Property</div>
38. <div class = "gfg">
39. <div class = "gfg1">JavaTpoint</div>
40. <div class = "gfg2">A best portal for learn Technologies</div>
41. </div>
42. </body>
43. </html>
Output
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).
• margin-top
• margin-right
• margin-bottom
• margin-left
• auto - the browser calculates the margin • length - specifies a margin in px, pt,
cm, etc.
• % - specifies a margin in % of the width of the containing element
• inherit - specifies that the margin should be inherited from the parent element
Example
Set different margins for all four sides of a <p> element:
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px; }
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).
• padding-top
• padding-right
• padding-bottom
• padding-left
• inherit - specifies that the padding should be inherited from the parent element
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px; }
Try it Yourself »
CSS Lists
There are various CSS properties that can be used to control lists. Lists can be classified
as ordered lists and unordered lists. In ordered lists, marking of the list items is with
alphabet and numbers, whereas in unordered lists, the list items are marked using
bullets.
We can style the lists using CSS. CSS list properties allow us to:
o Set the distance between the text and the marker in the list.
o Specify an image for the marker instead of using the number or bullet point. o
o Place the marker outside or inside the box that contains the list items. o Set the
o list-style-type: This property is responsible for controlling the appearance and shape of
the marker.
o list-style-image: It sets an image for the marker instead of the number or a bullet
point.
o marker-offset: It is used to specify the distance between the text and the marker. It is
unsupported in IE6 or Netscape 7.
https://www.javatpoint.com/css-lists
CSS Border
The CSS border is a shorthand property used to set the border on an element.
Value Description
o border-style
o border-color
o border-width
o border-radius
1) CSS border-style
The Border style property is used to specify the border type which you want to display
on the web page.
There are some border style values which are used with border-style property to define
a border.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. p.none {border-style: none;}
6. p.dotted {border-style: dotted;}
7. p.dashed {border-style: dashed;}
8. p.solid {border-style: solid;}
9. p.double {border-style: double;}
10. p.groove {border-style: groove;}
11. p.ridge {border-style: ridge;}
12. p.inset {border-style: inset;}
13. p.outset {border-style: outset;}
CSS border-width
The border-width property is used to set the border's width. It is set in pixels. You can
also use the one of the three pre-defined values, thin, medium or thick to set the width
of the border.
border-width: 5px;
Property Description
3) CSS border-color
There are three methods to set the color of the border.
o Name: It specifies the color name. For example: "red". o RGB: It specifies
the RGB value of the color. For example: "rgb(255,0,0)". o Hex: It specifies
the hex value of the color. For example: "#ff0000".
There is also a border color named "transparent". If the border color is not set it is
inherited from the color property of the element.
border-color: red;
This CSS
property includes the properties that are tabulated as follows:
rig
le