Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Adding CSS to a side bar on a wordpress page. Need advice for shorter code

I have a wordpress site and On the blog page I have a sidebar with a menu. I want to change the color of just that menu's buttons, this is the code im using now, Im pretty sure there is a better way to do this but just wanted to ask the community what they think.

A little about what I did. Ya see for some reason my site wants the links and button text to be the same color. So I made a css rule to change the button text to white. Well for some reason when I change the back ground of the button to blue the text color gets changed to black.... so I have to add the white color again. I know this isnt the best way but Ya its what im doing for now.

Here is the web site for ref http://olympiavaporworks.com/category/vaping-guides/ Please dont be too harsh im pretty new at this css stuff and coding so I may not be doing everything the best way

  • - - - - The Code - - - - - html ul style="list-style-type:none"> <li><a class="et_pb_promo_button et_pb_button "style="background: #4EB1BA; color: white; href="/study-hall/"><i class="fa fa-graduation-cap"></i>&nbsp;&nbsp;Study Hall</a></span></li> <li><a class="et_pb_promo_button et_pb_button "style="background: #4EB1BA; color: white; href="/tag/coil-builds/"><i class="fa fa-bolt"></i>&nbsp;&nbsp;Coil Building</a></span></li> <li><a class="et_pb_promo_button et_pb_button "style="background: #4EB1BA; color: white; href="/tag/new-vapers/"><i class="fa fa-info"></i>&nbsp;&nbsp;Vaping info</a></span></li> <li><a class="et_pb_promo_button et_pb_button "style="background: #4EB1BA; color: white; href="/tag/cloud-chasing/"><i class="fa fa-cloud"></i>&nbsp;&nbsp;Cloud Chasing</a></span></li> <li><a class="et_pb_promo_button et_pb_button "style="background: #4EB1BA; color: white; href="/e-juice-101/"><i class="fa fa-eyedropper "></i></i>&nbsp;&nbsp;E-juice 101</a></span></li> </u

Update: I tried to take the style color code out for the lettering and its odd in the side bar the text gets colored and when the bbutton is on a page or blog post the text is white. I think the theme has a special widget color that is messing that up

Hm... Normally I'd suggest pulling your CSS into a separate file. Are you interested in doing that?

If not, you can do it inline, like so:

<head>
    <style>
        #sidebar ul:first-child a {
            background-color: #4EB1BA;
            color: white; <!--if you need it-->
        } 
    </style>
</head>

<body>
    <ul>
        <li><a>Link 1</a></li>
        <li><a>Link 2</a></li>
    </ul>
</body>

The WordPress theme is pretty messy with classes, but I think the above selector will work. So, what you're doing is taking the first ul in your sidebar, and applying your styles to each link in that ul.

If that doesn't work, you can add your own class to that ul, and style it that way, like so:

<head>
    <style>
        .my-new-class a {
            background-color: #4EB1BA;
            color: white; <!--if you need it-->
        } 
    </style>
</head>

<body>
    <ul class="my-new-class">
        <li><a>Link 1</a></li>
        <li><a>Link 2</a></li>
    </ul>
</body>

Best practice is to keep style and content separate, but WordPress is pretty messy already, so if these are your only changes it might not be worth your trouble.

Thanks for the reply. I want to run the code off the page but im already having to style the button text in the style.css in my child theme because in my theme there seems to be 15 things that want to style that button text. so I had to do it that way. Plus im new to all this so i dont have all the tools to know all the ways to do it.

4 Answers

Are you able to change the styles of the buttons from your child theme .css file? Or did you write directly in the HTML file because the .css file was having no effect?

Two things about how to figure out which styles win in a "style this element" battle:

  1. The style loaded last wins... at least when you're using the same specificity of a selector (e.g. ".my-class ul" v. ".another-class ul"). If this is in a child theme, that should already be loaded after the base theme styles.

  2. The more specific selector wins. If you have a fight between ".my-class ul li a" and ".my-class ul a", the first one will win, because it's more specific, even though they're both targeting the same a elements.

If you can find where in the original theme these buttons are being styled, you can copy the selector they used for your own child theme stylesheet. I used Chrome's Developer Tools to do some investigation.

  1. Open up your page in Chrome and right click on the element you want
  2. Click on "Inspect Element"
  3. The dev tools panel should open at the bottom of the page. You should be on the "Elements" tab, with your element highlighted on the left (a class="et_pb_promo_button et_pb_button "). On the right is your styles applied to that element. You should see your inline styles right at the top: a teal background and white text color.
  4. Uncheck the boxes next to both of those styles. Your button will revert to its old colors: orange background with gray text.
  5. Scroll down the styles until you find the orange background box. It should be checked, with no strikethrough. This is what you need to override.
  6. Scroll back up to the top of that super-long list of selectors that the orange is referring to. The one that's not grayed out is the one you want.
  7. Repeat steps 5-6, looking for "color: #666".

All the changes you make in the dev tools panel will disappear when you refresh the page, so feel free to poke around. :)

Based on what we found in our investigation, try putting this in your child theme .css file:

body .et_pb_button {
    background-color: #4EB1BA;
} 

.et_pb_widget a {
    color: white;
}

If that doesn't work, or if these styles now affect more buttons that you didn't mean to change, try making them more specific, like so:

body #text-12 .et_pb_button {
    background-color: #4EB1BA;
} 

#text-12 .et_pb_widget a {
    color: white;
}

Let me know how it goes. :)

Hey thanks for he help. Ya its annoying when iI go inDEV tools for Chrome it looks like there are 5 things that style the buttons and text. Also when iI add the sidebar tothe shop page the styles go away that iI added inthe child theme CSS file.
I will try the new code you made and see if that does anything.

Well that is annoying even more.... Your code worked BUT in doing that I found out that the child theme css file doesnt seems to be working. When I put that code in there nothing changes. Yet when I place the code in the " custom css area" in my themes options the code works. I really dont like putting code in there but its the only place it seems to work. Gotta love this stuff lol

I'm glad the code worked for you. If you feel this issue has been resolved, you might want to mark an answer so others in the forum know that it's been taken care of.

If the child theme CSS doesn't seem to be loading, you may want to run through a tutorial on setting up child themes; you may be linking to the wrong stylesheet or have a different structure than it expects.

Good luck!

lol oh man ya its not working anymore. I think my site is starting to play tricks on me. I swear that code had it working, than I changed something els and it went back and now nothing will change..... This is really frustrating because I had it working a week ago and it stopped working and changed colors on me

I am talking about now the comment submit button you can see it here. at the very bottom of the post. Its class should be ```.et_pb_button { color: white; }

but it just stays the teal color mocking me from afar 
http://olympiavaporworks.com/best-temperature-controlled-box-mods/

Also Im pretty sure that is the code because when I do
```<a class="et_pb_button">text here</a></span>```

I get a orange button with white text. so somthing is changing the text white. one of the css classes I did is working . but somthing els is changing it back to blue when the button is in the comment submit area