Introduction:
This is the seventh part in my CSS Styling tutorials, in which I will be covering position.
What is Position?
Position is used to set the behaviour of how the elements display on-screen within the web browser.
Structure:
position: {type};
Example:
Values:
relative - Takes in to account the elements around it.
absolute - Does not take in to account the elements around it, margin and padding go from the edge of the containers or documents.
fixed - Only takes in to account the width and height of the browser window.
inherit - The value of the display property will be inherited from the parent element.
Linking CSS To a HTML Element:
To link your CSS to an HTML element (text, div, etc.) you will need to decide whether you want to use a class or id, you will also need a unique name. Once you have those, go to your element in HTML and add...
... to the attributes of that element if you chose a class, or...
if you chose an id.
Make sure you replace 'myClass' and/or 'myID' with your unique name. Then, in the CSS you will want to encase your properties with...
.myClass {
//Properties go here
}
if you chose a class, or...
#myID {
//Properties go here
}
(You can remove the line beginning with // if you wish).
Here's an example:
<html>
<head>
<style>
.fixed {
background-color: #000;
color: #fff;
width: 300px;
height: 100px;
position: fixed;
bottom: 0px;
//Fixed to the bottom of the document (footer)
}
.relative {
background-color: #000;
color: #fff;
width: 300px;
height: 100px;
position: relative;
left: 100px;
//Takes in to account other elements
}
.absolute {
background-color: #000;
color: #fff;
width: 300px;
height: 100px;
position: absolute;
left: 100px;
//Doesn't take in to account other elements.
}
</style>
</head>
<body>
<div class='fixed'>Fixed</div>
<div class='relative'>Relative</div>
<div class='absolute'>Absolute</div>
</body>
</html>