CSS Selectors Are fun

From my title, you may think I’m being sarcastic but I’m really not.  CSS selectors can be frustrating sometimes and when we don’t understand them but when we understand the power of them is really very impressive!  Some of things you can do with them border on Javascript like. (oooOOOOO).

Basic selectors

This part is pretty easy.  A “.” represents a class, no dot and you’re selecting just the element.  You can see it reflected in the Codepen below.  Isn’t pizza delicious?  You can also select with ids (“#”) but it’s generally seen as bad practice to use IDs as selectors in CSS.

[codepen_embed height=”268″ theme_id=”0″ slug_hash=”rVKdbG” default_tab=”html” user=”Fixyourcss”]See the Pen <a href=’http://codepen.io/Fixyourcss/pen/rVKdbG/’>rVKdbG</a> by Jordan Hansen (<a href=’http://codepen.io/Fixyourcss’>@Fixyourcss</a>) on <a href=’http://codepen.io’>CodePen</a>.[/codepen_embed]

CSS stands for “Cascading Style Sheets” which means the styles apply top to bottom (this goes for css files as well).  It is, however, interesting to note that there are exceptions to this rule based on specificity.  Selecting a class has more precedence than selecting just an element (otherwise the .sword would be overwritten by the div selector) because it is more specific than just the element selector.  There is an “!important” command, which allows you to overwrite anything (except another !important tag that is lower in the Cascade).  You generally want to avoid using this.  CSS tricks has written an article about when it’s okay to use !important and it’s worth reading.

Here at Fixyourcss, we generally don’t have access to all or any of the code.  It also can get very messy and we don’t know what file is doing what and what is being loaded in what order and if there is maybe some javascript applying some css styles.  So, that’s when we bust out the !important tag.

Advanced (read fun) selectors

These CSS selectors are really powerful.  Take a look below.  We can make active things change with “:hover” (this can also be used for :active, :focus, and :visited).  We can select children by placing a space between selectors, with it going from parent to child from left to right.  Want to only select an element with two specific selectors?  Just put them right next to each other, like “.spanContainer.secondContainer”.

And this little nuget that I think is pretty great!  At the very least it’s kind of helpful when you’re trying to figure out what styles are applying to what.  A selector like “.spanContainer:hover span” will make it so that when the parent is hovered over, the child span will receive the styles you want to apply to it!  Not knowing this (like I didn’t for a long time!) can make for some confusing times when you’re trying to figure out why the style isn’t being shown on the element that you are hovering over!

 [codepen_embed height="268" theme_id="0" slug_hash="MwXGWo" default_tab="result" user="Fixyourcss"]See the Pen <a href='http://codepen.io/Fixyourcss/pen/MwXGWo/'>MwXGWo</a> by Jordan Hansen (<a href='http://codepen.io/Fixyourcss'>@Fixyourcss</a>) on <a href='http://codepen.io'>CodePen</a>.[/codepen_embed]

The moral of all this is that you can do a LOT with CSS selectors.  They allow to easily touch any element in your page and apply the necessary styles.  Get used to them and use them!

This MailChimp shortcode is now deprecated. Please insert the new shortcode to display this form.

CSS Frameworks and why you (and everyone!) should use them

CSS frameworks are amazing.  Even if you are one who shuns the use of a template for your web page, I really don’t feel that there is any reason a framework should be not be used in pretty much every web project.  They offer so much with so little cost.  Here’s a list you should take a gander at if you’re not sure which one to use.  I personally use bootstrap for all of my projects.

Responsive

Having a responsive website has been important for the last few years but since Google’s change to actually boost the ranking of mobile-friendly sites (aka punish non mobile-friendly sites), it’s essential if you care about ranking at all.  Looking at the list I provided above, the first nine frameworks include responsive support.  That means by default your site will scale based on the media consuming it.

This is amazing.  You don’t even really have to think about it anymore.  You don’t have to hand craft your media queries for each possible outcome and test them all.  You can feel safe knowing that they have all already been tested and provide a great mobile experience right out of the box.

No need to reinvent the wheel

Need a modal?  How about a popover?  Tabs?  Accordions?  The modern frameworks have you covered.  Just include the js and css files, apply the classes to your elements and BAM you are set.

Have a situation that is a little trickier?  It’s likely that it’s been done already with the more popular frameworks (Bootstrap).  I like to use a site called Bootsnipp.  It has snippets that others have produced for their issues.  Things such as a custom login/registration, profile pictures, pricing tables, and spinning social icons.  It’s well worth checking out as it can save you a lot of time and headache.

Even if you can’t find what you need from Bootsnipp or the standard framework, using something like this allows you to reach out to the communities behind them and ask for help.  The tag “twitter-bootstrap” alone has 46,246 questions on Stackoverflow.  Lots of people will be there to help and easily understand your problem when you use a popular framework.

CSS Frameworks have great implementation and documentation

Like I said, I typically use Twitter’s bootstrap and the documentation they provide is amazing.  Just include the css and js files and you can lean on any of their classes.  The documentation they provide it outstanding, complete with code snippets that you can just copy and paste into your page.

Using CSS frameworks will make the front end CSS side of your project so much easier and I highly encourage.  There are a lot of great, open-source solutions that are worth using.

Have a framework that you love?  Have a reason to hate CSS frameworks?  Please drop a comment below and let me know!

This MailChimp shortcode is now deprecated. Please insert the new shortcode to display this form.