While CSS isn’t as widely used as JavaScript, it’s still in the top 10 coding languages, according to Redmonk. Since CSS is quite robust, reasonably easy to learn, and universal across different browsers, it’s popular among website developers.
As it is with every coding language, there are several shortcuts or hacks with CSS that allow you to write cleaner code, improve design elements, and save valuable time. Furthermore, you can directly insert these snippets to your site using a code editor.
It is also important to know that you don’t have to be a senior web developer to use CSS. Data from W3Techs show that CSS is used by 96 percent of all websites, and being able to use CSS to enhance the layout and appearance of a website is integral to the functioning of major open source CMSs like WordPress.
In fact, most of the biggest website builder tools (generally known for promoting a “What You See Is What You Get” or WYSIWYG approach) now allow users to insert custom CSS code.
If you’re new to CSS, freeCodeCamp has an excellent tutorial video on Youtube where you can learn the basics. If you already know the fundamental aspects, then let’s get started with these ten CSS hacks.
Table of Contents
1. How to position content in the center with CSS
Placing content in the middle of the screen might be tricky. However, you can use position: absolute
to override the dynamic placement and always position the content in the center.
It also works with every resolution across devices. However, always double-check if everything is positioned as you want it and the element looks natural even on smaller screens.
Example snippet:
2. How to fix an element’s position in CSS
Despite websites being dynamic, you might have some elements that you want to fix in certain positions. You can do so by using the position:absolute
script.
However, use this method carefully and test it beforehand on every screen size and resolution so that it doesn’t break your site’s design.
Following this script with a specific position node assures that the element remains in the same position for all users.
Example snippet:
3. How to fit images to the page in CSS
There are few things worse than your images spilling over your site visitor’s screen. It can absolutely break your site’s design and discourage users.
However, with this simple hack, you can make sure that your images always fit the visitor’s screen regardless of the device they’re using.
Example snippet:
4. How to edit style on a single page in CSS
If you want some of your page posts to look different from others, you can use the .single-post
script to override the site’s CSS style. This ensures that you only tweak a single page and leave the other pages’ format as default.
However, if you find yourself using this method a lot, it’s best to modify the overall CSS style to avoid writing unnecessary code lines.
Example snippet:
5. How to consolidate styling in CSS
If you know that you want to add CSS styling to multiple items, then writing these pieces of code out one by one takes time. However, when you separate items with commas and write the CSS style inside, then the style is added to all of them.
This helps you save time and lower your code’s weight because you don’t have to write a similar line of code multiple times.
Example snippet:
6. Visited link styling in CSS
The default style of visited links that users have clicked on might not work great with your current site’s style. You can use CSS code to tweak how the links look before and after visitors have clicked on them.
You can then match these with your site’s overall style to create a unique experience.
Example snippet:
7. Hovering effect delays in CSS
The :hover
selector is a CSS pseudo-class which allows you to create a hovering effect. However, you can further stylise this by adding a transition
element to delay the hovering effect.
While it looks neat, it also creates a sense of movement in the users’ eyes, further drawing attention to the element.
Example snippet:
8. How to disable text wrapping and add ellipsis in CSS
If you’re tight on space for your text, you might need to cut it short to fit other elements. Sure, you can manually tweak each of the text elements, but that takes time and some trial and error.
What you can do instead is combine overflow
, white-space
, and text-overflow
to create a natural break in the text that’s easy on the eyes.
The following example sets the limit on the text width, hides the overflowing part, disables the text wrapping, and adds an ellipsis (…) to indicate there’s more text for the users.
Example snippet:
9. Initial letter stylizing in CSS
The stylized initial letter or drop caps have been used in book and magazine design for a long time. It works by grabbing the readers’ attention and getting them interested in reading the first line.
While you may think that this style has aged, you can also design it to look modern and still capitalize on the psychological effect it creates for your visitors. Furthermore, drop caps option is also written into the CSS language, so you can effortlessly use it and give your paragraphs a new look.
Example snippet:
10. How to reset CSS styles
Last but not least, you might need to override all the default styling attributes across different browsers for your design to work flawlessly.
Each browser has its own style sheet, with built-in default styles, and this can sometimes be a problem when you’re trying to make your website look consistent on all browsers.
You can use a comprehensive CSS reset from Eric Meyer which covers almost all bases. However, you can also achieve a minimal reset result, which you can borrow from Jeff Starr:
And there you have it, ten hacks to make your CSS code look neater, decrease your coding time, and add visitor-friendly elements.
Thanks for reading!
I’m a writer passionate about digital marketing, web development, and cybersecurity. You can reach me on LinkedIn here.
This content was originally published here.