Professional Documents
Culture Documents
ENGINEERING COLLEGE
UNIT – 3
CSS
UNIT 3 - CSS
SYNTAX
• <html tag
style="cssproperty1:value;cssproperty2:value;”></htmltag>
INLINE STYLE
<!DOCTYPE html>
<html>
<body>
<h1 style=" color:blue; text-align:center;">This is a heading</h1>
<p style=" color:red;">This is a paragraph</p>
</body>
</html>
1.html
INTERNAL STYLE
• An internal style sheet may be used if one single HTML page has a unique
style.
• The internal style is defined inside the <element>, inside the head section.
<!DOCTYPE html>
<html> </head>
<head> <body>
<h1>This is a heading</h1>
<style> <p>This is a paragraph.</p>
body{ </body>
background-color:linen; </html>
}
h1{
color:maroon;
margin-left:40px;
}
</style> 2.html
EXTERNAL STYLE
• With an external style sheet, you can change the look of an entire website
by changing just one file.
• Each HTML page must include a reference to the external style sheet file
inside the <link> element, inside the head section.
<html>
<head>
<link rel="stylesheet” href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph</p>
</body>
</html> 3.html
EXTERNAL STYLE
• An external style sheet can be written in any text editor, and
must be saved with a .css extension.
• The external .css file should not contain any HTML tags.
body
{
background-color:lightblue;
}
h1
{
color:navy;
margin-left:20px;
}
CONFLICTING STYLES
• If there are two or more CSS rules that point to the same element, the selector
with the highest specificity value will “win” and its style declaration will be applied
to that HTML element.
• In this example, we have used the “p” element as selector, and specified a red
color for this element. The text will be red:
<!DOCTYPE html>
<html>
<head>
<style>
p{color:red;}
</style>
</head>
<body>
<p>Welcome Friends!</p>
</body> 4.html
</html>
CONFLICTING STYLES
• In this example, we have added a class selector (named “test”), and specified a
green color for this class The text will now be green (even though we have
specified a red color for the element selector “p”). This is because the class
selector is given higher priority.
<!DOCTYPEhtml>
<html>
<head>
<style>
.test{color:green;}
p{color:red;}
</style>
</head>
<body>
<p class="test">Welcome Friends!!</p>
</body>
</html> 5.html
CONFLICTING STYLES
• In this example, we have added the id selector (named “demo”). The text will
now be blue, because the id selector is given higher priority.
<html>
<head>
<style>
#demo{color:blue;}
.test{color:green;}
p{color:red;}
</style>
</head>
<body>
<p id="demo“ class="test">Welcome Friends!!</p>
</body>
</html> 6.html
CONFLICTING STYLES
• In this example, we have added an inline style for the “p” element. Now,
The text will be pink, because the inline style is given the highest priority.
<html>
<head>
<style>
#demo{color:blue;}
.test{color:green;}
p{color:red;}
</style>
</head>
<body>
<p id=“demo” class=“test” style=“color:pink;”>Welcome Friends!!</p>
</body>
</html> 7.html
LINKING EXTERNAL STYLE SHEETS
Create the Style Sheet
• Type CSS code into a plain text file, and save with a .css extension (for example, styles.css)
• Here’s an example of some CSS code.
body
{
background-color:darkslategrey;
color:azure;
font-size:1.1em;
}
h1
{
color:coral;
}
#intro
{
font-size: 1.3em;
}
.colorful
{
color:orange;
}
LINKING EXTERNAL STYLE SHEETS
Link to the Style Sheet from the HTML Documents:
<html>
<head>
<title>My Example</title>
<link rel ="stylesheet" href ="styles.css">
</head>
<body>
<h1>External Styles</h1>
<p id="intro">Allow you to define styles for the whole website.</p>
<p class="colorful">This has a style applied via a class.</p>
</body>
</html> 8.html
POSITIONING ELEMENTS
• The CSS position property is used to set position for an element it is also used to
place an element behind another and also useful for scripted animation effect.
• You can position an element using the top, bottom, left and right properties.
• These properties can be used only after position property is set first.
• A position element’s computed position property is relative, absolute, fixed or
sticky.
a) Fixed
b) Static
c) Relative
d) Absolute
e) Sticky
CSS Static Positioning
• This is a by default position for HTML elements It always positions an
element according to the normal flow of the page It is not affected by the
top, bottom, left and right properties.
<html>
<head>
<style>
div.static
{
position: static;
border:3px solid #73AD21;
}
</style>
</head>
CSS Static Positioning
<body>
<h2>position:static;</h2>
<p>An element with position static is not positioned in any special way it is
always positioned according to the normal flow of the page:</p>
<div class="static">
This div element has position static
</div>
</body>
</html>
9.html
CSS Fixed Positioning
• The fixed positioning property helps to put the text fixed on the browser This fixed test is
positioned relative to the browser window, and doesn't move even you scroll the window.
<html>
<head>
<style>
div.fixed{
position:fixed;
bottom:0;
right:0;
width:300px;
border:3px solid #73AD21;
}
</style>
</head>
CSS Fixed Positioning
<body>
<h2>position:fixed;</h2>
<p>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:</p>
<div class="fixed">
This div element has position fixed
</div>
</body>
</html> 10.html
CSS Relative Positioning
• An element with position relative is positioned relative to its normal position
• 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.
<html>
<head>
<style>
h2.pos_left
{
position:relative;
left:-10px;
}
h2.pos_right
{
position:relative;
left:30px;
}
</style>
</head>
<body>
<h2>This is a heading with no position</h2>
<h2 class=" pos_left">This heading is positioned left according to its normal
position</h2>
<h2 class=" pos_right">This heading is positioned right according to its
normal position</h2>
</body>
</html> 11.html
CSS Absolute Positioning
• The absolute positioning is used to position an element relative to the first parent element that
has a position other than static. If no such element is found, the containing block is HTML.
• With the absolute positioning, you can place an element anywhere on a page.
<html>
<head><style>
h2{
position:absolute;
left:150px;
top:250px;
}</style></head>
<body>
<h2>This heading has an absolute position</h2>
<p>The heading below is placed 150 px from the left and 250 px from the top of the page</p>
</body></html> 12.html
CSS Sticky Positioning
• An element with position sticky is positioned based on the user's
scroll position.
• 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).
Note: Internet Explorer does not support sticky positioning. Safari
requires a –webkit-prefix (see example below).
You must also specify at least one of top, right, bottom or left for
sticky positioning to work.
BACKGROUNDS
• The CSS background properties are used to define the background
effects for elements.
• There are lots of properties to design the background.
• CSS background properties are as follows:
- CSS Background-color Property
- CSS Background-image Property
- CSS Background-repeat Property
- CSS Background-attachment Property
- CSS Background-position Property
- CSS Background-origin Property
- CSS Background clip Property
CSS Background-color Property
• This property specifies the background color of an element.
• A color name can also be given as: “green”, a HEX value as “#5570f0”
an RGB value as “rgb(25, 255, 2)”.
Syntax:
body
{
background-color:color name;
}
CSS Background-color Property
<html>
<head>
<style>
h1{
background-color: blue;
}
</style>
</head>
<body>
<h1>WELCOME TO ENGINEERING</h1>
</body>
</html> 14.html
CSS Background-image Property
• This property specify an image to use as the background of an element.
• By default, the image is repeated so it covers the entire element.
Syntax
body
{
background-image: link;
}
CSS Background-image Property
<html>
<head>
<style>
body{
background-image:url("https://tse4.mm.bing.net/th?
id=OIP.U0SfqHcCr4A3TEW4cIDGOQHaEI&pid=Api&P=0.jpg");
}
</style></head>
<body>
<h1>LOVE THE MOTHER NATURE</h1>
</body>
</html> 15.html
CSS Background repeat Property
• By default the background image property repeats the image both horizontally and
vertically.
Syntax:
To repeat an image horizontally
body{
background-image:link;
background-repeat:repeat:x;
}
body{
background-image:url("img_tree.gif");
background-repeat:no-repeat;
}
body{
background-image:url("paper.gif");
background-repeat:repeat;
}
CSS Background repeat Property
<html>
<head>
<style>
body
{
background-image: url("https://tse4.mm.bing.net/th?
id=OIP.U0SfqHcCr4A3TEW4cIDGOQHaEI&pid=Api&P=0.jpg");
background-repeat: repeat-y;
}
</style>
</head>
<body>
<h1>The background-repeat Property</h1>
<p>Here, the background image is repeated only vertically.</p>
</body>
</html> 16.html
CSS Background-attachment Property
• This property is used to fix the background ground image. The image will not scroll with the page.
<html> <p>The background-image is fixed.</p>
<head>
<p>The background-image is fixed.</p>
<style>
body { <p>The background-image is fixed.</p>
background-image: url("img_tree.gif"); <p>The background-image is fixed.</p>
background-repeat: no-repeat; <p>The background-image is fixed.</p>
background-attachment: fixed;
<p>The background-image is fixed.</p>
}
</style>
<p>The background-image is fixed.</p>
</head> <p>The background-image is fixed.</p>
<body> <p>The background-image is fixed.</p>
<h1>The background-attachment Property</h1> <p>The background-image is fixed.</p>
<p>The background-image is fixed.</p>
<p>The background-image is fixed.</p>
<p>The background-image is fixed.</p>
<p>The background-image is fixed.</p> </body>
<p>The background-image is fixed.</p> </html>
<p>The background-image is fixed.</p>
<p>The background-image is fixed.</p> 17.html
CSS Background-position Property
This property is used to set the image to a particular position.
<html>
<head>
<style>
#example1 {
border: 10px dashed black;
padding: 25px;
background: url("https://tse4.mm.bing.net/th?
id=OIP.U0SfqHcCr4A3TEW4cIDGOQHaEI&pid=Api&P=0.jpg");
background-repeat: no-repeat;
background-origin: padding-box;
}
CSS Background-origin Property
#example2 {
border: 10px dashed black;
padding: 25px;
background: url("https://tse4.mm.bing.net/th?id=OIP.U0SfqHcCr4A3TEW4cIDGOQHaEI&pid=Api&P=0.jpg");
background-repeat: no-repeat;
background-origin: border-box;
}
#example3 {
border: 10px dashed black;
padding: 25px;
background: url("https://tse4.mm.bing.net/th?id=OIP.U0SfqHcCr4A3TEW4cIDGOQHaEI&pid=Api&P=0.jpg");
background-repeat: no-repeat;
background-origin: content-box;
}
</style>
</head>
CSS Background-origin Property
<body>
<h2>background-origin: padding-box (default):</h2>
<div id="example1">
<h2>Hello World</h2>
<p>The background image starts from the upper left corner of the padding edge.</p>
</div>
<h2>background-origin: border-box:</h2>
<div id="example2">
<h2>Hello World</h2>
<p>The background image starts from the upper left corner of the border.</p>
</div>
<h2>background-origin: content-box:</h2>
<div id="example3">
<h2>Hello World</h2>
<p>The background image starts from the upper left corner of the content.</p>
</div>
</body>
</html> 19.html
CSS Background-clip Property
• The background clip property defines how far the background (color or image) should extend within an
element.
<html>
<head>
<style>
#example1 {
border: 10px dotted black;
padding: 15px;
background: lightblue;
background-clip: border-box;
}
#example2 {
border: 10px dotted black;
padding: 15px;
background: lightblue;
background-clip: padding-box;
}
CSS Background-clip Property
#example3 {
border: 10px dotted black;
padding: 15px;
background: lightblue;
background-clip: content-box;
}
</style>
</head>
<body>
<h1>The background-clip Property</h1>
<p>The background-clip property defines how far the background should extend within an
element.</p>
<p>background-clip: border-box (this is default):</p>
<div id="example1">
<p>The background extends behind the border.</p>
CSS Background-clip Property
</div>
<p>background-clip: padding-box:</p>
<div id="example2">
<p>The background extends to the inside edge of the border.</p>
</div>
<p>background-clip: content-box:</p>
<div id="example3">
<p>The background extends to the edge of the content box.</p>
</div>
</body>
</html>
20.html
ELEMENT DIMENSIONS
• The border that surrounds every box ie. element, the padding that can
appear inside each box and the margin that can go around them We have
the following properties that allow us to control the dimensions of a box.
Auto - This is default. The browser calculates the height and width
Length - Defines the height/width in px cm, etc
% - Defines the height/width in percent of the containing block
Initial - Sets the height/width to its default value
Inherit - The height/width will be inherited from its parent value
The Height and Width Properties
<html>
<head>
<style>
div{
height:200px;
width:50%;
background-color: powderblue;
}
</style>
</head>
<body>
<h2>Set the height and width of an element</h2>
<div>This div element has a height of 200px and a width of 50%</div>
</body>
</html> 21.html
Setting max-width
• The max width property is used to set the maximum width of an
element.
• The max-width can be specified in length values, like px cm, etc., or in
percent of the containing block, or set to none (this is default Means
that there is no maximum width).
• The problem with the <div> above occurs when the browser window is
smaller than the width of the element (500px).
• The browser then adds a horizontal scrollbar to the page.
• Using max-width instead, in this situation, will improve the browser's
handling of small windows.
Setting max-width
<html>
<head>
<style>
div{
max-width:500px;
height:100px;
background-color: powderblue;
}
</style>
</head>
<body>
<h2>Set the max width of an element</h2>
<div>This div element has a height of 100 px and a max width of 500px.</div>
<p>Resize the browser window to see the effect.</p>
</body>
</html> 22.html
BOX MODEL AND TEXT FLOW
</style>
</head>
<body>
<h2>Using individual padding properties</h2>
<div>This div element has a top padding of 50px, a right padding of 30px,
a bottom
padding of 50px, and a left padding of 80px</div>
</body>
</html>
25.html
CSS Margins
• Margins are used to create space around elements, outside of any defined
borders.
<html>
<head>
<style>
div{
border:1px solid black;
margin-top:100px;
margin-bottom:100px;
margin-right:150px;
margin-left:80px;
background-color: lightblue;
}
CSS Margins
</style>
</head>
<body>
<h2>Using individual margin properties</h2>
<div>This div element has a top margin of 100px, a right margin of 150px, a
bottom
margin of 100px, and a left margin of 80px</h2>
</body>
</html>
26.html
Note: Margins Shorthand Property
The auto value – margin: auto;
CSS Borders
• The CSS border properties allow you to specify the style, width, and
color of an element's border. The border-style property specifies what
kind of border to display.
CSS Syntax
• text-overflow: clip|ellipsis|string|initial|inherit;
CSS text-overflow Property
<html>
<head>
<style>
div.a {
white-space: nowrap;
width: 50px;
overflow: hidden;
text-overflow: clip;
border: 1px solid #000000;
}
div.b {
white-space: nowrap;
width: 50px;
overflow: hidden;
text-overflow: ellipsis;
border: 1px solid #000000;
}
CSS text-overflow Property
div.c {
white-space: nowrap;
width: 50px;
overflow: hidden;
text-overflow: "----";
border: 1px solid #000000;
}</style></head>
<body>
<h1>The text-overflow Property</h1>
<p>The following two divs contains a text that will not fit in the box.</p>
<h2>text-overflow: clip (default):</h2>
<div class="a">Hello world!</div>
<h2>text-overflow: ellipsis:</h2>
<div class="b">Hello world!</div>
<h2>text-overflow: "----" (user defined string):</h2>
<div class="c">Hello world!</div>
<p><strong>Note:</strong> The text-overflow: "<em>string</em>" only works in Firefox.</p>
</body></html> 30.html
Media Types and Media Queries
• The @media rule, introduced in CSS2, made it possible to define
different style rules for different media types.
• Examples: You could have one set of style rules for computer
screens, one for printers, one for handheld devices, one for
television-type devices, and so on.
• Unfortunately these media types never got a lot of support by
devices, other than the print media type.
• CSS3 Introduced Media Queries
• Media queries in CSS3 extended the CSS2 media types idea:
Instead of looking for a type of device, they look at the capability
of the device.
Media Types and Media Queries
• With the CSS border radius property, you can give any element
"rounded corners“.
CSS border-radius Property
• The CSS border radius property defines the radius of an element's
corners
• Tip: This property allows you to add rounded corners to elements!
• first value applies to top-left corner, second value applies
to top-right corner, third value applies to bottom-right
corner, and fourth value applies to bottom-left corner):
CSS Rounded Corners
<html> <body>
<head> <h2>border-radius: 25px:</h2>
<style> <div id="example1">
#example1 { <p>The border-radius property defines the radius of the element's
border: 2px solid red; corners.</p>
padding: 10px; </div>
border-radius: 25px; <h2>border-radius: 50px 20px:</h2>
} <div id="example2">
#example2 { <p>If two values are set;
border: 2px solid red; the first one is for the top-left and bottom-right corner, the second
padding: 10px; one for the top-right and bottom-left corner.</p>
border-radius: 50px 20px; </div>
} </body>
</style> </html>
</head> 36.html
CSS COLOR PROPERTY
• Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA,
HSLA values. CSS/HTML support 140 standard color names.
<html>
<body>
<h1 style="background-color:Tomato";>Tomato</h1>
<h1 style="background-color:Orange";>Orange</h1>
<h1 style="background-color:DodgerBlue";>DodgerBlue</h1>
<h1 style="background-color:MediumSeaGreen";>MediumSeaGreen</h1>
<h1 style="background-color:Gray";>Gray</h1>
<h1 style="background-color:SlateBlue";>SlateBlue </h1>
<h1 style="background-color:Violet";>Violet</h1>
<h1 style="background-color:LightGray";>LightGray</h1>
</body>
</html> 37.html
CSS COLOR PROPERTY
<html>
<body>
<h3 style="color:Tomato;">Hello World</h3>
<p style="color:DodgerBlue;">Let’s face it life is messy and complicated Yet, it's
the small slivers of happiness that make it all worth it, despite the unforeseen
situations and the roller coaster of emotions experienced daily</p>
<p style="color:MediumSeaGreen";> While there doesn’t seem to be a clear answer
on what the purpose of life is, most people can agree that through it all they only
hope to live with a warm heart and pure joy On the tough days when it’s hard to
find something to smile about, funny stories, heartwarming pictures, and happy
quotes can be helpful in finding the joy in life again</p>
</body>
</html> 38.html
CSS BORDER COLOR
<!DOCTYPE html>
<html>
<body>
<h1 style="border: 2px solid Tomato;">Hello World</h1>
<h1 style="border: 2px solid DodgerBlue;">Hello World</h1>
<h1 style="border: 2px solid Violet;">Hello World</h1>
</body>
</html>
39.html
CSS Box Shadows
Syntax
background-image: radial-gradient(shape size at position, start-
color,…,last color);
<html> <body>
<head> <h1>The transition Property</h1>
<style> <p>Hover over the div element below, to see the transition effect:</p>
div { <div></div>
width: 100px; </body>
height: 100px; </html>
background: red;
transition: width 2s;
}
div:hover {
width: 300px;
}
</style>
</head>
57.html
Specify the Speed Curve of the Transition
The transition-timing-function property specifies the speed curve of the transition effect.
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
border: 1px solid black;
}
CSS 3D TRANSFORM
#myDiv {
transform: rotateZ(90deg);
}
</style>
</head>
<body>
<h1>The rotateZ() Method</h1>
<p>The rotateZ() method rotates an element around its Z-axis at a given degree.</p>
<div>
This a normal div element.
</div>
<div id="myDiv">
This div element is rotated 90 degrees.
</div>
</body>
</html> 64.html
CSS Flexbox
• Flexbox Elements
- CSS Flex Container
Syntax
.flex-container
{
display: flex;
}
• The flex container properties are:
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content
CSS Flexbox
<html>
<head>
<style>
.flex-container {
display: flex;
flex-wrap: nowrap;
background-color: DodgerBlue;
}