Thursday, December 6, 2012

Calc() in JavaScript

Is it possible to make a div 50px less than 100% in pure CSS? I want the <div> to be only 50px less than 100%. I don't want any JavaScript.


Yes you can. Without using the IE's expression(), you can do that in CSS3 by using calc().
div {
    width: 100%;
    width: -webkit-calc(100% - 50px);
    width: -moz-calc(100% - 50px);
    width: calc(100% - 50px);
}


But when it is re-sized you will loose complete div if less than 50px;


BEST SOLUTION :
DIV automatically takes its parent's width. So there is no need to define any width. Normally would simply write it like this:
div{
    margin-right:50px;
}
http://jsfiddle.net/Nw3yd/6/
http://jsfiddle.net/Nw3yd/3/

No comments: