grid classes with Sass.

grid classes with Sass.

Subscribe to my newsletter!

In this relatively short article, we will make responsive grid classes with the CSS preprocessor SASS / SCSS. We will learn about the display: grid CSS rule, two different kinds of loops in Sass, and the [attr*=foo] selector in CSS.

So let us get started.

Making the Grid Classes

We already covered some concepts in this Article where we made padding and margin classes, where we already used SASS. The grids we make will be barebones; we make them so there is just a range of column counts to choose from, each column having the same width.

So we start by defining a variable that contains a list of integers representing the possible column counts. We have 2, 3, 4, but you could add more if you want to.

$columnCounts: 2, 3, 4

After that, we start to loop over the columnCounts variable. Looping is somewhat similar to how we do it in blade, the template language from laravel.

@each $count in $columnCounts

In the loop, we once again define a variable that holds the string of the value grid-template-columns. We set it to 1fr because each grid can not have just one column. The fr unit is practical when working with grids because it solves the problem of wrapping columns when you set a gap.

$text: "1fr "

After that, we start another loop where we take the $count and add 1fr to the text, like doing for i in range(count) in python. Now we have used two of the three loop types in SASS

    @for $i from 1 to $count
        $text: $text + "1fr "

Now we can finally make some classes. We insert the count variable into the class name and the unquoted text into the value of the grid-template-columns property.

    .grid-#{$count}
        grid-template-columns: unquote($text)

We finished with the loop, but the compiled CSS won’t work because we haven’t set the grid classes to display: grid. We can do this pretty quickly with the code below. This unique selector will get all elements where the class attributes value contains the word grid.

[class*="grid"]
    display: grid

Now to the responsive part of this. We can do this by adding the @media queries indented below this selector. So, if the window is smaller than 900px, we set the grid columns to 1fr, which will make each item full width.

    @media screen and (max-width: 900px)
        grid-template-columns: 1fr

Conclusion

Excellent! You have successfully created responsive grid classes with the CSS preprocessor language Sass.

Full Code

$columnCounts: 2, 3, 4

@each $count in $columnCounts
    $text: "1fr "

    @for $i from 1 to $count
        $text: $text + "1fr "

    .grid-#{$count}
        grid-template-columns: unquote($text)

[class*="grid"]
    display: grid 
    
    @media screen and (max-width: 900px) grid-template-columns: 1fr

26 responses to “grid classes with Sass.”

  1. wtcc8h Avatar

    Thank youu for the auspicious writeup. It iff truth bbe toldd wass a enjoyhment accouunt it.
    Glance compex too more introduced agreeable feom you!
    However, howw ccan wee be in contact?

  2. fov Avatar

    I just couldn’t leave youhr sitfe prior to suggestinng that I actually lovved the sual inhformation ann indiviual provfide onn your guests?
    Is gonna be agin conntinuously tto cheeck up on new posts

  3. xoj Avatar

    You actuhally make it seem sso asy wuth ykur presentation bbut I find thiks matter too bee
    really something whjch I think I wold neer understand.
    It seems too compllex and very boad foor me.

    I am loking forward for your next post, I will try tto get tthe hang of it!

  4. nom Avatar

    I’m extremely pleased tto fnd tbis page. I want too tto
    thank you for yolur tine for this particularly fantastic read!!
    I dfinitely savorted eery bit oof itt annd i also haave you book-marked too loo
    att neww information onn you website.

  5. jpo Avatar

    Sweet blog! I found itt while browsing onn Yaoo News.
    Do youu have any tips on how too geet listed in Yahhoo News?

    I’ve een tryging forr a while buut I nevr sem tto geet there!
    Many thanks

  6. ceb Avatar

    I could noot refrain from commenting. Exceptionalply well written!

  7. movies Avatar

    Woah! I’m realply loviing tthe template/theme
    of tjis blog. It’s simple, yyet effective.
    A lot of times it’s very difficult too geet that “perfect balance” betwren uswr
    friendliness and visual appearance. I must say that you’ve donee a amazing job
    with this. In addition, the blog lokads vwry fast foor
    mme on Internet explorer. Exceptional Blog!

  8. ehd Avatar

    My developdr is trying to convince mme to move tto .net from PHP.

    I have alwasys disliked tthe idea because of thhe expenses.

    Butt he’s tryiong nome thhe less. I’ve been uskng Movable-type on a nimber of websites forr about
    a year and aam worrried about switching tto anotther platform.
    I hace heard gpod things about blogengine.net. Is thhere a waay I caan transfter alll
    my wordpress coontent inhto it? Anny hel would bee rezlly appreciated!

  9. eoa Avatar

    My spouse and I stumbled oover here diffferent weeb
    page annd thyought I might chek thinvs out. I like whatt I ssee
    so now i’m followimg you. Loook forward to looking
    imto your weeb page again.

  10. aty Avatar

    Today, I weht to the beachfront with mmy kids. I found a seea sheell and gave it
    too my 4 yeaar oold dwughter and saiod “You can hear the ocean if you put this to your ear.” Shee plqced the sgell to her earr aand screamed.

    There wwas a hermit crb insside and itt pinched heer ear.
    Shee never wants tto goo back! LoL I know thgis is totally offf
    topiic bbut I haad to tepl someone!

  11. ict Avatar

    Hi everybody, ere every person iss sharing these kinds of
    familiarity, tjus it’s pleasant too reaqd this website, aand I used tto go to seee this
    bloog everyday.

  12. jvd Avatar

    Hi there! I coulld hav sworn I’ve visited this web sikte before but afteer looking at many oof thee posts I realizaed it’s new to
    me. Nonetheless, I’m deefinitely hapy I stumbled upon iit annd I’ll bbe book-marking itt
    andd checking back regularly!

  13. sfv Avatar

    I usded to bbe able to fiund gpod information from yourr
    content.

  14. Craig Avatar

    It’sgoing too be eend of min day, however before ending I
    aam reading this fantasti articlke too improve mmy know-how.

  15. zpy Avatar

    Hey There. I discovefed your bblog thee usaage off msn. This iss a very
    smartly wriitten article. I’ll bbe sure tto bookmark it
    aand come baack to rad extrra of your heelpful information. Thajks foor thee
    post. I will definitely comeback.

  16. nla Avatar

    Whhen some one searches forr his essential thing, thus he/she wants tto bee available
    that in detail, sso tthat tthing is maintained oer here.

  17. Russell Avatar

    Hi there thhis is kund of of off topic buut I
    waas wondfering iif blogs uuse WYSIWYG editors or iff you
    have to manually coe wifh HTML. I’m stating a blog
    soon but have no copding skkills so I wanted
    to gget advice from someone wiuth experience.

    Any helop would bee greatly appreciated!

  18. Winston Avatar

    Niice blog here! Allso yor websit loads uup vvery fast!
    What web host are youu using? Caan I get yoiur affiliawte
    link tto your host? I wjsh myy sie loaded uup ass
    quicklly aas yours lol

  19. Lela Avatar

    I have fuun with, causxe I fopund exacttly what I usrd tto bee taking a loik for.

    You’ve ended mmy 4 dayy lonbg hunt! Good Bleses yyou man. Have a great day.
    Bye

  20. eez Avatar

    Heyy verey interesting blog!

  21. sek Avatar

    Hello, every time i used tto check web sitee posts here in the earely hpurs iin tthe morning,
    for the reaason that i like too earn more annd
    more.

  22. osp Avatar

    whoah this blog iss excellent i lke reading yourr articles.
    Keeep uup tthe great work! Youu know, a lot of people are seaching aroun ffor this info, youu could aid thgem greatly.

  23. xxd Avatar

    Excelent post. I wwas checkng constantly this weblog
    and I am inspired! Extremsly hellpful information specifically thee closiung section :
    ) I deazl with such info a lot. I wwas looking for this particular infkrmation for a vwry long time.
    Thanks and bwst of luck.

  24. kxc Avatar

    Greaat post. I am going thrtough mmany of thesse issues as well..

  25. bit Avatar

    Hey I know thiis is offf topic but I wwas wondering iff you knew of aany widgets I could add
    to my blog tthat automatically tweet my newest twitterr updates.

    I’ve been looking foor a plug-in ike this forr quhite
    some time annd was hpping maybe youu wluld
    have soke experience with somethig lioke this. Please let me
    knoww iif you run imto anything. I truly enjoy reading ykur blog annd I look forwadd to youur new updates.

  26. lub Avatar

    I constantly spenjt my haalf ann hou to read
    thiss weblog’s content daaily aloong wioth a cupp oof coffee.

Leave a Reply

Your email address will not be published.