Eddy Eddy - 1 year ago 84
CSS Question

Calculating window width minus an exact amount of pixels

How would I code this correctly, the (window.innerWidth minus 80px) part:

<div id="myDiv"></div>

right: (window.innerWidth minus 80px);
width: 40vw;
height 200px;
background-color: #333333;

I have a reason for doing it exactly this way. JSFIDDLE HERE

Answer Source

I would write

right: calc(100vw - 80px);

plus you have to add position: relative; for the right parameter to have any effect at all: https://jsfiddle.net/78hd0hf1/1/

That also works if #myDiv is nested in one or several wrappers which are narrower than the window width (which can be a problem when using 100% instead of 100vw).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download