Professional Documents
Culture Documents
Last week we learned how to add visual appeal to our sites through images. We can
take that even further with CSS. CSS stands for cascading style sheets. The style
sheets are what defines how the page is laid out and how it looks. You can manipulate
your text size and color, where your images are, alignment, margins, and much more.
Think of HTML as the bones of your website and CSS is how you flesh it out.
Developers can add CSS in three ways. First is inline style. This would be adding style
within your HTML elements, such as <p style=font-size: 2em>. But adding style to
each element separately can be tedious and if you want to make a change, you would
have to go through every element on the page. Often a better choice is using a
stylesheet. An internal style sheet, just as it sounds, is within your HTML file. It goes
inside the head tags. The example above would only set the font size for that one p
element, but if I used a stylesheet, it would apply to every paragraph on the page. And
if I wanted to change something, one change would change all elements that the style is
set to. An external style sheet involves creating a separate file for your CSS outside of
your HTML. This can be an advantage for a few reasons. You can link several pages
to one style sheet. Just like before, you can make one change and it will apply to
everywhere, but in the case of an external sheet, one change can apply to your entire
site. Also, having one stylesheet for multiple pages can help your website have a
consistent look and feel.
The Cascading part of the CSS acronym refers to the order in which the styles are
applied. The first two styles the browser checks are ones the developer cannot control.
It starts with the browser defaults. Then it checks for any changes made by user
settings. These would override the browser defaults. The next styles the browser
would check for are those in an external style sheet, which overrides the styles before it.
Then comes the internal style sheet, and last, the inline styles. As the browser moves
down the cascade, each type of style overrides the one before. One example of how
you might make use of this is if you had an external style that set all fonts to blue, but
you had one special section that you wanted to be red. If you used an inline style for
that one element, it would override your external style sheet and make that one element
red.
To create an inline style, add style= inside your opening tag. After that you need a style
rule which includes a property and a value. The rule goes in quotation marks:
Commenting in CSS
You can add comments to CSS much like you can in HTML, but the tags are different.
In CSS you will use /* to start and */ to end each comment. This will look like:
/*Your comment here*/
Colors
In CSS you can assign color to an element in several different ways. We have looked at
color names, which are often the easiest to remember. You know exactly what red,
blue, black, etc. are, but not all colors have a name assigned to them. Colors have an
RGB value. This stands for Red Green Blue and refers to the amount of each primary
color that makes up the color you are using. In your markup this will look like
rgb(0,0,255). You can also use hexadecimal values. These look like #123456. You
may be wondering how you know what numbers to use. There are several great
resources online. Here is one of my favorites. If you look at the menu on the left you
will see that it has the colors grouped in different ways to make it easier. You can look
by color group, by name, by rgb or hex value, and several different options. You should
have no trouble finding the color you need. Once you have the color name or code you
need, you can put it into your CSS.
border-color: blue;
Remember that two-word properties must be hyphenated and you need a semicolon at
the end of each declaration.
You can also set an opacity for your colors. Opacity makes objects transparent. An
opacity set to 1 has no transparency. 0 would be completely transparent. You can use
decimals to set a transparency anywhere in between. You can use the opacity property
for any element, like this:
opacity: 0.75;
or add it your color like this:
rgba(100,200,100,0.75)
Note that there is an "a" with the rgb and the opacity goes last.
Styles
You may sometimes find that you want to apply a different style to certain elements,
such as all the links in the aside should be a different color from the links elsewhere on
the page. In this case, we can use the hierarchy of page elements in our selectors. A
parent element is one that contains other elements. These inner elements are called
child elements. We can use both in our selector as
parent child {styles}
or in the case of our example
aside a {color: green}
This would override any styles you had set with a selector of only a.
We have discussed adding ids and classes to elements to help in using those later. In
your CSS you can make use of those as selectors. If you used an id on an element,
you can use it as a selector with a # in front of it. So if you gave an element id="red",
you can use #red as the selector. Classes work in a similar way, but with a . instead.
So an element with class="red" can use the selector .red. Because classes can be
used on multiple elements, you can include the element type with it. So if you want h1
elements with the class red, the selector would be h1.red.
Styling Fonts
You can specify many fonts for your web pages. There is a long list of fonts that are
standard on every browser, but just in case, it is recommended that you add a couple
items to your font declaration. You start with your preferred font, then a more common
font for the browser to use if it doesn't have that one, then a generic font type. This
might look like:
font-family: 'Brush Script', Arial, sans-serif;
Note that the two-word font name is in quotes. You can use single or double quotes, as
long as you use the same type of quotes on both sides.
Absolute measures
in inch
cm centimeter
mm millimeter
Font sizes can be in absolute or relative units, much like the images we discussed in
Week 2. Here are those charts for you again.
Notice that the relative measures are according to the size of the text in the parent
element. So if you use 2em as a text size, then the text will be two times the size of the
text in the parent element.
Relative measures