Eddy Eddy - 21 days ago 4x
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


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).