How do I centre a div element? (1 Viewer)

Tony Stark

Well-Known Member
Joined
Jan 22, 2025
Messages
225
Location
https://maps.app.goo.gl/pDzZ5pcqruzP5YPS6
Gender
Male
HSC
2025
How do I centre a div element? Example shown:
HTML:
!DOCTYPE html
<head>
    <title>My Handsome Website</title>
</head>
<body>
    <h1>
        Mustache.
    </h1>
    <div>
        <img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse4.mm.bing.net%2Fth%2Fid%2FOIP.nr4nS649uiAABvB2BdeoeQHaEK%3Fpid%3DApi&f=1&ipt=1afd701d4c872d504ed2230c96ad3268690e19e1cf4add861ab0c771b7c45e4b&ipo=images">
    </div>
</body>
 

mickeyd_357

New Member
Joined
Jul 21, 2025
Messages
14
Gender
Female
HSC
2026
Depends what you want to center
If it's the heading try this:
CSS:
.h1 {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
see if that works, you can adjust the translating
 

C2H6O

Well-Known Member
Joined
Oct 18, 2024
Messages
938
Gender
Male
HSC
2025
Nah its a joke with the web designers because everyone has come across this problem of centering a div
fkkk you just triggered my ptsd from my web design days its all coming back 😭
the 10000vh long chats with chatgpt trying to fix it
morning-sunshine.gif
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top