I'd like to center a div using only css
and less
.
Here is what I tried so far :
#exampleSquare {
position: absolute;
height: 100px;
width: 100px;
background-color: gray;
left: calc(~"`$(window).width()` / 2- 50px");
}
Warn :
- The above code is based on : using javascript calculated values in less
- Don't change the
position: absolute
, a library is putting it and handling it like so. I simplified the problem. - Don't change the
window.width
thing to50%
, in my case, the div is inside a lot of containers. So50%
would refer its parent size, not the window. - Don't add a container with
text-align: center;
, this div container (parent) is also handle by the library.
I just need the left
syntax to return me the correct offset.
EDIT :
From Nico O comments, here is what I have in my detailed CSS console:
EDIT :
Nico O pointed that this error comes from the fact that I was using Chromium v.32 and the vw
syntax is only supported since v.36 : reference