You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5.2 KiB
5.2 KiB
CSS - Typography, Icons
Lesson Objectives
- Explain how to load fonts into your website
- Explain what a sprite sheet is and why we use it
- Explain what an icon font is and what its advantages are
- Explain what SVG stands for, what it is, and what its advantages are
Explain how to load fonts into your website
- Up until recently, fonts on the web were limited to what was installed on a computer
- You could only depend on users having "System" fonts that were installed on every machine by default
- Now you can load fonts into your website so each user doesn't have to install them manually
Use a font-file
- You or a designer can download or create actual font files and then reference them in your css files.
- Good sites:
@font-face {
font-family: 'MyWebFont'; /* specify how you want the font to be referenced in the font-family property */
src: url('fonts/Architects_Daughter/ArchitectsDaughter.ttf'); /* location of the font file */
}
html {
font-family: 'MyWebFont'; /* call the web font */
}
Use a link to a CSS file
- Some font sites will give you the css file already, so you don't have to download the font files themselves
Explain what a sprite sheet is and why we use it
What and why
Most developers use sprite sheets instead of individual images for each of their icons.
- Making many HTTP requests for many small files is slower than downloading and processing one large file.
- Your browser will cache (save locally) the image
- If you visit a different page with different icons from the same sprite sheet, the image is already available and loads from memory, not the server.
How
- Create a tag with
background-image:url(),background-position-x,background-position-y,width,height; - use an
imgtag inside another tag with a specificwidth,height,position:relative, andoverflow: hidden;. Move theimgaround withtop,left, andposition:absolute
Explain what an icon font is and what its advantages are
- Sprite sheets have fixed size icons. You can make the icons bigger, but they can end up looking pixelated
- If you want the same icon in a different color (perhaps each page has a different color theme) you have to add a new image to the sprite sheet.
- Icon fonts are just fonts, but instead of looking like letters, each "character" looks like an icon.
http://fontello.com/
- Using the font's characters
- Fontello goes one extra step by assigning icon shapes to unprintable characters in case you want to use that font for normal characters.
- Behind the scenes, traditional characters are stored as numbers.
- There are lots of other unusual characters that most developers never use (bells, weird whitespace characters, foreign characters).
- http://unicode-table.com/en/
- Go to the "Customize Codes" tab
- You'll see above the icon is a box
- this represents an unprintable character
- Below the icon is the character's Unicode value (a four digit hexadecimal number)
- You can represent this character in two ways
- inside your HTML:
(E800 is the 4 digit hex value) - inside your CSS:
selector:after/before { content: '\E800' };- you can insert content using CSS
- inside your HTML:
- You could also change the character to be normal character
- Fontello goes one extra step by assigning icon shapes to unprintable characters in case you want to use that font for normal characters.
- You can use their css and give your html classes
Demonstrate how to make icons using SVG
- Icon fonts can only be one color
- SVG can be styled just like CSS
Many ways to add SVG to your page
-
One external image
- Can't restyle
- Hover, etc doesn't work
img { width: 200px; height: 200px; }<img src="img.svg" /><svg xmlns="http://www.w3.org/2000/svg"> <style> #checkmark { fill: red; } #checkmark:hover { fill: blue; } circle { stroke: #006600; fill : #00cc00; } circle:hover { stroke: darkblue; fill : lightblue; } </style> <path id="checkmark" d="M27 4l-15 15-7-7-5 5 12 12 20-20z"></path> <circle cx="40" cy="40" r="24" /> </svg> -
Write SVG directly into HTML
<svg xmlns="http://www.w3.org/2000/svg"> <path id="checkmark" d="M27 4l-15 15-7-7-5 5 12 12 20-20z"></path> <circle cx="40" cy="40" r="24" /> </svg>svg { width: 200px; height: 200px; } #checkmark { fill: red; } #checkmark:hover { fill: blue; } circle { stroke: #006600; fill : #00cc00; } circle:hover { stroke: darkblue; fill : lightblue; } -
External CSS/External image
- Each differently styled element must be included and given id
<svg> <use id="checkmark" xlink:href="img.svg#checkmark"></use> <use id="circle" xlink:href="img.svg#thecircle"></use> </svg><svg xmlns="http://www.w3.org/2000/svg"> <path id="checkmark" d="M27 4l-15 15-7-7-5 5 12 12 20-20z"></path> <circle id="thecircle" cx="40" cy="40" r="24" /> </svg>svg { width: 200px; height: 200px; } #checkmark { fill: red; } #checkmark:hover { fill: blue; } #circle { stroke: #006600; fill : #00cc00; } #circle:hover { stroke: darkblue; fill : lightblue; }