Combining Advanced CSS Selectors

If you are trying to familiarize yourself with all the cool tricks that CSS3 can offer, I cannot recommend another post as highly as this one from Smashing Magazine.

One cool thing that you can do with advanced CSS selectors is chain them together to create amazingly advanced selections within your documents. This post is just a quick overview of selection tricks you can do to make your life a bit easier at times when you come across difficult styling issues.

We are going to use this code as a sample:

<div class="second third fourth">Words 1</div>
<div class="first second third">Words 2</div>
<div class="first second third">Words 3</div>
<div class="first second third fourth">Words 4</div>

As a quick example, if we write the following CSS:

div[class*="fourth"]{
background-color:#666666;
}

This CSS rule will select any div that has the class “fourth.” Our result will look like this:

Using one styling ruleIf we modify that rule to look like the following:

div[class*="fourth"][class^="second"]{
background-color:#666666;
}

We will get the results that look like this:

A CSS rule with 2 selectors

Using the ^ symbol as we did above is the rule condition that states that is must being with the specified content. In this case, the class of the chosen element must begin with the word “second.”

I won’t make this post very long, but you can easily chain these advanced selectors when you need to hit specific elements in your page. Especially if elements are dynamically created.

Questions or comments, ask below!

Creating a WordPress Plug-in to customize the Dashboard

I’ve used many different plug-ins to accomplish the simple goal of modifying the dashboard to match the look and feel with the rest of the site. Some worked, some didn’t. And some were needlessly complicated. I thought “I bet this would be much easier if I just wrote my own custom CSS.”

But I didn’t want to mess with any core files, so I figured “Hey, if these plug-ins were loading additional CSS, I can too.” So I spent a bit time looking into how you write a plug-in and found it was really easy to load a CSS file.

This walk-through will go through the simple steps of creating your own WordPress plug-in that loads your own CSS file to the dashboard.

The first step we need to do is to create the folder for your plug-in and the default file. I created my plug-in for The Recipe Folder, so my folder is called “recipe-folder-admin-theme” and the default file is called, you guessed it, “recipe-folder-admin-theme.php”.

The contents of this file is similar to creating your own theme. In a php code tag add something like this:

/*Plugin Name: The Recipe Folder Admin Theme
Plugin URI: http://www.therecipefolder.com
Description: Custom Recipe Folder Admin Theme
Author: Daniel Gray
Version: 1.0
Author URI: http://demo.therecipefolder.com*/

Adjust to match your details. This adds the necessary information for WordPress to identify your new plug-in. To make the Dashboard load your custom CSS file, add the following lines (extra line breaks added to keep the post within the column of this post):

function my_wp_admin_css() {
echo 'css"
type="text/css" />';
}
add_action('admin_head','my_wp_admin_css');

As you can imagine, the CSS file can live pretty much wherever you want. You could host it remotely if you want to distribute changes automatically, though if you release it as a plug-in to people, they may not be fond of loading content from another source.

The add_action with the flag “admin_head” tells the plug-in where the function should be executed. In this case, the my_wp_admin_css function simply echos a link to a CSS file in the header of the admin page, which is the dashboard.

After that, it’s creating your CSS file and adjusting style to your heart’s content. Power up Firebug and start going to town.

A customized dashboard

Any questions or comments, be sure to leave them here!

Columns

Faux Mega Menu in WordPress

Sometimes you have so many items in your WordPress blog’s navigation that you feel that it’s far too long. But if they are in the proper logical division splitting them into other menus is not be an option.

There are fantastic tools to create “Mega Menus” with jQuery and CSS such as this one. But on the standard WordPress menu you find in themes like 2010 or Thematic, implementing can be a challenge sometimes.

Luckily for us we can use CSS3 for a quick fix to make your drop down menu split itself into columns. The most obvious answer is to use the new “Columns” property but unfortunately it is not widely supported, even among modern browsers.

For reference, in this example I am styling a drop down menu in Thematic.

.sub-menu{display:inline-block; width:500px !important;}

.sub-menu li{width:50% !important;}

.sub-menu li a:hover{color:#FFFFFF;}

.sub-menu li:nth-child(even){float:right;}

All we are telling it to do is to make the sub-menu 500px wide, make every list item 50% of that, and using the nth-child property, tell every even-numbered element to float right.

Making it 3 columns would be as easy as dropping the li width to 33% if we wanted. Applying styles is easy as well. If we wanted a border dividing the left and right column (if mine didn’t already have borders) we could further edit the nth-child(even) and give it a left border, or make a new rule for nth-child(odd) and give it a right border.

Super easy!

Fake Conditional Statements in CSS

For a first post, let’s start this off simple. I saw a question on Reddit the other day asking if you could dynamically change the size of elements depending on what the user clicked on without using Javascript. [http://goo.gl/SErS9]

You can almost create this effect with CSS3 selectors and no Javascript at all.
Here is the CSS:

#div1, #div2
{
width:200px;
border:solid 1px #000000;
float:left;
margin-right:10px;
height:50px;
overflow:hidden;
}
div[id^="div"]:hover
{
height:200px !important;
}
div:target
{
height:200px !important;
}

And the HTML:

<a href="conditional-test.html#div1">Link to Div1</a>

<a href="conditional-test.html#div2">Link to Div2</a>
<div id="div1">

<a name="div1">This is a bunch of data.</a>
This is more data you can't see yet.

</div>
<div id="div2">

<a name="div2">This is a bunch of other data.</a>
This is more data you can't see yet.

</div>

The first handy thing to point is the chunk of CSS that says div[id^=”div]

At first glace this might seem like unnecessary mark-up, but the carot symbol (^) is actually a CSS identifier that says “target anything that starts with.”

So in this case, I am looking to target divs ( div[id^=”div] ) whose ID starts with ( div[id^=“div] ) the word “div.” ( div[id^=”div] )

As you can see in the HTML I have to divs names “div1” and “div2” and this was just one way to target both of them. This becomes more effective the more elements you have that have similar names and share similar styles, but have their own variations or perhaps are dynamically generated by something like a C# control and they end up with weird additions to what were your nice cleanly defined classes.

At the top of the HTML, you can see that we have two links, each ending in a hash tag and the name of one of our divs. When these links are clicked on, that name is appended to the end of the URL and is referred to as the “target.”

This incorporates the final chunk of our CSS that pretty much says if there is a div on the page with the same name as this target in the URL, append this style which in this case happens to make the div tall enough to see the other sentence of content.

With this method you can’t do fun things like animating the application of styles, but the technique does work and is super simple to do if you want to implement it in a hurry.