Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Centring using <code>margin: 0 auto;</code> with <code>display: block;</code> works in almost all browser - the ones that support <code>&lt;canvas&gt;</code> for sure.</p> <p>Live example: <a href="http://jsbin.com/ovoziv/2" rel="noreferrer">http://jsbin.com/ovoziv/2</a></p> <p>HTML</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset=utf-8 /&gt; &lt;title&gt;Centring Canvas&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;canvas&gt;&lt;/canvas&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>CSS</p> <pre><code>canvas { display: block; background: #FFFFB7; width: 550px; height: 400px; margin: 0 auto; } </code></pre> <p><strong>EDIT: Updated answer to center vertically too. This CSS will do the trick:</strong></p> <pre><code>canvas { background-color: #FFFFB7; width: 550px; height: 400px; position: absolute; top: 50%; left: 50%; margin-left: -275px; margin-top: -200px; } </code></pre> <p>Now the explanation. We first place the canvas with 50% offset from the top and left-side using <code>position: absolute</code> by setting <code>top</code> and <code>left</code> to <code>50%</code>. Then, because your canvas has a static size, we add a negative margin (which you should never do when the element is not absolute positioned) for half of the width and size (550x400/2 = 275x200): <code>margin-left: -275px; margin-top: -200px;</code>.</p> <p>The canvas will now be displayed at the center of the screen. If you do this inside another element and want to center in that one, try adding <code>position: relative;</code> to that element, so it will use it's bounds instead of the body's.</p> <p>Live example here: <a href="http://jsbin.com/ovoziv/6" rel="noreferrer">http://jsbin.com/ovoziv/6</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload