6 Fun Things You Can Do With Just CSS

本文转自:6 Fun Things You Can Do With Just CSS

1.jpg

Love it or hate it, CSS(Cascading Style Sheets) is the language we use to style the HTML elements.

While there are some fabulous preprocessors like SCSS(that get compiled into CSS eventually) and offer more features than normal CSS, this does not mean that you can’t do amazing things by just using plain, standard CSS.

CSS has 520 distinct and while you may not need to know more than 30 or 50 properties, exploring the uncommon CSS properties can surely help you achieve more with just your stylesheets.

Below are 6 amazing things you can do with just CSS:

Create Portraits & Add Effects

CSS allows you to easily add some eye-catching filters and effects to your photos, without you even touching JavaScript.

2.png

The above effect is created just by using the background-clip property of CSS.

p{
  background-size: 70vh;
  background-position: center;
  line-height: 16px;
  background: url("photo.png");
  -webkit-background-clip: text;
  background-attachment: fixed;
  background-clip:text;
  background-repeat: no-repeat;
  -webkit-text-fill-color: rgba(255, 255, 255, 0);
}
body {
  background: black;
  overflow: hidden;
}

While my p tag contains some text.
You can also use mix-blend-mode which determines how content should blend with the content of the element’s parent and the element’s background.

3.gif

Linearly Scale Text In Accordance With Viewport

Making your website respond to change in the viewport can be a cumbersome process that often involves rewriting parts of CSS, and testing multiple breakpoints.

But it does not have to be so complicated and tiresome always.

Using just one line of CSS, you can scale text between a minimum and maximum sizes as the viewport increases or decreases.

微信图片_20220119151519.gif

This is done via the clamp property.

clamp(minimum, preferred, maximum);

The values are pretty self-explanatory. The clamp property returns a value that is between the minimum and the maximum values. The middle argument, i.e. “preferred” is a percentage of the viewport.

Let’s see this in practice.

width: clamp(500px, 50%, 1500px);

When the viewport width is 1600px then the preferred value will be 50% of 1600px, that is, 800px. When the viewport is 700px, the preferred should be 50% of 700, that is, 350px.

But since the minimum is 500px, it will return 500px. The same logic applies to the maximum value of 1500px.

You can see from the media provided above as well. The minimum was 450px, hence you don’t see any change when the viewport becomes less than 900px.

Simple Photoshop

CSS provides various properties to edit and apply effects to images. Some of these properties are discussed above, such as the mix-blend-mode .

CSS offers various properties to manipulate images. Some of them are:

  1. Brightness: filter: brightness(90%)
  2. Blur: filter: blur(9px)
  3. Saturation: filter: saturate(4)
  4. Opacity: filter: oapcity(0.3)
  5. Hue: filter: hue-rotate(45deg)
  6. Drop Shadow: filter: drop-shadow(30px 10px 4px #4444dd)

The picture below has a blur filter applied to it.

img{
   filter: blur(2px);
}
5.png

There are more filters and you can read about them here.

Fullscreen

If you have ever made a blog site or any site that involves displaying images, you must have provided some functionality to make the images go full screen.

Even Medium does it, when you click on an image it enlarges.

While you need JavaScript to make elements go full screen, by using the requestFullscreen() method, there is a CSS pseudo-class :fullscreen that allows you to style elements once they are in fullscreen mode.

:fullscreen {
  background-color: yellow;
}

You can easily apply filters or change background(since elements that go full screen preserve their aspect ratio, leaving a white background behind).

Pie Charts

You can easily create pie charts using CSS only, with just two lines of CSS.

Using conic-gradient() CSS function, you can create images consisting of the gradient with color transitions rotated around a center point.

The most practical use case of this function, in my opinion, is creating pie charts.

background: conic-gradient(red 20%, purple 0 60%, yellow 0);
border-radius: 50%;

The above code will render the following:

6.jpeg

The result of the conic-gradient() function is an object of the gradient datatype.

You can find browser compatibility here.

Gradient Text

Gradients when used correctly can provide your site with a breath of fresh air.

Chances are you already know how to work with gradients. If not, you can always use sites like Grabient which lets you customize the gradient and provide the CSS for it.

But do you know you can easily apply a gradient to text as well?

.text {
 background: linear-gradient(to right, red 0%, purple 100%);
 -webkit-background-clip: text;
 -webkit-text-fill-color: transparent;
 font: {
  size: 10vw;
 };
}

And you get awesome gradient text with just this.

7.jpeg

Conclusion

HTML and CSS are the fundamental building blocks of web development and it’s a myth you can’t build some great sites using just these two technologies.

While you need JavaScript for functionality, HTML and CSS alone can help you build beautiful marketing and landing sites with bare minimum JavaScript(for converting leads).

I have seen many freelancers get away with just CSS and HTML skills.

I hope you enjoyed this article!

Recommended reading

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容