Web Development Basics
Font, Colors, Images
by Dexter Leung, Aug-29, 2018

Font

How to change the font?

As if in document, email or presentation tools, you may need to format your textual contents using a variety of styles. In CSS, as discussed in the previous chapter, each style rule would apply to any elements described by the CSS selectors. However, what CSS properties are available for styling the texts?

font-family

CSS Syntax
font-family
[<font-family> | <generic-family>]#
<font-family> = <string> | ···<identifier>+
<generic-name> = serif | sans-serif | ···cursive | emoji | fangsong | fantasy | math | monospace | system-ui
W3C MDN
Full Syntax:
On
Off

font-family is a list of comma separated values defining the font family names or a generic group of family to be used. The list is of prioritized order, so the browser will display the text in the first font, and fallback to later fonts if it's not available on the device.

<font-family> is the exact name of the font and the string should be double-quoted, like "Times New Roman", "Calibri", "Helvetica", etc.

<generic-name> is a type of standard font that is defined by the system. Serif fonts are used in formal documents or traditional and retro style, while sans-serif fonts are used in presentations or user interface.
There are more types of generic fonts that you have reference on the W3C documentation.

font-weight

CSS Syntax
font-weight
normal | bold | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | ···bolder | lighter
W3C MDN
Full Syntax:
On
Off

font-weight refers to the boldness of the text. The values ranges the mulitple of 100 from 100 (thinnest) to 900 (boldest). Other than numerical values, keywords are allowed: normal refers to 400, and it's the default value, and bold refers to 700.

font-style

CSS Syntax
font-style
normal | italic | oblique
W3C MDN

font-style is a way to make the font italic which you may have been used to it. A new term oblique is introduced here that it is simply a sloped font while italic is usally cursive as well, and this difference can be typically found in English serif font. However, when either italic or oblique is not supported by the font-family, they will be interchangeably used. For example, abcijk (italic), abcijk (oblique - simulated), abcijk (oblique - actual) .

font-size

CSS Syntax
font-size
<length> | ···<length-percentage> | <absolute-size> | <relative-size>
<length-percentage> = <length> | <percentage> /* Percentage will resolve to a length */
<absolute-size> = xx-small | x-small | small | medium | large | x-large | xx-large
<relative-size> = smaller | larger
W3C MDN
Full Syntax:
On
Off

font-size is to describe the size of the text using a length value. I'll explain more on <length> in the next chapter - Layout, but now let me introduce 4 unit of measures that is useful in font-size values:

  • Absolute Lengths
    Usually in office suite, you may use pt as the unit of measure of the font size, and here you can in CSS:
    px: 1 pixel (Screen Pixel)
    pt: 1 point (1/72nd of 1 inch)
    These absolute length are defined no matter where the element places in the HTML document.
  • Font-Relative Lengths
    Font-relative lengths are particular useful for inheriting the length as a relative size of parent element.
    rem: ratio taking relative to the browser document default font size
    em: ratio taking relative to the parent element font size
Example 1
Font Size Relative Lengths
HTML
Copy All
<div id="ele1">
Element1
<div id="ele1_1">Element 1-1</div>
<div id="ele1_2">Element 1-2</div>
<div id="ele1_3">Element 1-3</div>
</div>
<div id="ele2">
Element2
<div id="ele2_1">Element 2-1</div>
<div id="ele2_2">Element 2-2</div>
</div>
Example 1
Font Size Relative Lengths
CSS

Try to edit the font-size to understand the usage of px, em and rem.

#ele1 { font-size: em ; }
#ele1_1 { font-size: em ; }
#ele1_2 { font-size: rem ; }
#ele1_3 { font-size: px ; }
#ele2 { font-size: px ; }
#ele2_1 { font-size: px ; }
#ele2_2 { font-size: em ; }
#ex1Dem #ele1 {font-size: $(ex1d1)em;} #ex1Dem #ele1_1 {font-size: $(ex1d2)em;} #ex1Dem #ele1_2 {font-size: $(ex1d3)rem;} #ex1Dem #ele1_3 {font-size: $(ex1d4)px;} #ex1Dem #ele2 {font-size: $(ex1d5)px;} #ex1Dem #ele2_1 {font-size: $(ex1d6)px;} #ex1Dem #ele2_2 {font-size: $(ex1d7)em;}
Element1
Element 1-1
Element 1-2
Element 1-3
Element2
Element 2-1
Element 2-2

line-height

CSS Syntax
line-height
normal | <number> | <percentage> | <length>
W3C MDN

line-height is the height of each line of text in that element. The number or percentage value should be greater than 0, and it's the ratio on the font size of the styled element. The normal value would give the number value around 1.0 - 1.2 for normal browsers.

Since it's defined as the ratio to its font size, if a <length> value is given with em as unit of measure, line-height value 1em = 1, 1.2em = 1.2, etc.
It's important to note that em is taking effect on the ratio of parent element, so the line-height attribute may be a bit odd to take effect on its children elements.

Example 2
Line Height Effects
HTML
Copy All
<div id="ele1">
Element1
<div id="ele1_1">Element 1-1</div>
<div id="ele1_2">Element 1-2</div>
<div id="ele1_3">Element 1-3</div>
</div>
<div id="ele2">
Element2
<div id="ele2_1">Element 2-1</div>
<div id="ele2_2">Element 2-2</div>
<div id="ele2_3">Element 2-3</div>
</div>
Example 2
Line Height Effects
CSS
Copy All
#ele1 { font-size: 1.2em ; line-height: 1.2em ; } /* Descendents will inherit relative ratio with respect to this element when using em. */
#ele1_1 { font-size: 1.5em ; }
#ele1_2 { font-size: 1.5rem ; }
#ele1_3 { font-size: 22px ; }
#ele2 { font-size: 1.2em ; line-height: 1.2 ; } /* Descendents will inherit relative ratio with respect to their own when using number/percentage. */
#ele2_1 { font-size: 1.5em ; }
#ele2_2 { font-size: 1.5rem ; }
#ele2_3 { font-size: 22px ; }
Demonstration
Element1
Element 1-1
Element 1-2
Element 1-3
Element2
Element 2-1
Element 2-2
Element 2-3

text-decoration

CSS Syntax
text-decoration
none | [ underline || line-through || overline ]
<'text-decoration-line'> || <'text-decoration-style'> || <'text-decoration-color'>
text-decoration-line : none | [ underline || line-through || overline ]
text-decoration-style : solid | double | dotted | dashed | wavy
text-decoration-color : <color>
W3C MDN
Full Syntax:
On
Off

text-decoration is used for adding a line to the text. You can make it underline, line-through (strike), or overline.

Latest version of Firefox and Chrome support full text-decoration definitions, while Edge only support the above simplified syntax.

Styling some single words

To style some single words in a paragraph, you can simply use the default HTML text-level elements like <strong> , <em>, etc. between the wordings.
If you'd like to have specific styles with indifferent usage meanings with those element types, you can use <span> elements. You can add attributes like class or id so your CSS selectors can formulate the styles of these special elements.

Example 3
Styling Text Level Elements
HTML
Copy All
<p>
It's high time for you to learn <strong>AI (artifical intelligence)</strong> in this year.
Many of us were crazy about <span class="highlighted">big data</span> in the past few years,
but the hardware technologies have been evolved and deep learning algorithms has become more sophisticated.
There have been many breakthroughs in <span class="highlighted">applying artificial intelligence</span> technologies in more advanced ways.
</p>
Example 3
Styling Text Level Elements
CSS
Copy All
.highlighted { background-color: yellow; color: black; }
strong { color: rgb(60, 195, 180); }
Demonstration

It's high time for you to learn AI (artifical intelligence) in this year. Many of us were crazy about big data in the past few years, but the hardware technologies have been evolved and deep learning algorithms has become more sophisticated. There have been many breakthroughs in applying artificial intelligence technologies in more advanced ways.

Explanations

As you can see, text-level elements can exist within the textual content of a paragraph. They can be individually styled using CSS selectors.

Colors

What are colors?

Color is one of the most common type of CSS property values. You may need to change the color of some texts, the background color, the border color, shadow color, etc. By changing the color value, it will change the display color of that CSS property. The following useful CSS properties would need a color value:

  • color: define the text color of an element
  • background-color: define the background fill color of an element
  • border-color: define the border color of an element (We'll discuss more on border in the next chapter)

Below will discuss how you can express a CSS color value:

rgb() / rgba()

A rgb() / rgba() function can return a CSS color. Let's talk about what is RGB in contextual meaning first:

When you look closer to each pixel of your screen, most likely, it is composed of 3 parts, a red, green and blue "light dots". In computational display, we control a color through the intensity of lightness of the 3 color dots. When all of them emits light at highest intensity, it is a white light. When none of them emits lights, it's simply a dark dot. If only one of them, say the red one, emits light at highest intensity, it shows red color. If the red and green emits together, it's yellow in color. Therefore, by controlling different intensity of red, green, and blue, the display color will be different.

The RGB term refers to the red, green and blue. In the rgb() function, you can return a specific color referring to through passing 3 parameters in the function for controlling red, green and blue intensity respectively. The RGBA term refers to the red, green, blue and alpha. Sometimes, elements will be stacked over one another, and we may have certain extent of transparency to look through the content.

  • r / Red:     [Number]     Min: 0; Max: 255
    The higher the value, the richer the red intensity of the color component is.
  • g / Green:     [Number]     Min: 0; Max: 255
    The higher the value, the richer the green intensity of the color component is.
  • b / Blue:     [Number]     Min: 0; Max: 255
    The higher the value, the richer the blue intensity of the color component is.
  • a / Alpha:     [Number]     Min: 0; Max: 1
    The higher the value, the less transparent the color is.

If the rgb values are the same, this would be grayscale as no one color component has a higher intensity than the others. If the values are higher, this would also results in a ligher color.

Example 4
CSS Colors - RGBA
HTML
Copy All
<div id="topEle">Top</div>
<div id="btmEle">Bottom</div>
Example 4
CSS Colors - RGBA
CSS
Copy All
#topEle {
background-color: rgba(255, 255, 0, 0.75);
color: rgba(120, 0, 0, 1);
/* There are more declarations to position the elements that are overlapsed. This will be discussed later. */
}
#btmEle {
background-color: rgba(90, 130, 220, 1);
color: rgba(255, 255, 255, 0.3);
}
Interactive Demo

Try to edit the rgba parameters to modify color values of the 2 <div> elements in this example.

#topEle {
background-color: rgba(, , , );
color: rgba(, , , );
}
#btmEle {
background-color: rgba(, , , );
color: rgba(, , , );
}
#ex4Dem #topEle { background-color: rgba($(ex4d1), $(ex4d2), $(ex4d3), $(ex4d4)); color: rgba($(ex4d5), $(ex4d6), $(ex4d7), $(ex4d8)); } #ex4Dem #btmEle { background-color: rgba($(ex4d9), $(ex4d10), $(ex4d11), $(ex4d12)); color: rgba($(ex4d13), $(ex4d14), $(ex4d15), $(ex4d16)); }
Top
Bottom

#RRGGBB[AA] / #RGB[A]

Instead of using rgb() / rgba() function, you can use this #RRGGBB[AA] / #RGB[A] hexadecimal notation values for expressing the colors in R-red / G-green / B-blue /[A-alpha] terms. Again, the A (alpha) is optional.

In hexadecimal notation, the full expression requires 2 digits for each RGBA values. The value goes from 00 to FF, which is the same as the rgba function from 0 to 256.
If you use the shortand expression, only 1 digit is used. 3 will stands for 33 and A would stand for AA.

To compare this notation with the rgba() function, let's look at the following equivalent values:

  • rgb(0, 0, 0) / #000000 / #000
  • rgb(255, 0, 120) / #FF0078
  • rgba(204, 47, 0, 1) / #CC2500FF
  • rgba(255, 85, 17, 0.797) / #FF5511CC / #F51C

hsl() / hsla()

A hsl() / hsla() function can also return a CSS color. HSLA stands for hue, saturation, lightness and alpha respectively. This is easier to understand contextually, and it should be familiar for those who have once used simple photo editors before touching with this concept of these parameters. Let's have a look on the meaning of the parameters in these two functions:

  • h / Hue:     [Number]     Any numbers, with every 360 as a full color spectrum
    The value would go through the color spectrum. Increasing the value will go from red (0), orange (30), yellow (60), green (120), cyan (180), blue (240), magenta (320) and finally red (360).
  • s / Saturation:     [Percentage]     Min: 0%; Max: 100%
    The higher the value, the higher the color saturation is. 0% would stand for grayscale.
  • l / Lightness:     [Percentage]     Min: 0%; Max: 100%
    The higher the value, the lighter the color is. 0% would always result in black while 100% would always result in white.
  • a / Alpha:     [Number]     Min: 0; Max: 1
    The higher the value, the less transparent the color is.
Interactive Demo

Try to edit the hsla() funcitons to modify color values of the 2 <div> elements.

#topEle {
background-color: hsla(, , , );
color: hsla(, , , );
}
#btmEle {
background-color: hsla(, , , );
color: hsla(, , , );
}
#ex4bDem #topEle2 { background-color: hsla($(ex4bd1), $(ex4bd2), $(ex4bd3), $(ex4bd4)); color: hsla($(ex4bd5), $(ex4bd6), $(ex4bd7), $(ex4bd8)); } #ex4bDem #btmEle2 { background-color: hsla($(ex4bd9), $(ex4bd10), $(ex4bd11), $(ex4bd12)); color: hsla($(ex4bd13), $(ex4bd14), $(ex4bd15), $(ex4bd16)); }
Top
Bottom

Named Color Values

Finally, there is an easier way to label the color, simply by what color names as if we're saying normally, like red, green, black, etc. However, often it is not clear in terms of color values what "red" is, and in real world "red" is oftern a range before we tell a color as "dark red". Therefore, W3C also defines the exact color value for each color name in the Color Specification. Below let's check with some named colors and their respective color values:

ColorColor Namergb()#RRGGBBhsl()
blackrgb(0, 0, 0)#000000hsl(0, 0%, 0%)
dimgray / dimgreyrgb(105, 105, 105)#696969hsl(0, 0%, 41.176%)
gray / greyrgb(128, 128, 128)#808080hsl(0, 0%, 50.196%)
silverrgb(192, 192, 192)#C0C0C0hsl(0, 0%, 75.294%)
whitergb(255, 255, 255)#FFFFFFhsl(0, 0%, 100%)
redrgb(255, 0, 0)#FF0000hsl(0, 100%, 50%)
orangergb(255, 165, 0)#FFA500hsl(38.824, 100%, 50%)
yellowrgb(255, 255, 0)#FF0000hsl(60, 100%, 50%)
limergb(0, 255, 0)#00FF00hsl(120, 100%, 50%)
greenrgb(0, 128, 0)#008000hsl(120, 100%, 25.098%)
aqua / cyanrgb(0, 255, 255)#00FFFFhsl(180, 100%, 50%)
bluergb(0, 0, 255)#0000FFhsl(240, 100%, 50%)
magenta / funchsiargb(255, 0, 255)#FF00FFhsl(300, 100%, 50%)
purplergb(128, 0, 128)#800080hsl(300, 100%, 25.098%)
deeppinkrgb(255, 20, 147)#FF1493hsl(327.574, 100%, 53.922%)
crimsonrgb(220, 20, 60)#DC143Chsl(348, 83.333%, 47.059%)
brownrgb(165, 42, 42)#A52A2Ahsl(0, 59.420%, 40.588%)
sandybrownrgb(244, 164, 96)#F4A460hsl(27.568, 87.059%, 66.667%)
goldrgb(255, 215, 0)#FFD700hsl(50.588, 100%, 50%)
springgreenrgb(0, 255, 127)#00FF7Fhsl(149.882, 100%, 50%)
skybluergb(135, 206, 236)#87CEEBhsl(197.822, 72.662%, 72.745%)
dodgerbluergb(30, 144, 255)#1E90FFhsl(209.6, 100%, 55.882%)
pinkrgb(255, 192, 203)#FFC0CBhsl(349.524, 100%, 87.647%)

Images

When to use images?

An impressive image would be memorable for the visitors in your website. You should wisely utilize images in your webpages. Images could be:

  • Theme-related Background Photo
  • Specially Designed Background Graphics
  • Content-related Images
  • Custom Images for GUI Icons

There are many ways to insert images or even draw canvas in an HTML page. In this chapter, we'll focus on the <img /> element and the background-image property.

<img />

The <img /> element is a box for you to display an image. You can specify the URI of the image where you have placed in your website folder using the src attribute. The width and height attributes defines the default display size (1 as 1px) of the image element.

In additinal to the default element size, you can resize the image using CSS properties to control the size of the embedding image bounding box using the width and height properties. These properties will be discussed in detail in the next chapter, while I have told you previously that it can be filled with a <length-percentage> value, like 8px or 29%. To keep the original aspect ratio, you may fill such values on either the width or height property, while the other one uses the autovalue.

If none of the elemental size attributes or CSS sizing declarations are defined, the image will be displayed as it's original resolution size.

object-fit

CSS Syntax
object-fit
fill | cover | contain | scale-down | none
W3C MDN

In some scenarios, the original aspect ratio of the image may not be the same as the image element container. You may need to resize and crop a rectangular image into a square to fit the layout design. In order to retain the aspect ratio of image display, you may style the <img /> element with the object-fit CSS property. This will act like a crop on the original image and fit into the designed size.

By default, the image is fill according to the width and height CSS sizing properties, which will stretch the image and the original aspect ratio won't be kept. Other values will resize or crop in the original aspect ratio with different characteristics depending on how to fill within the sized image container. You may try and observe different values on the upcoming demo.

Example 5
Image Sizing
HTML
Copy All
<img id="crescentImg" width="400" src="/img/travel/loc/xinyue1_s.jpg" />
Example 5
Image Sizing
CSS
Copy All
#crescentImg {
width: 250px;
height: auto;
object-fit: fill;
/* The CSS Sizing will control the size of the displayed image. */
}
Interactive Demo

Try to edit the sizing properties of the <img /> element in this example.

#crescentImg {
width: ;
height: ;
object-fit: ;
}
#ex5Dem #crescentImg { width: $(ex5d1); height: $(ex5d2); object-fit: $(ex5d3); }

background-image

CSS Syntax
background-image
<bg-image>
<bg-image> = none | <url> | ···<image-list> | <element-reference> | <gradient>
See full syntax on MDN.
W3C MDN
Full Syntax:
On
Off

background-image is a CSS property specifying an image showing as the background of an HTML element. It is noted that by the meaning of context, the element is not representing an image as a content delivery, but instead using based on a decorational reason.

Say, you may use image A as background-image on an <img /> element with image B, but image A will be displayed to users if image B does not fill the entire element size, or the image B has transparent pixels which can look through the background on image A. To conclude, image B is the main content of delivery but image A is only a decorative background.

background-size

CSS Syntax
background-size
<bg-size>
<bg-size> = [ <length-pecentage> | auto ]{1,2} | cover | contain
<length-percentage> = <length> | <percentage>
W3C MDN
Full Syntax:
On
Off

Sizing the background image requires the background-size CSS property. It provides values of cover and contain which act like the behavior of object-fit. A value of <length-pecentage> or auto is allowed to provide once or twice (separated by a space) such that the value represents the fixed-aspect ratio or corresponding x and y sizing of the background image. If a percentage is provided, the percentage is with respect to the element size.

Example 6
Background Image Sizing
HTML
Copy All
<div id="hiFlower">Hi, Flower!</div>
Example 6
Background Image Sizing
CSS
Copy All
#hiFlower {
width: 250px;
height: 250px;
background-image: url("/img/travel/home/1404_3s.jpg");
background-size: cover;
color: white;
font-weight: 800;
text-shadow: 0 0 .25rem black;
/*There are more CSS properties to control the position of the text within the element.*/;
}
Interactive Demo

Try to edit the background-size property and the element size properties of the <div> element in this example.

#hiFlower {
width: ;
height: ;
background-size: ;
}
#ex6Dem #hiFlower { width: $(ex6d1); height: $(ex6d2); background-size: $(ex6d3); }
Hi, Flower!

Image Positioning

The object-position CSS property provides declaring the position of the image in an <img /> element, and the background-position CSS property is for image(s) defined in background-image used. They have very similar value definition, and don't mix up for the 2 different target of uses.

CSS Syntax
object-position
<position>
<position> = [ [ center | left | right ] || [ center | top | bottom ] |
[ center | left | right | <length-percentage> ] [ center | top | bottom | <length-percentage> ]? |
[ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ] ]
W3C MDN
CSS Syntax
background-position
<bg-position>
<bg-position> = [ [ center | left | right | top | bottom | <length-percentage> ] |
[ center | left | right | <length-percentage> ] [ center | top | bottom | <length-percentage> ] |
[ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ] ]
W3C MDN

Let me explain more on this properties, and I'll focus on the background-position first.

By default, if an image is not fill and be stretched to the container's size, the image will be placed on the left-top corner of the container and crop according to the element's size. You can specify value keywords like center, left, right, top and bottom for defining the position of the image. One or two such semantic positiong values can be given, and should be separated by a space. In case only one value is given, like left, it will be placed at the left-middle of the cortainer. Therefore, it is simply placing a center value as the 2nd value.

One or two <length-percentage> can be given. Such value will be the length or percentange with respect to the left-top corner of the container. One value will be enforcing the same value for x & y relative coordinates, and two values will be referencing x & y coordinates in the x then y order.

The "x then y" ordering schema is also applied when <length-percentage> is used mixing with the semantic positiong values as discussed above. For example, the value background-position: right 30%; actually equals to background-position: 100% 30%;. Based on such rule, it is meaningless to state background-position: top 69%; because top is not a semantic value for horizontal positioning.

The last possible type is a 3/4-valued declaration. This is actually defining 2 groups of values, with each of them coposing of 1 or 2 values with the semantic positiong values comes first followed by an optional <length-percentage>. This is to specify the position relative to the horizontal and vertical edges. There is no x then y ordering. For example, background-position: bottom 69% left; is possible, referring a position located at 69% above the bottom edge and horizontally aligned at left, which is the same as background-position: 0% 31%;. You can also specify center if you don't want to position at the center for the second direction.

Interactive Demo

Re-try to flower example, and try to edit the background-position property.

#hiFlower {
width: ;
height: ;
background-size: ;
background-position: ;
}
#ex6bDem #hiFlower2 { width: $(ex6bd1); height: $(ex6bd2); background-size: $(ex6bd3); background-position: $(ex6bd4); }
Hi, Flower!

The object-position accepts the <position> syntax, which is a stricter form of the <bg-position>. It does not allows a 3-value syntax. Since <position> is used in many other CSS properties, the 3-value syntax may bring some ambiguities. Normally, if you are using the object-position, declaring the CSS property using <bg-position> definition is acceptable.

Now, we have a glimpse on basic styling. Let's probe deeper into layout in the next chapter, and we'll have a better understanding on sizing, positioning and more presentational CSS properties!