Web Development · Jan 15, 2013
CR
Clément Rigo · @mydnic
IT Business Artisan & Consultant CTO
Vous connaissez tous bien sûr le célèbre margin : 0 auto; qui vous permet de centrer un élément de votre page, pour autant bien sûr que ce même élément dispose d'une largeur donnée. Cette simple ligne de code centrera votre div au milieu, mais comment faire pour centrer cet élément verticalement?
Si vous connaissez la hauteur et la largeur de votre box, utilisez ce code très simple:
#main
{
position:absolute;
background-color:#00FF00;
width: X px ;
height: Y px ;
top:50%;
left:50%;
/*Diviser la largeur par deux ET mettre en négatif*/
margin-left: -X/2 px;
/*Diviser la hauteur par deux ET mettre en négatif*/
margin-top: -Y/2 px;
}
Attention, veillez bien à remplacer les valeurs X et Y par la largeur et la hauteur de votre élément! Comme vous pouvez le voir dans les lignes 9 et 10, vous devez diviser ces valeurs en deux et les mettre en négatif. Ensuite, magie! Votre élément ce situera toujours au milieu de votre page, même en la redimensionnant ;)
CR
Clément Rigo
IT Business Artisan & Consultant CTO. I've been writing this blog since 2010.
Personal
The Fastest Path to a $50K/Month DTC Brand
Joe Speiser built Petflow, a subscription pet food brand, into a $70M business. He later shared the exact playbook he'd follow if he had to start over and get to $50K/month as fast as possible. Here's that playbook, distilled. The plan comes down...
Web Development
mini-swe-agent: The 100 Line AI Agent That Solves GitHub Issues
I was scrolling through my GitHub stars looking for something to write about and stopped on this one again. mini-swe-agent is the kind of project I like: it takes something everyone assumes needs a huge framework (an AI coding agent) and does it i...
Personal · Web Development
How I Built An Automatic Highlight Clipper For Battlefield 6 (Python, OpenCV, FFmpeg)
I record every Battlefield 6 session with NVIDIA's ShadowPlay, because who doesn't want to relive that one clutch double kill at 2am? The problem is that "recording everything" very quickly turns into a graveyard of 4K DVR files nobody wil...

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.