• .
  • Chúc mừng giánh sinh và năm mới 2025
  • Smallrig phu kien ho tro quay gan gimbal rs3 rs2 rsc2.jpg
  • Balo túi đeo PGYTECH 3L 4L 6L 10L 11L 22L 25L 35L
  • Tilta Phụ kiện các loại giá tốt nhất
  • Hỗ trợ book Xanh SM giao liền

Myth – Writing CSS of the Future

CSS has introduced a slew of new features such as CSS Gradients, Shadows, Border Radius, and Animation that can all be achieved purely with CSS. There are also several features that have yet to be implemented due tolack of browser support for CSS variables and CSS calc() functions. But if you can’t really wait for the future, let’s check out Myth.

Myth, unlike other pre-processors that invent its own syntax, uses the same syntax as the standard spec. You can use variables, perform mathematical or color operations, and write new CSS properties in its official form. Its goal is to allow developers to write pure CSS, while also be able to utilize future-standard syntax, right now.

Getting Started

To get started, we need to install Myth binary to be able to compile it to the current CSS standard. There isn’t GUI application like Codekit or Koala that supports Myth at the time of writing, so this is the only way to compile Myth into browser-compliant CSS format.

In Terminal, type the following command:

  1. npm install -g myth  

You can then use this command below, for instance, to compile source.css into output.css.

  1. myth source.css output.css  

Or, type this to monitor the source.css and compile it to output.css for every change.

  1. myth --watch source.css output.css  

Myth does not introduce a new extension. It works with .css as shown above.

Writing CSS with Myth

Myth also does not introduce proprietary functions and rules like the other CSS Pre-processors, so you should be able to get used to Myth almost immediately. It is like plain CSS.

Variables

Let’s start with Variables. In CSS, a variable is declared, like so:

  1. :root {  
  2.     var-length: 10px;  
  3.     var-color#000;  
  4. }  
  5. .class {  
  6.     background-color: var(color);  
  7.     width: var(length);  
  8. }  

Myth compiles this code into browser-compliant format:

  1. .class {  
  2.     background-color#000;  
  3.     width20px;  
  4. }  

You can refer to our previous article about Using CSS Variables for more details.

Math Operations

As mentioned, we can also perform mathematical operations with the new CSS3 calc() function. We have also covered this function in our previous article: Using CSS3 Calc Function.

Let’s extend our first example with it:

  1. :root {  
  2.     var-length: 10px;  
  3.     var-color#000;  
  4. }  
  5. .class {  
  6.     background-color: var(color);  
  7.     width: calc(var(length) / 2);  
  8. }  

Myth compiles the above codes into:

  1. .class {  
  2.     background-color#000;  
  3.     width10px;  
  4. }  

Color Adjustments

Myth also supports some color operations or adjustments like in LESS or Sass. A new standard function for it is proposed to be included in CSS spec named color() — including a set of color-adjusting functions such as tint(), shade(), and blend() just to name a few.

Below is one example: we increase the background color’s lightness by 80% and decrease the border color by 50%.

  1. :root {  
  2.     var-length: 20px;  
  3.     var-black#000;  
  4.     var-white#fff;  
  5. }  
  6. .class {  
  7.     background-colorcolor(var(black) lightness(+ 80%));  
  8.     border: var(border-widthsolid color(var(white) lightness(- 50%));  
  9.     width: calc(var(length) / 2);  
  10. }  

That code will produce:

.class {
	background-color: rgb(204, 204, 204);
	border: 2px solid rgb(128, 128, 128);
	width: 10px;
}

Autoprefixer

Myth will also automatically add vendor prefix to CSS properties. We can simply write, for instance, CSS Box Shadow, this way:

  1. .class {  
  2.     box-shadow: 2px 1px 0px var(black);   
  3. }  

The output is:

  1. .class {  
  2.     -webkit-box-shadow: 2px 1px 0px #000;  
  3.     box-shadow: 2px 1px 0px #000;  

Share facebookShare facebook

Bạn đã đọc tin này chưa ?

Go Top
Chat hỗ trợ
Chat ngay