Sei sulla pagina 1di 9

Adding CSS to your HTML document

Three kinds of style


So far we've seen three different ways to add a style to your HTML. It's important to
understand all of them (they are all useful) but we are mostly going to use number 3 from
now on.
1. A style attribute

This only affects the particular HTML element that it's on.
2. A style element

Writing the style this way will add that style to all of the specified elements (in this example,
all h1 elements) in the document.
3. A linked stylesheet

This separate stylesheet works exactly the same as the style element, but it's nice to keep
the style entirely separate in a different file. This means you can use the same styles across
multiple HTML pages.

Setting the style on each element (every single time!) is annoying, especially if they're the
same. The following code changes the font size of all p elements to 10px.
<!DOCTYPE html>
<head>
<title>Font Styles</title>
</head>
<body>
<p style="font-size: 10px">A tiny paragraph</p>
<p style="font-size: 10px">Another tiny paragraph</p>
<p style="font-size: 10px">Another third tiny paragraph</p>
</body>

You've just written a little bit of CSS to change the font size. Watch for the colon (:) and the
semi-colon (0) they're important!

WEBSITE TASK #11


Change the font size to 12px.
Question: How many times did you have to make changes?

We can use a style element to set them just once, instead. The styles defined in
the style element apply to the whole document:
<!DOCTYPE html>
<head>
<title>Font Styles</title>
<style>
p{
font-size: 10px;
}
</style>
</head>
<body>
<p>A tiny paragraph</p>
<p>Another tiny paragraph</p>
</body>

WEBSITE TASK #12


Change the font size to 12px.
Question: How many times did you have to make changes?

You've just used a little bit more CSS inside the style element!
The first part, before the open brace ({), is called the selector. It selects which
HTML elements the styles should apply to. In this case, the p selector selects
every p element.
The styles inside the braces apply to the selected elements, in this case setting
the font-size of all paragraphs to 10px.

Text align
Text alignment determines where text is placed horizontally on a page. The four
main values for text alignment are left, right, center and justify.
<!DOCTYPE html>
<head>
<title>Font Styles</title>
<style>
h1 {
text-align: center;
width: 500px;
<!-- The width property controls how wide an element is. By default, headings and
paragraphs fill up the entire width available, but you can make it smaller. -->
}
p{
text-align: justify;
width: 500px;
}
</style>
</head>
<body>
<h1>Centered Heading</h1>
<p>Justified text lines up the left and right edges by enlarging or shrinking the spaces
between letters and words.</p>
<p>However, this can creates lines that are too cramped or have holes, and studies
have shown that text with ragged (non-justified) edges are easier to read, because
the difference in line length helps you keep track of which line you're on.</p>
</body>

WEBSITE TASK #13


You've collected some inspirational quotes and want to share them with your friends and
family!

Turn it into HTML


Convert the plaintext quotes into a full HTML document with headings and paragraphs. The
heading My Favourite Inspirational Quotes should be a level 1 heading. The quotes and
names should all be in separate paragraphs.
My Favourite Inspirational Quotes
I attribute my success to this: I never gave or took any excuse.
Florence Nightingale
The most difficult thing is the decision to act, the rest is merely tenacity.
Amelia Earhart
Twenty years from now you will be more disappointed by the things that you didn't do than
by the ones you did do, so throw off the bowlines, sail away from safe harbor, catch the
trade winds in your sails. Explore, Dream, Discover.
Mark Twain

Give it some style


Add a <style> element to this page, and set the following properties inside it:
The h1 element should be 350px wide (width) and centre aligned;
The paragraphs should be 350px wide and have line height 1.5.
Right align the paragraphs that contain names by setting style attributes on them.
The result should look like this:

Body Attributes
Background colour and Font colour
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: rgb(255, 20, 147);
color: white;
}
</style>
</head>
<body>
<h1>Page heading</h1>
</body>
</html>

WEBSITE TASK #14

Copy this code to a new webpage. Paste the output below.

Now change the attributes for the background and text colour. Paste the new output
below.

Adding Margins, Borders and Padding:


Margins
<!DOCTYPE html>
<html>
<head>
<style>
p{
margin-left: 50px;
<!--The margin property controls space between elements; you can specify
margin-top, margin-right, margin-bottom and margin-left.-->
}
</style>
</head>
<body>
<p>Emma Woodhouse, handsome, clever, and rich, with a comfortable home and happy
disposition, seemed to unite some of the best blessings of existence; and had lived
nearly twenty-one years in the world with very little to distress or vex her. </p>
</body>
</html>
Margin Short hand: to save time use the margin property:
margin: 10px; sets all four margins to that value.
margin: 0 50px; sets top and bottom to the first value, and left and right to the
second value.
margin: 2px 4px 6px 8px; sets the top, right, bottom, and left (clockwise from
top)
Borders
We add a border to an element using the border property.
Like margin, border is also a shorthand property. It sets the border-width, borderstyle and border-color (in that order):
p{
border: 2px dashed red;
}
<p>China borders the largest number of countries. Its 14 neighbours are India,
Pakistan, Afghanistan, Tajikistan, Kyrgyzstan, Kazakhstan, Mongolia, Russia, North
Korea, Vietnam, Laos, Myanmar, Bhutan and Nepal.</p>
Borders can be dashed, dotted etc
Add the code above to the margins code and test what it does.
Padding
The padding clears space around the content (inside the border of
an element).

WEBSITE TASK #15


Download the file AliceIndex.html and the image files from Moodle. Create the CSS file to
style the page to do the following:
You want to centre the poem on the page, but centre aligning makes it hard to see the
anagram. (HINT: Use auto-centred margins.)

Put your styles in a file named main.css


o Make the paragraph elements 240 pixels wide.
o Give the paragraphs 11 pixel margins on the top and bottom, and auto-centre
them with margins.
Centre the heading using text-align to centre the h1 heading.
Give the images some space
o Give them 20 pixels margin on the top but zero on the bottom.
o Give them 5 pixels margin to the left and right.

WEBSITE TASK #16


Download the LeapDayIndex.html file from Moodle.
Add the styles below to make a page which looks like a Wikipedia page.

Make the body margins 20 pixels on the top and bottom and 24 pixels on the sides.
Give all paragraphs a top margin of 0 pixels.
Give all headings (both h1 and h2!) a margin of 5 pixels on the bottom.
Put a border under each heading which is 1 pixel wide, and rgb(170, 170, 170).
Make the headings (not the other text) use the PT Serif webfont.
o Use @import url(https://fonts.google.com/specimen/PT+Serif); in the head
tags.
All the links should be coloured rgb(6, 69, 173) and have no underline.
a{
text-decoration: none;
}

Make the span element use font size of 14 pixels.


<span style="color: rgb(135, 206, 250)">text to turn a different colour here</span>

WEBSITE TASK #17


Every web designer needs a website! Let's help Sally with the landing page of hers. She's
done most of the website, and it looks very clean but those links need a bit of work!
Download the SallyIndex.html file from Moodle.
Add the styles below to make the file look as below.

Style the links: Make the link text look nicer by making the text coloured white, sized
24 pixels, and remove the underline (you may need to look back through the notes
to remember how to do this).

Give the links some padding they should have 10 pixels padding on the top and
bottom, and 25 pixels padding on the sides.

The links don't look clickable anymore, let's put a border around them. The border
should be:
o 2 pixels wide
o solid (not dashed)
o white
Make the corners rounded by giving it a border-radius of 26 pixels.

Potrebbero piacerti anche