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.

Comments

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 als

GIS Technology to identify all properties in BBMP Limits

          The Bruhat Bangalore Mahanagara Palike (BBMP) has for the last two years, been in the process of conducting a massive exercise to map the 13.87 lakh properties in the 198 wards in the city. Geographical Information System (GIS) has proven to be an effective tool for analysing and displaying thematic maps of all the roads for proper evaluation and correction of zones.            As of now, 11 lakh properties have come under the tax net. The aim of this programme, which is perhaps the first such exercise being conducted in the country on such a large scale, covering 800 square km, is to bring all properties under the tax net and ensure that the BBMP has accurate information pertaining to the properties’ dimension, built-up area, land use and classification. The process of validation of GIS maps will be completed in January 2011.           The process uses satellite digital maps of the BBMP area to generate a vector map from the information obtained. These high r

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 n

Playing with the markers and info window bubbles...

    In the last few posts, we have seen some marker examples and some information window examples. Now, lets do something interesting combining these two things. Just writing that "This is an info window" in the information bubble is not very interesting! And I know this...Have gone through the same phase!     So, today we will do something interesting! We will display the latitude- longitude co-ordinates of the point that the user clicks on the map! Doing this is not at all complex! Copy paste the following code and you will see for yourself a map coming to life!     The output of the above code looks as seen in the result section above! If you have any queries regarding the above code please comment on the blog post or feel free to contact me at my mail ID .

The bitter divorce of PSD and HTML

    Today's article is an interesting post that I read. The original post in Portuguese and authored by Fabricio Teixeira  can be found at arquiteturadeinformacao  (Now don't ask me pronounce this =)).     Some are calling it the death of PSD  but I prefer calling it a "divorce". PSD and HTML are both healthy and living strong, just that they do not live together anymore. "PSD to HTML", which for years was the most accurate and sometimes the only right path to web design process, seems like has its days counted.     Firstly you draw a page in Photoshop; impeccable layout, representing exactly how the web pages would appear when opened in a browser. After a sign-off on this picture (PSD) from the client the front end developer transforms these pictures into HTML, CSS and Javascript. The assets are cut, one by one, exported from the PSD and integrated into the HTML. Plugins and new tools are created in the process and some companies even charge upto $1