Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 71

CASCADING STYLE SHEET

css
• It is markup language used in web document
for Presentation Purpose.
• Primary intention is to separate web content
and the web Presentation.
• Various elements such as text, font and color
are used in CSS for presentation.
FEATURES OF CSS
• Separation between the content and presentation.
• Allows the user to decide the style of Presentation.
Thus the presentation can be made persistent.
• It allows the developer to give the consistent
appearance.
• If we use single style sheet for all the pages of the
website then all the pages will have a consistent look
and feel .
• It provides Precise control over font size, color,
background color and so on.
CORE SYNTAX
• The selector points to the HTML element you
want to style.
• The declaration block contains one or more
declarations separated by semicolons.
• Each declaration includes a CSS property name
and a value, separated by a colon.
• We can define each selector with the help of
property value .
• There are 7 categories of properties:
• Fonts
• List
• Alignment of text
• Color
• Margins backgrounds
• Borders
• backgrounds
CSS SELECTORS

• We can divide CSS selectors into five categories:


• Simple selectors (select elements based on name, id, class)
• Combinator selectors (select elements based on a specific
relationship between them)
• Pseudo-class selectors (select elements based on a certain
state)
• Pseudo-elements selectors (select and style a part of an
element)
• Attribute selectors (select elements based on an attribute
or attribute value)
CSS element Selector

• The element selector selects HTML elements based on the element name.
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p{
• text-align: center;
• color: red;
• }
• </style>
• </head>
• <body>
• <p>Every paragraph will be affected by the style.</p>
• <p id="para1">Me too!</p>
• <p>And me!</p>
• </body>
• </html>
OUTPUT
CSS id Selector

• The id selector uses the id attribute of an


HTML element to select a specific element.
• The id of an element is unique within a page,
so the id selector is used to select one unique
element!
• 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>
• <p id="para1">Hello World!</p>
• <p>This paragraph is not affected by the style.</p>
• </body>
• </html>
OUTPUT
 CSS class Selector
• The class selector selects HTML elements with
a specific class attribute.
• To select elements with a specific class, write a
period (.) character, followed by the class
name
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• .center {
• text-align: center;
• color: red;
• }
• </style>
• </head>
• <body>
• <h1 class="center">Red and center-aligned heading</h1>
• <p class="center">Red and center-aligned paragraph.</p>
• </body>
• </html>
OUPUT
<p> elements with class="center" will be red
and center-aligned
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p.center {
• text-align: center;
• color: red;
• }
• </style>
• </head>
• <body>

• <h1 class="center">This heading will not be affected</h1>


• <p class="center">This paragraph will be red and center-aligned.</p>

• </body>
• </html>
OUTPUT
HTML elements can also refer to more than
one class.
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p.center {
• text-align: center;
• color: red; \\Different styles to same element
• }
• p.large {
• font-size: 300%;
• }
• </style>
• </head>
• <body>
• <h1 class="center">This heading will not be affected</h1>
• <p class="center">This paragraph will be red and center-aligned.</p>
• <p class="center large">This paragraph will be red, center-aligned, and in a large font-size.</p>
• </body>
• </html>
OUTPUT
CSS Universal Selector

• The universal selector (*) selects all HTML elements on the page.
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• *{
• text-align: center;
• color: blue;
• }
• </style>
• </head>
• <body>
• <h1>Hello world!</h1>
• <p>Every element on the page will be affected by the style.</p>
• <p id="para1">Me too!</p>
• <p>And me!</p>
• </body>
• </html>
OUTPUT
CSS Grouping Selector

• <!DOCTYPE html>
• <html>
• <head>
• <style>
• h1, h2, p {
• text-align: center;
• color: red;
• }
• </style>
• </head>
• <body>
• <h1>Hello World!</h1>
• <h2>Smaller heading!</h2>
• <p>This is a paragraph.</p>
• </body>
• </html>
OUTPUT
STYLE SHEETS AND HTML
The information content of any web document can be represented with the help
of CSS.

There are three ways of inserting a style sheet:


• External CSS
• Internal CSS / EMBEDDED STYLE SHEET
• Inline CSS
External CSS

• 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.
Example

• External styles are defined within the <link> element, inside the <head> section of an
HTML page:
• <!DOCTYPE html>
• <html>
• <head>
• <link rel="stylesheet" href="mystyle.css">
• </head>
• <body>

• <h1>This is a heading</h1>
• <p>This is a paragraph.</p>

• </body>
• </html>
• 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.
• Here is how the "mystyle.css" file looks:
"mystyle.css"
body {
  background-color: lightblue;
}

h1 {
  color: navy;
  margin-left: 20px;
}
Internal CSS

• An internal style sheet may be used if one


single HTML page has a unique style.
• The internal style is defined inside the <style>
element, inside the head section.
example
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• body {
• background-color: linen;
• }
• h1 {
• color: maroon;
• margin-left: 40px;
• }
• </style>
• </head>
• <body>

• <h1>This is a heading</h1>
• <p>This is a paragraph.</p>

• </body>
• </html>
output
Inline CSS

• An inline style may be used to apply a unique


style for a single element.
• To use inline styles, add the style attribute to
the relevant element. The style attribute can
contain any CSS property.
example
• <!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>
output
CSS TEXT PROPERTIES

Text Color
• The color property is used to set the color of
the text. The color is specified by:
• a color name - like "red"
• a HEX value - like "#ff0000"
• an RGB value - like "rgb(255,0,0)"
Example
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• body {
• color: blue;
• }
• h1 {
• color: green;
• }
• </style>
• </head>
• <body>

• <h1>This is heading 1</h1>


• <p>This is an ordinary paragraph. Notice that this text is blue. The default text color for a page is defined in
the body selector.</p>
• <p>Another paragraph.</p>
• </body>
• </html>
output
T ext Co lo r an d B ack gro un d C olo r

• <!DOCTYPE html>
• <html>
• <head>
• <style>
• body {
• background-color: lightgrey;
• color: blue;
• }

• h1 {
• background-color: black;
• color: white;
• }

• div {
• background-color: blue;
• color: white;
• }
• </style>
• </head>
• <body>

• <h1>This is a Heading</h1>
• <p>This page has a grey background color and a blue text.</p>
• <div>This is a div.</div>

• </body>
• </html>
output
CSS Text Alignment
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• h1 {
• text-align: center;
• }

• h2 {
• text-align: left;
• }

• h3 {
• text-align: right;
• }
• </style>
• </head>
• <body>

• <h1>Heading 1 (center)</h1>
• <h2>Heading 2 (left)</h2>
• <h3>Heading 3 (right)</h3>

• <p>The three headings above are aligned center, left and right.</p>

• </body>
• </html>
output
CSS Text Decoration
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• h1 {
• text-decoration: overline;
•}
• h2 {
• text-decoration: line-through;
•}
• h3 {
• text-decoration: underline;
•}
• p.ex {
• text-decoration: overline underline;
•}
• </style>
• </head>
• <body>
• <h1>Overline text decoration</h1>
• <h2>Line-through text decoration</h2>
• <h3>Underline text decoration</h3>
• <p class="ex">Overline and underline text decoration.</p>
• <p><strong>Note:</strong> It is not recommended to underline text that is not a link, as this often confuses
• the reader.</p>
</body>
• </html>
output
Text spacing
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p.one {
• word-spacing: 10px;
• }

• p.two {
• word-spacing: -2px;
• }
• </style>
• </head>
• <body>

• <h1>Using word-spacing</h1>

• <p>This is a paragraph with normal word spacing.</p>

• <p class="one">This is a paragraph with larger word spacing.</p>

• <p class="two">This is a paragraph with smaller word spacing.</p>

• </body>
• </html>
output
Text shadow
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• h1 {
• text-shadow: 2px 2px red;
• }
• </style>
• </head>
• <body>

• <h1>Text-shadow effect!</h1>

• </body>
• </html>
output
Text Transformation

• The text-transform property is used to specify


uppercase and lowercase letters in a text.
• It can be used to turn everything into
uppercase or lowercase letters, or capitalize
the first letter of each word
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p.uppercase {
• text-transform: uppercase;
• }

• p.lowercase {
• text-transform: lowercase;
• }

• p.capitalize {
• text-transform: capitalize;
• }
• </style>
• </head>
• <body>

• <h1>Using the text-transform property</h1>

• <p class="uppercase">This text is transformed to uppercase.</p>


• <p class="lowercase">This text is transformed to lowercase.</p>
• <p class="capitalize">This text is capitalized.</p>

• </body>
• </html>
OUTPUT
Letter Spacing

• The letter-spacing property is used to specify


the space between the characters in a text.
• The following example demonstrates how to
increase or decrease the space between
characters:
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• h2 {
• letter-spacing: 5px;
• }

• h3 {
• letter-spacing: -2px;
• }
• </style>
• </head>
• <body>

• <h1>Using letter-spacing</h1>

• <h2>This is heading 1</h2>


• <h3>This is heading 2</h3>

• </body>
• </html>
OUTPUT
CSS BORDERS

• The CSS border properties allow you to specify


the style, width, and color of an element's
border.
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p.dotted {border-style: dotted;}
• p.dashed {border-style: dashed;}
• p.solid {border-style: solid;}
• p.double {border-style: double;}
• p.groove {border-style: groove;}
• p.ridge {border-style: ridge;}
• p.inset {border-style: inset;}
• p.outset {border-style: outset;}
• p.none {border-style: none;}
• p.hidden {border-style: hidden;}
• p.mix {border-style: dotted dashed solid double;}
• </style>
• </head>
• <body>

• <h2>The border-style Property</h2>


• <p>This property specifies what kind of border to display:</p>

• <p class="dotted">A dotted border.</p>


• <p class="dashed">A dashed border.</p>
• <p class="solid">A solid border.</p>
• <p class="double">A double border.</p>
• <p class="groove">A groove border.</p>
• <p class="ridge">A ridge border.</p>
• <p class="inset">An inset border.</p>
• <p class="outset">An outset border.</p>
• <p class="none">No border.</p>
• <p class="hidden">A hidden border.</p>
• <p class="mix">A mixed border.</p>

• </body>
• </html>
OUTPUT
CSS Outline

• An outline is a line drawn outside the


element's border.
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• p{
• border: 2px solid black;
• outline: #4CAF50 solid 10px;
• margin: auto;
• padding: 20px;
• text-align: center;
• }
• </style>
• </head>
• <body>

• <h2>CSS Outline</h2>
• <p>This element has a 2px black border and a green outline with a width of 10px.</p>

• </body>
• </html>
output
CSS INLINE

• The display: inline-block Value


• Compared to display: inline, the major difference is
that display: inline-block allows to set a width and height
on the element.
• Also, with display: inline-block, the top and bottom
margins/paddings are respected, but with display:
inline they are not.
• Compared to display: block, the major difference is
that display: inline-block does not add a line-break after
the element, so the element can sit next to other elements.
• The following example shows the different behavior
of display: inline, display: inline-block and display: block:
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• span.a {
• display: inline; /* the default for span */
• width: 100px;
• height: 100px;
• padding: 5px;
• border: 1px solid blue;
• background-color: yellow;
• }

• span.b {
• display: inline-block;
• width: 100px;
• height: 100px;
• padding: 5px;
• border: 1px solid blue;
• background-color: yellow;
• }
• span.c {
• display: block;
• width: 100px;
• height: 100px;
• padding: 5px;
• border: 1px solid blue;
• background-color: yellow;
• }
• </style>
• </head>
• <body>

• <h1>The display Property</h1>

• <h2>display: inline</h2>
• <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consequat
scelerisque elit sit amet consequat. Aliquam erat volutpat. <span
class="a">Aliquam</span> <span class="a">venenatis</span> gravida nisl sit amet
facilisis. Nullam cursus fermentum velit sed laoreet. </div>
• <h2>display: inline-block</h2>
• <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
consequat scelerisque elit sit amet consequat. Aliquam erat volutpat. <span
class="b">Aliquam</span> <span class="b">venenatis</span> gravida nisl sit
amet facilisis. Nullam cursus fermentum velit sed laoreet. </div>

• <h2>display: block</h2>
• <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
consequat scelerisque elit sit amet consequat. Aliquam erat volutpat. <span
class="c">Aliquam</span> <span class="c">venenatis</span> gravida nisl sit
amet facilisis. Nullam cursus fermentum velit sed laoreet. </div>

• </body>
• </html>
Css inline example 2
• Using inline-block to Create Navigation Links
• One common use for display: inline-block is to
display list items horizontally instead of
vertically. The following example creates
horizontal navigation links:
• <!DOCTYPE html>
• <html>
• <head>
• <style>
• .nav {
• background-color: yellow;
• list-style-type: none;
• text-align: center;
• margin: 0;
• padding: 0;
•}

• .nav li {
• display: inline-block;
• font-size: 20px;
• padding: 20px;
•}
• </style>
• </head>
• <body>

• <h1>Horizontal Navigation Links</h1>


• <p>By default, list items are displayed vertically. In this example we use display: inline-block to display them horizontally (side by side).</p>
• <p>Note: If you resize the browser window, the links will automatically break when it becomes too crowded.</p>

• <ul class="nav">
• <li><a href="#home">Home</a></li>
• <li><a href="#about">About Us</a></li>
• <li><a href="#clients">Our Clients</a></li>
• <li><a href="#contact">Contact Us</a></li>
• </ul>

• </body>
• </html>
output
INHERITANCE CSS
• The inherit keyword specifies that a property
should inherit its value from its parent
element. The inherit keyword can be used for
any CSS property, and on any HTML element.

You might also like