How to make a div 100% wide minus a certain amount?

So, I have a div that I want to be:

100% width (for viewing) - 150 pixels

How can I show this in CSS or Javascript?

+4
source share
4 answers

The container of your div should be position:relative (or absolute ... but not the default), and the div style should be something like this:

 position:relative; width:auto; margin:0px 150px 0px 0px; 
+6
source

You can use the jQuery $ (window) selector to get the width of the viewport and change the width of the required div

  var width = $(window).width(); $("div").css("width", width-150); 
+2
source

You might want to find a CSS style.

 box-sizing: border-box; 

Usually 100% is calculated for the size of the internal surfaces, which is completely useless if there are any additions or borders in your box. When choosing a window size, it is designed so that the outer border is such. Incredibly useful for making a% size div with zero padding and border correctly.

+2
source

All Articles