Layout absolute poz

17
1 WEB PAGE LAYOUT METHOD 1: * ABSOLUTE POSITIONING Absolutely positioning all the elements of an html page is not the most elegant, flexible or semantically proper way to go about making stuff go where you want it to go. But it’s a great way to get started. *

description

 

Transcript of Layout absolute poz

Page 1: Layout absolute poz

1

Web Page Layout MethoD 1: *

absoLute Positioning

Absolutely positioning all the elements of an html page is not the most elegant, flexible or semantically proper way to go about making stuff go where you want it to go. But it’s a great way to get started.

*

Page 2: Layout absolute poz

2

overvieW

➸ Start with an html page that has all the content marked up, but not yet styled with CSS.

➸ Make a div with the id wrapper (<div id=”wrapper”>) and put all your content (everything inside the body element ) in it.

➸ In the CSS, style the wrapper div like so: #wrapper { margin:0 auto; position:relative; width:(some # of pixels); height:(some # of pixels); }

➸ Put each chunk of content inside a new div. Give each div an id that describes the content.

➸ In the CSS, create a style selector for each div. Give each:

position:absolute; height:_________; width: _________; top: ___________; left: ___________;

Top and left values tell the div where to be inside the wrapper div, and your page is now beautifully arranged!

Page 3: Layout absolute poz

3

Start with an html page that has all the content marked up, but not yet styled with CSS.

absoLute Positioning steP-by-steP

The markup: In the Browser:<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”></style>

</head>

<body>

<img src=”orange_sp.gif” alt=”orange sperm whale”>

<h1> Chapter 1: The Whale</h1>

<p>Call me Ishmael. </p>

<p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

<p>Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet...</p>

<h3>Some Whales</h3><ul> <li>The Right Whale</li> <li>The Fin Back Whale</li> <li>The Hump-backed Whale</li> <li>The Razor Back Whale</li></ul>

</body>

</html>

1

Page 4: Layout absolute poz

4

Make a div with the id wrapper and put all your content (everything inside the body element ) in it.2

absoLute Positioning steP-by-steP

<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”></style>

</head>

<body>

<div id=”wrapper”><img src=”orange_sp.gif” alt=”orange sperm whale”>

<h1> Chapter 1: The Whale</h1>

<p>Call me Ishmael. </p>

<p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

<p>Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet...</p>

<h3>Some Whales</h3><ul> <li>The Right Whale</li> <li>The Fin Back Whale</li> <li>The Hump-backed Whale</li> <li>The Razor Back Whale</li></ul>

</div><!--end wrapper--></body>

</html>

The markup:

Now everything is tucked in a div

called wrapper

Page 5: Layout absolute poz

5

How does it look in the browser? Exactly the same. You haven’t styled div id wrapper.3

absoLute Positioning steP-by-steP

In the Browser:

Page 6: Layout absolute poz

6

So make a CSS style for div id wrapper: 4

absoLute Positioning steP-by-steP

<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”>

#wrapper { position:relative; width:600px; height:1000px; margin:0 auto;}

</style>

</head>

<body>

<div id=”wrapper”><img src=”orange_sp.gif” alt=”orange sperm whale”>

<h1> Chapter 1: The Whale</h1>

<p>Call me Ishmael. </p>

<p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

<p>Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet...</p>

<h3>Some Whales</h3><ul> <li>The Right Whale</li> <li>The Fin Back Whale</li> <li>The Hump-backed Whale</li>

Position:relative; will make the layout magic

happen later

Margin: 0 auto; makes the content – all the stuff inside

the div with the id “wrapper” – center inside the browser

window, no matter how wide the window.

Height and width give your web page a fixed width and height

Page 7: Layout absolute poz

7

Now how does your web page look? Centered, and 600px wide. W00t!5

absoLute Positioning steP-by-steP

In the Browser:

Page 8: Layout absolute poz

8

Next step: put each chunk of content you want to position inside a new div. Give each div an id that describes the content.6

absoLute Positioning steP-by-steP<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”>

#wrapper { position:relative; width:600px; height:1000px; margin:0 auto;}

</style>

</head>

<body>

<div id=”wrapper”>

<div id=”header”> </div><!-- end header --> <div id=”littlewhale”> <img src=”orange_sp.gif” alt=”orange sperm whale”> </div><!-- end little whale --> <div id=”chapter1”> <h1> Chapter 1: The Whale</h1> </div><!-- end chapter1 --> <div id=”maincontent”> <p>Call me Ishmael. </p> <p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p> <p>Whenever I fi nd myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I fi nd myself involuntarily pausing before coffi n warehouses, and bringing up the rear of every funeral I meet...</p> </div><!-- end maincontent --> <div id=”sidebar”> <h3>Some Whales</h3> <ul> <li>The Right Whale</li> <li>The Fin Back Whale</li> <li>The Hump-backed Whale</li> </ul> </div><!-- end sidebar -->

</div><!--end wrapper--></body>

</html>

The markup:

Div for image

Div for paragraphs

This div is empty. I created it to make the pink header box. I’ll use a background-im-age declaration in CSS to create the color.

Div for sidebar list

Div for headline

Page 9: Layout absolute poz

9

How does it look in the browser? Exactly the same. You haven’t created any CSS styles. So let’s do it!7

absoLute Positioning steP-by-steP

In the Browser:

Page 10: Layout absolute poz

10

It helps to start with a sketch in which you decide where you want to put stuff:8

absoLute Positioning steP-by-steP

Page 11: Layout absolute poz

11

Use CSS selectors to style the divs you made so that they go where you want them to go within div id wrapper.

If you recall, #wrapper centers everything in it. And its position:relative delcaration makes it possible to position the divs within it.

9

absoLute Positioning steP-by-steP

<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”>

#wrapper { position:relative; width:600px; height:1000px; margin:0 auto;}#header { position:absolute; width:600px; height:125px; background-color:#FFCCCC; top:0; left:0;}#littlewhale { position:absolute; top:0; left:0;}#chapter1 { position:absolute; top:33px; left:250px;}#maincontent { position:absolute; width: 350px; height: 400px; top:125px; left:250px;}#sidebar { position:absolute; top:125px; left: 0; width:225px; height:400px; border-right: 1px solid #FFCCCC;}</style>

</head>

<body>

<div id=”wrapper”>

<div id=”header”> </div><!-- end header --> <div id=”littlewhale”> <img src=”orange_sp.gif” alt=”orange sperm whale”> </div><!-- end little whale --> <div id=”chapter1”> <h1> Chapter 1: The Whale</h1> </div><!-- end chapter1 --> <div id=”maincontent”> <p>Call me Ishmael. </p> <p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

The “wrapper” selector has this declaration: position: relative;which enables all the divs inside of it to be positioned absolutely in relation to the wrapper div.

Page 12: Layout absolute poz

12

To create a pink header box and place it at the top of the page, create #header CSS:q

absoLute Positioning steP-by-steP

<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”>

#wrapper { position:relative; width:600px; height:1000px; margin:0 auto;}#header { position:absolute; width:600px; height:125px; background-color:#FFCCCC; top:0; left:0;}#littlewhale { position:absolute; top:0; left:0;}#chapter1 { position:absolute; top:33px; left:250px;}#maincontent { position:absolute; width: 350px; height: 400px; top:125px; left:250px;}#sidebar { position:absolute; top:125px; left: 0; width:225px; height:400px; border-right: 1px solid #FFCCCC;}</style>

</head>

<body>

<div id=”wrapper”>

<div id=”header”> </div><!-- end header --> <div id=”littlewhale”> <img src=”orange_sp.gif” alt=”orange sperm whale”> </div><!-- end little whale --> <div id=”chapter1”> <h1> Chapter 1: The Whale</h1> </div><!-- end chapter1 --> <div id=”maincontent”> <p>Call me Ishmael. </p> <p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

The #header CSS selector does the following:

• makes div id header into a 600px by 125px box;

• gives it a lovely pink background color;

• positions it absolutely (i.e., sticks it) 0px from the top of the wrapper div and 0px from the left of the wrapper div.

Page 13: Layout absolute poz

13

To make the little whale image stick up at the top and left, create #littlewhale CSS:

absoLute Positioning steP-by-steP

<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”>

#wrapper { position:relative; width:600px; height:1000px; margin:0 auto;}#header { position:absolute; width:600px; height:125px; background-color:#FFCCCC; top:0; left:0;}#littlewhale { position:absolute; top:0; left:0;}#chapter1 { position:absolute; top:33px; left:250px;}#maincontent { position:absolute; width: 350px; height: 400px; top:125px; left:250px;}#sidebar { position:absolute; top:125px; left: 0; width:225px; height:400px; border-right: 1px solid #FFCCCC;}</style>

</head>

<body>

<div id=”wrapper”>

<div id=”header”> </div><!-- end header --> <div id=”littlewhale”> <img src=”orange_sp.gif” alt=”orange sperm whale”> </div><!-- end little whale --> <div id=”chapter1”> <h1> Chapter 1: The Whale</h1> </div><!-- end chapter1 --> <div id=”maincontent”> <p>Call me Ishmael. </p> <p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

The #littlewhale CSS selector does thefollowing:

• makes div id littlewhale into a box that takes its dimensions from the contents - i.e., the image.

• positions the box absolutely (i.e., sticks it) 0px from the top of the wrapper div and 0px from the left of the wrapper div.

w

Page 14: Layout absolute poz

14

To make the title go to its place, create #chapter1 CSS:e

absoLute Positioning steP-by-steP

<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”>

#wrapper { position:relative; width:600px; height:1000px; margin:0 auto;}#header { position:absolute; width:600px; height:125px; background-color:#FFCCCC; top:0; left:0;}#littlewhale { position:absolute; top:0; left:0;}#chapter1 { position:absolute; top:33px; left:250px;}#maincontent { position:absolute; width: 350px; height: 400px; top:125px; left:250px;}#sidebar { position:absolute; top:125px; left: 0; width:225px; height:400px; border-right: 1px solid #FFCCCC;}</style>

</head>

<body>

<div id=”wrapper”>

<div id=”header”> </div><!-- end header --> <div id=”littlewhale”> <img src=”orange_sp.gif” alt=”orange sperm whale”> </div><!-- end little whale --> <div id=”chapter1”> <h1> Chapter 1: The Whale</h1> </div><!-- end chapter1 --> <div id=”maincontent”> <p>Call me Ishmael. </p> <p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

The #chapter1 CSS selector does thefollowing:

• makes div id chapter1 into a box that takes its size from its contents - i.e., the h1 element.

• positions the box it absolutely (i.e., sticks it) 33px from the top of the wrapper div and 250px from the left of the wrapper div.

Page 15: Layout absolute poz

15

To put the paragraphs in a main content section, create #maincontent CSS:r

absoLute Positioning steP-by-steP

<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”>

#wrapper { position:relative; width:600px; height:1000px; margin:0 auto;}#header { position:absolute; width:600px; height:125px; background-color:#FFCCCC; top:0; left:0;}#littlewhale { position:absolute; top:0; left:0;}#chapter1 { position:absolute; top:33px; left:250px;}#maincontent { position:absolute; width: 350px; height: 400px; top:125px; left:250px;}#sidebar { position:absolute; top:125px; left: 0; width:225px; height:400px; border-right: 1px solid #FFCCCC;}</style>

</head>

<body>

<div id=”wrapper”>

<div id=”header”> </div><!-- end header --> <div id=”littlewhale”> <img src=”orange_sp.gif” alt=”orange sperm whale”> </div><!-- end little whale --> <div id=”chapter1”> <h1> Chapter 1: The Whale</h1> </div><!-- end chapter1 --> <div id=”maincontent”> <p>Call me Ishmael. </p> <p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

The #maincontent CSS selector makes div id maincontent do the following:

• make an invisible 350x400px box around the paragraph elements inside it.

• positions the box it absolutely (i.e., sticks it) 125px from the top of the wrapper div and 250px from the left of the wrapper div.

• note that the sidebar div just scoots up into place like a sidebar. At this point, you wouldn’t have to make a CSS selector for it to style it any further

Page 16: Layout absolute poz

16

If you want to change the layout of div id sidebar, create #sidebar CSSt

absoLute Positioning steP-by-steP

<!DOCTYPE html>

<head><title>Moby HTML</title>

<meta charset=”UTF-8”>

<style type=”text/css”>

#wrapper { position:relative; width:600px; height:1000px; margin:0 auto;}#header { position:absolute; width:600px; height:125px; background-color:#FFCCCC; top:0; left:0;}#littlewhale { position:absolute; top:0; left:0;}#chapter1 { position:absolute; top:33px; left:250px;}#maincontent { position:absolute; width: 350px; height: 400px; top:125px; left:250px;}#sidebar { position:absolute; top:125px; left: 0; width:225px; height:400px; border-right: 1px solid #FFCCCC;}</style>

</head>

<body>

<div id=”wrapper”>

<div id=”header”> </div><!-- end header --> <div id=”littlewhale”> <img src=”orange_sp.gif” alt=”orange sperm whale”> </div><!-- end little whale --> <div id=”chapter1”> <h1> Chapter 1: The Whale</h1> </div><!-- end chapter1 --> <div id=”maincontent”> <p>Call me Ishmael. </p> <p> Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. </p>

The #sidebar CSS selector makes div id sidebar do the following:

• make an invisible 225x400px box around the list elements inside it.

• positions the box it absolutely (i.e., sticks it) 125px from the top of the wrapper div and 0px from the left of the wrapper div.

• Creates a 1 pixel border on the right side of the div.

Page 17: Layout absolute poz

17

absoLute Positioning steP-by-steP

Fin � �