CSS Menu Placement!

So much flexibility!

Introduction

One of the great things about CSS is how we may use it to control the layout of elements on our pages (and tinker with those layouts very easily). In this challenge we will experiment with the layout and placement of a menu on the page

In this challenge we'll investigate the followign concepts:

  • Positioning
  • Different display types
  • Changing list display styles
  • Creating hover effects
  • Adding content before items

Step 1 - The Template Code

Copy the starting template code below into a text file (remember to save with a .html extension) and open it up in a browser to see what it looks like.

menus-challenge.html

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Menu's Challenge</title>
  5. <meta name="description" content="Exploring CSS and layout">
  6. <meta name="keywords" content="css, layout">
  7. <style>
  8. /* Styles will go here */
  9. </style>
  10. </head>
  11. <body>
  12. <div id='content'>
  13. <h1>Our page</h1>
  14. <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis tristique lectus ut maximus. Nulla sit amet nisi at sapien pellentesque auctor. Vivamus et ex ut tortor sollicitudin varius. Vestibulum sed posuere urna, eget tempus ante. Phasellus ornare odio tempor velit tempor, in ullamcorper lectus suscipit. Fusce tellus justo, posuere in quam a, sollicitudin varius elit. Morbi pretium tellus et ex molestie mattis. Pellentesque pulvinar dictum lacus, vitae sollicitudin felis lacinia ac.</p>
  15. <p>Vivamus elementum magna enim, sit amet facilisis felis cursus id. Sed viverra elit nisi, id ultricies urna ultrices sed. Donec pulvinar justo erat, at volutpat orci gravida et. In ac nulla turpis. Maecenas id quam dapibus, ornare ex ac, iaculis lectus. Vivamus metus nibh, scelerisque et massa ut, placerat sagittis ipsum. Integer quis volutpat erat, vel facilisis nunc. Nam efficitur orci nec neque volutpat, a feugiat tortor efficitur. Donec venenatis sodales nibh, eget ornare ligula pellentesque eu.</p>
  16. </div>
  17. <div id='menu'>
  18. <ul>
  19. <li>Home</li>
  20. <li>Blog</li>
  21. <li>About</li>
  22. </ul>
  23. </div>
  24. </body>
  25. </html>
Template page

Our page

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla lobortis tristique lectus ut maximus. Nulla sit amet nisi at sapien pellentesque auctor. Vivamus et ex ut tortor sollicitudin varius. Vestibulum sed posuere urna, eget tempus ante. Phasellus ornare odio tempor velit tempor, in ullamcorper lectus suscipit. Fusce tellus justo, posuere in quam a, sollicitudin varius elit. Morbi pretium tellus et ex molestie mattis. Pellentesque pulvinar dictum lacus, vitae sollicitudin felis lacinia ac.

Vivamus elementum magna enim, sit amet facilisis felis cursus id. Sed viverra elit nisi, id ultricies urna ultrices sed. Donec pulvinar justo erat, at volutpat orci gravida et. In ac nulla turpis. Maecenas id quam dapibus, ornare ex ac, iaculis lectus. Vivamus metus nibh, scelerisque et massa ut, placerat sagittis ipsum. Integer quis volutpat erat, vel facilisis nunc. Nam efficitur orci nec neque volutpat, a feugiat tortor efficitur. Donec venenatis sodales nibh, eget ornare ligula pellentesque eu.

The dummy content was obtained from a Lorem Ipsum generator. It is an industry standard way of including dummy content.

For each of the steps below, make sure to save and view the file regularly so see what affect the elements you are adding and changing has on your page.

At the moment we have some simple content with a menu that is actually just a bullet point list at the bottom of the page. Over the next few steps we will tinker with this and see what we can do. One thing to notice is that we're pretty much only going to be playing with CSS. The HTML we won't touch.

Step 2 - Menu on the Left

We've put the menu at the bottom in our code so that it is out of the way. This puts our content right at the top of our page which is convenient from a coding point of view but is also good from an SEO (Search Engine Optimisation) point of view. This is because search enginges prioritise content higher up in our code meaning our content gets higher value than the menu.

Just because our menu is at the bottom in our code doesn't mean it has to be there when viewed in the browser. Copy the code below and place it in between your <style> tags.

Add between the <style> tags

  1. #menu {
  2. position: absolute;
  3. top: 70px;
  4. left: 10px;
  5. width: 100px;
  6. }
  7. #content {
  8. margin-left: 140px;
  9. }

What we did here was first position the menu absolutely on the page. The top and left properties specify where to abolutely position the element on the page. We give it a width as well as absolutely positioned elements will only be as wide as their content by default. You can adjust this value to suit the width of your menu items. You can also adjust the value of the top property to align the menu with your content, or heading or something else.

We have added a margin-left to the content so that there is space on the side of it for the menu to live. You can tweak this value to adjust the amount of gap between the menu and content to your liking / sidth of your menu items.

Step 3 - Style it up

The menu still looks like a list though. Let's make it look a bit more like a menu. Copy the styles below and place them after the styles we copied in on the previous step.

Add after the previous rules

  1. #menu ul {
  2. padding-left: 0px;
  3. }
  4. #menu li {
  5. list-style: none;
  6. display: block;
  7. padding: 5px 10px;
  8. border-bottom: 1px solid #3366ff;
  9. }

The first rule removes the spaceing to the left of the bullet points. The second rule removes the bullet points, spaces out the menu items adn indents them a bit.

This is just one example of how the menu items could look. You can be very creative here in how you style them and I would encourage you to come up with your own style rather than just accept my example. With a bit more tinkering I'm sure you can come up with a more stylish menu than mine.

Menu's are always more user friendly with a bit of feedback so let's make the menu items react to being hovered over. Copy the rule below and place it just after the previous rules.

Add after the previous rules

  1. #menu li:hover {
  2. background-color: #99ccff;
  3. padding-left: 20px;
  4. font-weight: bold;
  5. }

You can actually make any item on your page react to being hovered over by simply creating a rule relating to the item and adding :hover to the end of it. With a bit of creativity you can do some really funky things.

Step 4 - Menu on the Right

The page is starting to look better but then a new trend comes along and it's now the in thing to have menu's on the right hand side. No worry, that's easy. Just need a few tweaks to our code and it's done.

  • Change the property left to right for the rule #menu
  • Change the property margin-right to margin-left for the rule #content

Done!

And if you have developed your pages using a single external linked CSS file then you would make those changes in a single file and all your pages would automatically be changed over. How cool is that?

Step 5 - Menu along the Top

For the sake of completeness, let's now put the menu along the top.

  • Change the property margin-right to margin-top for the rule #content
  • Change the value of the property top from 70 to 20 for the rule #menu
  • Change the value of the property right from 10 to 30 and rename the property to left for the rule #menu

Refreshing the page in the browser we can see that the menu and content are in the right places but the menu is still going down whereas we would like it to go across the page. This is another easy fix.

  • Change the property width from 100px to 500px for the rule #menu
  • Change the value of display from block to inline-block for the rule #menu li
  • Add another property to the rule #menu li width: 70px
  • Add another property to the rule #menu ul width: 100%

Step 6 - Extra content

Let's add a final bit of style to the top menu, just to see what's possible.

Instead of a line under each menu item, it would be nicer to have the line to the side, to act as a divider.

  • Change the property from border-bottom to border-left for the rule #menu li

This will set the line to the side but it is also in front of the first item and it would be nice to have the dividers only between items. Add the rule below to the bottom of your CSS rules.

Add after the previous rules

  1. #menu li:first-child {
  2. border-left: none;
  3. }

Adding :first-child to a rule means it will only apply to the first item of that kind. You can all manner of things. Eg. maybe you wish the first paragraph on a page to be styled differently to others.

How about we also add something to the beginning of the menu item on hover. Add the following rule to your code.

Add after the previous rules

  1. #menu li:hover:before {
  2. content: '\00bb ';
  3. padding-right: 5px;
  4. }

The :before after the rule means that this will be implemented before the item when it is displayed in the browser. The content property states what is it we want to put before the item. In this case we are printing a special character (hence the character code) but we can print anything. Try replacing the character code with a normal word and see what happens.

Taking it Further

We've investigated quite a few concepts quite quickly here. At every stage we've left the content and menu quite rough around the edges. You should now go back and use your knowledge of HTML and CSS to style them up and make the page look a little more finished.

Experiment and see what you can come up with. Have fun!