Everyone appreciates the beauty and power of CSS as a markup language to develop beautiful, powerful, accessible and interactive websites. Its flexibility, styles and easiness always attract web designer and developers independently. The love continues to grow with the advent of CSS3 that brings us new possibilities for creating attractive layouts, typography and visual presentation of the content.
For those, who don’t know what is CSS and what it can do? CSS stands for Cascading Style Sheets – a popular style sheet language that is used to describe the look and formatting of documents written in HTML and XHTML. No matter whether you want to create a web page or merely customizes the look and feel of a website – CSS is always counted amongst a good option to start with.
But, we can’t deny a bitter truth that CSS is one-of-its-kind technology which is not always easy to deal with. Oftentimes, CSS coding becomes not less than a nightmare, especially for beginners when it comes to use this wonderful language for presenting web pages.
In this post, we are discussing a comprehensive toolbox for remarkably useful CSS-based Coding Tricks & Techniques without which CSS web developer can’t live. You can use these listed below CSS coding tips in your project to minimize your complexity of CSS and improve your CSS coding efficiency. For beginners I recommend using a tool like Generate CSS to help you with your coding. Hope you find this set of tricks useful.
Horizontal & Vertical Centering
CSS is most commonly used for the purpose of horizontally center block level elements. A block element can be defined as an element that has the full width and a line break it before and after. <h1>, <p>, <div> are some good examples. For horizontal centering of block elements, you are required to set a width on the element and then, adjust the left as well as right margins to auto. To sustain the look of center block level elements in Internet Explorer, you need to include a proper doctype which helps in proper functioning in all the previous IE versions. Simply insert the below line of codes to horizontally center block level elements in CSS :
You can also assign a vertical position to Block Level Elements by using CSS. For many developers, vertical centering with CSS is a serious challenge and so, they use <table>-based layouts. A simple trick for vertically center block level elements – position the top left corner of div 50% from the top and adjust a negative top margin equivalent to half of the height of div.
The HTML
The CSS
You can use same CSS trick for creating horizontal centering as well. Just change top to left; margin-top to margin-left and adjust the negative margin half of the element width. By inserting the below set of codes, you can position the block elements vertically as well as horizontally.
CSS Bar Graphs
Bar graphs always counted amongst a good source of data presentation in a simple manner. It is the best way to represent complex data in a simple yet descriptive manner. If you’re trying hard to create your own CSS based graph and looking for quick tricks then scroll down below to find amazingly easy techniques to design a beautifully crafted CSS Graph.
For creating a simple bar graph in CSS simply copy and paste the below set of codes. In these codes, I have used a simple CSS trick by making a clever use of CSS percentage width abilities for designing a percentage-based simple bar graph.
If you want to create a Complex CSS Bar Graph with a more complex visualization than you are required to follow the below instructions. With this trick, you can design a bar graph with different color schemes.
Here is another CSS trick for creating Vertical CSS Bar Graph under which you need to use the same principle vertically.
CSS-Based 3D Buttons
CSS has emerged as a great option to design 3D buttons. To get started, simply add borders of different colors to the elements. Use lighter shades where the light source shines and vice-versa.Insert the following line of CSS codes to create a button with the light source. You can also experiment with CSS to add more colors, gradients or shapes to the buttons and give special effects to the buttons.
Rounded Corners
Creating rounded corners with CSS is a serious challenge. The smart trick is to use CSS3 border-radius property that enables you to design rounder corners in the design elements without corner images or multiple div tags.
For simple CSS3 rounded corners that are fully supported across browsers :
In case, you want to design rounded corners for a specific browser refer the below set of lines. Use -webkit-border-radius for Safari, Chrome, and other Webkit browsers. You are required to use -moz-border-radius for creating rounder corners compatible in Firefox and other Mozilla versions :
Insert the following codes when Webkit and Mozilla using different syntax to describe one specific corner :
Conditional Comments
Conditional comments are one of the best alternates to deal with Internet Explorer’s inconsistencies. By using Conditional comments, you can easily target IE browsers and produce user-friendly outputs. Here is the basic form of a conditional comments :
In case, you want to target any specific version of Internet Explorer then simply use a combination of conditional comments that help you to create different CSS styles for various versions of IE. Here are the codes :
Few More CSS Tricks :
1) Don’t forget to give your classes proper names in case you’re developing a stylesheet for a client. You can use proper names such as .sidebar-container.
2) It is always a good idea to separate your stylesheet into logical sections such as Framework (columns, site structure), Left Column content, Center Column content, Right Column content, Footer, HTML definitions Generic and so on.
3) Avoid assigning too many classes to each and every page element. Instead of that, you can take an advantage of parents to deal with styling elements inside containers or block elements and define their wrapper. Isn’t it easy.
4) Every web browser produces an input box differently and so, a good idea is to implement a Reset stylesheet. It helps in removing all default browser attributes from elements such as padding and margins. Besides that, it also enables you to maintain greater compatibility between different web browsers.
5) Majority of CSS developers at beginner’s level hardly gives importance to the readability of CSS, which is not a good habit. Easily readable CSS coding structure plays an important role for the debugging purpose in the near future.
6) You can combine various elements in a stylesheet that shares similar properties together instead of re-writing previous set of codes again and again.
You can try HTML TRYIT editor to write and edit the code and to see the result.