Is there any way to use only CSS
to positioning center plus x pixel a div
?
I have now this CSS codes:
#mydiv {
display: block;
width: 200px;
margin: 200px auto;
}
I want to position this div to center + 300px to right. It is possible to do using only CSS?
(I know here is a way to using jQuery, but I want to do using only CSS.)
You could achieve this by adding transform: translateX(300px);
to #mydiv
.
Take a look on JSFiddle DEMO
#mydiv {
display: block;
width: 200px;
margin: 200px auto;
transform: translateX(300px);
}
Yes, it is, for browsers that support CSS3 calc()
.
#mydiv {
display: block;
width: 200px;
margin-left: calc(50% + 200px); /* 50% - 100px (half of the div) + 300px */
}
#mydiv {
display: block;
width: 200px; height: 50px;
margin-left: calc(50% + 200px);
/* 50% - 100px (half of the div) + 300px */
background: blue;
}
<div id="mydiv"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With