INF-250: Web Design and Development — CSS Text Formatting
Assignment review
Overall great work! Common issues:
- Forgetting to close tags.
- Leaving out the
<!doctype HTML>or one or more of the required HTML elements:<html></html><head></head><title></title><body></body>
- Having a complete
index.htmlfile but only partials in the other pages. - Using non-existing elements like
<welcome>or<p2>
Code Formatting
CSS Selector Specificity
When two CSS rules try to set the same property on the same element, CSS has to choose a “winner”. That’s the cascade.
If both rules match the element, the winner is usually: more specific selector. If specificity is tied: the rule that appears later in your CSS wins.
1) Source order
If the selectors are the same, then the rule that comes later wins.
/* Same selector, same property */
p {
color: red;
}
/* Later rule wins (it comes last) */
p {
color: blue;
}2) Specificity
Specificity is basically the “strength” of a selector. A helpful cheat sheet is from weakest to strongest:
- element (p, div, section)
- element element (descendant selector)
- .class
- element.class
- #id
- inline style
3) !important
!important can override the normal cascade. It’s sometimes useful, but it can also make future conflicts harder to fix.
/* !important overrides normal specificity (use sparingly) */
.intro {
color: purple !important;
}
p {
color: green;
}Use
!important sparingly. It’s often a sign that your CSS structure needs improvement. Style Inheritance
Some CSS properties automatically flow from a parent element down to its children. This is called inheritance.
Inheritance is mainly for text-related properties. Layout/box styling usually does not inherit.
colorfont-family,font-size,font-weightline-heighttext-align
background/background-colorbordermargin,paddingwidth,height
line-height
line-height controls the vertical space between lines of text. It’s one of the biggest “readability” settings.
normal(uses browser default)- Unitless number like
1.5(recommended; scales with font-size) pxlike24px
Example syntax:
body {
line-height: 1.6;
}
h1 {
line-height: 1.1;
}
p{
line-height: 24px;
}text-align
text-align aligns inline content (text) inside a block.
left,right,centerjustify(stretches lines to fill the width)
Example syntax:
/* text-align */
h1 {
text-align: center;
}
p {
text-align: left;
}p, div, section, etc.). It will have no effect on inline elements (like a, span, strong, etc.). <img /> elements are inline, so they will be affected by the text-align property of their parent. text-transform
text-transform changes the casing of text visually. It doesn’t change what you typed in your HTML.
none(default)uppercase,lowercase,capitalize
Example syntax:
/* text-transform */
section{
text-transform: uppercase;
}text-indent
text-indent indents the first line of a block of text.
emlike2em(common for paragraphs)pxlike24px%like10%(based on container width)
Example syntax:
/* text-indent */
/* Indent only the first line */
p {
text-indent: 2em;
}letter-spacing
letter-spacing adds (or removes) space between characters.
normal(default)- Lengths like
0.06emor1px(and negatives are allowed)
Example syntax:
/* letter-spacing */
h1 {
letter-spacing: 0.06em;
}
/* Negative values are possible, but use carefully */
small {
letter-spacing: -0.01em;
}Setting color values
Until now, we've been using text for colors, like red, blue, and green.
CSS actually has many different ways to specify colors, and you can even make your own custom colors. Here are a few examples:
#ff0000(hexadecimal RGB)rgb(255, 0, 0)(RGB function)hsl(0, 100%, 50%)(HSL function)rgba(255, 0, 0, 0.5)(RGB with alpha for transparency)
#ff0000, the first two digits (ff) represent the red value, the next two (00) represent green, and the last two (00) represent blue. So #ff0000 is pure red, while #00ff00 is pure green and #0000ff is pure blue. text-shadow
text-shadow adds a shadow behind text. Syntax: offset-x offset-y blur color.
offset-x: left/right, can be negativeoffset-y: up/down, can be negativeblur: softness (bigger value = softer shadow)color: the color of the shadow
Example syntax:
/* text-shadow */
/* offset-x offset-y blur color */
h1 {
text-shadow: 5px 2px 4px #ff0000;
}To apply multiple shadows, separate them with commas.
white-space (rules about spaces + new lines)
HTML normally collapses extra spaces and ignores line breaks in your source. white-space controls what gets preserved and whether text wraps.
normal: collapses spaces, wraps lines (default)nowrap: collapses spaces, does not wrappre: preserves spaces/newlines, does not wrappre-wrap: preserves spaces/newlines, wraps
Example syntax:
/* white-space */
/* Preserves spaces + new lines, and still wraps */
p {
white-space: pre-wrap;
}Using Google Fonts
- Select your font
- Click "Get Font" button
- Click "Get Embed Code" button
- For CodePen, use the "@import" option. For VSCode and GitHub, use the "link" option.
- Copy over the snippet. The @import rule goes in your CSS, and the <link> tag goes in your HTML
<head>element. - Find the
font-familyrule form the example CSS and copy it to your CSS.