Lesson 3. Cascading Style Sheets
Lesson 3. Cascading Style Sheets
What is CSS?
Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to
transform the presentation of a Web Pages as well as many ostensibly nonweb environments.
CSS handles the look and feel part of a web page. Using CSS, you can control the color of the
text, the style of fonts, the spacing between paragraphs, how columns are sized and laid out, what
background images or colors are used, layout designs,variations in display for different devices
and screen sizes as well as a variety of other effects.
CSS is easy to learn and understand but it provides powerful control over the presentation of an
HTML document. Most commonly, CSS is combined with the markup languages HTML or XHTML.
Strictly speaking CSS is not a programming language but it does require abstract thought. It is
also not purely a design tool, but it does require some creativity.
Aspects of CSS can be found in the syntax used by JavaScript and its frameworks, so we can
say CSS is everywhere!
History of CSS
Cascading Style Sheets level 1 (CSS1) came out of W3C as a recommendation in December
1996. This version describes the CSS language as well as a simple visual formatting model for
all the HTML tags.
CSS2 became a W3C recommendation in May 1998 and builds on top of CSS1. This version
adds support for media-specific style sheets e.g. printers and aural devices, downloadable fonts,
element positioning and tables.
CSS3 became a W3C recommendation in June 2012 and builds on older versions CSS. it has
divided into documentations called as Modules and here each module having new extension
features defined in CSS2.
Year Description
HÃ¥kon Wium Lie proposed the idea of CSS to allow web designers to change the
1994
layout, colors, and fonts of their websites.
The first version of CSS was released while the newly established CSS Working
1996
Group moved forward with CSS2.
The second version of CSS was released and work on CSS-3 started at the same
1998
time.
A clarified version of CSS2 called CSS2.1, was released, which fixed the errors found
2011
in CSS 2
As of June 2012, there are over fifty CSS modules published from the CSS-3 Working
2012
Group.
Style Sheets Origin
The stylesheets you add to your web page aren’t the only ones the browser applies. There are
different types, or origins, of stylesheets:
• User agent styles - This is the default style which browser applies to any web page.
• Author styles - Your stylesheets are called author styles which override user agent styles.
• User stylesheet - Some browsers let users define a user stylesheet and these are rarely
used.
The World Wide Web Consortium, or W3C is a group that makes recommendations about how
the Internet works and how it should evolve.
CSS Modules
A CSS Module is a CSS file in which all class names and animation names are scoped locally by
default. There are number of modules available CSS3, we are going to list a few of them:
• Selectors
• Box Model
• Backgrounds and Borders
• Image Values and Replaced Content
• Text Effects
• 2D/3D Transformations
• Animations
• Multiple Column Layout
• User Interface
IM Developed by: Date Created: Revision No.
COMP 20163 WEB DEVELOPMENT Page No.
Mr. Owen Harvey Balocon INSTRUCTIONAL MATERIAL October 24, 2023 2023 - 000 ___ of ___
Instructor
Polytechnic University of the Philippines COMP 20163 WEB DEVELOPMENT
Santa Rosa Branch INSTRUCTIONAL MATERIAL
A CSS comprises of style rules that are interpreted by the browser and then applied to the
corresponding elements in your document. A style rule is made of three parts −
• Selector − A selector is an HTML tag at which a style will be applied. This could be any
tag like <h1> or <table> etc.
• Property − A property is a type of attribute of HTML tag. Put simply, all the HTML attributes
are converted into CSS properties. They could be color, border etc.
• Value − Values are assigned to properties. For example, color property can have value
either red or #F1F1F1 etc.
Here table is a selector and border is a property and given value 1px solid #C00 is the value of
that property.
You can define selectors in various simple ways based on your comfort. Let me put these selectors
one by one.
h1 {
color: #36CFFF;
}
This rule renders the content of every element in our document in black.
This rule renders the content in black for every element with class attribute set to black in our
document. You can make it a bit more particular. For example −
h1.black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with class attribute set to black.
You can apply more than one class selectors to given element. Consider the following example −
<p class = "center bold">
This para will be styled by the classes center and bold.
</p>
The ID Selectors
You can define style rules based on the id attribute of the elements. All the elements having
that id will be formatted according to the defined rule.
#black {
color: #000000;
}
This rule renders the content in black for every element with id attribute set to black in our
document. You can make it a bit more particular. For example −
h1#black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with id attribute set to black.
The true power of id selectors is when they are used as the foundation for descendant selectors,
For example −
#black h2 {
color: #000000;
}
In this example all level 2 headings will be displayed in black color when those headings will lie
with in tags having id attribute set to black.
This rule will render all the paragraphs in black if they are direct child of <body> element. Other
paragraphs put inside other elements like <div> or <td> would not have any effect of this rule.
The Attribute Selectors
You can also apply styles to HTML elements with particular attributes. The style rule below will
match all the input elements having a type attribute with a value of text −
input[type = "text"] {
color: #000000;
}
The advantage to this method is that the <input type = "submit" /> element is unaffected, and the
color applied only to the desired text fields.
There are following rules applied to attribute selector.
• p[lang] − Selects all paragraph elements with a lang attribute.
• p[lang="fr"] − Selects all paragraph elements whose lang attribute has a value of exactly
"fr".
• p[lang~="fr"] − Selects all paragraph elements whose lang attribute contains the word
"fr".
• p[lang|="en"] − Selects all paragraph elements whose lang attribute contains values that
are exactly "en", or begin with "en-".
margin-bottom: 1em;
text-transform: lowercase;
}
Here all the property and value pairs are separated by a semicolon (;). You can keep them in a
single line or multiple lines. For better readability, we keep them in separate lines.
For a while, don't bother about the properties mentioned in the above block. These properties will
be explained in the coming chapters and you can find complete detail about properties in CSS
References
Grouping Selectors
You can apply a style to many selectors if you like. Just separate the selectors with a comma, as
given in the following example −
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
This define style rule will be applicable to h1, h2 and h3 element as well. The order of the list is
irrelevant. All the elements in the selector will have the corresponding declarations applied to
them.
CSS selectors are patterns used to select and style HTML elements on a web page. They allow
you to target specific elements or groups of elements to apply styles like colors, fonts, margins,
and more. CSS selectors are a fundamental part of Cascading Style Sheets (CSS), which is a
language used to control the presentation and layout of web documents.
The element or elements that are selected by the selector are referred to as subject of the
selector.
Selector lists
If same CSS is used by more than one selector, then these selectors can be combined together
to form a selector list. Thus the CSS rule is applied to all the individual selectors.
For example, if the same CSS, color: crimson is applied to p element and .sample class, it is
written as:
p{
color: crimson;
}
.sample {
color: crimson;
}
But, we can combine these two rules into one selector list, by adding a comma to separate them
as shown below:
p, .sample {
color: crimson;
}
Following syntax will be deemed invalid, as one of the selector is invalid (..sample - is an incorrect
way of defining a class).
p{
color: crimson;
}
..sample {
color: crimson;
}
p, ..sample {
color: crimson;
}
• A white space is acceptable before or after the comma in a selector list declaration.
• If any of the selectors in the selector list is invalid, the whole rule gets ignored and deemed
invalid.
• It is advisable to define each selector in a new line, as it makes it more legible.
CSS Selector - Type Selector
A type selector targets an HTML element, such as <h1>, <p>, etc.
p{
color: green;
}
h1 {
text-decoration-line: underline;
}
p{
color: green;
}
h1 {
text-decoration-line: underline;
}
</style>
</head>
<body>
<div>
<h1>Type selector</h1>
<p>div with border and text-aligned to center</p>
<p>paragraph with green color</p>
<p>h1 with an underline</p>
</div>
</body>
</html>
.style-p {
color: green;
font-size: 25px;
}
Following example demonstrates the use of a class selector, where .style-p, .style-h1 and .style-
div are class selectors:
<html>
<head>
<style>
.style-div {
.style-p {
color: green;
font-size: 25px;
}
.style-h1 {
text-decoration-line: underline;
}
</style>
</head>
<body>
<div class="style-div">
<h1 class="style-h1">class selector</h1>
<p class="style-p">class .style-p applied</p>
<p>No class applied on this p element</p>
</div>
</body>
</html>
CSS Selector - ID Selector
An ID selector targets an element with a specific value for its id attribute.
#style-p {
color: green;
font-size: 25px;
}
#style-h1 {
text-decoration-line: underline;
color: red;
}
Following example demonstrates the use of an id selector, where #style-p, #style-h1 and #style-
div are the ide selectors applied on the elements:
<html>
<head>
<style>
#style-div {
border: 5px inset purple;
width: 300px;
text-align: center;
background-color: lightgoldenrodyellow;
}
#style-p {
color: green;
font-size: 25px;
}
#style-h1 {
text-decoration-line: underline;
color: red;
}
</style>
</head>
<body>
<div id="style-div">
<h1 id="style-h1">ID selector</h1>
<p id="style-p">id #style-p applied</p>
<p>No id applied on this p element</p>
</div>
</body>
</html>
<a href="#">Tutorialspoint</a>
<a href="#" target="_blank">google</a>
<a href="#" target="_self">wikipedia</a>
</body>
</html>
content: url('images/smiley.png');
}
a::after {
content: " Pseudo-element ::after applied";
color: red;
background-color: chartreuse;
}
</style>
</head>
<body>
<h2>Pseudo-element selector</h2>
<p>Styling applied to anchor element with a pseudo-element:</p>
<a href="#">Tutorialspoint</a>
</body>
</html>
Combinators
Combinator shows the relationship between the selectors. Two or more simple selectors can be
combined using a combinator, to form a selector. You can read more about combinator here.
Following example demonstrates the use of a descendant selector (space) and child combinator:
<html>
<head>
<style>
/* style applied to only div */
div {
border: 2px solid black;
width: 300px;
}
/* style applied to all li elements directly under ul */
ul li {
background-color: lightpink;
color: purple;
font-size: 1.5em;
padding: 5px;
margin-right: 15px;
}
</style>
</head>
<body>
<div>
<ul>
<li>Item One</li>
<li>Item Two
<ol>
<li>Nested 1</li>
<li>Nested 2</li>
</ol></li>
<li>Item Three</li>
<li>Item Four</li>
<li>Item Five
<ol>
<li>Nested 3</li>
<li>Nested 4</li>
</ol>
</li>
</ul>
</div>
</body>
</html>
<body>
<h1>Universal selector (*)</h1>
<div>Parent element
<p>Child paragraph 1</p>
<p>Child paragraph 2</p>
</div>
<p>Paragraph 3</p>
</body>
</html>
</p>
</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.
Syntax
CSS allows to set colors of an element in a variety of ways. Let's check all the possible available
syntax to set the height of an element.
Named Colors
A CSS named color is the name of a color, such as red, blue, black, or lightseagreen.
color: blue;
border: 1px solid red;
background: aliceblue;
Black
Red
Blue
Green
Aquamarine
Example
Here is an example:
<html>
<head>
<style>
div {
background-color: aqua;
padding: 10px;
}
</style>
</head>
<body>
<h3>Color Keyword - example</h3>
IM Developed by: Date Created: Revision No.
COMP 20163 WEB DEVELOPMENT Page No.
Mr. Owen Harvey Balocon INSTRUCTIONAL MATERIAL October 24, 2023 2023 - 000 ___ of ___
Instructor
Polytechnic University of the Philippines COMP 20163 WEB DEVELOPMENT
Santa Rosa Branch INSTRUCTIONAL MATERIAL
<p>As the keyword passed is aqua, the background will appear as aqua colored..</p>
<div>
This div element has a colored background based on the color keyword passed, i.e aqua.
</div>
</body>
</html>
#000000
#FF0000
#00FF00
#0000FF
#FFFF00
#00FFFF
#FF00FF
#C0C0C0
#FFFFFF
Example
Here is an example:
<html>
<head>
<style>
div{
background-color: #00ff00;
padding: 10px;
}
</style>
</head>
<body>
IM Developed by: Date Created: Revision No.
COMP 20163 WEB DEVELOPMENT Page No.
Mr. Owen Harvey Balocon INSTRUCTIONAL MATERIAL October 24, 2023 2023 - 000 ___ of ___
Instructor
Polytechnic University of the Philippines COMP 20163 WEB DEVELOPMENT
Santa Rosa Branch INSTRUCTIONAL MATERIAL
#000
#F00
#0F0
#0FF
#FF0
#0FF
#F0F
#FFF
Example
Here is an example:
<html>
<head>
<style>
div{
background-color: #00f;
padding: 10px;
color:#fff;
}
</style>
</head>
<body>
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)
Example
Here is an example:
<html>
<head>
<style>
div{
background-color: rgb(255,0,255);
padding: 10px;
}
</style>
</head>
<body>
<h3>RGB - example</h3>
IM Developed by: Date Created: Revision No.
COMP 20163 WEB DEVELOPMENT Page No.
Mr. Owen Harvey Balocon INSTRUCTIONAL MATERIAL October 24, 2023 2023 - 000 ___ of ___
Instructor
Polytechnic University of the Philippines COMP 20163 WEB DEVELOPMENT
Santa Rosa Branch INSTRUCTIONAL MATERIAL
Example
Here is an example:
<html>
<head>
<style>
div {
background-color: rgba(255,0,255,0.2);
padding: 10px;
}
</style>
</head>
<body>
<h3>RGBA - example</h3>
<p>As the rgba(255,0,255,0.2) is set the background will appear with transparency value of
0.2.</p>
<div>
IM Developed by: Date Created: Revision No.
COMP 20163 WEB DEVELOPMENT Page No.
Mr. Owen Harvey Balocon INSTRUCTIONAL MATERIAL October 24, 2023 2023 - 000 ___ of ___
Instructor
Polytechnic University of the Philippines COMP 20163 WEB DEVELOPMENT
Santa Rosa Branch INSTRUCTIONAL MATERIAL
This div element has a colored background based on the rgba values.
</div>
</body>
</html>
hsl(0,0%,50%)
hsl(255,80%,70%)
hsl(290,100%,60%)
hsl(360,70%,20%)
hsl(89,80%,67%)
Example
Here is an example:
<html>
<head>
<style>
div{
background-color: hsl(355,70%,50%);
padding: 10px;
}
</style>
</head>
<body>
<h3>HSL - example</h3>
<p>As the hsl(355,70%,50%) is set the background will appear based on the hsl values
passed.</p>
<div>
This div element has a colored background based on the hsl values hsl(355,70%,50%).
</div>
</body>
</html>
Example
Here is an example:
<html>
<head>
<style>
div{
background-color: hsla(355,70%,50%,0.4);
padding: 10px;
}
</style>
</head>
<body>
<h3>HSLA - example</h3>
<p>As the hsla(355,70%,50%,0.4) is set the background will appear based on the hsla values
passed, with high transparency.</p>
<div>
This div element has a colored background based on the hsl values hsla(355,70%,50%,0.4).
</div>
</body>
</html>
This chapter teaches you how to set backgrounds of various HTML elements. You can set the
following background properties of an element −
<body>
<p style = "background-color:yellow;">
This text has a yellow background color.
</p>
</body>
</html>
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-color: #cccccc;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
<html>
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat;
}
</style>
</head>
<body>
<p>Tutorials point</p>
</body>
</html>
The following example which demonstrates how to repeat the background image vertically.
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat-y;
}
</style>
</head>
<body>
<p>Tutorials point</p>
</body>
</html>
The following example demonstrates how to repeat the background image horizontally.
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<p>Tutorials point</p>
</body>
</html>
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-position:100px;
}
</style>
</head>
<body>
<p>Tutorials point</p>
</body>
</html>
The following example demonstrates how to set the background image position 100 pixels away
from the left side and 200 pixels down from the top.
<html>
<head>
<style>
body {
background-image: url("/css/images/css.jpg");
background-position:100px 200px;
}
</style>
</head>
<body>
<p>Tutorials point</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('/css/images/css.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
}
</style>
</head>
<body>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
</body>
</html>
The following example demonstrates how to set the scrolling background image.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url('/css/images/css.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-attachment:scroll;
}
</style>
</head>
<body>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
Shorthand Property
You can use the background property to set all the background properties at once. For example
−
<p style = "background:url(/https/www.scribd.com/images/pattern1.gif) repeat fixed;">
This parapgraph has fixed repeated background image.
</p>
A font is a set of text characters with a consistent design and style. It includes the shape, size,
weight, and other attributes of the characters in a typeface.
• Fonts are used to display and represent text in various mediums, such as print, digital
screens, and graphic design.
• Fonts can be chosen to convey a specific mood or aesthetic, enhance readability, or align
with a brand's identity.
• Fonts play a crucial role in communication and design.
• Fonts can evoke different emotions and convey specific messages to the audience.
This chapter demonstrates how to set fonts of a content, available in an HTML element. You can
set following font properties of an element −
• font: It is a shorthand for all the other font-related properties that can be passed in one
single declaration.
• font-family: Specifies the font family or list of font families to be used for text.
• font-feature-settings: Controls advanced typographic features in OpenType fonts, such
as swashes, small caps and ligatures.
• font-kerning: Determines the way specific pairs of letters are spaced.
• font-language-override: Overrides the typeface behaviour for a particular language.
• font-optical-sizing: Sets whether rendering of text should be optimized to view at different
sizes.
• font-palette: Allows you to specify one of the various palettes that are contained in a font.
• font-size: Sets the size of the text. It can be specified in pixels (px), points (pt),
percentages (%), ems (em), or other units.
• font-size-adjust: Sets the size of the lowercase letters in accordance with the current font
size.
• font-stretch: Sets the expanded, condensed or a normal font face.
• font-style: Specifies the style of the text, such as "italic", "oblique", or "normal".
• font-weight: Determines the thickness or boldness of the text. Common values are
"normal" and "bold", though numeric values (e.g., 100, 200, 300) and keywords (e.g.,
"lighter", "bolder") can also be used.
• font-synthesis: Determines whether or not the browser should synthesize the bold, italic,
and / or small-caps typefaces that are missing in a font-family.
• font-synthesis-small-caps: Determines whether or not the browser should synthesize
the small-caps typeface, that is missing in a font-family.
• font-synthesis-style: Determines whether or not the browser should synthesize the
oblique typeface, that is missing in a font-family.
• font-synthesis-weight: Determines whether or not the browser should synthesize the
bold typeface, that is missing in a font-family.
• font-variant: Controls the use of small caps for lowercase letters in the text. Values can
be "normal" or "small-caps".
• font-variant-alternates: Controls the usage of alternate glyphs.
• font-variant-caps: Controls the usage of alternate glyphs for capital letters.
• font-variant-east-asian: Controls the usage of alternate glyphs for East-Asian scripts like
Chinese and Japanese.
• font-variant-ligatures: Controls the textual content of element as to which ligature or
contextual form should be used.
• font-variant-numeric: Controls the usage of alternate glyphs for numbers, fractions and
ordinal markers.
• font-variant-position: Controls the usage of alternate, smaller glyphs, positioned as
superscript or subscript.
• font-variation-settings: Specifies the four-letter axis names of variable font
characteristics.
• line-height: Sets the height of the line box. Also sets the distance between the lines of
text.
CSS Font Shorthand
In order to shorten the code, CSS allows a shorthand property for font where all the following
properties can be specified in one single declaration:
The font property is a shorthand for:
• font-style
• font-variant
• font-weight
• font-stretch
• font-size
• line-height
• font-family
Points to remember:
• The properties font-style, font-variant and font-weight must always precede font-size.
• The values normal and small-caps may only be specified with the property font-variant.
• The property font-stretch may only be a single keyword value.
• The property line-height must follow the property font-size, preceded by "/". For
example: "20px/3".
• The property font-family must be the last value specified.
The values for font-family and font-size are required, and in case any of the values is missing,
the default values are used.
System Font Values
</p>
<p class="small-caps">
Font feature settings-small caps
</p>
</div>
</body>
</html>
CSS Font Kerning
The font-kerning property provided by CSS is useful in determining the way specific pairs of
letters are spaced.
Every character has a set space on its left and right edges and when two characters are placed
side-by-side, their side bearings get added up from its actual space.
In order to adjuts these side bearings of characters, the font-kerning property is used for. You
can enable or disable this property.
Possible values for this property are as follows:
• auto: Browser decides whether font kerning should be applied or not.
• normal: Information of font kerning stored in the font must be used.
• none: Information of font kerning stored in the font is disabled.
Example
Here is an example:
<html>
<head>
<style>
div {
font-size: 2rem;
font-family: serif;
}
#nokern {
font-kerning: none;
}
#kern {
font-kerning: normal;
}
#auto {
font-kerning: auto;
}
</style>
</head>
<body>
<h2>Font kerning</h2>
<div id="kern">AV</div>
<div id="nokern">AV</div>
<div id="auto">AV</div>
</body>
</html>
}
.opt-no {
font-optical-sizing: none;
}
.opt-yes {
font-optical-sizing: auto;
}
</style>
</head>
<body>
<h3>Optical sizing -none</h3>
<p class="eng opt-no">Check the optical sizing</p>
h2.more-spicy {
font-palette: --bungee-more-spicy;
}
</style>
</head>
<body>
<h2>Font face</h2>
<h2 class="more-spicy">Hot & burning</h2>
</body>
</html>
CSS Font Size
Font size refers to the height of the characters in relation to the text's baseline. Larger fonts are
typically used for headlines or titles to grab attention, while smaller fonts are suitable for body text
or captions to ensure readability.
The possible values for font-size in CSS are:
• Absolute size keywords: These values are based on the default size of uesr's font-size
(medium). It includes:
o xx-small
o x-small
o small
o medium
o large
o large
o x-large
o xx-large
o xxx-large
• Relative size keywords: These values are relatively larger or smaller to the font-size of
the parent element. It includes:
o larger
o smaller
• <length>: These are predefined positive <length> value. It includes em, ex, rem, px, in,
etc.
• <percentage>: A positive <percentage> value, relative to the font-size of the parent
element.
• math: It is the computed value of font-size for math elements relative to the font-size of
the parent element.
Example
Here is an example:
<html>
<head>
<style>
p{
padding: 5px;
border: 2px solid blue;
}
</style>
</head>
<body>
<h2>Font-size</h2>
<p style = "font-size: large;">
The font-size is large.
</p>
<p style = "font-size: 15px">
The font-size is 15px.
</p>
<p style = "font-size: x-small;">
The font-size is x-small.
</p>
<p style = "font-size: 60%;">
The font-size is 60%.
</p>
<p style = "font-size: 2vw;">
The font-size 2vw (viewport width). Change the viewport size to see the effect.
</p>
</body>
</html>
CSS Font Size Adjust
The font-size-adjust property can be used to specify a font's aspect value (numeric ratio) that
controls the x-height (height of lowercase letters) of the font.
This ensures that the fallback font maintains a consistent visual size and proportion relative to the
specified font.
The font-size-adjust property can take one of the following values:
• <number>: Specifies a numerical value that represents the aspect value, which is the
ratio of the x-height of the font to the x-height of the font specified by the font-
family property.
• none: Specifies that no font size adjustment is made based on the x-height.
• <keyword>: Specifies the font metric to normalize on:
o ex-height: x-height divided by font-size.
o cap-height: Using cap-height by font-size.
o ch-width: Horizontal narrow pitch of the fonts is normalized.
o ic-width: Horizontal wide pitch of the fonts is normalized.
o ic-height: Vertical wide pitch of the fonts is normalized.
The browser support for font-size-adjust may vary, and it is not widely used in modern web
development. The 'Firefox' and Safari browser supports this property.
Here is an example (check the result on Firffox or Safari browser):
<html>
<head>
<style>
p{
padding: 5px;
ultra-condensed 50%
extra-condensed 62.5%
condensed 75%
semi-condensed 87.5%
normal 100%
semi-expanded 112.5%
expanded 125%
extra-expanded 150%
ultra-expanded 200%
Example
Here is an example:
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<h2>Font-stretch</h2>
<p style = "font-stretch: normal;">
Normal
</p>
<p style = "font-stretch: condensed;">
Condensed
</p>
<p style = "font-stretch: expanded;">
Expanded
</p>
<p style = "font-stretch: 170%;">
170% stretched
</p>
</body>
IM Developed by: Date Created: Revision No.
COMP 20163 WEB DEVELOPMENT Page No.
Mr. Owen Harvey Balocon INSTRUCTIONAL MATERIAL October 24, 2023 2023 - 000 ___ of ___
Instructor
Polytechnic University of the Philippines COMP 20163 WEB DEVELOPMENT
Santa Rosa Branch INSTRUCTIONAL MATERIAL
</html>
CSS Font Style
Font styles are used to change the appearance or style of text. The font-style property is used
to specify the font style for the selected text.
There are three main font styles available in CSS:
• normal: Default font style where the text is displayed normally, without any slant or
obliqueness.
• italic: Text appears in an italicized style, with a slight slant to the right.
• oblique: Text appears similar to italic but with a more exaggerated slant or angularity.
• oblique <angle>: An oblique text with an angle to specify the slantness of the text.
o If no oblique font face available, the browser will slant the text by specified angle.
o Valid values for angle are degree values between -90deg to 90deg inclusive.
o If no angle specified, 14deg is set by the browser.
o Positive angle value slants the text to the end of line.
o Negative angle values slant the text towards the beginning.
o Larger angle values are preferred.
These font styles can be applied to any HTML elements using CSS selectors and can be
combined with other font properties like font-family, font-size, etc., to create desired text styles.
Note: The browser compatible for the font-style:oblique <angle> is firefox.
Example
Here is an example:
<html>
<head>
<style>
p{
padding: 5px;
border: 2px solid blue;
}
</style>
</head>
<body>
<h2>Font-style</h2>
<p style = "font-style: normal;">
The text will be normal.
</p>
<p style = "font-style: italic;">
The text will be italic.
</p>
<p style = "font-style: oblique;">
The text will be obligue.
</p>
<p style = "font-style: oblique 45deg;">
The text will be obligue and slanted 45deg.
</p>
</body>
</html>
CSS Font Weight
Font weight refers to the thickness or boldness of the characters. Different weights within a font
family can be used to create visual hierarchy or emphasis within a text.
The possible values for font-weight are as follows:
• normal: Font weight normal. Equivalent to 400.
• bold: Font weight bold. Equivalent to 700.
• <number>: Value between 1 and 1000 define thickness of font. Higher numbers represent
weights that are bolder than (or as bold as) lower numbers.
• lighter: Lighter font weight relative to the parent element's font weight.
• bolder: Bolder font weight relative to the parent element's font weight.
Here is an example:
<html>
<head>
<style>
p{
padding: 5px;
border: 2px solid blue;
}
</style>
</head>
<body>
<h2>Font-weight</h2>
<p style = "font-weight: normal;">
The font-weight is normal.
</p>
<p style = "font-weight: bold;">
The font-weight is bold.
</p>
<p style = "font-weight: 500;">
The font-weight is 500 (medium).
</p>
</body>
</html>
A text refers to a piece of written or printed information in the form of words or characters that can
be read and understood. Texts can include content such as books, articles, emails, messages,
web pages, etc. Styling a text involves modifying its appearance to make it more visually
appealing or to convey a particular message.
Text Formatting using CSS
CSS provides a number of properties to format text in beautiful way. This tutorial demonstrates
how to format text using CSS properties. Following properties provided by CSS can be used to
format the text:
• color: Sets the color of the text.
• text-align Sets the alignment of the text.
• text-align-last: Sets the alignment of the last line of a block of text.
• direction: Sets the direction of the text of an element.
• text-indent: Sets the indentation of the first line of the text.
• letter-spacing: Specifies the space between the letters or characters that make up a word
or text.
• word-spacing: Specifies the space between the words in a text.
• white-space: Controls the white space flow inside the text in an element.
• text-decoration: Adds an underline, overline, and strikethrough over a piece of text.
• text-decoration-skip: Determines what part of the content of the element, where text
decoration is affecting, needs to be skipped.
• text-decoration-skip-ink: Specifies how the overline and underline text decoration lines
are drawn around glyph ascenders and descenders.
• text-transform: Converts text to uppercase or lowercase letters.
• text-emphasis: Applies emphasis marks to text (except spaces and control characters).
• text-shadow: Adds shadow to the text.
• line-break: Controls how to set the rule for a line break.
• word-break: Controls how to set the rule for a word break.
CSS - Setting Text Color
Altering the color of the text can add visual interest or align with a specific design scheme.
The CSS color property is used to set the color of the text. The possible values for this property
are as follows:
• Color Name: Example = red, blue, green.
• Hexadecimal value: Example = #ff00ff.
• RGB value: Example = rgb(125, 255, 0).
Example
Here is an example:
<html>
<head>
</head>
<body>
<h2>Text Color</h2>
<p style = "color: blueviolet;">
Color Name
</p>
<p style = "color:#ff00ff;">
Hexadecimal value
</p>
<p style = "color: rgb(255,124,100);">
RGB value
</p>
</body>
</html>
CSS - Setting Text Alignment
The position or placement of text on a page is called its alignment. The text is aligned based on
the left and right margins of the page.
The CSS property text-align defines the alignment of the text on a web page. The text-
align property is used to set the horizontal alignment of the text.
Following are the possible values which can be used to set text-align property.
• start: Same as left, if direction is LTR and right if direction is RTL.
• end: Same as right, if direction is LTR and left if direction is RTL.
• left: Aligned with the left-margin.
• right: Aligned with the right-margin.
• center: Aligned at the center of the page.
• justify: Aligned with both the margins.
• justify-all: Same as justify, making the last line justified as well.
• match-parent: Similar to inherit. Values of start and right taken from parent's value and
replaced by left and right.
When the value of direction is ltr, the default alignment is left-aligned and when the value
of direction is rtl, the default alignment is right-aligned.
Example
Here is an example:
<html>
<head>
</head>
<body>
<h2>Setting Text Alignment using CSS</h2>
<p style="text-align: left;">Text Left Alignment.</p>
<p style="text-align: right;">Text Right Alignment.</p>
<p style="text-align: center;">Text Center Alignment.</p>
<p style="text-align: justify; border: 2px solid red; width: 200px; height: 100px;">
Text Justify Alignment. This alignment aligns the text based on both the margins, left and
right.
</p>
</body>
</html>
CSS - Setting Vertical Alignment
The CSS vertical-align property sets Vertical Alignment of an inline, inline-block or table-cell
box.
Images play an important role in any webpage. Though it is not recommended to include a lot of
images, but it is still important to use good images wherever required.
CSS plays a good role to control image display. You can set the following image properties using
CSS.
• The border property is used to set the width of an image border.
• The height property is used to set the height of an image.
• The width property is used to set the width of an image.
• The -moz-opacity property is used to set the opacity of an image.
<html>
<head>
</head>
<body>
<img style = "border:0px;" src = "/css/images/logo.png" />
<br />
<img style = "border:3px dashed red;" src = "/css/images/logo.png" />
</body>
</html>
<html>
<head>
</head>
<body>
<img style = "border:1px solid red; height:100px;" src = "/css/images/logo.png" />
<br />
<img style = "border:1px solid red; height:50%;" src = "/css/images/logo.png" />
</body>
</html>
<html>
<head>
</head>
<body>
<img style = "border:1px solid red; width:150px;" src = "/css/images/logo.png" />
<br />
<img style = "border:1px solid red; width:100%;" src = "/css/images/logo.png" />
</body>
</html>
<html>
<head>
</head>
<body>
<img style = "border:1px solid red; -moz-opacity:0.4; filter:alpha(opacity=40);" src =
"/css/images/logo.png" />
</body>
</html>