Top Banner

of 19

How to Create a Pure CSS Dropdown Menu

Jun 02, 2018

Download

Documents

sudheeshns007
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    1/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 1/19

    With the help of some advanced selectors a dropdown menu can be easily created with CSS. Throw in some fancy CSS3 properties and you

    can create a design that was once only achievable with background images and Javascript. Follow this tutorial to see the step by step

    process of building your own pure CSS dropdown menu.

    (http://line25.com/wp-content/uploads/2012/css-menu/demo/index.html)

    The menu well be creating features two sub categories that appear once the parent link is activated by a hover. The first series of sub-links

    How To Create a Pure CSS Dropdown Menu

    http://line25.com/wp-content/uploads/2012/css-menu/demo/index.htmlhttp://line25.com/wp-content/uploads/2012/css-menu/demo/index.htmlhttp://line25.com/wp-content/uploads/2012/css-menu/demo/index.htmlhttp://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menuhttp://line25.com/wp-content/uploads/2012/css-menu/demo/index.html
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    2/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 2/19

    appear underneath main nav bar, then the second series of links fly out horizontally from the first dropdown. Take a look at the CSS

    dropdown menu demo to see it all in action.

    View the pure CSS dropdown menu demo (http://line25.com/wp-content/uploads/2012/css-menu/demo/index.html)

    Home

    Tutorials

    Articles

    Inspiration

    First up well need to create the HTML structure for our CSS menu. Well use HTML5 to house the navigation menu in a element,

    then add the primary navigation links in a simple unordered list.

    Home

    Tutorials

    Photoshop

    The HTML Structure

    http://line25.com/wp-content/uploads/2012/css-menu/demo/index.html
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    3/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 3/19

    Illustrator

    Web Design

    Articles

    Web Design

    User Experience

    Inspiration

    The first sets of sub-menus can then be added under the Tutorials and Articles links, each one being a complete unordered list inserted

    within the of its parent menu option.

    Home

    Tutorials

    Photoshop

    Illustrator

    Web Design

    HTML

    CSS ul {

    display: block;

    }

    Lets begin the CSS by getting the basic dropdown functionality working. With CSS specificity and advanced selectors we can target

    individual elements buried deep in the HTML structure without the need for extra IDs or classes. First hide the sub menus by targeting

    any ULs within a UL with the display:none; declaration. In order to make these menus reappear they need to be converted back to

    block elements on hover of the LI. The > child selector makes sure only the child UL of the LI being hovered is targeted, rather than all

    sub menus appearing at once.

    The CSS Styling

  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    6/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 6/19

    nav ul {

    background: #efefef;

    background: linear-gradient(top, #efefef 0%, #bbbbbb 100%);

    background: -moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%);

    background: -webkit-linear-gradient(top, #efefef 0%,#bbbbbb 100%)

    box-shadow: 0px 0px 9px rgba(0,0,0,0.15);

    padding: 0 20px;

    border-radius: 10px;

    list-style: none;

    position: relative;

    display: inline-table;

    }

    nav ul:after {

    content: ""; clear: both; display: block;

    }

    We can then start to style up the main navigation menu with the help of CSS3 properties such as gradients, box shadows and border radius.

    Adding position:relative; will allow us to absolutely position the sub menus according to this main nav bar, then

    display:inline-table will condense the width of the menu to fit. The clearfix sty le rule will clear the floats used on the subsequent

    list items without the use of overflow:hidden , which would hide the sub menus and prevent them from appearing.

  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    7/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 7/19

    nav ul li {

    float: left;

    }

    nav ul li:hover {

    background: #4b545f;

    background: linear-gradient(top, #4f5964 0%, #5f6975 40%)

    background: -moz-linear-gradient(top, #4f5964 0%, #5f697

    background: -webkit-linear-gradient(top, #4f5964 0%,#5f6

    }

    nav ul li:hover a {

    color: #fff;

    }

    nav ul li a {

    display: block; padding: 25px 40px;

    color: #757575; text-decoration: none;

    }

    The individual menu items are then styled up with CSS rules added to the and the nested anchor. In the browser this will make the

  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    8/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 8/19

    link change to a blue gradient background and white text.

    nav ul ul {

    background: #5f6975; border-radius: 0px; padding: 0;

    position: absolute; top: 100%;

    }

    nav ul ul li {

    float: none;border-top: 1px solid #6b727c;

    border-bottom: 1px solid #575f6a;

    position: relative;

    }

    nav ul ul li a {

    padding: 15px 40px;

    color: #fff;

    }

  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    9/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 9/19

    Line25

    nav ul ul li a:hover {

    background: #4b545f;

    }

    The main navigation bar is now all styled up, but the sub menus still need some work. They are currently inheriting sty les from their

    parent elements, so a change of background and the removal of the border-radius and padding fixes their appearance. To make sure they

    fly out underneath the main menu they are positioned absolutely 100% from the top of the UL (ie, the bottom).

    The LIs of each UL in the sub menu dont need floating side by side, instead theyre listed vertically with thin borders separating each one.

    A quick hover effect then darkens the background to act as a visual cue.

    http://line25.com/
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    10/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 10/19

    nav ul ul ul {

    position: absolute; left: 100%; top:0;

    }

    The final step is to position the sub-sub-menus accordingly. These menus will be inheriting all the sub-menu styling already, so all they

    need is to be positioned absolutely to the right (left:100%) of the relative position of the parent .

    (http://line25.com/wp-content/uploads/2012/css-menu/demo/index.html)

    The Completed Pure CSS Dropdown Menu

    http://line25.com/wp-content/uploads/2012/css-menu/demo/index.html
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    11/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 11/19

    Enter your email address Subscribe

    Written by Chris Spooner

    Chris Spooner(/about) is a designer who loves experimenting with new

    web design techniques collating creative website designs. Check out

    Chris' design tutorials and articles at Blog.SpoonGraphics

    (http://blog.spoongraphics.co.uk) or follow his daily findings on Twitter

    (http://twitter.com/chrisspooner) .

    View the pure CSS dropdown menu demo (http://line25.com/wp-content/uploads/2012/css-menu/demo/index.html)

    Join the mailing list to have new content delivered straight to your email inbox. Every subscriber gets a free pack of realistic web

    shadows(/subscribe) .

    http://line25.com/subscribehttp://line25.com/subscribehttp://line25.com/wp-content/uploads/2012/css-menu/demo/index.htmlhttp://twitter.com/chrisspoonerhttp://blog.spoongraphics.co.uk/http://line25.com/abouthttp://line25.com/abouthttp://line25.com/about
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    12/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 12/19

    38 Comments

    Justin

    Where did you learn to create a pure CSS drop down menu.

    Bob Rockefeller

    Have you updated this to handle the responsive design issues with devices not having a :hover ability?

    ren

    Love :-) Simple, straightforward, and well-explained. And I really appreciate that you included the styling to make it pretty, not just functional.

    Thanks!

    ust

    nice...

    thx and god bless you and the world...

    a sincere blessing from hong kong..

    ^___^

    walif

    It was so amazing post and awesome collection so that i share it my cousin and FB,Twitter friends.Great writer.Care on and write good

    http://disqus.com/disqus_hpgu81Zlpd/http://disqus.com/bobrocke/http://disqus.com/disqus_hpgu81Zlpd/http://disqus.com/bobrocke/
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    13/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 13/19

    content more beautiful pics.

    web design south wales

    Good tutorial but does have some IE issues although who doesn&apost have issues with IE?

    enny

    very well written tutorial. that is how a tutorial should be. HOwever, i see the demo, the sizes of the button and navbar are pretty big.

    Ofcourse i can tweak it.

    good work

    thanks

    kuldeepdaftary

    It dosent work in ie9 > Which is really sad! I think post title is misleading . it must be

    How To Create a Pure "CSS3" Dropdown Menu.

    nice code otherwise.

    abhinav singh

    it&aposs cool tutorial....loved it :)

    Mark Narusson

    Great tutorial. Looks nice and clean - love it dude.

    Sohail Gul

    26/8/2014 H T C t P CSS D d M

    http://disqus.com/kuldeepdaftary/
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    14/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 14/19

    Awesome tutorial Thank you Chris!!! !

    Jon

    Awesome tutorial, that&aposs a beautiful menu!

    ebdesign Lover

    Hey Chris, this is really great tutorial for creating a pure css dropdown menu. As I love to design, I always prefer to use CSS for style because

    CSS is really much smoother than option.

    Arran

    Great post, the more we can step away from heavy technologies and utilise elegant CSS the better.

    web design springfield mo

    Pretty cool stuff. We&aposve been so frustrated by dropdown menus that we&aposve started moving away from them, keeping the most

    important pages in the main navigation menu while relegating less important pages to footer menus. We&aposll give this a try. I&aposm

    optimistic that it while make dropdown menus less frustrating. Thanks for the tip!

    jpoblam

    In the course of learning responsive design, I was inspired by a new idea. It is, the idea that the entire site need not be linked from each menu

    (often eliminating the need for a drop-down). Keeping links within context, with a link-back to upper levels makes the site more usable, too.

    Thus, in your example, selection of "Tutorials" might lead to a page on which the most important (e.g. "Photoshop") is featured with an across-

    the-top including "Home", "Illustrator", and "Web Design".

    itoctopus

    A drop down menu in CSS is much smoother that one written in JavaScript - and it won&apost get affected at all if the person chooses to

    26/8/2014 HowTo Create a Pure CSS Dropdown Menu

  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    15/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 15/19

    disable JS on his browser (it&aposll still work).

    Thanks for sharing!

    Osho Garg

    Hello Chris Thank You Very Much For Sharing This :)

    I Am Going To Install This Widget On My Blog. This DropMenu Really Rocks With Javascript :)

    canciller

    thank you, thank you, thank you...

    nice tut.

    Steve I&aposve seen this technique used in a few WordPress themes; it looks great! The Internet Explorer compatibility issue makes me hesitant to

    use this sort of thing on my main site, though.

    I&aposm also wondering, does an advanced search engine crawler like Google&aposs consider this masking or something else that could

    reduce the quality of the site as far as the crawler is concerned?

    Brighton Electrician

    Cracking tutorial! I think I&aposll have to use some of these techniques on my own website.

    I designed my website using HTML and CSS only so this will work really well I&aposm sure!

    Thanks Chris :)

    Carlos Campos

    26/8/2014 HowTo Create a Pure CSS Dropdown Menu

  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    16/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 16/19

    or s n :

    http://necolas.github.com/norm...

    Simon Duck

    Thanks for the tutorial. I&aposve recently started a new project and making the mock-up and as I didn&apost want to produce a full blown

    thing, just HTML and CSS, I really needed a decent article to follow.

    Regards,

    Simon Duck

    Envira

    Thats cute and awesome.

    I love it,will use in future projects :)

    Seham Syed

    The > child selector makes sure only the child UL of the LI being hovered is targeted, rather than all sub menus appearing at once.

    FvG

    Can anyone tell me what the > means for css? nav ul li:hover > ul

    Idraki

    The immediate child element the selector met. In this case, the first ul after the hovered li.

    Jon Ewing

    Not true, Beata - this menu works in IE9+ but not in IE8 and earlier.

    26/8/2014 HowTo Create a Pure CSS Dropdown Menu

    http://necolas.github.com/normalize.css/
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    17/19

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 17/19

    However, I do think one thing is missing aside from the accessibility (which I agree, Greg, is a real and important consideration) - I think

    it&aposs important to have a graphical element that indicates the difference between a menu item with a drop-down or fly-out and one without.

    Something like an arrow or triangle to indicate that there is hidden content.

    But that&aposs not a criticism of the tutorial. Such niceties are really for the individual designer to add. And this is a very clear and well-written

    tutorial.

    Garry Conn

    You know, drop down menus have always been a challenge for me. For starters, there&aposs so many other sites that provide tutorials on

    them, but they&aposre really hard to follow or don&apost provide enough details to get me thru the snags. This has to be probably one of the

    best drop down menu tutorials I have read. Plus the design craftsmanship behind it is amazing. Thanks for posting this Chris. It will totally be a

    tutorial I will find myself coming back to often.

    Beata

    It&aposs a pity it is not working with IE.

    Alex

    it&aposs a pity that IE sucks.

    :-)

    Greg Tarnoff

    This isn&apost very accessible for keyboard navigation. While the top level is focusable, no flyouts happen or even focus on those elements

    when hidden. I tried adding a :focus to the hover events and that still doesn&apost work. 1 in 5 people struggle to use the web. Using a menu

    like this doesn&apost help them.

    Fernanda

    26/8/2014 How To Create a Pure CSS Dropdown Menu

  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    18/19

    p

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 18/19

    ere s e own oa s o arquves

    Andrei

    Does it work in IE7, IE8?

    Jason Nope. As is the case with nearly every CSS3 element, you&aposll need another solution for IE.

    smashinghub

    amazing and easy dropdown menu tutorial

    thanks

    Dima Taras

    great tutorial!!!

    will start using dropdown lists from now)

    eb design Maidenhead

    Useful technique, am thinking about using it on future projects.

    26/8/2014 How To Create a Pure CSS Dropdown Menu

    http://disqus.com/AucT/https://disqus.com/websites/?utm_source=line25&utm_medium=Disqus-Footerhttp://disqus.com/
  • 8/10/2019 How to Create a Pure CSS Dropdown Menu

    19/19

    http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu 19/19