CSS is quite unpopular, might look boring, and very few people are able to master it the right way. However, it is a skill that can give you a great competitive advantage over other developers. If you like solving puzzles, then you will find joy in writing CSS by looking at it as a puzzle to solve.
One of the (good) side effects of being fluent in CSS is that your HTML becomes smaller – you won’t need as many nodes to create a certain look. Also, you will find it a lot easier to make not only your websites but also your apps responsive. In the end, you will save a lot of time, headache, and discussions with designers.
Keeping all of this in mind, we have created a list of 10 practical CSS tips that you can use in your everyday life. We are sure you will find their application in a lot of situations. Let’s dive right in then.
Table of Contents
- CSS Trick #1 – Adding … when text is too long
- CSS Trick #2 – Adding a shadow
- CSS Trick #3 – SVG Alignment and Color
- CSS Trick #4 – Perfect your Buttons
- CSS Trick #5 – Button Bars
- CSS Trick #6 – Fixed Header
- CSS Trick #7 – Center Content
- CSS Trick #8 – Resize Image
- CSS Trick #9 – Tooltips
- CSS Trick #10 – Zoom Effect
CSS Trick #1 – Adding … when text is too long
We have all been there – finished our nice-looking design just to discover that when real data comes in, the text simply doesn’t fill in the space assigned to it. Long titles of products in an e-commerce store is a common example. Having the text over two lines will not always align with the design since that will leave us with no space for description. Check the example in the image below:
I would rather prefer a design like the one shown below. Here, we have enough room to place a short description or even a buy button.
To achieve this, we need a combination of 3 properties:
You can view the example below:
CSS Trick #2 – Adding a shadow
One of the biggest advantages of CSS is that your design looks more professional. You might have spent 150 hours on a project but if you work on it just an extra 5 hours adjusting the look, your effort is visible right away. This gives you an advantage when looking for better clients or better jobs as you deliver higher quality. Shadows are one of those subtle effects that will increase your quality instantly.
One of the most simple yet powerful effects is box-shadow. The best part is you can do it using a single property. Check the example below to see how effective this is:
Now, let’s look at the values box-shadow uses:
As a bit of extra advice, the key to having nice subtle shadows is to use rgba values instead of a regular #hex rgb. This is because with rgba values we can make our shadows have an opacity which will make them more subtle and will look a lot more professional.
CSS Trick #3 – SVG Alignment and Color
Icons are used everywhere nowadays, be it menu icons, cart icons, favourite icons, and a variety of others. We run into problems with icons since we need to have our pages work over multiple screens with multiple resolutions. SVGs come to your rescue in such scenarios!
SVGs are perfect for icons since they look great at every resolution. However, if you want to add SVGs as the source of an image tag, there are several things you will be unable to do, like controlling the color. So, the best way to deal with an SVG file is to simply copy the content of the file and place it directly into the HTML code.
One quick way to do that is to go to a website like fontAwesome, search for the icon you like and download the file. Once you download the file, you can view the source and paste the content in your HTML like this:
Having done that, let’s now proceed to create a 30×30 circular button and center our icon inside it:
The result is the following:
SVGs can be colored via CSS which means we can use hover selectors to change the color of the icon. However, icons must be prepared for that, where the SVG file typically uses fill=”currentColor”. Fortunately, icons available at fontawesome and similar places exhibit this behaviour out-of-the-box.
Another handy tip is to use “transform: translate” to move a node according to the size of the node itself and not its parent as “top, left” properties do. Basically, we position the icon in the center and use transform to center it using the size of the icon.
CSS Trick #4 – Perfect your Buttons
There are multiple ways of achieving the same output in CSS as is the case with any programming language. This means there might always be a better way of doing the same thing and achieving an equivalent result.
Buttons are one of the elements which are used over almost every single page. In some cases, they even contain the purpose of the page e.g. buying an item, subscribing to an email list, answering a survey.
Given this context on buttons, let’s see what we can do to make our buttons more appealing:
- Size: To have a button clickable on a mobile device we should have an area of at least 40×40 px.
- Box-shadow: We can add a box-shadow to give a depth effect to the button.
- Min-Width: When the text is too short in a button, we can add a min-width to have a minimum size for the button. Also, remember to use textAlign to center the text.
- Hover: Hover makes your buttons interactive. When you change the color of a button on the mouse hover, you are able to grab the attention of the user easily.
- Transitions: Transitions add that little extra touch signifying that you are a pro, by adding a transition of colors
- Round Corners: Adding a bit of round corners will always make your buttons look more welcoming (check if this aligns with your brand guidelines though).
- Line-height: Are you tired of trying to vertically center the text in a button with padding? You should definitely try line-height. Line-height will set the space between lines but in the case of a single line and an element with display: block or inline-block will be the height of the element.
Let’s take a look at all of it together:
CSS Trick #5 – Button Bars
Button bars are bars where we group buttons with different actions. Essentially, they are critical for interaction. These bars contain multiple buttons and most of the time, we aren’t sure about the total number of buttons or size of each button. It is critical to be as flexible as possible while writing your CSS so that you can have the most amount of variations possible.
Now, we need to find a way of making the buttons work independent of the alignment (center, left, right), the number of buttons, and the size of each button. Planning your CSS ahead of execution is the key to pull off this kind of flexibility.
To achieve the same spacing between the bar and the buttons, you need to first understand that the space between the buttons is doubled. This is because the right margin, 5px, of the first button adds up with the left margin, 5px, of the second button giving you a total of 10px which we eventually use for top and bottom. This is why you see that the left and right margins are 5px but the top and bottom are 10px.
Finally, there is an extra horizontal space added by the padding to add that extra 5px missing between the last button and the bar. The text alignment will now work no matter which option you choose.
CSS Trick #6 – Fixed Header
Headers are one of the most common elements on any website or app. Hence, it is critical to handle them correctly. Having a sticky header (making it stay at the top of the page) is something most people desire and achieve by using “position: fixed”. The problem with “position: fixed” is that there is a need to make an empty space at the beginning of the page corresponding to the size of the header.
This has several issues like when the page resizes, the height of the header might change. When this happens, you will need to adjust the space whenever the height of the header changes. Also, part of the scrollbar might be covered by the header, but this can be easily solved by using flex.
What is important when you use this technique is that the element you want to be scrollable contains the property “flex: 1” and its overflow is set to auto. Also, the container of both the header and content should have its height defined and overflow set to hidden.
CSS Trick #7 – Center Content
In today’s world, it is extremely important that our content runs properly on multiple devices with multiple resolutions. This can be extremely difficult to achieve at times and gets very complex when you involve media queries using the same HTML to create a completely different layout.
We are going to start with the most simple scenario which is resizing the content of the page according to the size of the browser. We are also going to assume the content inside a div is text.
We don’t want our page to go extremely wide as it can become very difficult to read when each line is say, 1500px. So, we are going to limit how wide our content can be. Finally, since we are limiting the width of our content, we want to center it horizontally over the screen since it won’t cover the entire width on higher resolution.
There are 3 crucial properties we will use here:
- Max-width: Limits the size of our page to a maximum width
- Margin: “0 auto” – This helps us have the same left and right margins, which will center our div horizontally with “class=page”
- Padding: Helps us to have some space between the screen and our content
CSS Trick #8 – Resize Image
Once we have dealt with resizing our page, we need to be able to resize our images. Images are special because you don’t want the image to be scaling up. For low-resolution images, it should be kept small so that it doesn’t look blurry.
This time, instead of trying to limit the size to a certain amount of pixels, we would want the maximum size to be the original size. Meaning, if the image is 300px wide, the maximum width should be 300px.
The solution is quite similar to the previous example, but with a key difference. The trick relies on the fact that on images, we define the maximum width as percentage. We will set max width to a certain percentage of the screen width.
Let’s consider 2 scenarios where our image is 500px wide and max-width is set as 100%:
1. Screen is bigger than the image
If the screen (or parent element) is 1000px wide, the max width turns out to be 1000px. Note that since the width of the image is just 500px, it will actually display as 500px.
2. Image is bigger than the screen
If the screen is 320px, the max-width is 100%, which is set as 320px. The image in this case will scale down to 320px wide (maintaining the original aspect ratio).
This creates an effect of the image resizing down on smaller screens but having its original resolution on wider screens.
CSS Trick #9 – Tooltips
Tooltip is a simple way of giving more information to the user when he/ she hover an element. This becomes necessary because sometimes there is much more information that can fit into the screen. Some of these tooltips will actually open on onClick rather than on mouseOver which allows them to work on mobile devices as well.
One of the biggest issues with tooltips is positioning since most of them need to resize according to the content. So we need something that is flexible and which position it is depending on the position of the element you want to place the tooltip over.
The solution relies on positioning the elements correctly and playing with transform to make adjustments depending on the content. As an example, we will try to emulate the tooltip of engagement info on Instagram shown below:
The first thing you would have noticed is that the position of the tooltip is absolute. We use bottom and left to align the tooltip to the button. However, it still isn’t centered. To complete the trick, we must use transform. Transforms are calculated based on the size of the node to which the transform belongs.
In this example, we have also created an element from CSS that will render in HTML. For that, we have used :after and set the content as empty. Finally, we have used borders to create a triangle.
CSS Trick #10 – Zoom Effect
CSS is all about giving final touches to your project. If you have invested your time and effort in building a product but missed to give those extra touches, the perceived value of your entire product falls down. That’s why making your app look great is not only essential for visual appeal but also, in a saturated/ competitive market, it might be the difference between you and your competition. So, let’s give a cool touch to your project by using animations.
Avatars are used in a lot of places. Let’s create something with an effect on top of it. When we hover over an avatar, we want to make a light zoom over the image. This will make your app look great.
The solution is extremely simple. First, we need to create our avatar using an image tag since we will be scaling the image using transform and transition to perform the animation. This trick relies on one important fact. When you use transform, only that element is affected. We will not change the position of the rest of the elements on the screen in any way. This is also a great way of creating effects without worrying about how the rest of the elements will flow since they won’t be affected at all.
I hope you enjoyed the CSS tricks we discussed and that they will help you make your page look better and coding life easier.