Professional Documents
Culture Documents
Unit2 Css
Unit2 Css
CSS describes how HTML elements are to be displayed on screen, paper, or in other media
CSS saves a lot of work. It can control the layout of multiple web pages all at once
CSS is used to define styles for your web pages, including the design, layout and variations in display for
different devices and screen sizes.
The word cascading means that a style applied to a parent element will also apply to all children
elements within the parent. So, if you set the color of the body text to "blue", all headings, paragraphs,
and other text elements within the body will also get the same color (unless you specify something
else)!
CSS Syntax
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
h1 {
color: white;
text-align: center;
p{
font-family: verdana;
font-size: 20px;
</style>
</head>
<body>
<p>This is a paragraph.</p>
</body>
</html>
Using CSS
The most common way to add CSS, is to keep the styles in external CSS files. However, in this tutorial we
will use inline and internal styles, because this is easier to demonstrate, and easier for you to try it
yourself.
Inline CSS
The following example sets the text color of the <h1> element to blue, and the text color of
the <p> element to red:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Internal CSS
An internal CSS is defined in the <head> section of an HTML page, within a <style> element.
The following example sets the text color of ALL the <h1> elements (on that page) to blue, and the text
color of ALL the <p> elements to red. In addition, the page will be displayed with a "powderblue"
background color:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
External CSS
An external style sheet is used to define the style for many HTML pages.
To use an external style sheet, add a link to it in the <head> section of each HTML page:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
The external style sheet can be written in any text editor. The file must not contain any HTML code, and
must be saved with a .css extension.
"styles.css":
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
Tip: With an external style sheet, you can change the look of an entire web site, by changing one file!
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>Me too!</p>
<p>And me!</p>
</body>
</html>
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!
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
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>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
p.large {
font-size: 300%;
</style>
</head>
<body>
<p class="center large">This paragraph will be red, center-aligned, and in a large font-size.</p>
</body>
</html>
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>
</body>
</html>
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a later date.
A CSS comment is placed inside the <style> element, and starts with /* and ends with */:
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
CSS Colors
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.
<!DOCTYPE html>
<html>
<body>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex
ea commodo consequat.
</p>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p style="color:DodgerBlue;">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p style="color:MediumSeaGreen;">Ut wisi enim ad minim veniam, quis nostrud exerci tation
ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
In CSS, colors can also be specified using RGB values, HEX values, HSL values, RGBA values, and HSLA
values:
#ff6347
CSS Backgrounds
The CSS background properties are used to add background effects for elements.
background-color
background-image
background-repeat
background-attachment
background-position
groove - Defines a 3D grooved border. The effect depends on the border-color value
ridge - Defines a 3D ridged border. The effect depends on the border-color value
inset - Defines a 3D inset border. The effect depends on the border-color value
outset - Defines a 3D outset border. The effect depends on the border-color value
The border-style property can have from one to four values (for the top border, right border,
bottom border, and the left border).
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).
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
CSS Padding
The CSS padding properties are used to generate space around an element's content, inside of any
defined borders.
With CSS, you have full control over the padding. There are properties for setting the padding for each
side of an element (top, right, bottom, and left).
padding-top
padding-right
padding-bottom
padding-left
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.
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:
Content - The content of the box, where text and images appear
The box model allows us to add a border around elements, and to define space between
elements. Demonstration of the box model:
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
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 Text
Text Color
The color property is used to set the color of the text. The color is specified by:
text-align
text-align-last
direction
unicode-bidi
vertical-align
text-decoration-line
text-decoration-color
text-decoration-style
text-decoration-thickness
text-decoration
CSS Fonts
Font Style
oblique - The text is "leaning" (oblique is very similar to italic, but less supported)
Font Weight
Font Variant
The font-variant property specifies whether or not a text should be displayed in a small-caps font.
In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted
uppercase letters appears in a smaller font size than the original uppercase letters in the text.
CSS Icons
Icons can easily be added to your HTML page, by using an icon library.
To use the Font Awesome icons, go to fontawesome.com, sign in, and get a code to add in
the <head> section of your HTML page:
<!DOCTYPE html>
<html>
<head>
<body>
</body>
</html>
CSS Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
In addition, links can be styled differently depending on what state they are in.
The background-color property can be used to specify a background color for links
Link Buttons
<!DOCTYPE html>
<html>
<head>
<style>
a:link, a:visited {
background-color: #f44336;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
a:hover, a:active {
background-color: red;
</style>
</head>
<body>
<h2>Link Button</h2>
</body>
</html>
The float Property
The float property is used for positioning and formatting content e.g. let an image float left to the text
in a container.
none - The element does not float (will be displayed just where it occurs in the text). This is default
In its simplest use, the float property can be used to wrap text around images.
<!DOCTYPE html>
<html>
<head>
<style>
img {
float: right;
</style>
</head>
<body>
<h2>Float Right</h2>
<p>In this example, the image will float to the right in the paragraph, and the text in the paragraph will
wrap around the image.</p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum,
nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec
congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis
sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc
sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed
ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non
fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.</p>
</body>
</html>
When we use the float property, and we want the next element below (not on right or left), we will
have to use the clear property.
The clear property specifies what should happen with the element that is next to a floating element.
none - The element is not pushed below left or right floated elements. This is default
both - The element is pushed below both left and right floated elements
inherit - The element inherits the clear value from its parent
When clearing floats, you should match the clear to the float: If an element is floated to the left, then
you should clear to the left. Your floated element will continue to float, but the cleared element will
appear below it on the web page.
<!DOCTYPE html>
<html>
<head>
<style>
.div1 {
float: left;
padding: 10px;
.div2 {
padding: 10px;
.div3 {
float: left;
padding: 10px;
.div4 {
padding: 10px;
clear: left;
</style>
</head>
<body>
<h2>Without clear</h2>
<div class="div1">div1</div>
<div class="div2">div2 - Notice that div2 is after div1 in the HTML code. However, since div1 floats to the
left, the text in div2 flows around div1.</div>
<br><br>
<h2>With clear</h2>
<div class="div3">div3</div>
<div class="div4">div4 - Here, clear: left; moves div4 down below the floating div3. The value "left"
clears elements floated to the left. You can also clear "right" and "both".</div>
</body>
</html>