Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS positioning to center plus x pixel

Tags:

html

jquery

css

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

like image 409
netdjw Avatar asked Sep 30 '14 10:09

netdjw


2 Answers

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); 
} 
like image 101
Anonymous Avatar answered Sep 22 '22 01:09

Anonymous


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>
like image 34
Scimonster Avatar answered Sep 23 '22 01:09

Scimonster