snipt

Ctrl+h for KB shortcuts

CSS

Center with Position Absolute

1
2
3
4
5
6
7
#div_to_centre{
    width: 400px;
    position: absolute;
    top: What you want;
    left: 50%;
    margin-left: -200px; /* half of the with (400px) */
}
https://snipt.net/embed/55e0ddc1d1ee00e82d9622f027fe0bf5/
https://snipt.net/raw/55e0ddc1d1ee00e82d9622f027fe0bf5/
55e0ddc1d1ee00e82d9622f027fe0bf5
css
CSS
8
2016-09-23T23:26:34
True
False
False
Oct 02, 2013 at 09:30 AM
/api/public/snipt/84550/
center-with-position-absolute
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nf">#div_to_centre</span><span class="p">{</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">width</span><span class="o">:</span> <span class="m">400px</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">position</span><span class="o">:</span> <span class="k">absolute</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">top</span><span class="o">:</span> <span class="n">What</span> <span class="n">you</span> <span class="n">want</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">left</span><span class="o">:</span> <span class="m">50%</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">margin-left</span><span class="o">:</span> <span class="m">-200px</span><span class="p">;</span> <span class="c">/* half of the with (400px) */</span> </span><span id="L-7"><a name="L-7"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
"css position", absolute, center

CSS

Horizontal Center

1
2
3
4
5
6
#main
{
  width: 920px;
  margin-left: auto;
  margin-right: auto;
}
https://snipt.net/embed/e19bc9ee10948979786c0ac1ade06361/
https://snipt.net/raw/e19bc9ee10948979786c0ac1ade06361/
e19bc9ee10948979786c0ac1ade06361
css
CSS
6
2016-09-08T05:21:25
True
False
False
/api/public/snipt/41056/
horizontal-center
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="nf">#main</span> </span><span id="L-2"><a name="L-2"></a><span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="k">width</span><span class="o">:</span> <span class="m">920px</span><span class="p">;</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">margin-left</span><span class="o">:</span> <span class="k">auto</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">margin-right</span><span class="o">:</span> <span class="k">auto</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
center
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.