Skip to main content

SASS - Nesting the CSS

    To begin with SASS we need Ruby to be installed on our machine. To set up Ruby and SASS on your machine refer to the following steps.

    If you’re using OS X, you’ll already have Ruby installed. Windows users can install Ruby via Windows installer, and Linux users can install it via their package manager. Once you have Ruby installed, you can install Sass by running:
gem install sass

    To translate your Sass file into a CSS file, run
sass --watch style.scss:style.css

    Now whenever you change style.scss, Sass will automatically update style.css with the changes. Later on when you have several Sass files, you can also watch an entire directory:
sass --watch stylesheets/sass:stylesheets/compiled

    We don't need to do so many things to get up and running. We just need our good old friend jsFiddle. We will take a look at the various features of SASS today onward. Let's discuss the most basic and an important feature today - Nesting. In all I will be covering the following features in various posts on this blog.

  1. Nesting
  2. Parent References
  3. Variables
  4. Operations and Functions using variables
  5. Interpolation
  6. Mixins (You'll love this!)
  7. Arguments based Mixins (You'll thoroughly enjoy this!)
    Enough of the intro and the overview. Let's get our hands dirty, right? So here we are...Let's start with the nesting feature today.

    Often when writing CSS, you’ll have several selectors that all begin with the same thing. For example, you might have “#navbar ul”, “#navbar li”, and “#navbar li a”. It’s a pain to repeat the beginning over and over again, especially when it gets long. SASS allows you to avoid this by nesting the child selectors within the parent selector.

    Let's take a look at the example that follows to understand the nesting feature in further detail. Before that, let's understand what we are trying to achieve. We want to create what we see in the image below.


    Now that we are clear with what has to be the output,let's dive into the code.


    As you can see in the above fiddle, we have got what we wanted to create. Take a look at the HTML. What we have is a ul-li within a div with id="navbar" and then there is an anchor within the li. I have created the HTML in such a way so that we can come to appreciate the advantages of SASS's nesting feature.

    In the CSS tab, you will find SASS CSS and the commented regular CSS. The uncommented SASS CSS is where you can see that the CSS properties for ul, li and a are all nested within the "navbar"'s properties. If we hadn't used SASS, we would have to written CSS in our regular way which is commented in the example above. The SASS CSS when compiled results in our regular CSS only.

    Hope you have followed the post and now understand the nesting feature of SASS. SASS makes life easier where you have large amount of CSS to write or maintain. Hope you enjoyed the post and you can take a look at the Jquery Mobile and Google Maps API examples that I have posted on the blog. Stay tuned for more SASS examples.

Recommended for You

Where does Google get it's live traffic data from?

Referring to a post that I wrote earlier, Google’s - Live traffic Layer, ever wondered how Google collected this data? I was wondering the other day, how Google received live data to display it on their maps as a layer! I looked up the web and found something very interesting and am sharing the same with you all.As we all know, the traffic layer is available most accurately in several states in USA. Most major metro areas in the US have sensors embedded in their highways. These sensors track real time traffic data. Easy to miss at high speeds (hopefully anyway, traffic permitting), more commonly noticed may be the similar sensors that often exist at many busy intersections that help the traffic lights most efficiently let the most amount of people through. The information from these tracking sensors is reported back to the Department of Transportation (DOT). The DOT uses this data to update some of the digital signs that report traffic conditions in many metro areas. They also…

jQuery Mobile's Next Big Step

Spatial Unlimited changes to The UI Dev


After being hosted on blogger 😣 for the last 6 years 📆, this page has finally been moved to Github.io
This means a few things for you, dear reader!

You will be redirected to the new page shortly! ⏩ ⏩ ⏩


Once crapy HTML is now better looking Markdown! 😍 😍


The entire blog is a Github repo! 😍 😍


Spatial Unlimited is now The UI Dev 😍 😍


ES6 101 - Class

Spatial Unlimited changes to The UI Dev


After being hosted on blogger 😣 for the last 6 years 📆, this page has finally been moved to Github.io
This means a few things for you, dear reader!

You will be redirected to the new page shortly! ⏩ ⏩ ⏩


Once crapy HTML is now better looking Markdown! 😍 😍


The entire blog is a Github repo! 😍 😍


Spatial Unlimited is now The UI Dev 😍 😍


Two maps on the same page - Side-by-side

How good I am feeling to post a code example after such a long time! It's been all "news" over the past so many posts! Well now that I am finally doing a code example, I am posting a very highly requested code sample. Placing two Google Maps on the same page (Now that's simple you would say!), but side by side. Now this is the thing that most people struggle with. Well, implementing the second part is also very simple, as you will see in today's code.

    Let's see the code. Here it is!



    The output of the above code will be as seen in the result section above.

    As most of you will realize, there are two maps, one centered at "Pune" and other at "Noida". Why I chose these two locations? Well, just like that!...The main issue of concern is how the maps appeared side-by-side and not one below the other as would be normal behavior of two "div" elements used in the same page. Now here is the trick! Check out the the first

Geodesic Polyline

Today we will have a look at a very interesting polyline example - "The geodesic polyline". Now the first question that will pop is "What is geodesic?". Mathematically, geodesic means the shortest line between two points on a mathematically defined surface, as a straight line on a plain or an arc of a great circle or sphere.

    The next question after reading the above definition is clearly, "Why do we need geodesic polylines?" and that would be followed up with "What is this Great Circle?". We will discuss this first, before we move on to the actual example today. The example is very very similar to the normal polyline example, with just a small change.

    Having said so, I will now try to explain why we need a geodesic polyline? The shortest distance between two locations on the earth is rarely a straight line as the earth is roughly spherical in nature. So any two points on the earth, even if they are very close lie on a curve and not …