Week 12 - Working With CSS
Week 12 - Working With CSS
Using CSS, you can specify a number of style properties for a given HTML tag. Each property
has a name and a value separated by a colon(:). Each property declared must be terminated
by a semi-colon (;).
To better understand how CSS works, please refer to the HTML codes below. Both examples
use the html paragraph <P> tag and font <FONT> tag with the font associated attributes to
indicate text color and font size.
HTML only
~--'-------------------------------~
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE> Cascad i ng Style Sheets</TITLE>
</HEAD>
<BODY>
<P><FONT color - "green" s i ze - "7">HTML CSS Module!</FONT>-</P>
</BODY>
</HTML>
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE> Cascading Style Sheets</ TITLE>
<STYLE type= "text/CSS">
H2{
text-align:center;
}
</STYLE>
</H EAD>
<BODY>
<H2>CSS is Fun!</H2>
</BODY>
</HTML>
c. External
This is done by placing the style tags using an external CSS file. To do this, you can use the
<LINK> tag as shown in the example below.
<!DOCTY PE html>
<HTM L>
<HEAD>
<TITLE> Cascading Styl e Sheets</TI TLE>
<LIN K rel= "style sh eet" href="myCSSFile . css" type ="text/CSS"/>
</H EAD>
<BODY>
... s om e htm l code s he re . ..
</ BODY>
</HTM L>
Class as a Selector
The class selector allows you to select element(s) where an exact property will be applied.
To select elements with a specific class, write a period (.) character, followed by the name of
the class. All elements assigned with that class will have the properties applied.
You can also indicate that only specific HTML elements should be affected by a class. To do
this, start with the element name, then write the period (.) character, followed by the name
of the class.
Class Syntax
.class {
css declarations;
}
Or
p.classTest {
background-color: red;
}
Below is an example:
<!DDCTYPE html>
<HTML>
<HEAD>
<TITLE> CSS Styles </TITLE>
<STYLE>
P. classTest{
background:gray;
}
</STYLE>
</HEAD>
<BODY>
<P>Welcome to CSS Class . </P>
<P class =''classTest''>This sentence uses the classTest assigned to P tag</P>
<P> This line of text does not use the paragraph formatting . </P>
<P class= " classTest">Have a great day.</P>
</BODY>
</HTML>
.
_·
.
...
.
---.
.
--- '
Notice that the formatting set to <P> tag is only implemented on specific <P> tags with a class
indicator.
In addition, a specific element can have more than one class associated with it, while an
element can only have one id. For example, you can give a div two classes whose styles will
both take effect.
ID as Selector
An ID selector is used when you have one element on the page that will have specific style
unique to other elements. Remember that IDs must be unique. All elements having the given
ID will be set according to the defined rule. An ID in CSS always starts with a number sign
(#).
#blueColor {
color: #0000/f;
}
This rule implies to the content in blue for every element with ID attribute set to blueColor
in your web page.
Another example is shown below
,-----------~
h1 #blueColor {
color: #0000/f;
}
The above rule implies the content in blue for <Hl> elements or tags with id attribute set
to blueColor.
Contextual Selectors
There are some instances where you may want selectors to match elements that appear in a
certain context. In such cases, contextual selectors can be used to specify context. Context is
described as an ancestor/descendent relationship between elements in the document tree.
For example, consider the following rules if we want all <EM> elements on our page to be
red.
We use the CSS em { color: red}, as shown in the example below:
Welcome to my Webpage
Follo,v 1ne on Facebook
However, if we only want all <EM> inside <H1 > be red, the we can use Hl em { color: red}.
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE> CSS Styles </TITLE>
<STYLE>
Hl em{
color:red;
}
</STYLE>
</HEAD>
<BODY>
<Hl>Welcome to my <EH> Webpage</EM> </Hl>
<P>Follow me on <EM> Faceboo k </EM></P>
</BODY>
</HTM L>
[j testHTML.html X
- ~
••
C (D fi le:/// C:/WebProject/ testHTML.html
* •
Welcome to my Webpage
Follo,Y n1e on Facebook
You might observe that the property color:red only affects the <EM> tag inside the <Hl>
tag.
Lesson 3: CSS Syntax Rules
A CSS is composed of style rules that are understood by a web browser and then applied to
the corresponding elements within your document.
t t '
t
Tag Property Value Property Value
A selector can be any HTML tag at which a style will be applied. This could be any tag like
<Hl>, <A> or <TABLE> etc. A property is a type of attribute of HTML tag. All HTML attributes
can be converted into CSS properties. Values are assigned to properties.
Selector Declaration
Hl {
t e xt - align: l e f t :
colo r : blue ;
}
In the given example above, since the rule is applied to the <Hl> tag, all <Hl> tags in your
web page will have an alignment of left with blue colored text.
Style grouping
CSS allows you to apply style to many selectors at once. If you want to use multiple selectors,
or apply a particular style to multiple selectors at once, separate each selector with a comma.
Hl, H2, Hl {
fon t- f ami l y: Verda na;
fon t-size: llpx;
fon t-weig ht: norma l :
l e t ter-spac i ng: .4ern :
}
The above example shows the way to apply styles to three selectors. Each selector will have
a font face of Verdana, a font size of 11, with the normal or default weight, and a letter spacing
of .4em.
~ testHTI\IIL.htm l
C ••
(D file:///C:/WebProject/ testHTML.html
Contents l1ere...
* •
~ onL1ne Ioed
V educatlon
www .amauo nline.com
CSSFonts
Fonts can also be configured through CSS in various ways. CSS Font properties can help
identify the font family, the boldness of font, and even the font style.
a. The font-family property is used to change the face of a font.
b. The font-style property is used to make a font italic or oblique.
c. The font-variant property is used to create a small-caps effect.
d. The font-weight property is used to increase or decrease how bold or light a font
appears.
e. The font-size property is used to increase or decrease the size of a font.
<!DOCTYPE html>
<html>
<head>
<title>CSS Background</title>
</head>
<body>
<p style= "font-style:italic; font:family;Arial , Helvetica , Sans-Serif ;">
This text will be displayed in either Arial , Helvetica , or the default
Sans Serif font depending on which font you have on your system.
</p>
</body>
</html>
The above code will set all texts enclosed inside the paragraph <P></P> tag to ITALICS and
will also be using the font family (depending on font availability) "Arial", "Helvetica" and
sans-serif. The succeeding texts after the declaration of the <P> </P> tags will use the default
font setting.
Font Variant, Weight and Size
Web content (such as text) can be modified in various ways.
The Font-Variant property allows you to change the uppercase display of text or use a font's
default setting.
Font-Weight on the other hand, allows you to set the font weight of an element. Possible
values could be normal, bold, bolder, lighter, 100, 200, 300, 400,500,600, 700, 800, 900.
The other property which can be used to modify font is the size property. The font-size
property is used to control the size of fonts. Possible values could be xx-small, x-small, small,
medium, large, x-large, xx-large, smaller, larger, size in pixels or in %.
The sample code below shows the different font structures in application.
<!DOCTYPE html>
<html>
<head>
<title>CSS Fonts</title>
</head>
<body>
<p style - "font-variant:small-caps;">This text will be displayed in small caps . </p>
<p style - "font-we ight: bold;">This font is displayed in bold. </p>
<p style - "font-weight:lighter;">This font is displayed lighter . </p>
<p style - "font-weight:600;">This font weight is 600.</p>
<p style - "font-size: 30px;">This font size is 30 pixels</p>
<p style - "font- size : small;">This font size is small</p>
<p style - "font-size:large;">This font size is large</p>
</body>
</html>
3. Rich, J.R. (2017). Working in the Cloud: Using Web-Based Applications and Tools to
Collaborate Online. Indianapolis, Indiana: QUE Publishing