Module -3
Module -3
• CSS is a standard style sheet language used for describing the presentation of
the web pages.
• To control the presentation of multiple pages of a website with a single style sheet
• To style dynamic states of elements such as hover, focus, etc. that isn't possible
otherwise.
• To change the position of an element on a web page without changing the markup.
Uses of CSS
• To alter the display of existing HTML elements.
• Easy Maintenance
1. Inline styles — Using the style attribute in the HTML start tag.
3. External style sheets — Using the <link> element, pointing to an external CSS
file.
Inline Styles
• Inline styles are used to apply the unique style rules to an element by putting
the CSS rules directly into the start tag.
• The style attribute includes a series of CSS property and value pairs.
Inline Styles
Each "property: value" pair is separated by a semicolon (;)
Example:
• You can define any number of <style> elements in a HTML document but they
must appear between the <head> and </head> tags
Embedded (or) Internal Style Sheets
<html> </style>
<head>
</head>
<title>Embedded Style Sheet</title>
<style> <body>
body
<h1>This is a heading</h1>
{
background-color: YellowGreen; <p>This is a paragraph of text.</p>
}
</body>
p
{ </html>
color: #fff;
}
External Style Sheets
• An external style sheet is ideal when the style is applied to many pages of the
website.
• An external style sheet holds all the style rules in a separate document that you
can link from any HTML file on your site
• External style sheets are the most flexible because with an external style sheet,
you can change the look of an entire website by changing just one file
External Style Sheets
style1.css extcss.html
body <html>
{ <head>
background: lightyellow; <title>My HTML Document</title>
font: 18px Arial, sans-serif; <link rel="stylesheet" href="style1.css">
} </head>
h1 <body>
{ <h1>This is a heading</h1>
color: orange; <p>This is a paragraph of text.</p>
} </body>
</html>
CSS Syntax
• CSS rules have two main parts, a selector and one or more declarations:
• The declarations that appear in the block that follows the selector may be applied to all
elements of a specific type, or only those elements that match a certain attribute
• The declarations that appear in the block that follows the selector may be applied to all
elements of a specific type, or only those elements that match a certain attribute
2. Id Selectors
3. Class Selectors
Element Type Selector - Id Selectors -
Class Selectors
<head>
#error { <body>
<title>CSS selector</title>
color: #ff0000; <h1>This is heading</h1>
<style>
} <p>This is a paragraph.</p>
h1 {
.blue { <p id=“error">This is a id selector</p>
color: Yellow;
color: #0000ff; <p class="blue">This is a class selector</p>
}
} </body>
p{
</style> </html>
color: green;
</head>
}
Backgrounds
CSS background properties:
• background-color
• background-image
• background-repeat
• background-attachment
• background-position
• background-size
• background-clip
• background-origin
Backgrounds
Property Name Value
background-color red, blue
background-image url("gradient_bg.png")
background-repeat repeat| repeat-x | repeat-y | no-repeat
bacground-attachment scroll | fixed
left top | left center | left bottom | right top | right center | right
background-position
bottom | center top | center center | center bottom
Hex Code:
#RRGGBB
Example: color:#FF0000;
Decimal Code:
(R,G,B)
Example:color:rgb(255,0,0);
Thinking Inside the Box
• In CSS, the term "box model" is used when talking about design and layout.
• The CSS box model is essentially a box that wraps around every HTML element. It
consists of:
1. margins,
2. borders,
3. padding, and
• Margin
• Padding
• Content
Thinking Inside the Box
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.
Thinking Inside the Box
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.
Thinking Inside the Box
<html> <head> <style> <body>
div { <h2>Demonstrating the Box Model</h2>
background-color: lightgrey; <p>The CSS box model is essentially a box that wraps
width: 300px; around every HTML element.</p>
border: 15px solid green; <div>This text is the content of the box. We have added a
padding: 50px; 50px padding, 20px margin and a 15px green
margin: 20px; border.</div>
} </body>
</style> </head> </html>
Width and Height of an Element
<html> <head> <body>
<style> <h2>Calculate the total width:</h2>
div { <img src=“small.jpg" width="350" height="263" alt="Klematis">
width: 320px; <div>The picture above is 350px wide. The total width of this
padding: 10px; element is also 350px.</div>
border: 5px solid gray; </body>
margin: 0; </html>
}
</style> </head>
Width and Height of an Element
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
Floating and Positioning
• The CSS float property is a positioning property.
• It is used to push an element to the left or right, allowing another element to wrap
around it. It is generally used with images and layouts.
Floating and Positioning
• Elements are floated only horizontally. So it is possible only to float elements left or
right, not up or down.
1. A floated element may be moved as far to the left or the right as possible. Simply, it
means that a floated element can display at extreme left or extreme right.
2. The elements after the floating element will flow around it.
4. If the image floated to the right, the texts flow around it, to the left and if the image
floated to the left, the text flows around it, to the right.
Floating and Positioning
Property Description Values
1. static
2. relative
3. fixed
4. absolute
5. sticky
Positioning
• Elements are then positioned using the top, bottom, left, and right properties.
• However, these properties will not work unless the position property is set first.
• Setting the top, right, bottom, and left properties of a relatively-positioned element
will cause it to be adjusted away from its normal position.
• Other content will not be adjusted to fit into any gap left by the element.
Position: relative
<html>
<body>
<head>
<h2>position: relative;</h2>
<style>
<p>An element with position: relative; is positioned
div.relative {
relative to its normal position:</p>
position: relative;
<div class="relative">
left: 30px;
This div element has position: relative;
border: 3px solid #73AD21;
</div>
}
</body>
</style>
</html>
</head>
Position: fixed
• An element with position: fixed; is positioned relative to the viewport, which means it
always stays in the same place even if the page is scrolled.
• The top, right, bottom, and left properties are used to position the element.
• A fixed element does not leave a gap in the page where it would normally have been
located.
Position: fixed
<html> <head>
<body>
<style>
<h2>position: fixed;</h2>
div.fixed {
<p>An element with position: fixed; is positioned relative
position: fixed;
to the viewport.</p>
bottom: 0;
<div class="fixed">
right: 0;
This div element has position: fixed;
width: 300px;
</div>
border: 3px solid #73AD21;
</body>
}
</html>
</style> </head>
Position: absolute
• An element with position: absolute; is positioned relative to the nearest positioned
ancestor (instead of positioned relative to the viewport, like fixed).
• A sticky element toggles between relative and fixed, depending on the scroll position.
• It is positioned relative until a given offset position is met in the viewport - then it
"sticks" in place (like position:fixed).
Position: sticky
<html> <head> <style>
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
padding: 5px;
background-color: #cae8ca;
border: 2px solid #4CAF50;
}
</style> </head>
Position: sticky
<body>
<p>Try to <b>scroll</b> inside this frame to understand how sticky positioning works.</p>
<div class="sticky">I am sticky!</div>
<div style="padding-bottom:2000px">
<p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach
its scroll position.</p>
<p>Scroll back up to remove the stickyness.</p>
</div>
</body> </html>