UECS2094_2194_-_Topic_3_-_CSS_-_Jan_2025
UECS2094_2194_-_Topic_3_-_CSS_-_Jan_2025
TOPIC 3: CSS
UECS 2094/ UECS 2194 Web Application Development
Jan 2024
2
OUTLINE
• CSS basic syntax
• CSS properties
• Lists
• Tables
• Selectors
4
INTRODUCTION TO CSS
• CSS, or Cascading Style Sheets, is used to style each HTML.
• It's how we determine the colour, text size, and spacing, as well as other
visual aspects of your Web page.
• CSS can also be used to change the layout of your Web page.
5
EXAMPLE
<p>She looked over the top of her book and
whispered <q>I'm hungry.</q> My heart stopped.</p>
6
THE <style>TAG
• The best practice when working with CSS is to keep it in an external file using
the <link> tag. However, when starting, it is simpler to merely place it
directly into the document under edit.
• To place CSS directly into an HTML document, we use the <style> tag. This
tag can appear anywhere in an HTML document, however, the most
common practice is to place it in the <head> section.
7
THE <style>TAG
8
THE <link>TAG
• While <style> is convenient, the better practice is to put the CSS into a
separate file. One of the key advantages of using a separate file is that the
CSS styles can easily be re-used between your different .html pages. Many
authors further divide their CSS up into different files (for example: one for
text styles and another one for layout).
• Simply put your CSS into a separate file. This file does not need any HTML
markup (i.e., no <style> tag required). Use the .css file extension and use
a <link> tag to bind it in. The <link> tag must appear in
the <head> section. By convention, css files are kept in a directory named
css.
9
THE <link>TAG
10
INLINE CSS
• 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.
COMMENTS
• CSS can include "comments" as well, by which you, the developer today,
can leave notes and reminders to you, a different developer tomorrow. Or
to others who might read your CSS.
• Comments begin with /* and must end with */ and they can span several
lines. But they cannot be nested.
15
font-size
• font-size can be used to size the text of a tag. The value for the font-size has
two parts: a number and a unit. Some of the most common units
are: px, em, %, vh. For example:
p { font-size: 18px; }
q { font-size: .8em; }
blockquote { font-size: 10vh; }
• Additionally, font-size supports a more readable set of values that many authors
prefer: xx-small, x-small, small, medium, large, x-large, xx-large
and relative sizing (relative to the text of the parent): larger, smaller. For example:
p { font-size: medium; }
q { font-size: small; }
blockquote { font-size: larger; }
17
line-height
• Whereas font-size may drive the size of the text itself, the line-height property
drives the height of the space it is drawn into. A large line-height will give the text
more spacing. A small line-height will smash the text lines together.
• For example, all of the Middlemarch text below has font-size:16px;
But on the left, we see line-height:0.5; and on the right, line-height:3;
The used value is this unitless <number> multiplied by the element's font size. The computed
value is the same as the specified <number>. In most cases this is the preferred way to set line-
height with no unexpected results in case of inheritance.
18
text-align
• Anyone familiar with a text editor will be familiar with this property. It can be used to align
the text left, center or right. There are additional possible values
like justify and justify-all . It usually defaults to left. However, remember that you
shouldn't use text-align unnecessarily.
• Note that text-align may not work as expected if applied to elements that are the same
width as their text, or whose width is determined by the text within them (i.e., inline
elements). The tags <span>, <a>, <i>, <b>, <q> and others are considered "inline"
because they do not receive their own new line when used. And text-align is often not
useful on these tags.
• But it is useful on block level text tags, such as <p>, <li>, <ul>, <ol>, <div>,
and <blockquote>
p { text-align: left; }
blockquote { text-align: right; }
19
text-align
20
text-decoration (underline)
• How do I underline text? This is a common question. In CSS, this is done via
the text-decoration property. The values for this are: underline,
overline, line-through, and none; They can combined.
p { text-decoration: underline; }
a { text-decoration: none; } /* hyperlinks are underlined by
default, but that can be removed */
span { text-decoration: overline; }
span { text-decoration: underline overline; } /* apply two with
just a space between the values */
span { text-decoration: underline overline line-through; } /*
everything */
21
font-weight (bold)
• Any tag can make the text bolder (or less bold) via the font-weight CSS
property. While common values are normal and bold, text can also be
made bolder (or less bold) than its parent with the values bolder and lighter.
Lastly, the font-weight can be set explicitly as a numeric value. The choices
are: 100, 200, 300, 400, 500, 600, 700, 800 and 900.
• normal maps to 400 and bold to 700. However, the different numeric
choices will only work for fonts that support a full range of font-weights. Many
times the numeric weights will simply be mapped back to bold or normal.
22
font-weight (bold)
p { font-weight: bold; }
blockquote { font-weight: 900; }
23
font-style (italic)
• The <i> and <em> tags could make text italicized. But, just as we saw when
discussing font-weight, this can be changed with CSS, and any tag can
make its text italic or oblique with the font-style property. The choices
of values for this property are normal and italic.
24
font-family
• Want to set the font for an item on the page? The font-family is the correct property
for the task, however
• the various browsers only guarantee a few standard choices: serif, sans-
serif, monospace, cursive, and fantasy.
• To help ameliorate these limitations, the font-family property accepts a list of
possible font choices. The browser will start with trying the first font listed, and if not
available (or not having a needed glyph) it will then proceed to the next font in the
list, and so on. Here is a typical font-family declaration:
The rule above says to first try the font named "Helvetica". If it isn't available, try "Verdana",
failing that "Arial", and lastly fall back to the built in sans-serif browser font.
25
MARGIN
• The margin property is the lynch-pin for positioning elements. Whenever you
want to move something a little the margin property should be your
first thought, when having layout problems, it is the first thing you should
check.
COLOR
• The color property can be used to set the text color of an element. There are several possible
formats for the value.
• Named colors
p { color: blue; }
b { color: transparent; } /* transparent */
i { color: lightgrey; }
• rgb/rgba
p { color: rgb(10, 200, 255); }
p { color: rgb(0, 0, 0); } /* 0,0,0 is black */
p { color: rgb(255, 255, 255); } /* 255,255,255 is white */
b { color: rgba(10, 200, 255, 0.5); } /* semi-transparent */
• Hex code
p { color: #3A2BFF; }
27
UNITS
px
• 'px' is short for 'pixel', which is a single dot on the screen. So text with font-
size:20px is 20 pixels tall on-screen. In actuality, due to browser zooming,
retina displays, or other factors, this may or may not match to 20 physical on-
screen pixels.
• px are useful for both horizontal and vertical dimensions.
29
em
• 'em' is a typographic term that has come to the Web. On the Web, em units are
usually used for vertical dimensions. One 'em' maps to the height of one capital
letter in the parent context.
• All the text sizes above are relative to the pages base sizes. You'll see radically different
results on the rest of the page from either of these rules applied to the body, but relative to
one another they'll remain sized correctly.
30
rem
• 'rem' is much like 'em', except that 'em' sizes an element relative to its parent, and
'rem' always derives its size relative to the root. In an HTML document with lots of
nested elements, 'rem' will generally prove to be more reliable than 'em'. 'rem' is
supported in all modern day browsers, including mobile, but not older ones.
• Using the CSS rules from the em section, nested list items
(<li>birds<ul><li>hawk</li></ul></li>)would get increasingly smaller. And if
'rem' units were used, they would be the same size.
• Note: to ensure you are setting the root size, use both the html and body selectors.
• The percentage unit (%) is relative to the parent dimension. This is a useful unit for
both horizontal and vertical dimensions, though often more useful in the horizontal.
p {
margin-left: 10%;
margin-right: 10%; /* 10% of parent width will be spent on the two
side margins */
}
32
vh / vw
• 'vh' stands for viewport height, and 'vw' for viewport width. The vh and vw units
work much like the percentage ( % ) unit. But instead of percentage of the parent, it
is percentage of the screen (aka viewport). Obviously, vh is for vertical dimensions,
and vw for horizontal dimensions.
• vh and vw do not suffer the parent limitation that the % unit does. Most modern
browsers support these units, but there are some exceptions on older mobile
browsers.
p {
margin-left: 10vw;
margin-right: 10vw; /* 10% of screen width will be spent on the two
side margins */
}
33
BACKGROUND
• The background-color CSS property will fill the rectangle of the given element with
a solid background color. In addition to the values of transparent and none, there
are all the values of that we saw applicable to the color property.
BACKGROUND
• The background-image property is used to set an external image file as the
background to a particular HTML element. The path can be a URL, or a
path relative from the file the CSS is in.
BACKGROUND
• By default, if the rectangular area of an element is bigger than the image itself, then the
image will repeat and fill the space, like tiles.
• The background-repeat property can be used to control this. It's more commonly used
values are: repeat, repeat-x, repeat-y, and no-repeat. The no-repeat value is very useful,
and bears repeating.
CSS Result
.tile {
background-image:
url("https://www.w3.org/2008/sit
e/images/logo-w3c-mobile-lg");
/* use padding to keep the
text away from the edges of the
paragraph box */
padding-top: 15px;
padding-right: 50px;
padding-bottom: 30px;
padding-left: 100px;
}
36
BACKGROUND
CSS Result
.tile {
background-image:
url("https://www.w3.org/2008/sit
e/images/logo-w3c-mobile-lg");
/* use padding to keep the
text away from the edges of the
paragraph box */
padding-top: 15px;
padding-right: 50px;
padding-bottom: 30px;
padding-left: 100px;
background-repeat: space
}
37
BACKGROUND
• When not repeating, it is very useful to size a background image to fit its
element. The background-size can be used for this. There are two very
useful values: contain and cover.
• The contain value will put the entire image into the space of the element,
however, the space of the element may not be completely filled if the
aspect ratio of the element and the image do not match. The cover value
is the opposite. It will completely fill the element but the image may be
cropped off two opposite sides. Neither contain or cover will distort or
squish the image. Its aspect ratio is maintained.
38
BACKGROUND
background-size: contain background-size: cover
39
BACKGROUND
• Like background-size, background-position can be used to place or
offset a background image in the element. It takes two values (x and y)
separated by a space when used to exactly specify a position.
• The most useful is the center value. It is position the center of the image in
the center of the element. This is useful even with repeating tiles.
40
BACKGROUND
background-position:center
background-size: contain background-size: cover
41
STYLING LISTS
• list-style-type governs the little list marker that is usually positioned to the
left of any list item.
• For un-ordered lists (<ul>), there are several popular values:
• disc, circle, square, and none.
42
STYLING LISTS
• For ordered lists (<ol>) you can choose different ways of having the numbers
shown: decimal, decimal-leading-zero, lower-roman, upper-
roman, lower-alpha, upper-alpha, as well as several of the worlds
languages: armenian, georgian, simp-chinese-formal, and many others.
43
STYLING LISTS
• Besides choosing the type of marker applied to each list item, you may also
want to govern how closely it is positioned to the list itself. The list-style-
position property handles that. The two values are inside and outside.
• They govern whether the markers are positioned inside the box of the list, or
outside. This is most evident if a border or background or similar is applied to the
list. Below, we have put a blue border on the list.
44
STYLING LISTS
• The little markers on a list can also be customized to be an image of your
choosing using list-style-image. This will require you to have a small image
in a Web compatible format (PNG or JPEG recommended) and to know the
path from the place where the CSS is being defined to the image.
45
p { border-style: solid; }
46
p {
border-left-style: solid;
border-right-style: dotted; This same thing can be done
border-bottom-style: dashed; with border-width and border-
border-left-style: hidden; color ( border-left-
} color, border-top-width, etc.).
47
GENERAL GUIDELINE
3. do not use foreground and background colors that are too close to one
another, in other words, ensure there is good color contrast
EXERCISE
STYLING TABLE
• We now know how to put a basic table together in Topic 2. Here, we will look
at some useful CSS elements to style your table.
• border is best specified in CSS. It is a shorthand property meaning you can
set several CSS properties simultaneously.
STYLING TABLE
• To give a border to <table>, <th> and <td>:
• This creates two borders creating a double line. In order to collapse them all
into a single border, we use the border-collapse CSS property:
table { border-collapse: collapse; }
default
With border-collapse
56
STYLING TABLE
• You can also set the table width and height.
• With CSS, you can also explicitly set the dimensions of your cells. Width and
height can be specified in:
• units of length like pixels, percentage (relative to the table width), etc.
• auto: the browser will calculate and select a width for the specified element
(default value)
table.eg3
{
width: 100%;
height: 100%
}
57
STYLING TABLE
• text-align - This property is used to align the text of <th> and <td> cells
left, right or center.
• vertical-align - This property is used to align the text
of <th> and <td> cells: top, bottom or middle.
• padding - We use the padding property on <th> and <td> to provide some
space between border and content in cells. Padding is set in units of length
like px, cm, em and % (relative to parent container's width).
58
STYLING TABLE
th, td { padding: 0.1em 0.4em; } /* top and bottom, left and right*/
th, td { padding: 20px 30px 40px; } /* top, left and right, bottom*/
59
STYLING TABLE
• A zebra table has alternating colors for table rows making it easier to differentiate
data between rows. You can specify which rows you want to differentiate using a
different color. Typically, you apply this property to a set of even and/or odd table
rows to created a striped effect.
• The 'nth-child' selector matches every element that is the nth child of the table or
any parent element. Below is an example code to make the every third list item
grey.
tr:nth-child(3n) { background-color: grey; }
60
• visible - Content is not clipped and may be rendered outside the padding
box.
• hidden - Content that has overflowed is hidden. This makes the overflowed
content inaccessible.
• scroll - Content that has overflowed is made accessible via a fixed scroll bar.
• auto - Content that has overflowed is made accessible via a scroll bar, which
appears automatically when required.
62
EXAMPLE USAGE
table { border-collapse: collapse; }
div { height: 200px; width: 200px; border: 1px solid blue; }
table, th, td { padding: 30px; border-bottom: 1px solid black; }
OVERALL EXAMPLE
64
POSITION OF AN ELEMENT
• There are four properties which can be used to adjust or set the position of
an element. Those properties are: left, top, right, and bottom.
position PROPERTY
• static
• A position property of static is the default for all elements. It simply
means that all elements follow the "flowing text"model of layout and the only
properties influencing their position are margins, padding, and the display
property that selects block level layout, inline or inline-block. Static
elements ignore the positioning properties we read about earlier
(left, top, right, and bottom).
position PROPERTY
• fixed
• A fixed positioned element respects the positioning properties (left, top, right,
and bottom). A fixed positioned element is positioned against
the window rectangle (aka the viewport). This means that fixed position
elements will not scroll with the rest of the page. Fixed position elements can
easily "stick" to the side or bottom or top of the browser.
position: fixed;
67
SELECTORS
• tag selector
• We've already seen this one. A CSS selector that consists solely of a single
tag (without punctuation or spacing) will be applied to any matching tag on the
page.
• id selector
• You may remember the id attribute (short for "identifier"). Recall that No two tags
are allowed to have the same id. You may also recall that the id cannot contain
spaces, nor most punctuation, nor begin with numbers.
68
SELECTORS
69
SELECTORS
• class selector
• The class attribute is similar to the id. However, whereas the id must be
unique and singular, the values of the class attribute can be shared by multiple
tags. And, multiple classes can be assigned to a tag by simply separating them
with spaces.
70
SELECTORS
• The class selector is simply a period (.) followed by the class name itself.
71
COMBINING SELECTORS
• Being able to define a CSS selector in terms of a tag, class or id is very powerful. But it's
not practical to place classes on every tag in your document, much less to put unique
ids throughout.
• It's also inconvenient to constantly repeat CSS rules. But by combining composing
selectors, all that can be avoided.
72
COMBINING SELECTORS
• Comma separated selectors
73
COMBINING SELECTORS
• Comma separated selectors
The joined version on the right is much easier to read and maintain.
If the "speech" items need to also be bold, that can simply be added by an additional rule:
blockquote,
q,
.speech {
color: red;
font-style: italic;
}
.speech { font-weight: bold; }
74
COMBINING SELECTORS
• Specialized selectors
• If two selectors of different types (like tag and class) appear next to each
other with no spacing separating them, then they form a
specialized selector. To match, a candidate must match both rules. If a tag
selector is used, it must appear first.
COMBINING SELECTORS
• Specialized selectors
76
COMBINING SELECTORS
• Descendant selectors
• Consider this chunk of code:
<section id="intro">Welcome to <a href="#palaceland">PalaceLand</a>, world renown <q>Land of
endless palaces and <a href="#delight">delights</a></q>. As you make your way about, remember
the words of our founder <blockquote>Shouldn't we have <a href="#chairs">chairs</a>? Never made
much sense wandering room a room looking for a place to sit a spell. Folk that don't sit are
not likely all right in the <a href="#head">head</a></blockquote></section>
What if we wanted all the links in the introductory section to be red, but all the link in the guideline
section to be green? That is what descendant selectors are for.
77
COMBINING SELECTORS
• Descendant selectors
• We add this CSS code, merely separate the tag, identifier, or class selectors by a
space.
#intro a { color: red; }
#guideline a { color: #00FF00; }
78
COMBINING SELECTORS
• Direct descendant selectors ( > )
• Sometimes you don't want to apply a style to any _possible_ child, but to only to the
direct children. This can be done with the > symbol. Use it between selectors to limit
the application to the direct children of the parent.
• For example, this rule, if applied to the HTML of the previous selector, would cause
the links in the intro section to be larger, but not the links in any nested quotes or
blockquotes. :
COMBINING SELECTORS
• Everything selector (*)
• The asterisk (*) can be used to match any tag. By itself, this is only marginally
useful. But combined with other selectors into a descendant selector, it can
be pretty useful.
PRECEDENCE RULES
1. Most specific rule
• A more specific rule takes precedence over a less specific rule. A rule that more
tightly matches a particular element than a general rule will be applied.
• In the example above, both rules are attempting to set a span color for a span
inside a list item. However, the second rule will "win" when there is a conflict (like
color in this case).
81
PRECEDENCE RULES
2. id selector is the most specific
• Rules with an id selector (e.g. #someid ) are considered more specific than rules
without.
PRECEDENCE RULES
4. Rules that come later override those that come earlier
• This guideline is for two CSS rulesets with the same selector. Where there are
conflicts, the rules from the later one apply.
• In the example above, an element with the .hortense class will be underlined and
its color will be blue, because that rule came later than when it was set red.
83
END OF TOPIC 3