{"id":379,"date":"2024-01-08T17:22:24","date_gmt":"2024-01-08T11:52:24","guid":{"rendered":"http:\/\/codetheory.in\/?p=379---a8c2298f-d0a1-4e6b-872d-d97ec2fb4d7a"},"modified":"2024-01-08T17:22:24","modified_gmt":"2024-01-08T11:52:24","slug":"convert-your-css-code-to-scss-sass","status":"publish","type":"post","link":"https:\/\/codetheory.in\/convert-your-css-code-to-scss-sass\/","title":{"rendered":"Convert Your CSS Code to SCSS\/Sass"},"content":{"rendered":"

Ever wanted to make your own CSS2Sass convertor ? When working on projects, a tool that can quickly convert our CSS code to Sass can be ridiculously useful. Let’s see how to do it.
\n<\/p>\n

Using sass-convert<\/h2>\n

The Sass Ruby Gem ships with a sass-convert<\/code> executable. Its quite funny that many people still do not know about it or get confused thinking that its for SCSS -> Sass and Sass -> SCSS conversion. Well, after all SCSS is CSS, right ?<\/p>\n

This command will help you convert your CSS code to Sass or SCSS:<\/p>\n

\r\nsass-convert style.css style.sass\r\nsass-convert style.css style.scss\r\n<\/pre>\n

style.css<\/code> should be an existing CSS file, while style.sass<\/code> and style.scss<\/code> files will be generated if they don’t exist.<\/p>\n

In Ruby Code<\/h2>\n

I quickly dig into the sass<\/code> gem and tried to figure out what sass-convert<\/code> basically does, so that one could also implement the exact same functionality in plain Ruby Code (it can be really useful sometimes). Here’s what I came up with:<\/p>\n

\r\nrequire 'sass\/css'\r\n\r\n# CSS Code\r\ncss = %Q{\r\nbody {\r\n  color: red;\r\n}\r\n}\r\n\r\n# SASS\r\nputs ::Sass::CSS.new(css).render(:sass)\r\n# SCSS\r\nputs ::Sass::CSS.new(css).render(:scss)\r\n<\/pre>\n

Downsides<\/h2>\n

If I am not wrong, I don’t think the conversion preserves any variables, mixins, inheritance, etc. Although nesting is added appropriately. So, the result might not be spot on always, but can still be a time saver sometimes.<\/p>\n","protected":false},"excerpt":{"rendered":"

Ever wanted to make your own CSS2Sass convertor ? When working on projects, a tool that can quickly convert our CSS code to Sass can be ridiculously useful. Let’s see how to do it.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[7,47,48,49],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/379"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=379"}],"version-history":[{"count":11,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/379\/revisions"}],"predecessor-version":[{"id":1570,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/379\/revisions\/1570"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}