• We just launched and are currently in beta. Join us as we build and grow the community.

Position

DarSider

Data Pipeline Engineer
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
77
Likes
179
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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:
  1. position

    :

    fixed

    ;

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...

  1. class=

    'myClass'

... to the attributes of that element if you chose a class, or...

  1. id=

    'myID'

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...

  1. .myClass

    {
  2. //Properties go here
  3. }

if you chose a class, or...

  1. #myID

    {
  2. //Properties go here
  3. }

(You can remove the line beginning with // if you wish).

Here's an example:

  1. <html>
  2. <head>
  3. <style>
  4. .fixed

    {
  5. background-color

    :

    #000

    ;
  6. color

    :

    #fff

    ;
  7. width

    :

    300px

    ;
  8. height

    :

    100px

    ;
  9. position

    :

    fixed

    ;
  10. bottom

    :

    0px

    ;
  11. //Fixed to the bottom

    of the document (

    footer)
  12. }

  13. .relative

    {
  14. background-color

    :

    #000

    ;
  15. color

    :

    #fff

    ;
  16. width

    :

    300px

    ;
  17. height

    :

    100px

    ;
  18. position

    :

    relative

    ;
  19. left

    :

    100px

    ;
  20. //Takes in to account other elements
  21. }

  22. .absolute

    {
  23. background-color

    :

    #000

    ;
  24. color

    :

    #fff

    ;
  25. width

    :

    300px

    ;
  26. height

    :

    100px

    ;
  27. position

    :

    absolute

    ;
  28. left

    :

    100px

    ;
  29. //Doesn't take in to account other elements.
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class='

    fixed

    '>Fixed</div>
  35. <div class='

    relative

    '>Relative</div>
  36. <div class='

    absolute

    '>Absolute</div>
  37. </body>
  38. </html>

Book traversal links for Position

  • ‹ Overflow
  • Up
  • Pseudo Classes/States ›

 

442,401

317,942

317,951

Top