Making the design to be responsive is very easy as shown in my Responsive Design in 3 Steps tutorial, but maintaining the elements to look aesthetically balanced on all breakpoint layouts is an art. Today I'm going to share 5 of my commonly used CSS tricks along with sample cases for coding responsive designs. They are simple CSS properties such as min-width, max-width, overflow, and relative value — but these properties play an important part in responsive design.
1. Responsive Video (demo)
This responsive video CSS trick was discovered by tjkdesign.com. I've blogged about it before, you may read the details here. It makes the video embed to expand fullwidth to the boundary.
.video {
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
}
.video iframe,
.video object,
.video embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
2. Min & Max Width (demo)
Max-width property allows you to set the max width of the element. The purpose of max-width is to prevent the element from extending the boundary.
Max-Width Container
In the example below, I specify the container to display at 800px if possible, but it should not exceed 90% of the boundary width.
.container {
width: 800px;
max-width: 90%;
}
Responsive Image
You can make the image auto resize to the max width of the boundary by using max-width:100% and height:auto.
img {
max-width: 100%;
height: auto;
}
The above responsive image CSS works on IE7 and IE9, but doesn't work on IE8. To fix it, add width:auto. You may apply a conditional CSS specifically for IE8 or use the IE hack below:
@media \0screen {
img {
width: auto; /* for ie 8 */
}
}
Min-Width
Min-width is opposit to max-width. It sets the minimum width of an element. In the example form below, min-width is used on the input text field to prevent the input from getting very small when scaling down.
3. Relative Values (demo)
In responsive design, knowing when to use relative value can simplify the CSS and maximize the best layout result. Below are some examples.
Relative Margin
Below is an example of a commentlist where relative left margin is used to space out the threaded comments. Instead of using fixed pixel value, I used percentage value to space out the sub-lists. As shown on the left side of the screenshot, the content box in the sub-lists gets very small on mobile resolution if pixel left margin was used.
Relative Font Size
With relative value (eg. em or %), the font size, line-height and margin spacing can be inherited. For example, I can change the font size on all descendant elements by simply changing the font-size on the parent element.
Relative Padding
The screenshot below shows it is better to use relative percentage padding as opposed to fixed pixel padding. The box on the left shows an unbalanced padding space if pixel padding was used. The box with percentage padding on the right shows that the content area is maximized.
4. Overflow:hidden Trick (demo)
As posted in my previous article, you can clear float with the overflow property. This trick is extremely useful. You can clear the float from the previous element and keep the content running within the container by applying overflow:hidden.
5. Word-break (demo)
I also talked about the word-wrap property before. You can force unbreaking text (eg. long URL text) to wrap instead of running in a single line.
.break-word {
word-wrap: break-word;
}
Good CSS Tricks,It’s very helpful for me~
Thank you very much for this article, some neat tricks I didn’t yet know of. (such as the responsive image IE8 hack ;) )
Muchas Gracias
I’m shaking my penor :3
Thanks for these informations.
you’re tutorials are fantastic but I can’t help to wonder why no one really explains or shows a demo in using a css background image while using responsive design? At least I haven’t seen any. Basically I’m wondering if the img element was a css image rather than an html element, how would this work with responsive design? For designing purposes I use css backgrounds and wanted a quick insight on how this is done.
that was meant for Nick, not you Aydin. sorry.
Great tricks for building responsive websites and a GREAT POST!! Thank you for sharing.
Shrink, Share & Get Paid!
Post your Links on Facebook, Twitter, Blogs & Websites!
Go To http://netfly.com/?r=1 to get paid for your posted links!
Nice responsive tutorial,
Thanks, this is great information. Enhanching some responsive web projects with such simple css tricks, nice
It is excellent.
Thanks, this is great information.
very nice and advance blog, You blog provide useful tutorial about CSS. thanks for sharing this wonderful tutorial.
Regards,
website designing
Really fantastic article. You have cover everything with astonish way. What i enjoy most is that with every written part you had a draw for better undertsaning
Useful tutorial. Nice job on posting!
Fantastic sharing. Useful tips for every web developer.
these tricks are really helpfull and i am gonna use these tricks in my work. BTW thanks to webdesignerwall.com for this wonderfull post.
Perth website designer
A very useful trick, thank you!
Nice post!
I also think the h tags cause a lot of troubles when dealing with design, so here a trick to fix it:
http://www.thebestdata.com/zoom.aspx?menutype=1&auto=3906&t=CSS-fixed-header-margin-auto
Scott, about the link you posted => with a good reset CSS you don’t need to clear margins on a H2 and even write a post about it ;-)
Mark, Good idea thanks!
What the best way you advice to reset CSS ?
A very useful trick! Thank you very much for this article,
excellent. Responsive Web Design, very important
Detailed article, I learned new css, lun reading is done.
thank you much!
Very simple yet useful tricks. For those who are interested, check out my tutorial on responsive design http://shingokko.com/blog-entry/responsively-design-a-blog.html