Thursday 17 May 2012

Show and hide, simply, using CSS

I was recently laying out a new HTML5 website.  I needed a list in a sidebar to act like a vertical menu, with sub-items hidden until the parent item was hovered over.

In previous situations I would have used javascript to show and hide the items using a unique element id.  This method is a bit passe these days, so here is my CSS solution.

We'll start with a handful of nested lists


<!DOCTYPE html>   <!-- HTML5 document declaration -->
<html lang="en">
   
    <head>      
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>ShowHideDemo</title>
        
      <style type="text/css">
        
       </style>
    </head>

    <body>
        <ul>
            <li>Fruit
                <ul>
                    <li>Apple</li>
                    <li>Orange</li>
                    <li>Banana</li>
                </ul>
            </li>
            <li>Animals
                <ul>
                    <li>Goat</li>
                    <li>Sheep</li>
                    <li>Pig</li>
                </ul>
            </li>
            <li>Paint.NET   
                <ul>
                    <li>Plugins</li>
                    <li>Layers</li>
                    <li>Forum</li>
                </ul>
            </li>
        </ul>
   
    </body>   
</html>


We want to hide the sub-items by default.  We can achieve this with a tiny little bit of CSS.  Place the following snippet in between the style tags in the above document.


                li ul {display: none }


This tells us that any Unordered List (ul) that is a child of a List Item (li) should not be displayed.  Easy.

Now we want to display the hidden items when the parent item receives the focus (hover).  Add this line of CSS below the last one, keeping it inside the style tags.



                   li:hover ul {display: block}



This states that any Unordered List (ul) that is a child of a List Item (li) that has the focus (hover) should be displayed (as a block).

That's it!   Mousing over the parent item, displays the sub-menu.  Just what I wanted.  Of course there is still a little styling to be done, but this doesn't change the core code of the effect.



<!DOCTYPE html>   <!-- HTML5 document declaration -->
<html lang="en">
  
    <head>     
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>ShowHideDemo</title>
        <style type="text/css">
                li ul {
                      display: none;  
                      width: 200px;   
                        -webkit-border-top-right-radius: 8px;
                       -webkit-border-bottom-left-radius: 12px;
                      -moz-border-radius-topright: 12px;
                      -moz-border-radius-bottomleft: 12px;
                      border-top-right-radius: 12px;
                      border-bottom-left-radius: 12px;
                     -webkit-box-shadow: 2px 2px rgba(0,0,0,.6);
                     -moz-box-shadow: 2px 2px 2px rgba(0,0,0,.6);
                     box-shadow: 2px 2px 2px rgba(0,0,0,.6);
                     background: #DFFF8E; 
}
                li:hover ul {
                  display: block;
                  position: relative;
                 margin-bottom: 8px;
                 -webkit-animation: fadeIn 1s;
                 -moz-animation: fadeIn 1s;
                -ms-animation: fadeIn 1s;}
  
    @-webkit-keyframes fadeIn {
    from { opacity: 0; }
      to { opacity: 1; }
}

@-moz-keyframes fadeIn {
    from { opacity: 0; }
      to { opacity: 1; }
}

@-ms-keyframes fadeIn {
    from { opacity: 0; }
      to { opacity: 1; }
}
        </style>
    </head>

    <body>
  
        <ul>
            <li>Fruit
                <ul>
                    <li>Apple</li>
                    <li>Orange</li>
                    <li>Banana</li>
                </ul>
            </li>
            <li>Animals
                <ul>
                    <li>Goat</li>
                    <li>Sheep</li>
                    <li>Pig</li>
                </ul>
            </li>
            <li>Paint.NET  
                <ul>
                    <li>Plugins</li>
                    <li>Layers</li>
                    <li>Forum</li>
                </ul>
            </li>
        </ul>
  
    </body>
     
</html>


Here we're using some color, width, rounded corners and shadowing to style the sub-menu.  All using CSS.  The final touch is a nice little transition which fades the sub-menu in  using the opacity value.

So why didn't I use display:block and display:none instead of the visibility: hidden statement? 

Simple: the visibility:hidden statement does not display the item, but it does display the same amount of whitespace that the content would have taken if it were visible.  This would have spaced out my parent menu items rather than keeping them together.

Note:  I'd always use separate style sheets.  The code shown above includes the styling to make copying and pasting easier.


Tuesday 15 May 2012

Money for nothing (and your chicks for free)

I’m regularly asked to help people out with PC issues or websites.  That’s cool, it’s what I do.  I’m good at it and I charge for my expertise.  That’s right, I’m not a charity despite numerous recent inquiries from people who obviously think I am.

A few months back there was the ‘adult’ products company who wanted me to investigate why they weren’t getting any sales from their website.  OK, I’m an adult and nothing on the site was illegal, so I gave them an idea of how I could help them, including an estimate of the hours involved.

Silence ensued.

Funny how these people were keen to tell me how they spent thousands of dollars setting up the site, but wouldn’t spend a few hundred bucks making it work properly.

I lost another client because their free host died.  The fact that they hadn’t paid a cent for the hosting was somehow my responsibility because I had charged them for designing their site.

Another client asked me to add another page to their site so they could start selling stuff.  Of course they didn’t want to pay any extra for the work.   Apparently this was part of the deal (which I gave them at a very reasonable rate because they were a start-up I chose to support).   Sigh.

You get what you pay for in this world and I’m sick of the Takers in this world trying it on.

Yes I do some free work for organizations I support.  I also often charge reduced rates for genuine charities.   Sometimes this is just not enough for some people.

So to all you Takers: Take your business elsewhere, the answer is NO.

Free Chicken!

Introducing Blogger.com

I gave up trying to code my own blog.

Not that I failed in any way.  I actually liked what I had developed and it will form the base of the redesigned Scriptus web site some time in the future.  The reason I'm using blogger.com is for simplicity and ease of use.

I signed up and within 20 minutes had begun copying over my previous blogs to the new blog.  It was so easy!

So this is is.   The Scriptus Web Design blog lives here on blogger.com.


Goodbye Thingamablog

I've set up my own IIS server under Win7 and have both MySQL and PHP running on it.

I've also been designing websites and writing C# software for a few years now, so I like to think I'm not all that dim.  Yet I tried to configure Thingamablog for more than six hours before I finally gave up.

Nice idea, but I found it just too much trouble to get it to work.  Goodbye Thingamablog!

What am I going to do about a blog now?

I've seriously thought about using Wordpress.  Even have it installed on my localhost.  The trouble is that the Scriptus Web Design website (http://www.scriptus.co.nz) is a static one (no PHP or MySQL).  This means that Wordpress is not going to play nicely with the site host.  Damn.

I like to call myself a web designer.  Surely I can code this myself?  What I'll do is write it up in HTML5 and CSS and FTP it to my website host (I was going to have to FTP the blog to the web site anyway using Thingamablog).   Now I have ultimate control of how my blog looks and works.  If you're reading this then I've been successful publishing it.

Or so I thought.....,


Reading web pages on your Kindle

Here's something I stumbled upon recently Send2Reader.com.

I've been doing a lot of research on HTML5 and CSS3 lately as part of learning these emergent technologies.  As usual, I found a wealth of information on the Internet.   The trouble was, I wanted to read this stuff off line when I had more time.

In steps Send2Reader.

With this little bookmarklet added to your browser tool bar you can send the current web page directly to your Kindle.   The bookmarklet wraps the text and images in a Mobi friendly format and wirelessly sends it to your Kindle.  Brilliant!   Even more brilliant, the ads are automatically filtered out too!

It takes a moment to drag the bookmarklet to your browser tool bar (thats right, just drag it there - no installation is required).  You do have to configure your Kindle to receive pages from Send2Reader, and also create and configure your account on the Send2reader website.   Neither task is onerous.

If you have a Kindle - grab this and start using it.  You'll wonder how you managed without it!

Hello Thingamablog

I decided to start blogging about web design in 2012.  Very quickly I found this new bit of kit called Thingamablog.

Thingamablog is a piece of blogging software that encodes your blog as XML and allows you to upload it via FTP to your website.  The really neat part is that it works with the static websites (that have no PHP or MySQL). 

Using Thingamablog, I can write my blog entries (like this one) and FTP the post to the static website.

How cool is that?