Awesome CSS Tricks (Part 1)
Tags: css, webdev, html
Published: 2021-08-16 11:52:42

## 1. Background for text

Tired of traditional colors like red, yellow, green...? and want to try something new? This trick is for you, besides colors, you can use images, videos, and linear-gradient, radial-gradient,... to "paint" your text.

An example:

![Background for text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/356zg4n55r98bhb2zq0m.png)

Look so cool, right? It would be more stunning if you have some effect with the background image of the text (like move around,...).

The snippet for the above code is: 

```css
background: url(...);
-webkit-background-clip: text;
color: transparent;
```

But the downside of this trick's that it's not well supported by all browsers: 

[https://caniuse.com/?search=-webkit-background-clip](https://caniuse.com/?search=-webkit-background-clip)

So be careful when using it if you want your website to work perfectly in all browsers. 

## 2. Round border

I think most of you guys have ever made a button/link that has round borders like this: 

![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/79ae7fb8-dd43-45b1-b4b2-f1c2a224f3e6/Capture.png](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F79ae7fb8-dd43-45b1-b4b2-f1c2a224f3e6%2FCapture.png?table=block&id=8f458761-5fa4-4d75-8214-4a6fd0a38976&cache=v2)

A very simple way to retrieve this is using border-radius then set that attribute to a number in pixel. But a thing that newbies usually struggle with is what number is enough? Remember back these days when I was a newbie, I always opened the devtools and then set a random number, then change it back and forth until I got the exact result I wanted. If you spend time considering border-radius attribute you will figure out that after a specific value in pixels if you keep increasing the value of border-radius then the roundness of the border will remain the same. So this is when our idea comes into play. 🤤 You just need to set border-radius to a very large value then we will archive the round borders no matter the width, height, padding, margin of our button. My favorite value is 999px. It's very easy to remember and in my opinion, it's a nice number.  

## 3. Dividing columns for text content

Instead of normal content, with CSS you can show text content in columns (like when you read the magazine or newspaper for saving area purposes) like this: 

![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/477134df-ef02-4d06-9060-679af4e222cb/3.1.png](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F477134df-ef02-4d06-9060-679af4e222cb%2F3.1.png?table=block&id=38a128d9-5bd4-4cd9-a24c-a15411eaab80&cache=v2)

You can use this code snippet:  

```css
-webkit-column-count: 3 // the number of columns
column-count: 3
-webkit-column-gap: 40px; // the gap between column
column-gap: 40px;
```

Change the column-count and column-gap to whatever number you want to fit with your requirement. 

## 4. Parallax scrolling

Parallax scrolling is a stunning effect that nowadays, more and more designers and front-end developers like to put in their websites, portfolios.

According to w3school, Parallax scrolling is a website trend where the background content (i.e. an image) is moved at a different speed than the foreground content while scrolling. Click on the links below to see the difference between a website with and without parallax scrolling.

This is an example of this technique: [Link](https://codepen.io/trunghieu99tt/pen/MWwBdXg)

In the beginning, I thought we had to use Javascript to retrieve that effect. Surprisingly, with CSS, it's just like a piece of cake. Create a section with a background image, in CSS of that section, add this line:

```css
background-attachment: fixed
```

For example: 

```jsx
.parallax{
	background-image: url(...); // your background image
	... // some attribute to make your background looks good
	background-attachment: fixed; // this is where the magic happens
}
```

W3School have a tryIt example, if you're interested in it, let's give it a shot: [Link](https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_parallax_device)

## 5. Object-fit

Sometimes, you have to show a list of items, a list of images,... but the width and height of the images might not be the same which breaks your layout. At that time, if changing the size of images is not available, I think the best solution is to set a specific height or width (maybe both of them) to the images? But since when you set sizes of the image that way, you might break the image ratio aspect which leads to some problems i.e the image will be stretched or squished (in an uncontrolled way). Like this: 

This is the original example (400x300)

![https://www.w3schools.com/css/paris.jpg](https://www.w3schools.com/css/paris.jpg)

If we set the size of that picture to  200x300, it would be like this:

![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2b588e01-80f6-466b-b532-80ad598630be/Untitled.png](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F2b588e01-80f6-466b-b532-80ad598630be%2FUntitled.png?table=block&id=4d800a7f-a9f5-4d51-8690-435697715abc&cache=v2)

Awful, we have a completely new picture, so it's time for object-fit comes into play. The CSS object-fit is used to specify how an img or video should be resized or fit its container.

Here is where the `object-fit` property comes in. The `object-fit` property can take one of the following values:

- `fill` - This is the default. The image is resized to fill the given dimension. If necessary, the image will be stretched or squished to fit
- `contain` - The image keeps its aspect ratio, but is resized to fit within the given dimension
- `cover` - The image keeps its aspect ratio and fills the given dimension. The image will be clipped to fit
- `none` - The image is not resized
- `scale-down` - the image is scaled down to the smallest version of `none` or `contain`

In most cases, I usually use cover and contain.

If we use object-fit: cover; we will have a picture like this: 

![https://s3-us-west-2.amazonaws.com/secure.notion-static.com/00fee526-a96e-47b2-b802-43c90b04d421/Untitled.png](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F00fee526-a96e-47b2-b802-43c90b04d421%2FUntitled.png?table=block&id=72c97ebb-70b5-48ae-8c96-bf29f4ea1813&cache=v2)

The image will remain its aspect ratio but will be clipped. It's not really good in some case, but it's a trade off.

You can read for more examples and detail here: [Link](https://www.w3schools.com/css/css3_object-fit.asp)

## 6. Limit text length to x lines

To limit the length of text, we can use Javascript, but at that time, we can only control the number of characters not the number of lines. What if I want text should be shown in 4 lines than if the text is longer than that, we will show something else (maybe ... or something). Yeah, if we want, we also can do that using Javascript, but it would be more complex than just limit the number of characters. 

We can retrieve that using CSS only, sounds great, right? 

The snippet code for doing that job is: 

```css
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: $line;
overflow: hidden;
text-overflow: ellipsis;
```

Replace $line with the number of lines you want the text to be displayed. 

Check out the example: [Link](https://codepen.io/trunghieu99tt/pen/GRJBbKX)

![Untitled](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F3ba6db78-c8ac-458b-b3a0-197444598ea8%2FUntitled.png?table=block&id=bfa5da8d-5b57-4181-8725-47fc77e18e3c&cache=v2)

text-overflow property defines how the remaining text will be displayed, if text length exceeds the number of lines we want in the above code snippet, it will be cut off and displayed as "...". For more details, you can go [here](https://www.w3schools.com/cssref/css3_pr_text-overflow.asp)

The box-orient property sets whether an element lays out its contents horizontally or vertically. For more details, you can go [here](https://developer.mozilla.org/en-US/docs/Web/CSS/box-orient)

⚠️ The bad news is that the attributes I mentioned in this trick aren't supported by all versions of all browsers. So once again, be careful when you use it. Even though the downside, I think it's still a powerful trick you might want to take a look at and, maybe you will want to use it sometimes.