Using variables in CSS

Basic usage of CSS custom properties — variables


CSS3

Using variables in CSS is useful and awesome. It can reduce some overlapping codes in your stylesheet, results shorten your codes throughout your stylesheet. And it also increases efficiency due to change a single line of code affects dependent codes, which means you don’t have to fix each same codes one by one with ctrl+F.

What you have to do is:

  1. Declare a variable at the top of your stylesheet with value
  2. Apply it into your CSS code

I have managed my website for my design portfolio from a year ago. Every time I needed to change something on my website like font-size or background-color, I’ve been wander here and there in my stylesheet with ctrl+F to find and fix all the same codes that need to be revised, until I found this lovely stuff — CSS Variables. And now, I’m gonna tell you how to use it, and how to apply it in a stylesheet.

Basic usage

Declaration

Pseudo-class :root acts as a selector in your stylesheet. You can put this at the top of your stylesheet, and declare a name of variable like --var-name. After that, save value which you want to like below.


Reference

Now you can refer your variable elsewhere you want with var() like below, which I declared above.

It results every p tags colored in --paragraph-color in your stylesheet. The advantages of variable is after you declare it once, you can refer it anywhere you want and as many times you want. If you want to revise it, all you have to do is just fix the value in variable you’ve declared. I’ll show you how.


In case of above, I referenced --paragraph-color to two tags and one class. It affects them all in the same way — to colored with #656c7a — because I saved that value in variable --paragraph-color at the :root selector.

If you want to change value of --paragraph-color, you don’t need to change every --paragraph-color in your stylesheet. Just changing value of it at the :root selector affects every lines that have --paragraph-color variable.

Applications

Variables in CSS also have function to calculate it’s own value. This means your variable that declared with number value can be calculated and used in multiple ways.


In this case, I declared variable called --font-size with value of ‘1’. I can use this value as is, but calc() let us to use it in other way we want. See the examples below.

The value of --font-size is integer ‘1’. We can use it with var() as I’ve shown you before. If you want to use value ‘3’, you can declare another variable at the :root selector, but that’s inefficient way. The solution is to use calc() before var() in the line you want to calculate the variable of it’s value like above. In this case,

The value of margin-top property in this class is 16px.
The value of font-size property in this class is 3rem.
The value of line-height property in this class is 1.5em.

In this way, you can reuse your single variable as a multiple value, in a various properties. With calc(), you can calculate your value with other operators like plus, subtract, multiply and divide operator.

See the Pen CSS3 custom properties by Hyouk Seo (@Spemer) on CodePen.

Can I Use?

At this point in my writing this article today(November 18, 2017), search result on caniuse.com says 76.28% of browsers fully support for CSS variables, and 1.62% of browsers partially support for it in global usage. You can check the latest report on caniuse.com.

caniuse.com
Usage report from caniuse.com

Takeaway

You can check how I used it in my website’s stylesheet on my GitHub, or you can just visit my website to see the results.


If you’re unfamiliar with CSS preprocessors like SASS or LESS, CSS variables would be a great alternatives. It’s easy to learn and use, incredibly powerful. That’s why I decided to apply CSS variables in my website’s stylesheet, and it works just same as before, with more efficiency.


Thank you for reading my article, hope you enjoyed.

Title : Using variables in CSS
Date : November 18, 2017
Writer : Hyouk Seo (Spemer)


代开银行存款证明公司濮阳存款证明怎么样柳州代办存款证明威海企业资信证明好不好临沂银行存款证明作用商洛定做出国留学资金证明徐州企业资金证明制作惠州企业资信证明怎么样郴州出国留学存款证明打印运城企业资信证明怎么打印忻州存款证明信誉保证安庆存款证明图片雅安出国留学资金证明怎么打印忻州打企业资信证明景德镇企业资金证明打印荆门代开留学存款证明玉溪定做出国留学资金证明郑州企业资信证明打印固原资金证明用途绵阳出国留学存款证明供应商泉州打印存款证明深圳办出国留学存款证明鹰潭开企业资金证明锦州银行定期存单打印平凉办银行存款证明孝感打银行存款证明庆阳制作出国留学存款证明包头代做定期存单济南办理定期存单曲靖出国留学存款证明办理内江定期存单哪家好香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声汪小菲曝离婚始末卫健委通报少年有偿捐血浆16次猝死单亲妈妈陷入热恋 14岁儿子报警雅江山火三名扑火人员牺牲系谣言手机成瘾是影响睡眠质量重要因素男子被猫抓伤后确诊“猫抓病”中国拥有亿元资产的家庭达13.3万户高校汽车撞人致3死16伤 司机系学生315晚会后胖东来又人满为患了男孩8年未见母亲被告知被遗忘张家界的山上“长”满了韩国人?倪萍分享减重40斤方法许家印被限制高消费网友洛杉矶偶遇贾玲何赛飞追着代拍打小米汽车超级工厂正式揭幕男子被流浪猫绊倒 投喂者赔24万沉迷短剧的人就像掉进了杀猪盘特朗普无法缴纳4.54亿美元罚金周杰伦一审败诉网易杨倩无缘巴黎奥运专访95后高颜值猪保姆德国打算提及普京时仅用姓名西双版纳热带植物园回应蜉蝣大爆发七年后宇文玥被薅头发捞上岸房客欠租失踪 房东直发愁“重生之我在北大当嫡校长”校方回应护栏损坏小学生课间坠楼当地回应沈阳致3死车祸车主疑毒驾事业单位女子向同事水杯投不明物质路边卖淀粉肠阿姨主动出示声明书黑马情侣提车了奥巴马现身唐宁街 黑色着装引猜测老人退休金被冒领16年 金额超20万张立群任西安交通大学校长王树国卸任西安交大校长 师生送别西藏招商引资投资者子女可当地高考胖东来员工每周单休无小长假兔狲“狲大娘”因病死亡外国人感慨凌晨的中国很安全恒大被罚41.75亿到底怎么缴考生莫言也上北大硕士复试名单了专家建议不必谈骨泥色变“开封王婆”爆火:促成四五十对测试车高速逃费 小米:已补缴天水麻辣烫把捣辣椒大爷累坏了

代开银行存款证明公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化