Computer 4
Computer 4
CLASS-X
COMPUTER APPLICATION
What is CSS?
CSS is the language we use to style a Web page.
CSS Syntax
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are
surrounded by curly braces. example :
all <p> elements will be center-aligned, with a red text color:
p { color: red;
text-align: center; }
Example Explained
• p is a selector in CSS (it points to the HTML element you want to style: <p>).
• color is a property, and red is the property value
• text-align is a property, and center is the property value
element,element,.. div, p Selects all <div> elements and all <p> elements
• External CSS
• Internal CSS
• 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.
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.
"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
Internal styles are defined within the <style> element, inside the <head> section of an HTML
page:
<!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>
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.
<!DOCTYPE html>
<html>
<body>
</body>
</html>
CSS uses color values to specify a color. Typically, these are used to set a color either for the
foreground of an element (i.e., its text) or else for the background of the element. They can also be
used to affect the color of borders and other decorative effects.
You can specify your color values in various formats. Following table lists all the possible formats −
Format Syntax Example
#000000
#FF0000
#00FF00
#0000FF
#FFFF00
#00FFFF
#FF00FF
#C0C0C0
#FFFFFF
#000
#F00
#0F0
#0FF
#FF0
#0FF
#F0F
#FFF
rgb(0,0,0)
rgb(255,0,0)
rgb(0,255,0)
rgb(0,0,255)
rgb(255,255,0)
rgb(0,255,255)
rgb(255,0,255)
rgb(192,192,192)
rgb(255,255,255)
• <html>
• <head>
• </head>
•
• <body>
• <p style = "background-color:yellow;">
• This text has a yellow background color.
• </p>
• </body>
• </html>
Output:
This text has a yellow background color.
Value Description
none
Default value. Specifies no border
hidden The same as "none", except in border conflict resolution for table elements
dotted
Specifies a dotted border
double
Specifies a double border
groove Specifies a 3D grooved border. The effect depends on the border-color value
ridge Specifies a 3D ridged border. The effect depends on the border-color value
inset
Specifies a 3D inset border. The effect depends on the border-color value
outset Specifies a 3D outset border. The effect depends on the border-color value
div { border-style:
outset; border-color:
coral; border-width:
7px; }
CSS Margins
The CSS margin properties are used to create space around elements, outside of any defined
borders.
With CSS, you have full control over the margins. There are properties for setting the margin
for each side of an element (top, right, bottom, and left).
• margin-top
• margin-right
• margin-bottom
• margin-left
• auto - the browser calculates the margin • length - specifies a margin in px, pt, cm, etc.
• % - specifies a margin in % of the width of the containing element
• inherit - specifies that the margin should be inherited from the parent element
• Example
• p {
margin-top: 100px; margin-
bottom: 100px; margin-right:
150px; margin-left: 80px;
}
Margin - Shorthand Property
To shorten the code, it is possible to specify all the margin properties in one property.
The margin property is a shorthand property for the following individual margin properties:
• margin-top
• margin-right
• margin-bottom
• margin-left
• margin: 25px 50px 75px 100px; o top margin is 25px o right margin is 50px o bottom
margin is 75px o left margin is 100px
Example
Use the margin shorthand property with four values:
p { margin: 25px 50px 75px
100px; }
Example
Use the margin shorthand property with three values:
Example
Use the margin shorthand property with two values:
p { margin: 25px
50px; }
If the margin property has one value:
Example
Use the margin shorthand property with one value:
p {
margin: 25px;
}
The element will then take up the specified width, and the remaining space will be split
equally between the left and right margins.
Example
Use margin: auto:
Example
Use of the inherit value:
p.ex1 { margin-left:
inherit; }
CSS Layout - float
The CSS float property specifies how an element should float.
The CSS clear property specifies what elements can float beside the cleared
element and on which side.
In its simplest use, the float property can be used to wrap text around images.
Example
img { float:
right; }
The CSS max-width property is used to set the maximum 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
Example
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).
Property Description
Example
Demonstration of the different outline styles: