Professional Documents
Culture Documents
Unit 2-1
Unit 2-1
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
• 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
• </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.
• 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
• <h1>This is a heading</h1>
• <p>This is a paragraph.</p>
• </body>
• </html>
output
Inline CSS
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>
• <!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>
• </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
• p.lowercase {
• text-transform: lowercase;
• }
• p.capitalize {
• text-transform: capitalize;
• }
• </style>
• </head>
• <body>
• </body>
• </html>
OUTPUT
Letter Spacing
• h3 {
• letter-spacing: -2px;
• }
• </style>
• </head>
• <body>
• <h1>Using letter-spacing</h1>
• </body>
• </html>
OUTPUT
CSS BORDERS
• </body>
• </html>
OUTPUT
CSS Outline
• <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
• 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>
• <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>
• <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.