WP Theme Lesson #13: Styling Sidebar

tutorial-series.gif Follow this WordPress Theme Tutorial Series from the beginning.

After styling the Sidebar, this tutorial series will be almost over. Already? Yes, but don’t worry. I’ll follow up with case studies (break down of how I created my themes).

Again, you don’t need the index.php file today. Open Xampp, theme’s folder, Firefox, IE, and style.css in Notepad.

Step 1

Type this under .sidebar{}:

.sidebar ul{
list-style-type: none;
margin: 0;
padding: 0 10px 0 10px;
}

Now that you’ve styled the parent unordered list (UL) tag for the Sidebar, all child ULs or nested ULs will inherit the same style. In this case, it’s none, zero margin, and 10-pixel side paddings.

For example:

inheritance.gif

The second-level (or nesting) UL inherits the styles from the first UL. If you apply a border to the first UL, the second UL will also get a border.

Save and refresh to see that your list items (LI) are no longer bulleted.

Notice how you add padding to the top and bottom.

no-top-padding.gif

Step 2

Type this under .sidebar ul{}:

.sidebar ul li{
padding: 10px 0 10px 0;
}

Here’s the padding now:

top-padding.gif

Why not add the 10-pixel padding to the UL tag in the first place? You need a way to separate one LI from another. Before the added padding to .sidebar ul li{}, there was no space in between the search form and calendar and no space in between Calendar and Pages. The spacing in between each block is what 10-pixel top and bottom paddings for .sidebar ul li{} are for. If the UL tag had padding for all four sides instead of just left and right (0 10px 0 10px), you would end up with a 20px top padding or 20px bottom padding in trying to separate the LIs after padding the UL. Get what I’m saying?

If you don’t, go ahead and add top and bottom padding for .sidebar ul{}.

Step 3

Type this under .sidebar ul li{}:

.sidebar ul li h2{
font-family: Georgia, Sans-serif;
font-size: 14px;
}

Remember that styling the sub-heading under .post{} didn’t matter because you were targetting sub-headings only under .post{}? Now, you’re styling the unaffected sub-headings in the Sidebar. Here’s the result:

h2-and-unnecessary-padding.gif

That’s how my Pages links listing looks. You probably have only one link, About. I added multiple links or page levels to my offline WordPress install to test and see how lower level links would look. Notice the unnecessary extra padding at the bottom that I circled. That’s a perfect example of style inheritance. Instead of 10 pixels, it’s 20 pixels.

Because you added padding to .sidebar ul li{}, the lower level LIs inherited that padding. To fix it, do Step 4.

Step 4

Type this under .sidebar ul li h2{}:

.sidebar ul ul li{
padding: 0;
}

The consecutive ULs in .sidebar ul ul li{} indicates that you’re targeting the lower level LIs. Again, when all values are 0, you don’t need the px suffix.

Here’s the result:

ul-ul-li.gif

The line spacing is too close so lets make the line height equal 24px.

Add line-height: 24px; to .sidebar ul ul li{}.

line-height.gif

By the way, if you’re getting extra spacing under the search form in Internet Explorer. Add form to:

body, h1, h2, h3, h4, h5, h6, address, blockquote, dd, dl, hr, p{
margin: 0;
padding: 0;
}

like this:

body, h1, h2, h3, h4, h5, h6, address, blockquote, dd, dl, hr, p, form{
margin: 0;
padding: 0;
}

Step 5 (Optional)

Do this step if you want the calendar dates to stretch and cover the full sidebar width. Your calendar currently looks like this:

calendar1.gif

To style the calendar, find out tag it’s sitting in and what the name or id of that tag. To do that, go to View > Page Source or Source. Your sidebar is at the bottom so go to the bottom of the source codes and look for the Calendar.

caledar-id.gif

Now that you know the calendar sits in a TABLE tag with wp-calendar as id. How do you target the wp-calendar table in in the style.css file?

table#wp-calendar{} is the answer. Why? Earlier, you learned to use the pound sign when styling DIV named with an id instead of a class. In this case, it’s a table instead of a DIV, with an id value, wp-calendar.

#wp-calendar{} would work because it’s unique and WordPress doesn’t use #wp-calendar for any other tag. But, you should try to be specific when you can. If you want to be more specific, use .sidebar ul li table#wp-calendar{}. Even more specific? Fine, use .sidebar ul li#calendar table#wp-calendar{}. That’s because the list item (LI) containing the Calendar sub-heading and Calendar TABLE has an id named calendar.

Now that you know what to use, how do you make the table stretch? Use width: 100%; for the table.

Type this under .sidebar ul ul li{}:

table#wp-calendar{
width: 100%;
}

Save and refresh. Here’s the result:

calendar-width.gif

width: 100%; because you want the Calendar table to adjust to the width of the Sidebar, no matter how many pixels you want to change the Sidebar to.

It doesn’t look good, but you’re learning how to do it, in case you want to. This calendar needs more styling to look right. Hint: Look in source codes again, find out what tags under the TABLE that you can style.

That’s the end of today’s lesson.

What's Next?
Related Posts

Now this is the bit I’ve been having problems with in the theme I use, all those nested lists!

I hope this will help me crack it - it certainly makes it much clearer. Thanks!

Back in Lesson 6, you said that wp_list_cats() will add the list tag, so it doesn’t need them.

But do I “count” that list when I’m adding CSS styling, as you show above? (Such as in:

.sidebar ul ul li{
padding: 0;
}

Marksson:

Hey there, SP. Like many others here, I find these tutorials really helpful!

In step 5, you talk about being specific in the way you address objects in css. Trying to understand this better, I (blindly) copied and pasted your “most specific” option, but without success. Then I found out that in those lines, you use ” #calendar” instead of “#wp-calendar” at the end.

But you probably meant do to that to test us, right? Hehe. Well, it worked for me ;)

Keep up the great work!

Marksson, you’re too kind. That was my mistake. Thanks for spotting that.

Marksson:

Off-topic: if that is in fact your picture at the top of the page, we have something in common, hehe. Check my hyves page (Dutch version of MySpace)

Hey SP!

Firstly, these tutorials are really great. I appreciate the time and effort you put into making them. Nothing else like it on the web!

Just a quick question for you…I’ve noticed the sidebar ends up not spanning the entire length of the page. For example, there is always a gap between the footer and the sidebar with the background exposed. How do I make the sidebar expand/contract and touch the footer depending on what’s posted? Hope that makes sense! Take care.

Sorry, that didn’t make sense.

Hey, sorry about that. I’ve uploaded my theme to test.literallyfresh.com.

And I guess the problem is not with the sidebar, but with the main content…do you see how the white background doesn’t extend all the way down? It wraps up as soon as the post is complete.

Thanks!

Lou:

Hello

First off all: Thank you for this great tutorial, it really helped me out.

But i wonder what i have to do to get rid of the ‘-structure’, because i want to make a navigationlist with an image of a little arrow before my categories, links, etc.

Greetz

That has to do with styling, nothing to do with structure.

mikra:

Hello,

First, thanks for the great tutorials.
Everything works fine, but one thing:
I have the problem, that in Internet Explorer 6 the search form of the sidebar (and the widget text 1) are right of the sidebar (on top, outside of the sidebar). Both have a dot near the heading. The heading of text 1 is also taller as the other headings.
Firefox displays both correct.

Copying your css-template 1:1, shows the same problem.

How can this be fixed?

[…] Intro #10 hex Codes and Styling Links #11 Widths and Floats #12 Post Formatting and Miscellaneous #13 Styling Sidebar #14 Footer and Diving Index #15 Sub-template Files #16 Comments […]

Hi man, sorry for my post out.of.date, but I was trying your Sleepy WP Theme, and I can’t add an image (200×200px) to the sidebar :

That’s how I do, I dunno if it’s wrong, I don’t know much coding-modifying WP but I’m on start. My code is fine or not? And why is not working?

Bene - Codes don’t show up in comments.

[…] 翻译自:WP Theme Lesson #13: Styling Sidebar […]

Daiyaan:

thanks for this tutorial series!

Hey Small Potato, I’ve been wanting to learn how to create a theme for a long time, thanks for writing this.

I wanted to ask you though, how do you make the sidebar flow all the way down the page? I’ve tried

html, body{
height: 100%;
}

then making the sidebar’s height: 100% but that didn’t work. Any help you can give would be appreciated.

Matt - Look up Faux Columns

I’ve been looking for a tutorial JUST like this one… Thank you!

How (or where) would a second sidebar be styled? I have set up a second setup in my functions.php, using h2. It is totally off from the standard sidebar! http://fbcwaynesboro.org/wp

The second sidebar doesn’t need separate styling. It only depends on where you want to place it.

From div to /div of the second sidebar, just copy and paste it where you want to place it.

Vinit:

How to convert psd file into a wordpress theme ? Can u guide me plz ? Your tutorials r really great. . .
Thx.

Vinit. I’m sure there are others on the web that can explain how to do that better than I can. A quick search on google should do it for ya.

jicoro:

thanks again,,

Small Potato,

Have you ever run across the problem of having the sidebar align itself with the top of the page, next to the header (as seen on http://www.thenightshade.com/wordpress), instead of below the header? I would be more than happy to send my style.css if that’d help you answer the question.

Regards,

Cree

P.S. Browser: Mozilla Firefox 2.0.0.14, FTP/text editor:Adobe Dreamweaver CS3

All very embarrassing, I just forgot to check my spelling… (siderbar)

Thanks Small Potato for the great tutorial!

Cree Dalene
Owner, Nightshade Web Solutions

Reply
Comment Policy
  • Theme support questions should be posted at the support forums.
  • Name and Email are required. Email is never published.
  • You grant this site perpetual license to reproduce your words and name/website in attribution.
  • Inappropriate comments will be removed at my discretion.
Close
Powered by ShareThis