CSS (Cascading Style Sheet)
CSS (Cascading Style Sheet)
By : Parul Madan(CSE)
GEU
What is CSS?
Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to simplify
the process of making web pages presentable.
some of the key advantages of learning CSS:
• Create Stunning Web site - 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.
• Become a web designer - If you want to start a carrer as a professional web designer, HTML and
CSS designing is a must skill.
• Control web - 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.
• Learn other languages - Once you understands the basic of HTML and CSS then other related
technologies like javascript, php, or angular are become easier to understand.
Applications 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.
Prerequisites
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.
<body>
<p style = "color:#009900; font-size:50px;
font-style:italic; text-align:center;">
GRAPHIC ERA
</p>
</body>
</html>
Internal or Embedded CSS: This can be used when a single HTML document must be styled uniquely. The
CSS rule set should be within the HTML file in the head section i.e. the CSS is embedded within the HTML
file.
.GEU {
E.g. font-style: bold;
<!DOCTYPE html> font-size:20px;
<html> }
</style>
<head>
</head>
<title>Internal CSS</title>
</head>
<style> <body>
.main { <div class = "main">
text-align: center; <div class ="GFG">GEU</div>
}
.GFG { <div class =“GEU">
color:#009900; A computer science portal for geeks
</div>
font-size:50px;
</div>
font-weight: bold;
</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.
body { <!DOCTYPE html>
background- <html>
color:powderblue; <head>
} <link rel="stylesheet" href=“GEU.css"/>
.main { </head>
text-align:center;
<body>
} <div class = "main">
.GFG { <div class ="GFG">GeeksForGeeks</div>
color:#009900; <div id ="geeks">
font-size:50px; A computer science portal for geeks
font-weight:bold; </div>
} </div>
</body>
#geeks {
</html>
font-style:bold;
font-size:20px; • 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
Save this file with GEU.css sheet file.
Properties of CSS
Properties of CSS: Inline CSS has the highest priority, then comes Internal/Embedded
followed by External CSS which has the least priority. Multiple style sheets can be defined
on one page. If for an HTML tag, styles are defined in multiple style sheets then the below
order will be followed.
• As Inline has the highest priority, any styles that are defined in the internal and external
style sheets are overridden by Inline styles.
• Internal or Embedded stands second in the priority list and overrides the styles in the
external style sheet.
• External style sheets have the least priority. If there are no styles defined either in inline
or internal style sheet then external style sheet rules are applied for the HTML tags.
CSS Selectors
</body>
</html>
4. Universal Selectors
<html>
<head>
<style>
*{
color: blue;
}
</style>
</head>
<body>
</body>
</html>
5.Group selectors
<html>
<head>
h1, h2, h3 {
<style>
h1, h2, h3 {
color: #36C;
color: #36C;
font-weight: normal;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em; letter-spacing: .4em;
text-transform: lowercase;
} margin-bottom: 1em;
</style>
</head> text-transform: lowercase;
<body>
<h1 id="intro">Header 1</h1> }
<h2 id="intro">Header 1</h2>
<h3 id="intro">Header 1</h3>
<p>A paragraph.</p>
</body>
</html>
6.The Attribute Selectors
You can also apply styles to HTML elements with particular attributes. The style rule below will
match all the input elements having a type attribute with a value of text −
input[type = "text"] {
color: #000000;
}
The advantage to this method is that the <input type = "submit" /> element is unaffected, and the
color applied only to the desired text fields.
There are following rules applied to attribute selector.
• p[lang] − Selects all paragraph elements with a lang attribute.
• p[lang="fr"] − Selects all paragraph elements whose lang attribute has a value of exactly "fr".
• p[lang~="fr"] − Selects all paragraph elements whose lang attribute contains the word "fr".
• p[lang|="en"] − Selects all paragraph elements whose lang attribute contains values that are
exactly "en", or begin with "en-".
7. Descendant Selectors
Suppose you want to apply a style rule to a particular element only when it lies
inside a particular element. As given in the following example, style rule will apply
to <em> element only when it lies inside <ul> tag.
ul em {
color: #000000;
}
8. Child Selectors
You have seen the descendant selectors. There is one more type of selector, which is
very similar to descendants but have different functionality. Consider the following
example −
body > p {
color: #000000;
}
CSS Text Color
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
• CSS Border Color
<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>
• CSS Color Values
rgb(255, 99, 71)
#ff6347
hsl(9, 100%, 64%)
rgba(255, 99, 71, 0.5)
hsla(9, 100%, 64%, 0.5)
CSS Backgrounds
div {
background-color: lightblue;
}
p{
background-color: yellow;
}
Opacity / Transparency: It can take a value from 0.0 - 1.0
div {
background-color: green;
opacity: 0.3;
Background image
The background-image property specifies an image to use as the background of an element.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper.gif");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
CSS background-repeat
•background-color
•background-image
•background-repeat
•background-attachment
•background-position
CSS Border Properties
The CSS border properties allow you to specify the style, width, and color of an element's border.
CSS Border Style:
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: dotted;
border-width: 2px;
}
p.four {
border-style: dotted;
border-width: thick;
}
CSS Border Color
The border-color property is used to set the color of the four borders.
The color can be set by:
• name - specify a color name, like "red"
• HEX - specify a HEX value, like "#ff0000"
• RGB - specify a RGB value, like "rgb(255,0,0)"
• HSL - specify a HSL value, like "hsl(0, 100%, 50%)"
• Transparent
<style>
p.one
{
border-style: solid;
border-color: red green blue yellow; /* red top, green right, blue bottom and yellow left */
}
</style>
CSS Border - Shorthand Property
The CSS margin properties are used to create space around elements,
outside of any defined borders.
CSS has properties for specifying the margin for each side of an
element:
• margin-top
• margin-right
• margin-bottom
• margin-left
Margin - Shorthand Property
The margin property is a shorthand property for the following individual margin properties:
margin: 25px 50px 75px 100px;
• Top margin is 25px
• right margin is 50px
• bottom margin is 75px
• left margin is 100px
margin: 25px 50px 75px;
• top margin is 25px
• right and left margins are 50px
• bottom margin is 75px
margin: 25px 50px;top and bottom margins are 25px
• right and left margins are 50px
margin: 25px;
• all four margins are 25px
CSS Margin Collapse
• Top and bottom margins of elements are sometimes collapsed into a single margin
that is equal to the largest of the two margins.
• This does not happen on left and right margins. Only top and bottom margins.
h1 {
margin: 0 0 50px 0;
}
h2 {
margin: 20px 0 0 0;
}
common sense would seem to suggest that the vertical margin between the <h1> and
the <h2> would be a total of 70px (50px + 20px). But due to margin collapse, the actual
margin ends up being 50px.
CSS Padding
The CSS padding properties are used to generate space around an element's content, inside of any
defined borders.
CSS has properties for specifying the padding for each side of an element:
• padding-top
• padding-right
• padding-bottom
• padding-left
e.g.
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
CSS height and width Values
The height and width properties are used to set the height and width of an element.
The height and width properties do not include padding, borders, or margins. It sets the height/width
of the area inside the padding, border, and margin of the element.
The height and width properties may have the following values:
• 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
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
The CSS Box Model
All HTML elements can be considered as boxes. 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: margins, borders, padding, and the actual content. The image below illustrates the box model:
• Explanation of the different parts:
• Content - The content of the box, where text and images appear
• Padding - Clears an area around the content. The padding is transparent
• Border - A border that goes around the padding and content
• Margin - Clears an area outside the border. The margin is transparent
CSS Outline
An outline is a line that is drawn around elements, OUTSIDE the
borders, to make the element "stand out".
CSS has the following outline properties:
• outline-style
• outline-color
• outline-width
• outline-offset
• outline
CSS Outline Shorthand
The outline property is a shorthand property for setting the following individual
outline properties:
• outline-width
• outline-style (required)
• outline-color
CSS Outline Offset
The overflow property specifies whether to clip the content or to add scrollbars when the content of an element is too big to
fit in the specified area.
The overflow property has the following values:
• visible - Default. The overflow is not clipped. The content renders outside the element's box
• hidden - The overflow is clipped, and the rest of the content will be invisible
• scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content
• auto - Similar to scroll, but it adds scrollbars only when necessary
div {
width: 200px;
height: 50px;
background-color: #eee;
overflow: visible;//default is visible.
}