douxie3625 2017-05-13 13:33
浏览 111
已采纳

编译css / scss的目的是什么? [关闭]

I was just wondering, why do we need to set our PHP/HTML projects to use a compiler? Why don't we just ctrl+s it and then get the desired results? What I'm trying to point out is that, what is the difference in using a compiler vs. using the basic ctrl+s in saving a web application. Thank you. :)

  • 写回答

1条回答 默认 最新

  • douyun7285 2017-05-13 13:46
    关注

    I'm confused. The question title refers to compiling CSS/SCSS, the question refers to compiling PHP/HTML. Since you tagged the question with sass, I'm assuming you're trying to ask why we need to compile SASS to CSS, instead of just being able to include the SASS file into our projects?

    If that is your question, it's because SASS is not always valid CSS by itself. It's a more developer-friendly way to write CSS, because in SASS we can inherit styles. For instance, we can define a basic CSS class that sets the font size and weight (e.g. .arial-big), and then we can inherit that in other classes that set the color (e.g. .arial-big-red and .arial-big-green). We don't have to set the font size and weight in those classes, because we can just inherit from .arial-big. Basic CSS does not support doing that. If you never use any of those features and your SASS files contain already-valid CSS code, then why are you using SASS anyway?

    Now that's a really simplistic example, it's usually far more complicated than that, and it's possible to inherit from multiple CSS classes in SASS. But like I said, this is not valid in regular CSS, so we need to compile our SASS into basic CSS that browsers will understand.

    If you want to automatically compile your SASS to CSS whenever it changes, that's quite possible. The JetBrains suite of editors support file watchers that can automatically trigger a command when a file is changed. You can set that up to call the compiler when you hit CTRL+S on a SASS file. If you're using a different editor, they might have similar functionality or you can probably do the same using basic OS features. I'm not sure about Windows, but in Linux you can use inotify/incron and OS X has its own file watcher features that you can hook into to call the compiler when a file changes.

    Edit: To add to this, if you're using SASS in a PHP project, there are libraries available that compile SASS to CSS on-the-fly when the visitor tries to load the CSS. Instead of using a <link> tag pointing to a pre-compiled CSS file, you can have one that points to a PHP file belonging to that library, and it will compile the SASS files at that time. An example of this is panique/php-sass.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)