5 Simple CSS Tips

5 Simple CSS Tips

Cascading Style Sheets, or CSS is a common application used to style web pages. It has been known to reduce bandwidth, increase uniformity among different browsers, and even add some stylistic flair to web pages. Here are a couple of tips to help you along the way.

1. CSS and Text Overflow

If text has been cut off and you want to leave a visual clue to the user, use CSS to leave an ellipses to inform them that there is more to read.
example code:

text-overflow: ellipsis-word;

2. Rotating Text With CSS

By using the “transform” property in Webkit and Firefox, and the BasicImage filter in Internet Explorer, web designers have the ability to rotate text as they please. Designers can use this tool to rotate images as well making it a valuable asset for almost any design.
example codes:

-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);

3. Using CSS to Center Layouts

For web sites that rely heavily on large pictures, it is crucial to present the pictures correctly to prevent any problems. This code allows for proper centering, fixing problems associated with overlapping images, repeating images, or images that cut off neat the edges of the screen.
example code:

#container {margin: 0 auto; width: 30px; text-align: left;}

4. Resetting CSS

Resetting CSS gives users the ability to work uniformly across multiple browsers and platforms, fixing problems with margins, font size, headings, default line heights and other variables that often change between browsers.
example code:

{padding: 0; margin: 0;}

5. Using Shorthand CSS

Especially in larger projects, using shorthand CSS helps to keep file size small, which greatly benefits users as web pages don’t take as long to load. The biggest benefit, however, is its large boost in efficiency. When coding takes a significantly short time, you can do more work in a shorter amount of time.
example code:

#box p {margin-top: 15px; margin-right: 5px; margin-bottom: 15px; margin-left: 5px; }

can be shortened to…

#box p {margin:15px 5px; }

read more

Rounded Corners using CSS

Rounded Corners using CSS

Ever since web pages came into being, designers have wanted to add more and more style into their creations, one of the most important being rounded corners.

Rounded corners using images

At first, this was achieved by using huge images to represent the buttons, forms or any other element.

Later, the huge images were cut into smaller ones to represent the corners, positioned perfectly using CSS (see image on the right).

The HTML 5 spec makes it simpler than ever to create rounded corners, forgoing the need for images altogether and saving us from the horrors of funky CSS.

One can now simply add the border-radius property to any element to achieve the same effect. Here we have a simple span with the following attributes:

Simple Border


margin: 20px;
padding: 4px 10px;
border: 1px solid black;
background-color: #ffff00;

Simply adding border-radius: 6px; turns it into:

Rounded Border


margin: 20px;
padding: 4px 10px;
border: 1px solid black;
border-radius: 6px;
background-color: #ffff00;

That’s all there is to it. Almost makes it seem like a scam after the horrors we have been through in the past.

Let’s use it to create the featured image at the top that was originally created using a photo-editor. We’ll do it using spans:

Span 1

display:block;
width: 182px;
height: 182px;
background:-webkit-linear-gradient(#ccc, #444);
background:-moz-linear-gradient(#ccc, #444);
background:-o-linear-gradient(#ccc, #444);
border-top:4px solid #888;
border-left:4px solid #888;

Span 2 (embedded inside Span 1)

display:block;
width: 140px;
height: 140px;
background-color:#bb0000;
border:1px solid #bb0000;
border-radius:20px;
position:relative;
top:18px;
left:18px;

Close enough. Now how about using this to improve your buttons?

Note: -moz-border-radius has to be added to make it compatible with Mozilla browsers. However, as of Firefox 4, just border-radius is supported.

read more

Text Shadow

Text Shadow

Text Shadow Property

We have long wanted to use 3D text effects on web pages and until now, that meant using images or tricky javascript that slowed down your website.

With CSS 3, this is as simple as making text bold or colored blue.

The text-shadow property allows you to turn any piece of plain text into one that stands out. Simply specify the color and offsets of the shadow, and you’re done.

The syntax is:
text-shadow: <color> <x-axis offset> <y-axis offset> <fuzz amount>

which might look something like this within the stylesheet:
text-shadow: #000 0.01em 2px 0.1em

These offsets may be specified using the standard CSS units. We’ll stick to pixels and em. The fuzz amount specifies how much the shadow should be stretched.

 

Text Shadow Examples

Adding this CSS for your paragraph tag:
p {color: red; text-shadow: #888 2px 2px 0.12em;}

will turn something like this:
That the quick brown fox jumps over the lazy dog
into this:
That the quick brown fox jumps over the lazy dog

 

There are a number of creative and interesting effects you could achieve with this, such as using the same color as the background for the text and adding a shadow:
p {color: white; text-shadow: #888 2px 2px 0.12em;}
That the quick brown fox jumps over the lazy dog

Be careful. The text-shadow property will only work for browsers that support CSS 3 (which is most decent browsers these days). It will be ignored by those that don’t so your text above will be invisible. Should take that into account.

 

You could also stack multiple shadows to create a colorful montage of sorts or a flame text effect:
p {color: #ff6; text-shadow: #f63 0 1px 0.2em, #ff3 -2px -1px 0.12em, #f93 2px -2px 0.12em, #f63 0px -3px 0.3em;}

That the quick brown fox jumps over the lazy dog

 

Text Shadows in Buttons

Now, we could use the same effect for buttons to give them a little more character.

Simply add the “text-shadow” property to the CSS for your input box:
input {text-shadow: #999 0.14em 0.14em 0.2em;}

 

Although there are a lot of effects that you simply can't achieve with cascading style sheets, the text-shadow property is a huge leap in that direction.

read more

Content is King

Just in case you haven’t heard. If you want your website to succeed online, Content is King. Search engines rank you based on originality. Adsense will display advertisements based on your content. And users will stick with you as long as you provide interesting, relevant and original content.

So don’t try to find a shortcut and copy and paste the data from other sites (especially not from Wikipedia). Write/create new content yourself that people will love.

Choosing the type of content is another thing you have to keep in mind. Certain topics are searched more and also provide better returns in terms of advertisements. Others may be interesting to you, but not to your would-be readers.

One way is to find the most searched terms and base your content around that.

Looking at Google’s Hot Trends page, you can see that Steve Perry and other terms related to entertainment are the most searched items at the moment. You might want to write about that.

In the long run however, posts on technical subjects often get the most hits. Any ads that you run on such posts also tend to be worth more so a post on say optimized network routing could be a lot better than my cat throwing up (then again, a funny video of a cat throwing up could become viral on Youtube and earn you lots of money and fame).

read more

Optimizing WordPress post titles

Optimizing WordPress post titles

You should be aware of the importance a site or page’s title plays in Search Engine Optimization. By default, WordPress displays the title of your blog posts in the format:
Site Name » Post Name

This post would be displayed with the title:
CSS Button » Optimizing WordPress post titles.

This is fine if you aren’t too concerned about SEO, or your site is named Apple and ranks top for it, but otherwise, you’ll be like the thousands of other sites out there. To set yourself apart, you’ll need to experiment and optimize WordPress a little, especially the title.

All in One SEO is one of my favorite plugins for WordPress. It runs a number of optimizations on your site and most importantly, it switches your site’s name with your post’s name, so your title is displayed in the format:
Post Name | Site Name

So this post would now look like:
Optimizing WordPress post titles | CSS Button

This is a lot better than the WordPress default, since now, the most important keywords are in the beginning of the title. But there’s still a lot more you can do.

Your site’s name is usually something unique and you should already be ranked pretty highly for that (try searching for “css button”). However, what’s the point of adding that baggage to your wonderful posts? You want your post to stand out and removing the site’s name will definitely help focus on your current topic. Something pretty simple to do with the All in One SEO plugin.

In your admin panel, under the Settings menu, click All in One SEO. The field Post Title Format: should contain:
%post_title% | %blog_title%

Change this to just:
%post_title%

That should do it. Wait and see what it does to your search engine ranking. It will also help your titles on social media such as Facebook and Digg.

read more

Digg users and Adsense

Digg users and Adsense

It is a popular theory that users coming to your website from Digg and other social media websites, are usually blind to ads and therefore aren’t likely to click them.

This is one reason why many publishers prefer to display Google Adsense (or other forms of advertisement) only to users coming in via search engines.

Some people have argued that doing this won’t hurt your overall earnings and you may just be wasting ad space that could have earned something, even if the click-through rate (CTR) went way down.

However, think about it from an advertiser’s point of view. If I’m paying for ads on your website and getting only a few or no clicks in return, either there is something wrong with my ad or I need to find another website.

By that rationale, my preference as a publisher would be to have a higher CTR to attract higher-paying ads. This is the code I use to check for the referring website and hide my ads from Diggers:

<?php
# Let publish_ads be TRUE to start off with
$publish_ads = TRUE;
# Check that we have an HTTP_REFERER header
if(isset($_SERVER['HTTP_REFERER'])) {
    # See if the header contains "digg.com"
    if (strpos($_SERVER['HTTP_REFERER'], 'digg.com')) {
        # It does, so set publish_ads to FALSE
        $publish_ads = FALSE;
    }
}
?>

We could use a similar approach to do the opposite. i.e. display ads only to search users (users coming in from Google searches, for example).

<?php
# Let publish_ads be FALSE to start off with
$publish_ads = FALSE;
# Check that we have an HTTP_REFERER header
if(isset($_SERVER['HTTP_REFERER'])) {
    # See if the header contains "google.co"
    if (strpos($_SERVER['HTTP_REFERER'], 'google.co')) {
        # It does, so set publish_ads to TRUE
        $publish_ads = TRUE;
    }
}
?>

Now you can check for $publish_ads any place you want to display or suppress the ads:

<?php
# Check if $publish_ads is set to TRUE and display the adsense code if it is
if ($publish_ads == TRUE) {
?>
<script type="text/javascript"><!--
google_ad_client = "pub-0000000000000000";
/* css, 468x60, inline-banner, created 1/1/01 */
google_ad_slot = "0000000000";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<?php
#Now we close the if
}
?>

There you have it.

read more

Dashes vs Underscores for SEO

Dashes vs Underscores for SEO

There has been a lot of debate about how Google indexes URLs and whether underscores are better for Search Engine Optimization or would dashes give better results?

I have yet to find a proper conclusion to that debate, though due to another realization, I’m now inclined in favor of dashes. Here’s why:

I’ve been using underscores for most of my filenames and URLs and have been avoiding dashes like the plague, probably because it felt like I was subtracting something and because underscores look a little neater.

However, if you know anything about regular expressions, you’ll know that \w is commonly used to match characters or words in a string. In standard regex \w will only match the letters a-z, A-Z and the underscore character.

This means that in a string such as That-the-quick-brown_Fox, \w* will have four matches (“That”, “the”, “quick” and “brown_Fox”) instead of five. If I was writing a script to chop up URLs and index the letters, this is what I’d get.

Even if Google has updated their algorithms to treat underscores and dashes similarly, chances are, most other search engines, applications and websites have not. Therefore, from now onwards, I’m sticking to the da4shes whenever I can. WordPress already sets the URL using dashes, but the same should come in handy for custom URLs and image filenames.

What are your thoughts? (More info)

read more