A simple login dialog with HTML, CSS and jQuery

I’m currently working on a new software and I am working on both an administration page and a home page for the software. I also made a login page. Currently the login page looks kind of strange because it basically only has a few elements and takes up almost the whole screen (except a header, a navigation bar and a footer). So the screen is much too big for the few elements I have in there. So I decided to move the login page to a floating login dialog which is much smaller and thus looks better. Here is a short tutorial how to build such a dialog with HTML, CSS and jQuery.

First, you need to understand what dialogs in this context are. Dialogs are usually implemented in such a way that you have an HTML element floating on top of the page. In order to have it not displayed in the page or below the page, you need to define a z-index which defines where the element is displayed on the Z axis. This is done in CSS with the following:

        .dialog-popup {
            display: none;
            left: 50%;
            position: fixed;
            top: 50%;
            z-index: 99999; 
        }

Here a short explanation about the different CSS properties set above:

  • display: none => the dialog is not displayed when you open the page containing it but only when you click on some login link.
  • left: 50% / top: 50% => we want to have the dialog centered on the screen. This defines that the top left corner of the dialog will be in the middle of the screen. This means we’ll have to add some negative left and top margins when we display the dialog in order to have the middle of the dialog centered (the exact offset of course depends on the height and width of the displayed dialog.
  • position: fixed => this means that the position of the dialog doesn’t depend on the position of other elements but has a fixed position on the screen. Also note that the default value for position is static and with this set, the z-index will be ignored. So it’s important to set it to fixed.
  • z-index: 99999 => All elements with a z-index smaller than 99999 will be displayed below the dialog. If elements have no z-index defined, they inherit the z-index of their parent and if none is defined for any element, they all have a z-index of 0.

To make it look nicer, we’ll also had the following to the CSS for .dialog_popup:

            background: none repeat scroll 0 0 #CCCCCC;
            border: 2px solid #222222;
            border-radius: 3px 3px 3px 3px;
            font-family: Verdana,Geneva,sans-serif;
            padding: 10px;

Note that you may want to add the corresponding browser specific properties for border-radius.

Now let’s first move to the HTML code before we get back to the CSS. We need a link which will display the dialog:

<a href="#" data-selector="#login-dialog" class="dialog-link">Login</a>

The data-selector attribute is a custom attribute which is used to indicate which div ID is to be opened in the dialog when the link is clicked.

Now we’ll define the div for the dialog itself:

<div id="login-dialog" class="dialog-popup">
	<form method="post" action="#">
		<fieldset>
		</fieldset>
		<button class="submit" type="button">Login</button>
	</form>
</div>

To actually open the dialog, we’ll use jQuery. First we’ll load jQuery from Google:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

Then we’ll define that the dialog with the id defined in the data-selector attribute will be made visible with a fade in effect and its margins will be adapted so that it’s properly centered:

    <script type="text/javascript">
        $(document).ready(function() {
            $('a.dialog-link').click(function() {
                var dialog_id = $(this).attr('data-selector');
                $(dialog_id).fadeIn(200);
                $(dialog_id).css({ 
                    'margin-top' : -($(dialog_id).height() + 4) / 2,
                    'margin-left' : -($(dialog_id).width() + 4) / 2
                });
                return false;
            });
        });
    </script>

To go from the top left corner to the middle of the dialog, we’re defining a negative top and left margin corresponding to half the height and width of the dialog. We also add 4 pixels to the height and width because of the 2 pixels border on all 4 sides of the dialog.

If you want the dialog to be displayed faster, you should reduce the 200 parameter in the fadeIn call. If it’s too fast for you, increase it.

Now we’ll want to add some content to our login dialog:

  • A close button.
  • A username field.
  • A password field.
  • A link for users who forgot their password.

For the close button, I’ll this nice little icon:

close

With these additions, the HTML code for the dialog looks like this:

<div id="login-dialog" class="dialog-popup">
	<a href="#" class="close"><img src="close.png" class="close-button" title="Close this dalog" alt="Close" /></a>
	<form method="post" action="#">
		<fieldset>
			<label for="username">
				<span>Username</span>
				<input type="text" id="username" name="username" placeholder="Username" value=""></input>
			</label>
			<label for="password">
				<span>Password</span>
				<input type="password" id="password" name="password" placeholder="Password" value=""></input>
			</label>
			</fieldset>
		<button class="submit" type="button">Login</button>
		<a href="#" id="forgot-password-link">Forgot your password? Click here.</a>
	</form>
</div>

We’ll need to style it a little bit. First place the close button on the top right corner:

.dialog-popup .close-button {
	float: right;
	margin: -26px -26px 0 0;
}

Then we’ll remove the border and padding from the fieldset:

.dialog-popup form fieldset {
	border: medium none;
	padding: 0;
}

Then we’ll make the labels and input elements look nicer:

.dialog-popup form fieldset label {
	display: block;
	font-size: 1.2em;
}

.dialog-popup form fieldset label span,
.dialog-popup form fieldset label input,
.dialog-popup form button,
.dialog-popup form a {
	display: block;
	margin-bottom: 10px;
}

.dialog-popup form fieldset label input {
	background: none repeat scroll 0 0 #EEEEEE;
	border-color: #FFFFFF #DDDDDD #DDDDDD #FFFFFF;
	border-radius: 3px 3px 3px 3px;
	border-style: solid;
	border-width: 1px;
	color: #000000;
	padding: 6px 6px 4px;
	width: 200px;
}

Style the submit button:

.dialog-popup form button.submit {
	border-color: #000000;
	border-radius: 3px 3px 3px 3px;
	border-width: 1px;
	cursor: pointer;
	margin-top: 10px;
	padding: 6px 6px 4px;
	width: 200px;
}

.dialog-popup form button.submit:hover {
	background-color: #E0E0E0;
}

And the link for those who forgot their password:

#forgot-password-link {
	color: #00AE00;
	font-size: 0.7em;
	text-align: center;
	text-decoration: none;
	width: 200px;
}

Now that everything looks nice, we’ll also add an overlay below the dialog, so that we can make the page under the dialog darker. This way it is clear to the user, that he/she has to focus on the dialog and not the page in the background anymore. So first we need to add a div for the overlay e.g. after the definition of the dialog:

<div id="dialog-overlay" title="Click here to close the dialog">

This overlay is also hidden when the page is first displayed (just like the dialog itself) and will be displayed when the link is clicked. The overlay should cover the whole page, be dark and semi-transparent.

Being hidden by default means:

display: none;

Below the dialog but over of the page:

z-index: 99998;

Covering the whole page:

position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 100%;
width: 100%;
margin-left: auto;
margin-right: auto;

Being dark and semi-transparent:

background: #000;
opacity: 0.75;

This makes the page in the background still visible but not really readable. If it’s too dark for you, just reduce the opacity: 0 means transparent, 1 means opaque (i.e. the page is not visible anymore).

Now that we’ve defined the overlay below the dialog, we need to have it display at the same time as the dialog itself. This is done by adding the following line just above the similar line making the dialog appear:

$('#dialog-overlay').fadeIn(200);

And we want both to disappear when the close button is clicked. Actually we also want both to disappear when you click on the overlay (so outside of the dialog). It is easy to do it with jQuery:

$('.dialog-popup a.close, #dialog-overlay').click(function() { 
	$('.dialog-popup').fadeOut(200);
	$('#dialog-overlay').fadeOut(200);
	return false;
});

And we’re done. The dialog looks like this when it is displayed:

login

In case you do not like puzzle games and would rather see the whole code at once, here it is:

<html>
    <head>
    <style>
        #dialog-overlay {
            background: #000;
            bottom: 0;
            display: none;
            height: 100%;
            left: 0;
            margin-left: auto;
            margin-right: auto;
            opacity: 0.75;
            position: fixed;
            right: 0;
            top: 0;
            width: 100%;
            z-index: 99998;
        }

        .dialog-popup {
            background: none repeat scroll 0 0 #CCCCCC;
            border: 2px solid #222222;
            border-radius: 3px 3px 3px 3px;
            font-family: Verdana,Geneva,sans-serif;
            padding: 10px;
            display: none;
            left: 50%;
            position: fixed;
            top: 50%;
            z-index: 99999; 
        }

        .dialog-popup .close-button {
            float: right;
            margin: -26px -26px 0 0;
        }

        .dialog-popup form fieldset {
            border: medium none;
            padding: 0;
        }

        .dialog-popup form fieldset label {
            display: block;
            font-size: 1.2em;
        }

        .dialog-popup form fieldset label span,
        .dialog-popup form fieldset label input,
        .dialog-popup form button,
        .dialog-popup form a {
            display: block;
            margin-bottom: 10px;
        }

        .dialog-popup form fieldset label input {
            background: none repeat scroll 0 0 #EEEEEE;
            border-color: #FFFFFF #DDDDDD #DDDDDD #FFFFFF;
            border-radius: 3px 3px 3px 3px;
            border-style: solid;
            border-width: 1px;
            color: #000000;
            padding: 6px 6px 4px;
            width: 200px;
        }

        .dialog-popup form button.submit {
            border-color: #000000;
            border-radius: 3px 3px 3px 3px;
            border-width: 1px;
            cursor: pointer;
            margin-top: 10px;
            padding: 6px 6px 4px;
            width: 200px;
        }

        .dialog-popup form button.submit:hover {
            background-color: #E0E0E0;
        }    

        #forgot-password-link {
            color: #00AE00;
            font-size: 0.7em;
            text-align: center;
            text-decoration: none;
            width: 200px;
        }
    </style>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('a.dialog-link').click(function() {
                var dialog_id = $(this).attr('data-selector');
                $('#dialog-overlay').fadeIn(200);
                $(dialog_id).fadeIn(200);
                $(dialog_id).css({ 
                    'margin-top' : -($(dialog_id).height() + 4) / 2,
                    'margin-left' : -($(dialog_id).width() + 4) / 2
                });
                return false;
            });

            $('.dialog-popup a.close, #dialog-overlay').click(function() { 
                $('.dialog-popup').fadeOut(200);
                $('#dialog-overlay').fadeOut(200);
                return false;
            });
        });
    </script>
    </head>
    <body>
        <a href="#" data-selector="#login-dialog" class="dialog-link">Login</a>
        <div id="login-dialog" class="dialog-popup">
            <a href="#" class="close"><img src="close.png" class="close-button" title="Close this dalog" alt="Close" /></a>
            <form method="post" action="#">
                <fieldset>
                    <label for="username">
                        <span>Username</span>
                        <input type="text" id="username" name="username" placeholder="Username" value=""></input>
                    </label>
                    <label for="password">
                        <span>Password</span>
                        <input type="password" id="password" name="password" placeholder="Password" value=""></input>
                    </label>
                    </fieldset>
                <button class="submit" type="button">Login</button>
                <a href="#" id="forgot-password-link">Forgot your password? Click here.</a>
            </form>
        </div>
        <div id="dialog-overlay" title="Click here to close the dialog">
    </body>
</html>

jQuery: style current page link in navigation

I’m currently working on a web page which has a navigation area in the header and looks like this:

<nav>
    <ol class="main_menu">
        <li><a href="http://localhost/demo/index.php/home" class="active">home</a></li>
        <li><a href="http://localhost/demo/index.php/features">Features</a></li>
        <li><a href="http://localhost/demo/index.php/pricing">Pricing</a></li>
        <li><a href="http://localhost/demo/index.php/support">Support</a></li>
        <li><a href="http://localhost/demo/index.php/blog">Blog</a></li>
        <li><a href="http://localhost/demo/index.php/register">Sign up</a></li>
        <li><a href="http://localhost/demo/index.php/login">Login</a></li>
    </ol>
</nav>

Now I wanted to have the active page somehow highlighted so that the user can clearly see where he is. So first I defined some styles for this highlighting (basically just making it bold and underlined):

<style type="text/css">
    nav .active {
        font-weight: bold;
        text-decoration: underline;
    }
</style>

So what I needed now was some JavaScript to check the currently loaded URL and check which of the navigation links should be highlighted. All URLs start with http://localhost/demo/index.php. So I just needed to check the 4th part of the location path, find the navigation link related to that (so basically the link which ends with the same 4th part). Additionally if you go to http://localhost/demo/index.php you are shown the Home page even though the URL doesn’t end with /home. So I also needed to handle this special case.

So first I needed a JavaScript function to get the last part of the URL. Here I just needed to split the path using a slash as a separator and get the last one. There is just one exception: if the URL ends with a slash, I do not get the last one but the second to last (so that I do not get an empty result):

function getLastPart(url) {
    var parts = url.split("/");
    return (url.lastIndexOf('/') !== url.length - 1 ? parts[parts.length - 1] : parts[parts.length - 2]);
}

Now that we have it, we’ll get the last part of the URL and activate the link which ends with this last part. Additionally if the last part is index.php, we’ll highlight the Home link:

$(function() {
    var lastpart = getLastPart(window.location.href);
    $('nav a[href$="/' + lastpart + '"]').addClass('active');
    if (lastpart == "index.php") {
        $('nav a[href$="/home"]').addClass('active');
}

About the jQuery selector for the link selection:

nav a[href$="/home"]

It basically says to find a link in the navigation (i.e. nav a) having a href attribute (i.e. nav a[href…]) ending with (i.e. href$=) “/home”.

Of course if your URLs look different e.g. if you have query parameters or it is not the last part of the URL which is relevant for the link selection, you’ll need to adapt the getLastPart function. If it is not the last part of the URL which is relevant, you’ll also need to use a different selector.

Here the complete JavaScript:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript">
<script type="text/javascript">
<!--
$(function() {
    var lastpart = getLastPart(window.location.href);
    $('nav a[href$="/' + lastpart + '"]').addClass('active');
    if (lastpart == "index.php") {
        $('nav a[href$="/home"]').addClass('active');
}
  });
function getLastPart(url) {
    var parts = url.split("/");
    return (url.lastIndexOf('/') !== url.length - 1 ? parts[parts.length - 1] : parts[parts.length - 2]);
}
//-->
</script>

Post-it cards and 3D shadow effect with CSS3

After having a plain 2D post-it and a 3D rotated post-it, let’s now move to 3D shadow effects.

First we’ll start with a pretty simple post-it card:

<html>
	<body>
		<style>
			.card {
				width: 250px;
				height: 100px;
				padding: 10px;
				background: #FFFFA5;
				font-family: "Lucida Handwriting",cursive;
				font-size: 15px;
			}
		</style>
		<div class="card">
			<p>Buy some milk</p>
		</div>
	</body>
</html>

It looks like this:

simple post-it card

Then we’ll add some shadow so that it looks like the card is not stuck to the background:

.card {
	-webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1);
	box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1);
}

It now looks like this:

simple post-it card with shadow

Note that the shadow is not the same all over the card. It makes it more like a real card with real light and shadow.

Now we want to have a effect so that it looks like the card is not completely rigid but the corners are slightly curved i.e. they look further away from the background. Since we cannot just rotate the card and say that the rotation is non-linear (at least I don’t know a way to do it), we’ll have to use some kind of optical illusion using shadows.

Since our card already has a shadow, we’ll use two additional elements which will throw an additional shadow. Later on, we’ll use pseudo-elements to display them but right now, we’ll just use some normal divs for the sake of explaining how it works:

<style>
.shadow-element-before,
.shadow-element-after {
	width: 125px;
	height: 20px;
}
</style>
<div class="shadow-element-before"></div>
<div class="shadow-element-after"></div>

Note that these elements need to have half the width of the card (here it’s a fixed width but when building it in with pseudo-element you can use width: 50%). The height depends on how you like it, you should just experiment a little bit. I find 20% of the height of the card to be a good height for the effect.

We’ll also add the shadow we need for the effect:

.shadow-element-before,
.shadow-element-after {
	-webkit-box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7);
	box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7);
}

These elements will look like this:

shadow elements without rotation

Now we’ll want to rotate them slightly to create the illusion that the corner of the cards which are going up (even though actually it’s the shadow of these elements which is rotated):

.shadow-element-before {
	-webkit-transform: rotate(-2deg);
	transform: rotate(-2deg);
}

.shadow-element-after {
	-webkit-transform: rotate(2deg);
	transform: rotate(2deg);
}

The left one is rotated counter-clockwise and the right one clockwise. Here I only use the CSS3 property and the WebKit specific property as newer versions of FireFox do not need a browser specific prefix and I’m not much into Opera or IE. But if required, you could define copies of the box-shadow property with the following additional prefixes: -moz-, -o- and -ms-.

Here’s how the shadow elements look like with the rotation:

shadow elements with rotation

Now we can have them displayed as hidden pseudo-element. Hidden meaning they are displayed below the opaque card. So we have to do the following:

  1. Define the pseudo elements with :before and :after.
  2. Set an empty content for the pseudo element.
  3. Change the z-index of the pseudo-elements to have them displayed below the card.
  4. In order to be able to properly position the shadow elements, they have to have the position property set to absolute and the card should be set to relative.
  5. Since we’ve defined a bottom shadow for the pseudo element of 15 pixels, we’ll place them 15 pixel from the bottom so that you only see the blur of the shadow. If you want, you can reduce it to see a little more of the shadow.
  6. Position the shadow elements 10px from the left resp. from the right hand side so that the shadow below the card is not visible in the corners. This then looks like the corners are slightly lifted.

The code then looks like this:

<style>
	.card, .simple-card {
		width: 250px;
		height: 100px;
		padding: 10px;
		background: #FFFFA5;
		font-family: "Lucida Handwriting",cursive;
		font-size: 15px;
	}

	.card-shadow {
		-webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1);
		box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1);
	}

	.card {
		position: relative;
	}
	
	.card:before,
	.card:after {
		content: "";
		position: absolute;
		z-index: -5;
		bottom: 15px;
		width: 50%;
		height: 20%;
		-webkit-box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7);
		box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7);
	}

	.card:before {
		left: 10px;
		-webkit-transform: rotate(-2deg);
		transform: rotate(-2deg);
	}

	.card:after {
		right: 10px;
		-webkit-transform: rotate(2deg);
		transform: rotate(2deg);
	}

	.simple-card, .card-before, .card-after {
		margin-bottom: 40px;
	}
	
	.card-before,
	.card-after {
		content: "";
		width: 125px;
		height: 20px;
		-webkit-box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7);
		box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7);
	}

	.card-before {
		left: 10px;
		-webkit-transform: rotate(-2deg);
		transform: rotate(-2deg);
	}

	.card-after {
		right: 10px;
		left: auto;
		-webkit-transform: rotate(2deg);
		transform: rotate(2deg);
	}
</style>
<div class="card card-shadow">
	<p>Buy some milk</p>
</div>

And the card like this:

post-it card with 3D shadow effect

Just play around with the different properties and change their values to have the card look as you like.

3D effect on post-it cards with CSS

Some time ago, I’ve written a post about creating Post-it cards with CSS.

Now, instead of rotating the card, I wanted to have a 3D effect showing the card stuck on the board at the top and loose at the bottom. This can also be achieved with CSS3 transforms.

What you need for this is to define a container which will basically host the 3D effect and the card in it:

<html>
	<body>
		<div class='container'>
			<div class='card'>Buy some milk</div>
		</div>
	</body>
</html>

Now you need to define the size of the card on the container and have the card fully occupy it:

.container {
	width:250px; 
	height:150px;
	position: relative;
	margin: 10px;
}

.card {
	width: 100%;
	height: 100%;
	padding: 15px;
	position: absolute;
}

The paddings and margins are just there that it looks nicer. The container is set to relative positioning and the card to absolute positioning.

So that it looks like a post-it card, you’ll need to style it a little bit:

.card {
	width: 100%;
	height: 100%;
	padding: 15px;
	position: absolute;
	background: none repeat scroll 0 0 #FFFFA5;
	border: 1px solid black;
	box-shadow: 2px 4px 6px #444444;
	-moz-box-shadow: 2px 4px 6px #444444;
	-webkit-box-shadow: 2px 4px 6px #444444;
	font-family: "Lucida Handwriting",cursive;
	font-size: 15px;
}

It will look like this:

Post-it without 3D effect

Now to get the 3d effect, you need to:

  1. Define a perspective on the container. It basically defines how many pixels a 3D element is placed from the view.
  2. Define a transformation on the card slightly rotating in the middle on the horizontal axis.

The following should be added to the style of the container:

perspective: 100px;
-webkit-perspective: 100px;

Note that unlike what you will find googling for it, Firefox does seem to support the CSS perspective property without -moz- prefix (but you might need it for older versions of the browser). Also note that Chrome (and I guess Safari too) does require a browser specific prefix.

And the following should be added to the style of the card:

transform: rotateX(4deg);
-webkit-transform: rotateX(4deg);

Here again note that you require a -webkit- property for Chrome but can use the one without browser prefix for the newer versions of Firefox.

And you’re done. The card then looks like this:

Post-it with 3D effect

Here the complete sample code in case you just want to quickly copy and paste it:

<html>
	<head>
		<style>
.container {
	width:250px; 
	height:150px;
	position: relative;
	margin: 10px;
	perspective: 100px;
	-webkit-perspective: 100px;
}

.card {
	width: 100%;
	height: 100%;
	padding: 15px;
	position: absolute;
	background: none repeat scroll 0 0 #FFFFA5;
	border: 1px solid black;
	box-shadow: 2px 4px 6px #444444;
	-moz-box-shadow: 2px 4px 6px #444444;
	-webkit-box-shadow: 2px 4px 6px #444444;
	font-family: "Lucida Handwriting",cursive;
	font-size: 15px;
	transform: rotateX(4deg);
	-webkit-transform: rotateX(4deg);
}
		</style>
	</head>
	<body>
		<div class='container'>
			<div class='card'>Buy some milk</div>
		</div>
	</body>
</html>

CSS :after pseudo class with 2 icons as content

I was again working on my post-it cards for a Kanban board. Now I wanted to display dots (red and green) depending on some attributes of the task. Let’s say there should be a red dot for a task involving testing and a green dot on a card involving documentation. Of course if the task involves both testing and documentation, I’d like to see two dots – a red one and a green one.

To make it even more complex, I want to display a pin icon on top of the card (whereas the dots would be at the botton).

First, I implemented the pin with an :after pseudo class:

.card .cardTitle:after {
    content: url("pin.png");
    left: 100px;
    position: absolute;
    top: -10px;
}

This just adds a pin icon after the title and positions it in the middle of the top part of the card.

Now I wanted to use the same trick and add a dot for tasks involving testing:

.card.test .cardTitle:after {
    content: url("red_dot.png");
    left: 84px;
    position: absolute;
    bottom: 0px;
}

Now the pin was gone and insted of the pin, I could see a dot. Ok, so it looks like I cannot use 2 :after pseudo class on the same object.
The solution was to use another pseudo class:

.card.test .cardTitle:before {
    content: url("red.png");
    left: 84px;
    position: absolute;
    bottom: 0px;
}

Since we’re positioning absolutely, we do not care whether the dot is added using the :after or the :before pseudo class.
This works well as long as I want to display either a green OR a red dot. If I want to display both and define something like this:

.card.test .cardTitle:before {
    content: url("red.png");
    left: 84px;
    position: absolute;
    bottom: 0px;
}
.card.documentation .cardTitle:before {
    content: url("green.png");
    left: 116px;
    position: absolute;
    bottom: 0px;
}

Well, this doesn’t work because it will only display the green one instead of both dots.
It took me while until I figured out, I can just define all combinations and use two URLs in my content:

.card.test .cardTitle:before {
    content: url("red.png");
    left: 84px;
    position: absolute;
    bottom: 0px;
}
.card.documentation .cardTitle:before {
    content: url("green.png");
    left: 116px;
    position: absolute;
    bottom: 0px;
}
.card.test.documentation .cardTitle:before {
    content: url("red.png") url("green.png");
    left: 84px;
    position: absolute;
    bottom: 0px;
}

And it worked ! Of course if you have more dot colors, the number of combinations just inreases exponentially!

Post-it cards with CSS

For a Kanban board I needed to create a virtual card on the board which would look like the post-its which are used on analog boards. Here is how to do it very easily with CSS just using a few steps.

First we need a card:

<html>
	<body>
		<div>Buy some milk</div>
	</body>
</html>

It looks like this:
post-it.1

The first step is obvious: a post-it should be yellow, so we’ll start with the following CSS:

background:#ffffa5;

It now looks like this:

post-it.2

Then we need to actually give it the form of a post-it using the following:

width:250px; height:150px;

Now it looks like a yellow rectangular card: post-it.3 Now we need to fix the position of the next: it’s much too close to the border of the card:

padding:15px;

It slowly does look like a real post-it…

post-it.4

So that it looks like the cards on the current board, you need to actually have them look handwritten. Let’s change the font:

font-family: "Lucida Handwriting", cursive;
font-size:15px;

There are many other handwriting fonts available but this one is pretty much available on all modern PCs.

post-it.5

It does already look quite good, but it still doesn’t feel like a paper card… Maybe with some shadow:

box-shadow: 2px 4px 6px #444;
-moz-box-shadow: 2px 4px 6px #444;
-webkit-box-shadow: 2px 4px 6px #444;

This handles the following browsers:

  • Internet Explorer 9.0 and higher
  • Firefox 3.5 and higher
  • Opera 10.5 and higher
  • Safari 3.0 and higher
  • Chrome 1.0 and higher

It then looks like this:

post-it.6

Now this looks good ! The only thing which still looks very computer like and less human like is that the card is perfectly horizontal. This is probably never the case when you stick something on a board. This can be done with some CSS:

transform: rotate(-3deg);
-moz-transform: rotate(-3deg);
-webkit-transform: rotate(-3deg);
-o-transform: rotate(-3deg);
-ms-transform: rotate(-3deg);

And looks like this:
post-it.7

We’re done ! Now you can still improve it by looking at different shadow effects, increasing or decreasing the angle…

Quick Tip: How to Code a Scrolling Navigation Bar

So you’ve seen them on other people’s websites and you want to know how you can have one too? The scrolling navigation menu seen on many websites is really easy to replicate in WordPress. Help your readers out, let them click the menu from any point on the page. Here’s how…

This is basically only about some relatively basic CSS but I guess it might be interesting to time of you.

Check username availability using jQuery and PHP

I’m working on a new website. I currently only have a screen where the new users can register. Since I want to make it as easy as possible for a new user to register (partly to avoid having them give up in the first few minutes), I need to take care of the username issue:
When you register somewhere, you have to enter a few things about yourself and though I’m sticking to only username, email address and password it’s still a pain to fill it all in, click submit and get the message that the username is not free.
It’s much easier on the new user, if you immediately give him feedback whether the username he has chosen is still free. And it’s much better, if he doesn’t have to click X times on the “check availability” button.

So basically what I need is a form with a username text field. And whenever the user types in something, I want to give some visual feedback (like changing the color of the text field border) whether this username is available or not.

First I need a form element (in the body):

<form method="post" action="register.php">
	<label for="username">Username</label>
	<input type="text" maxlength="30" name="username" id="username">
	<input type="submit" value="submit">
</form>

Of course such a form with only a username makes no sense, but it’s good enough to explain what I’m doing…

Next, we need to do some jQuery magic (in the head section):

<script src="jquery-1.8.0.min.js"></script>
<script>
$(document).ready(function(){
	$('#username').keyup(check_username);
});

function check_username(){
	var username = $('#username').val();
	if(username == '' || username.length < 6){
		$('#username').removeClass("available").removeClass("notavailable");
	}
	else {
		jQuery.ajax({
			type: 'POST',
			url: 'check_username.php',
			data: 'username='+ username,
			cache: false,
			success: function(response){
				if(response == 1){
					$('#username').removeClass("available").addClass("notavailable");
				}
				else {
					$('#username').removeClass("notavailable").addClass("available");
				}
			}
		});
	}
}
</script>

A few comments on this part:

  • I use keyup to install my function because I want the check to happen as the user types. If it creates too much network traffic for your taste or it’s good enough for you to only check the availability when the user leaves this text field, you could use change() or blur().
  • Since in my case the username needs to have at least 6 characters I do not check usernames with less than 6 characters.

As you see I use some CSS classes to change the appearance of the text field based on the availability of the username, here the CSS part:

.notavailable {
    border: 3px #C33 solid !important;
}
.available {
    border: 3px #090 solid !important;
}

Instead of changing the border color, you could also change the background color or show a tick or cross sign…

Now let’s have a look at the last piece: the PHP part on the server, check_username.php (which is called using Ajax):

<?php
$username= mysql_real_escape_string($_REQUEST["username"]);
$con = mysql_connect("xxxxxx","xxxxxx","xxxxxx");
if (!$con)
{
	echo 0;
}
else {
	mysql_select_db("xxxxxx", $con);
	$result = mysql_query("SELECT * FROM users WHERE username='" . $username . "'");
	$num = mysql_num_rows($result);
echo $num; //it will always return 1 or 0 since we do not allow multiple users with the same user name.
}
mysql_close();

That’s it ! And this is how it looks like:
Green border when the user name is available
Red border when the user name is not available

CSS: center a DIV

If you need to center a DIV you can use the following CSS code:

#mydivid {
position:absolute;  /* to be able to set the position within the containing div (from left and top) */
left:50%; /* start at 50% of the parent div */
top:50%; /* start at 50% of the parent div */
width: XXXpx; /* only works with fixed width divs since you need to divide by 2 below */
height: YYYpx; /* only works with fixed height divs since you need to divide by 2 below */
margin-left:-xxxpx; /* xxx = -XXX/2 : moving the center to the right position */
margin-top:-yyypx; /* yyy = -YYY/2 : moving the center to the right position */
}

Note that absolute positioning means positioning relatively to lowest parent in the hierarchy with a position attribute not equal to static. If all parents have position: static (or no position:xxx) then it will position relatively to the <body> element.

An alternative to position: absolute is position: relative. This means that the position defined is relative to the position, the element should have had (if it had position: static). This means it will be relative to the parent if its parent as no children before it with position:static. If it has siblings preceding it with position: static, it will not be centered properly (of course you can then define the siblings after this element to solve the problem).

If you want to center it on the page and give it a fixed position (i.e. it will not move while scrolling), you should replace:

position:absolute

by:

position:fixed

If what you’re centering is a background image, you can use either:

body {
background: url(background.png) center center no-repeat;
width: 100%;
height: 100%;
}

or:

body {
background-image: url(background.png);
background-repeat: no-repeat;
background-position: 50% 50%
}

If you want the background image to always be displayed at the same position (kind of like a watermark), you can just add the following:

background-attachment: fixed;

Three column layout

The first possibility (and oldest one) is to simply use HTML tables to create the three columns. Basically, you create a table which only has one row and 3 columns.

<table width=”100%”>
    <tr>
        <td width=”20%” style=”vertical-align: top;”>  
        …
        </td>
        <td width=”50%” style=”vertical-align: top;”>
        …
        </td>
        <td width=”30%” style=”vertical-align: top;”>
        …
        </td>
    </tr>
</table>

Pros: 

  • wide browser support

Cons: 

  • polluting the HTML with non-semantic tags
  • the content is not displayed the same when in a div tag or in a table cell

In order not to pollute our content with non-semantic tags (<table>,<tr> and <td>), the obvious solution is to switch to CSS tables. This allows to use semantically meaningful div tags by only having the formatting tables in CSS.


Here the HTML code:

<div id=”leftcol”>


</div>
<div id=”middlecol”>

</div>
<div id=”rightcol”>


</div>

And the CSS:

body {

    display: table; width: 100%
}
 
#leftcol, #middlecol, #rightcol {
    display: table-cell;
}
 
#leftcol {
    width: 20%;
}
 
#middlecol {
    width: 50%;
}
 
#rightcol {
    width: 30%;

}

Pros:

  • not polluting the HTML with non-semantic tags



Cons:

  • doesn’t work in IE7 and earlier
  • using CSS tables does affect the rendering of the contents
The third possibility is to use CSS floats. Here we basically define a central column with a margin as wide as the left column and then have the left and right columns respectively float on the left and right side of the central column.

Here’s the HTML:

<div id=”middlecol”>

</div>
<div id=”leftcol”>

</div>
<div id=”rightcol”>

</div>

It’s important to first define the div for the middle column. From a semantic point of view, we could consider it being actually better since it’s usually the main content column and the other 2 usually contain the navigation and sidebar.

Here’s the CSS:

#middlecol
{
    float: left;
    width: 50%;
    margin-left: 20%;
}
#leftcol
{
    float: left;
    width: 20%;
    margin-left: -70%;
}
#rightcol
{
    float: left;
    width: 30%;
}

Pros:

  • not polluting the HTML with non-semantic tags
  • doesn’t impact the rendering of the columns (except their positions)

Cons:
  • doesn’t work in older browser