U1B. Introduction to CSS
U1B. Introduction to CSS
Pranav More
School of AI & Future Technologies
DEVELOPMENT
(CSS- CASCADING
STYLE SHEETS)
Contents of the unit:
• Basics of Cascading Style sheets,
• Advantages of CSS,
• CSS Syntax,
• Color, Background,
• Font, Images
What is CSS?
• CSS stands for Cascading Style Sheets
• 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
• With an external stylesheet file, you can change the look of an entire website by
changing just one file!
What is CSS? Cascading Style
Sheet Cascading: Falling of
• Stylesheet Language Styles.
• Standards-based set of properties and attributes to define
Style: Adding designs/
styles Styling our HTML tags.
• To describe the presentation a document written in a ‘markup Sheets: Writing our style
in different documents
language’ like HTML or XML
• Markup encoding: <p>My paragraph here.</p>
• Cascading
markup.
Why CSS?
• Separate Content from Form
• Content is the text and images, marked up to define regions of specific types
• Form defines the “style” for the content
<font size=“14px”>
My First Header
</font>
<font size=“12px” color=“red” face=“Verdana”>
My information 1 goes here.
The old way: </font>
<font size=“14px”>
My Second Header
</font>
<font size=“12px” color=“red” face=“Verdana”>
Different information goes here.
</font>
Why CSS? Continued.
• Form or Style
.header { font-size:14px;}
.info { font-family: verdana;
font-color: blue;
font-size: 12px; }
What does this separation get us?
• Separate Content from Form
• Specify the style once for every instance of that class.
• Example: Specify the font once for all text on the HTML page that you’ve
identified as a “header”.
• The stylesheet can be a separate file which all HTML pages on your entire site
can link to.
• Only have to specify the style once for your ENITRE SITE
• Can change the style for your entire site by editing only ONE FILE.
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
+
<p class=“info”>My Information 1 goes here</p>
skin1.css
.info { background-color: White;
font-family: Verdana;
font-color: Blue; }
=
My Information 1 goes here
CSS Skinning 2
• “Skinning” - changing the look of a page or your site
• Selecting an appearance by choosing which stylesheet to use.
+
<p class=“info”>My Information 1 goes here</p>
skin2.css
.info { background-color: Blue;
font-family: Serif;
font-color: White; }
=
My Information 1 goes here
Three CSS Definition Locations
• Inline styles are defined right in the (X)HTML file along side the element
you want to style. See example below.
• Note, the selector for inline CSS is the tag which contains the style attribute.
<p style=“font-color:red;font-size:10px;”>Content</p>
Three CSS Definition Locations
Internal: the <style> markup tag
•This way you are simply placing the CSS code within the <head></head> tags
of each (X)HTML file you want to style with the CSS.
•With this method each (X)HTML file contains the CSS code needed to style
the page. Meaning that any changes you want to make to one page, will have
to be made to all. This method can be good if you need to style only one
page, or if you want different pages to have varying styles.
<head> <html><head><style>
<title><title> p{ background-color: Red;
<style type=”text/css”> font-family: serif;
CSS Content Goes Here font-color: White; }
</style> </style></head><body>
</head> <p>Content</p>
<body> </body></html>
Three CSS Definition Locations
External: the .css stylesheet file
•An external CSS file can be created with any text or HTML editor such as
“Notepad” or “Dreamweaver”. A CSS file contains no (X)HTML, only CSS.
•You simply save it with the .css file extension. You can link to the file externally by
placing one of the following links in the head section of every (X)HTML file you
want to style with the CSS file.
•By using an external style sheet, all of your (X)HTML files link to one CSS file in
order to style the pages. This means, that if you need to alter the design of all
your pages, you only need to edit one .css file to make global changes to your
entire website.
• It consists of 4 edges
• Content edge – It comprises of the actual content
• Padding edge – It lies in between content and border edge
• Border edge – Padding is followed by the border edge
• Margin edge – It is an outside border and controls the margin of the
element
CSS Box Model
• Content: The content area consists of content like image, text, or other forms of media
content. The height and width properties help to modify the box dimensions.
• Padding: The padding area is the space around the content area and within the border-
box. It can be applied to all sides of the box or to the specific, selected side(s) - top, right,
bottom, and/or left.
• Border: The border area surrounds the padding and the content, and can be applied to
all the sides of the box or to selected side(s) - top, right, bottom, and/or left.
• Margin: The margin area consists of space between the border and the margin. The
margin does not possess its own background color and is completely transparent. It
shows the background color of the element, like the body element.
CSS Box Model
• Important: When you set the width and height properties of an element with CSS,
you just set the width and height of the content area. To calculate the full size of an
element, you must also add padding, borders and margins.
Example:
<body>
div {
<h2>Calculate the total width:</h2>
width: 320px;
<img src="k4_big.jpg" width="350" height="263" alt="Klematis">
padding: 10px;
<div>The picture above is 350px wide. The total width of this
border: 5px solid gray;
element is also 350px</div>
margin: 0;
</body>
}
Width and Height of an Element
• Here is the calculation:
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
Total element width = width + left padding + right padding + left border + right border +
left margin + right margin
Total element height = height + top padding + bottom padding + top border + bottom
border + top margin + bottom margin
Width and Height of an Element
p{ font-size: 10px;
font-color: White; }
<p>Content</p>
The Class Attribute – precede the class with a period
<p class=“myinfo”>Content</p>
<div class=“myinfo”>Other content</div>
CSS Selectors
• CSS selectors are used to "find" (or select) the HTML elements you want to style.
The class is assigned to an element and The name of the Id starts with the "#"
its name starts with "." followed by the symbol followed by a unique id name.
name of the class.
We can attach multiple class selectors to We can attach only one ID selector to an
an element. element.
Simple CSS Selectors
The CSS Universal Selector
•The universal selector (*) selects all HTML elements on the page.
h1 {
text-align: center;
color: red;
}
h1, h2, p {
h2 {
text-align: center;
text-align: center;
color: red; color: red;
}
}
p{
text-align: center;
color: red;
}
Simple CSS Selectors
CSS Selectors
• CSS selectors are used to "find" (or select) the HTML elements you want to style.
• A CSS selector can contain more than one simple selector. Between the simple
selectors, we can include a combinator.
Syntax:
The syntax of pseudo-classes:
selector:pseudo-class
{
property: value;
}
CSS Pseudo-elements
What are Pseudo-Elements?
•A CSS pseudo-element is used to style specified parts of an element.
Syntax:
The syntax of pseudo-elements:
selector::pseudo-element
{
property: value;
}
CSS Attribute Selectors
Style HTML Elements With Specific Attributes
•It is possible to style HTML elements that have specific attributes or attribute values.
The following example selects all <a> elements with a target attribute:
a[target]
{
background-color: yellow;
}
CSS Colors
• 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 −
CSS Backgrounds
This teaches you how to set backgrounds of various HTML elements. You can set the following
background properties of an element −
•The font-weight property is used to increase or decrease how bold or light a font
appears.
•The font property is used as shorthand to specify a number of other font properties.
CSS Texts
This teaches you how to manipulate text using CSS properties. You can set following text
properties of an element −
•The color property is used to set the color of a text.
•The direction property is used to set the text direction.
•The letter-spacing property is used to add or subtract space between the letters that
make up a word.
•The word-spacing property is used to add or subtract space between the words of a
sentence.
•The text-indent property is used to indent the text of a paragraph.
•The text-align property is used to align the text of a document.
•The text-decoration property is used to underline, overline, and strikethrough text.
•The text-transform property is used to capitalize text or convert text to uppercase or
lowercase letters.
•The white-space property is used to control the flow and formatting of text.
•The text-shadow property is used to set the text shadow around a text.
CSS Table
• This will teach you how to set different properties of an HTML table using CSS. You can
set following properties of a table −
• The border-collapse specifies whether the browser should control the appearance of
the adjacent borders that touch each other or whether each cell should maintain its
style.
• The border-spacing specifies the width that should appear between table cells.
• The caption-side captions are presented in the <caption> element. By default, these
are rendered above the table in the document. You use the caption-side property to
control the placement of the table caption.
• The empty-cells specifies whether the border should be shown if a cell is empty.
• The table-layout allows browsers to speed up layout of a table by using the first width
properties it comes across for the rest of a column rather than having to load the
whole table before rendering it.
CSS Scrollbar
• There may be a case when an element's content might be larger than the amount of space
allocated to it. For example, given width and height properties do not allow enough room to
accommodate the content of the element.
• CSS provides a property called overflow which tells the browser what to do if the box's contents is
larger than the box itself. This property can take one of the following values −
CSS Border
The border properties allow you to specify how the border of the box representing an element
should look. There are three properties of a border you can change −
•The border-color specifies the color of a border.
border-bottom-color changes the color of bottom border.
border-top-color changes the color of top border.
border-left-color changes the color of left border.
border-right-color changes the color of right border.
•The border-style specifies whether a border should be solid, dashed line, double line, or one of the
other possible values.
none − No border. solid − Border is a single solid line.
dotted − Series of dots. dashed − Series of short lines.
double − two solid lines. groove − Border carved into the page.
ridge − Opposite of groove. inset − look like it is embedded in the page.
Outset- look like it is coming out of the canvas.
hidden − Same as none, except in terms of border-conflict resolution for table
•The border-width specifies the width of a border.
border-bottom-width border-top-width
border-left-width border-right-width
CSS Lists
Lists are very helpful in conveying a set of either numbered or bullet points. This teaches you how to
control list type, position, style, etc., using CSS. We have the following five CSS properties, which can
be used to control lists −
•The list-style-type allows you to control the shape or appearance of the marker.
•The list-style-position specifies whether a long point that wraps to a second line should align with
the first line or start underneath the start of the marker.
•The list-style-image specifies an image for the marker rather than a bullet point or number.
The list-style-image allows you to specify an image so that you can use your own bullet style.
•The marker-offset specifies the distance between a marker and the text in the list.
CSS Self Study Concepts
CSS Margins
CSS Padding
CSS Outline
CSS Dimension
CSS Visibility
CSS Positioning
CSS Layers
•column-count: Used to count the number of columns that element should be divided.
•border-radius: Use this element for setting four boarder radius property.
•border-top-left-radius: Use this element for setting the boarder of top left corner.
•border-top-right-radius: Use this element for setting the boarder of top right corner.
•border-bottom-right-radius: Use this element for setting the boarder of bottom right corner.
•border-bottom-left-radius: Use this element for setting the boarder of bottom left corner.
Syntax:
#rcorners7 {
border-radius: 60px/15px;
background: #FF0000;
padding: 20px;
width: 200px;
height: 150px;
}