Posts Tagged ‘cascading style sheets’

How to center an image with CSS

I had a funny problem the other day when I was rebuilding my portfolio, I’d completely forgotten how to center an image using CSS. What I wanted to do was align my portfolio piece images to the center of the screen but regardless of how hard I tried they kept ending up on the left.

girls are evil left How to center an image with CSS image

This is the default position for an image of course, so I wasn’t too concerned about it. If I wanted to ensure that it was always on the left I could of course always add the following code to my CSS file:

[source lang='css']img {
float: left;}[/source]

This would ensure that my image not only aligned to the left but also allowed text to wrap around it’s right hand side. If I wanted to only align the image to the left, without allowing wrapping text I would add:

[source lang='css']img {
float: left;
clear: both;}[/source]

Or if I wanted to do exactly the same thing to the right, I would use the following CSS code:

[source lang='css']img {
float: right;
clear: both;}[/source]

girls are evil right How to center an image with CSS image

Right and left alignments in CSS are really easy, but what about centering the content? If I was a lazy web designer, I could always use HTML to center the image but that’s a bad practice to get into. HTML is designed to publish content, not format it. 

[source lang='html']

 How to center an image with CSS image
[/source]

Since the center tag is used to format the appearance of HTML I try to avoid it, which turns me back to CSS (Cascading Style Sheets) to center my content.

girls are evil center How to center an image with CSS image

Generally in CSS if you want to center something you simply set the left and right margins, if you don’t know the width of your object you can specify it in the CSS but in my case each portfolio piece width is different so I needed to be more generic.

[source lang='css']img {
margin-left: auto;
margin-right: auto;
display:block;}[/source]

The trick (if it really is a trick) is to include the display:block line. As we all know (cough), HTML elements are made up of two types of objects. The block objects such as <p>, <h1>…<h6>, <div>,<ul> ect. and inline objects such as <strong>, <b>, <em>, <i> etc., the <img> tag is by default an inline object which means that web browsers do not automatically attempt to associate a buffer for the object.

Since the <img> tag does not have a pre-existing space or area, it can not be centered in an area until you instruct the web browser to assign it an area. For example if you wrapped the image in a <div> or <p> tag, you could text-align the <p> or <div> tag as such:

[source lang='html']

 How to center an image with CSS image

[/source]

[source lang='css']#centerme {
text-align: center;}[/source]

This example works because the <div> tag is a block and therefore the web browser already knows it has a space it can occupy but if you don’t want to clutter your HTML with unnecessary <div> tags, you can simply use the CSS display:block; property to instruct web browsers to treat the <img> tag as a block and accomplish much the same trick.

Happy coding!

I want to sell you a new website, because you need it.

The other day I was at a business lunch and learn and somebody asked me why I wanted to sell them a website, I couldn’t imagine such a silly question and so I answered him ‘because you need it.’ Sadly, it wasn’t just me being snarky (I don’t get to use that word often enough) it was the simple truth and most likely, you need one too.

This isn’t a sales pitch, you’re welcome to contact me if you feel you need a new website design or help with your WordPress installation and my portfolio is always online if you’d like to see the type of work that I do but I’m not writing this piece to convince you to buy from me, I’m writing this piece to help you understand that you should buy from somebody.

Most websites fall into one of three categories, and each has points that damage your businesses ability to compete in the modern world of technology. Remember, in 2009 people are more likely to visit your website than visit your office. If your website doesn’t fall into these three categories, congratulations you’re one in a million:

Out of Date

Web sites are like living, breathing organisms. You have to constantly feed them and that takes fresh content. Every web page on your website should be reviewed and updated at a minimum every three months for accuracy.  Your home page should change once a week at the very least, which is where a blog comes in handy. Blogs (web logs) are used to promote events and feature news from key employees much the same way newsletters used to reach clients.

Unlike other forms of technology, websites are still relatively young and the basic technology which makes up a website is still evolving. To determine if your website is compatible with the majority of web browsers today, test both your home page and an internal page (such as the about page) with the W3C validator.

Effective modern websites should make use of Cascading Style Sheets and feature tableless design. This is critical to meeting the needs of 10% of the population as websites which feature tables are poorly interpreted by  web browsers for the visually impaired. You can test if your website has tables by viewing the source code of your document (often found under the Edit menu of your web browser) and you can also test the quality of your CSS using an online validator.

Modern websites are not strictly used as online brochures, they are also feature rich tools for social networking. This “web 2.0″ technology allows users to access data from your website using a number of tools including XML (extendable markup language) sitemap documents, RSS feeds and interconnectivity to tools such as Twitter and Facebook. Each of these technologies has it’s own place in making your website a successful online tool for both yourself and your prospective clients.

Difficult to Use

One of the biggest problems facing website owners is a misunderstanding of the purpose of a website. Web sites are sales tools, not technology projects. Web Sites use technology to deliver your message, don’t be a slave to what your web design can (or can’t) do and don’t assume that your IT person knows the first thing about websites.

I have an image in my mind of some printer in the 18th century buying a brand new printing press only to discover it didn’t ship with the letter R so instead of admitting he couldn’t do the job right and hiring another printer to get his clients work out the door, he pinted eveything using othe lettes o wose simply didn’t do it at all. That’s what I think most web designers are doing, they lack the skill to do it right so they don’t do it at all.

Here’s the honest truth, there is no reason your website doesn’t work as well as Apple’s or Dell’s. Go on over to those websites and try to order a computer. See how easy it is to find what you’re looking for? Notice that it’s easy to order? Guess what, they’re using the same HTML as your website uses so there’s no reason your website sucks.

Poor Representation 

Take a good long look at your website, now take a good long look at the business that you’ve spent years building up and ask yourself honestly if what you see on screen is what you want the public to think of your business. Ouch. I have no tolerance for bad web design and even less for business owners who allow it to happen. The web is the great equalizer, you can do anything your competition can do with a website and you can do it faster, cheaper and more often than any other time in history so honesty ask yourself … does your website represent your business in a positive light? If the answer is no, then fire the team you’ve working with and look around for a new team.

Conclusion

So, if you don’t need a new website and you’re proud of what you have … leave a comment with the URL to your site below and I will feature it in a new write up, people need to see great websites.

Fundraising Thermometer Plugin for Wordpress

The plugin interface, for WordPress

Features

Example of a fund raising image from the new plugin

The fund raising tool is a true WordPress plug-in, testing with WordPress 2.7 and fully functional. It features:

  • W3C compliant Cascading Style Sheets (CSS)
  • Dynamically updated text including target and current fund status
  • Money formating options for use internationally
  • Theme options, allowing designers to customize the look of the thermometer
  • Call either the graphic or a formated number to display in text.

Admin Features

The new interface allows uses to set both the current amount (what has been raised) as well as the target amount (what you want to raise). The script automatically generates the ten steps in between and displays them for the user.


The plugin auto detects if the money_format() function is present and will use it automatically if found, otherwise it will display the currency using the number format function automatically.

How It Works

To display the current amount of money raised (in currently format) place the following code in your theme:

<?php echo show_ourprogress();?>

For a graphical version (by default the thermometer), include the following code:

<?php show_ourprogress_graphic();?>

Where to Get It

The plugin can be downloaded from the official WordPress repository: 

download file Fundraising Thermometer Plugin for Wordpress image

How You Can Help

I love developing plugins for WordPress and do my best to always keep them free but of course it take a lot of time and effort to build these plugins so if you’d like to say thanks, the best way is to take a few moments and write about the plugin on your own website, include a link to my website from your posts or download and rate the plugin on the official plugin directory. 

Support forums are now online! If you have any questions, please visit http://forums.thisismyurl.com.

Using CSS to highlight a link

Now you know how to add a really cool hidden link in CSS but what about if you want to make certain links stand out? Just as easy

First, when you add a link to your website we want to assign a class to it like so:

<a href=’http://www.thisismyurl.com’ class=’standout’>

In my case, I’ve labeled this link with the special tag standout which will let me affect the appearance. Next in the CSS, let’s add a special rule for it:

a.standout {
/* this is what we want the normal link to appear as */
text-decoration: none;
font-color: #990000; /* (assuming you want your link blood red */
font-weight: bolder;
}

a.standout:hover {
/* when people mouse over the link */
font-color: #000000; /* (assuming your default text is black) */
cursor: default;
text-decoration: underline;
}

In this example, I’ve made the link red but still taken the underline away. I’ve put the underline back when you mouse over the link which calls extra attention to it and shows the user there’s something interactive with the link.

How’s it work? Pretty simple really, any time your HTML calls an <a> tag with the class standout, the CSS will be loaded and in turn change the appearance of your site to accommodate the new new.

Using CSS to disguise a link.

Sometimes on a website we want to link to another section for Google or other robots but we don’t want to distract our users with too many links, how do we do it? Cascading Style Sheets and a little HTML knowledge.

First, when you add a link to your website we want to assign a class to it like so:

<a href=’http://www.thisismyurl.com’ class=’noshow’>

In my case, I’ve labeled this link with the special tag noshow which will let me affect the appearance. Next in the CSS, let’s add a special rule for it:

a.noshow {
     /* this is what we want the normal link to appear as */
     text-decoration: none;
     font-color: #000000; /* (assuming your default text is black) */
}

a.noshow:hover {
     /* when people mouse over the link */
     font-color: #000000; /* (assuming your default text is black) */
     cursor: default;
}

As you’ll see since rules cascade from in a logical order, the text-decoration rule automatically applies to the hover state since it applies to the normal state but since hover states have their own color, we have to redefine it.

There you go … now you can put links on your website that nobody knows are there.

Directory Tips for Web Designers

Building a great website is about a lot more than putting together a bunch of text or graphics on a page, it’s critical that a website be manageable and expandable if you want it to work properly in the future. With that in mind, here are some basic tips for new web designers that a lot of books seem to overlook but I feel are critical.

Directory Structure

First off, organize your files effectively. It may seem silly but there’s nothing worse than having to go back and find a file in a months time and it only take a few minutes to do it right the first time. Building a proper directory structure is simple, first lets start with the root folder (that’s the main directory of your website) and add a few new folders:

  • css – cascading style sheets directory
  • images – used for all the images on our site
  • includes – common files that are loaded by multiple pages
  • js – the javascripts for our site
  • media – a directory for rich media

Now these new folders will store all of our future files and make the job of locating the related content much easier. Remember, we always want to lowercase our directory names to make it easier to remember.

File Names

A common mistake in naming files is to use MiXed CaSe or Capital Case. This works great for labelling filenames on your personal computer but many web servers are case sensitive meaning two files may have the same name as long as they have a different mix of upper and lower case characters. For example, Index.php and index.php are considered separate files on standard web servers.In addition to the quirks of web servers, we also want to avoid using mixed or upper case names for our users. That way a user only has to remember the path to your specific page and not the case structure of your web naming conventions. If you do have to separate a filename into recognizable sections, I prefer to use an underscore (_) or a dash (-) to do it such as contact_form.php or conact-form.php which allows users to easily see and dictate the spelling to others.

File Extensions

There are few things more frustrating than trying to remember the proper syntax of a filename when you’re coding or looking for a specific file. Adding to the confusion is file extensions which often vary from one web server to another. To simplify the process, I strongly recommend always using the .php (for Linux servers) or .asp (for Windows servers) for all your HTML and scripting files. The additional server time needed to load these pages will be minimal and it allows you the easy of standardizing the extensions for your development speed.

Hiding Directories

Sometimes we create web directories that we don’t want the outside world to see. For example, there is no purpose in allowing users access to the /root/images/ directory but we still want people to be able to view the images stored there, so we need an easy way to limit peoples access to that particular directory. Without getting into complex .htaccess files, one of the easiest means to accomplish this goal is the create a new index.php file and upload it to the directory, this will cause people to see a blank page upon reaching the page.If you want something slightly more interesting, you can bounce people back to the directory above the restricted path by including the code <?php header(“location:../”);?> in the index.php file. This little piece of code will force the browser to refresh immediately to the directory above or in our example, if placed in the /root/index/ directory it will force the user back to the /root/ directory.