TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

“Googles 'vertical center div css' for the 100th time. reads for 7 minutes”

296 pointsby sebkomianosalmost 10 years ago

25 comments

jonahxalmost 10 years ago
One of the wonderful ironies of CSS is that after years of puritanical preaching about the evils of non-semantic table elements, the best-practice recommendation for centering a div of unknown dimensions, assuming you want any kind of legacy support, is[0]:<p><pre><code> &lt;div style=&quot;display:table;&quot;&gt; &lt;div style=&quot;display:table-cell;vertical-align:middle;&quot;&gt; &lt;div style=&quot;margin-left:auto;margin-right:auto;&quot;&gt;&lt;&#x2F;div&gt; &lt;&#x2F;div&gt; &lt;&#x2F;div&gt; </code></pre> [0] <a href="http:&#x2F;&#x2F;howtocenterincss.com&#x2F;#contentType=div&amp;horizontal=center&amp;vertical=middle&amp;browser.IE=8" rel="nofollow">http:&#x2F;&#x2F;howtocenterincss.com&#x2F;#contentType=div&amp;horizontal=cent...</a>
评论 #9993414 未加载
评论 #9993553 未加载
评论 #9994964 未加载
评论 #9993802 未加载
评论 #9994466 未加载
评论 #9993926 未加载
dennisbestalmost 10 years ago
Just use flex-box. And by &quot;use&quot;, I mean get ready to study it, change the way you write CSS, give it all of your worldly possessions, and eventually collect money for it at the airport.
评论 #9993380 未加载
评论 #9993531 未加载
评论 #9993930 未加载
评论 #9993954 未加载
评论 #10001042 未加载
Animatsalmost 10 years ago
You see, kiddies, we used to have programs that did all that for you. There was this program called Dreamweaver. You just opened up a design window and laid out the page by pointing and clicking. Anyone could do it. But if anyone could do it, &quot;web programmers&quot; weren&#x27;t needed. So CSS was invented to make it more like programming, and especially to make it so complicated that it couldn&#x27;t be done by pointing and clicking.
评论 #9994138 未加载
评论 #9994144 未加载
lkrubneralmost 10 years ago
This is an example where the headline on Hacker News is 100x better than the original headline. I do wish Hacker News allowed these kinds of headlines, which draw out the parts of the article that are of most interest to the Hacker News crowd.
评论 #9993376 未加载
评论 #9994244 未加载
评论 #9995105 未加载
supercoderalmost 10 years ago
All you need to do is simply take the 20 part video course <a href="http:&#x2F;&#x2F;flexbox.io&#x2F;" rel="nofollow">http:&#x2F;&#x2F;flexbox.io&#x2F;</a>. Couldn&#x27;t be easier !
bla2almost 10 years ago
This is has been a problem for a long time, but it&#x27;s fixed now: <a href="https:&#x2F;&#x2F;philipwalton.github.io&#x2F;solved-by-flexbox&#x2F;demos&#x2F;vertical-centering&#x2F;" rel="nofollow">https:&#x2F;&#x2F;philipwalton.github.io&#x2F;solved-by-flexbox&#x2F;demos&#x2F;verti...</a> <a href="http:&#x2F;&#x2F;caniuse.com&#x2F;#feat=flexbox" rel="nofollow">http:&#x2F;&#x2F;caniuse.com&#x2F;#feat=flexbox</a>
评论 #9993564 未加载
grumblestumblealmost 10 years ago
<p><pre><code> .centered { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </code></pre> now just put it in a relatively positioned container and you&#x27;re done. i thought this was common knowledge by now, i haven&#x27;t used `table-cell` in years.
评论 #9993687 未加载
bleonardalmost 10 years ago
Follow-up: Does anyone know how to best host this site? a) with less than 50 users all-time b) easily updated daily (blog-style) by a 7 year old c) is approximately free<p>Wordpress with her custom theme seems to fail C. Github pages &#x2F; Jekyll is failing B
评论 #9993647 未加载
评论 #9993672 未加载
评论 #9993632 未加载
评论 #9994262 未加载
评论 #9994024 未加载
评论 #9993610 未加载
评论 #9994310 未加载
评论 #9993597 未加载
评论 #9993833 未加载
rsuelzeralmost 10 years ago
This was a cute article. I laughed at the end. Thank you for sharing this, it put a smile on my face. :)
willsteppalmost 10 years ago
display:table and its kin are the greatest thing I ever discovered in css. No more centering problems and easy to reason about. I&#x27;m not sure if people just don&#x27;t know about it, or there are use-cases where it doesn&#x27;t work for them. I&#x27;ll definitely use Flexbox when its more widely supported, though.
评论 #9993423 未加载
elixxalmost 10 years ago
At which point you use a bootstrap jumbotron like 2&#x2F;3 of the sites I see.
nathan_f77almost 10 years ago
I love &quot;display: flex&quot; so much. I&#x27;m working on extensions for modern browsers, so I can use it everywhere. Makes everything so much easier. Also cases where you would normally use &quot;.left { float: left; }&quot; and &quot;.right { float: right; }&quot;.
logicalleealmost 10 years ago
Why isn&#x27;t this actually an easy, obvious part of CSS? It doesn&#x27;t seem like there is anything that makes this more complicated to implement than anything else people use daily... it&#x27;s also an obvious feature. Reasons?
zajdalmost 10 years ago
<p><pre><code> body { position: relative; height: 100vh; } #content { height: &lt;defined&gt;; top: 0; right: 0; left: 0; bottom: 0; position: absolute }</code></pre>
vayarajeshalmost 10 years ago
A father-daughter moment of a techie :) your daughter see you as a hero when she learns more about vertical center and CSS in general
mattwadalmost 10 years ago
If you know the height of your centered box, it&#x27;s not <i>that hard</i>. Create a wrapper div whose top is set 50% down the page but has a negative offset which is 50% the height of the box:<p><pre><code> .vertically-centered { position: absolute top: 50%; height: 200px; margin-top: -100px; &#x2F;* minus half of `height` *&#x2F; } </code></pre> If you have trouble, try setting `height` to 100% on the &lt;body&gt; tag and any parents
bbcbasicalmost 10 years ago
And if you google &#x27;vertical center div css&#x27; you will now find this page (in a news subsection). Try it. Ymmv.
jnpatelalmost 10 years ago
Aw man, I was hoping to see the final website your daughter ended up with! :)
seanwessmithalmost 10 years ago
Why isn&#x27;t there a MS Word for creating HTML?
评论 #9993374 未加载
评论 #9993446 未加载
评论 #9993307 未加载
评论 #9993472 未加载
评论 #9993375 未加载
评论 #9993369 未加载
throwaway000002almost 10 years ago
Has anybody, possibly, probably, an academic, given HTML+CSS a semantics?<p>I&#x27;m thinking something like whatever minimal core would be considered the PCF of the web world.
评论 #9994476 未加载
teenalmost 10 years ago
this is extremely easy to do in flexbox. despite what people say it&#x27;s actually very quick to pick up
pcunitealmost 10 years ago
Very funny ... thanks for sharing.
BurningFrogalmost 10 years ago
But Flexbox?
SimeVidasalmost 10 years ago
Tattoo “flexbox” on your hand :P
justindocantoalmost 10 years ago
It&#x27;s really not that hard.<p>&lt;div class=&quot;center-this&quot;&gt;&lt;div&gt;Stuff&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;<p>.center-this {display:table;} .center-this &gt; div {display:table-cell;height:100%;vertical-align:middle;}<p>I even code pen&#x27;d it for you: <a href="http:&#x2F;&#x2F;codepen.io&#x2F;anon&#x2F;pen&#x2F;RPEypL" rel="nofollow">http:&#x2F;&#x2F;codepen.io&#x2F;anon&#x2F;pen&#x2F;RPEypL</a>