The only 2 correct ways to center a div

Поделиться
HTML-код
  • Опубликовано: 7 янв 2025

Комментарии • 128

  • @Wineblood
    @Wineblood Год назад +243

    If your div is big enough, it's always centered.

    • @okaybossiamonyoutubeakkskd
      @okaybossiamonyoutubeakkskd Год назад +3

      haha, nice one

    • @Rhidayah
      @Rhidayah Год назад +1

      Lol

    • @AbduganiXolmatjonov
      @AbduganiXolmatjonov Год назад +1

      it's funny but true af

    • @overdrive112
      @overdrive112 Год назад

      Is the div classed as „parent“ or why does it start with .parent? (I’m new)

    • @pabllo7023
      @pabllo7023 Год назад

      ​@@overdrive112it's probably just an example, if its .parent then its a class="parent" (I'm 99% sure)

  • @SASA_maxillo
    @SASA_maxillo Год назад +49

    10 seconds later: "oh, lemme google how to center a div again" 😂😂😂

    • @subyouwont
      @subyouwont 4 месяца назад

      Literally 😭 😭

  • @refrigegator7955
    @refrigegator7955 Год назад +9

    You are a god amongst men. Working on a project for a class and in seconds you gave me an answer that other videos took minutes to get incorrect.

    • @chrikke
      @chrikke Год назад

      Except he is so wrong about these being the only correct ways. Remember, the correct way is the way that works

  • @bn5055
    @bn5055 Год назад +86

    Unless for some reason you need to use position: fixed or absolute in which case you can use inset: 0; margin: auto; 😉

    • @danalenne5565
      @danalenne5565 Год назад +3

      Thank you!

    • @xadrain
      @xadrain 11 месяцев назад

      Only option if you want to rotate object in center point when you centered it.

  • @deadliftdoll
    @deadliftdoll Год назад +39

    the relief i felt when my way is the first one you mentioned 😅

  • @self-learner4934
    @self-learner4934 Год назад +42

    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%)

  • @HloniTwala
    @HloniTwala Год назад

    Saving lives of those who are beginners in coding, thank you !

  • @alibhamjee8002
    @alibhamjee8002 Год назад

    Wooooewewwww thank you. Bro best video I’ve found

  • @eruptic6503
    @eruptic6503 Год назад +2

    "Carefully, he's a hero"

  • @laralopes8619
    @laralopes8619 10 месяцев назад

    that grid one... you were sent by god

  • @abdurrafaywali4756
    @abdurrafaywali4756 Год назад

    Man that was a greeeeeeaaaaaattttt problem solveeeeeedddddd, AMAZINGGGGGGG!!!!
    THAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANKS!!!!

  • @gilr.7
    @gilr.7 Год назад

    This vídeo helped me a lot

  • @karaz526
    @karaz526 Год назад

    thanks man i've been searching for a way to center a div without all the extra crap

  • @MinecraftAchievers
    @MinecraftAchievers 2 года назад +1

    this was so helpful thank you so much

  • @thewandererog
    @thewandererog Год назад

    justify-content : center is what I was looking for

  • @_tanzil_
    @_tanzil_ Год назад

    It's 2023 and they are still correct

  • @learnbypreet
    @learnbypreet 7 месяцев назад

    love it!

  • @TheGreyMotion
    @TheGreyMotion Год назад

    place-items: center works in flex aswell.. is a shorthand.. also.. inset prop..

  • @GgggGggg-mm7ci
    @GgggGggg-mm7ci 9 месяцев назад +1

    JESUS MY SAVIOR MY SAVIORRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

  • @jamemadison6499
    @jamemadison6499 Год назад

    that grid part is new ... thanks man

  • @ratulhasan-pu1js
    @ratulhasan-pu1js Год назад

    Brother, thank you very much.

  • @funkdefied1
    @funkdefied1 Год назад

    Honestly after learning all these I still kinda like “left: 50vw; translateX: 50%” + repeat for top/y

  • @dmgxmaximus
    @dmgxmaximus Год назад +1

    thanks buddy

  • @Horrordelic
    @Horrordelic Год назад +2

    Yeah you need to do display: absolute/fixed center as well :)

    • @SuprEmpth
      @SuprEmpth Год назад

      Can you explain why? I'm learning and not understanding all these different methods

    • @kdensport
      @kdensport 2 месяца назад

      Absolute/fixed is a values from position, nothing related with display bruh

  • @AkunRumahDayeuhkolot
    @AkunRumahDayeuhkolot 4 месяца назад

    thankyou so much!!!!!!

  • @sargaa8471
    @sargaa8471 7 месяцев назад

    0:00 for the normal version

  • @sumitprajapati-jc6sn
    @sumitprajapati-jc6sn 7 месяцев назад

    thanks brother

  • @YeneSpace
    @YeneSpace Год назад +6

    sadly it didnt work out for me. I tried both correctly but I dont know where its falling apart.

    • @Thorin-uf4yt
      @Thorin-uf4yt Год назад +2

      You need to set the parent div height

    • @indhurajc3814
      @indhurajc3814 Год назад +1

      ​@@Thorin-uf4yt but.,
      If I want particular size of the div box.,
      That time how to i put value of Height in vh.,
      If I put like ur suggestion.,
      The div height is collapsed..😢

  • @timecircle8420
    @timecircle8420 Год назад

    Just subbed

  • @ajothestranger
    @ajothestranger 9 месяцев назад

    thank you!

  • @michaelpatrick777
    @michaelpatrick777 11 месяцев назад

    Me :
    margin-top: 50em;
    😂

  • @TechBox2026
    @TechBox2026 5 месяцев назад

    Very cool 🎉

  • @grkb
    @grkb 2 года назад +3

    for me if i want to center only horizontally i just do it like in the old days and use the center html tag (although i think it’s deprecated), but if I want to center both ways i use flex like in the video.

    • @Redlaw
      @Redlaw Год назад

      I see you also dont see red markings in vscode as errors and rather as suggestions... i, too, used center tag a while back but can't stand red in vscode, so I stopped, but yeah, it still works in all browsers

  • @alphakhangaming4073
    @alphakhangaming4073 Год назад

    Thanks you Soo much

  • @joshuarackal
    @joshuarackal Год назад

    Thank u so much

  • @givowo
    @givowo Год назад

    rip

  • @raflifirdaus2788
    @raflifirdaus2788 Год назад

    ohh... thank jesus that's what i am searching for ..

  • @power_napppp
    @power_napppp Год назад

    its 2023 man! make another video

  • @sharonedwin1403
    @sharonedwin1403 Год назад

    Thank youu!

  • @sayyednaila4369
    @sayyednaila4369 Год назад

    my way to center a div (first one)😅

  • @kakashi99908
    @kakashi99908 9 месяцев назад

    I cant remember positioning for more than 1 hour 😅

  • @am2383-o2q
    @am2383-o2q Год назад +38

    i will use "margin: 0 auto" forever

    • @MultiEMitchell
      @MultiEMitchell Год назад +6

      The king of inline-block

    • @chiggywiggy524
      @chiggywiggy524 Год назад +4

      .container {
      width: 90%
      margin: 0 auto
      }

    • @gon2064
      @gon2064 Год назад

      Is it different if i use margin: auto; ? Because its work too

    • @Va9bhav
      @Va9bhav Год назад +1

      ​​​@@gon2064actually its not a good practice because it will set all 4 direction to auto so @arm162 code is perfect because first value js for top and bottom and second value is for left and right
      Btw U can use
      Margin-inline:auto;
      Sry for bad English

    • @mikemhz
      @mikemhz 11 месяцев назад +1

      This has never worked for me

  • @Semilore317
    @Semilore317 Год назад

    There are other ways like:
    Display: block;
    Margin: 0 auto;
    And
    Parent {
    Position : relative;
    }
    Child{
    Position: absolute;
    Top: 0;
    Left: o;
    Transform: translate (-50%,-50%)
    }

  • @theworldfacts520
    @theworldfacts520 Месяц назад

    Best way
    Margin-top: 10px;
    Margin-left:10px;
    😅😅😅😅😅😅😅

  • @melitajm4286
    @melitajm4286 11 месяцев назад

    Thanku😊

  • @geicothegecko8124
    @geicothegecko8124 Год назад

    justify content center saved me thanks programming jesus

  • @patahgaming
    @patahgaming Год назад

    Align-item center for no reason won't change 💀

  • @war_xs
    @war_xs Год назад

    Ty!!

  • @Aqmedk
    @Aqmedk Год назад

    thanks!

  • @CodingLearner-t4o
    @CodingLearner-t4o Год назад

    What about margin:auto;

  • @josephquintiliano2904
    @josephquintiliano2904 Год назад

    This only works when I include the height of the parent container (100vh) - but now my question is… why doesn’t 100% work, and how do I stay away from vh units, leaning towards percentages while accomplishing the same thing?

  • @sandhilt
    @sandhilt Год назад

    Position absolute and transform.

  • @techtipstricks2776
    @techtipstricks2776 Год назад

    thanks dear

  • @jakubschulz4353
    @jakubschulz4353 Год назад

    So what's wrong with margin: auto; or position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) and many other solutions?

    • @Robin270
      @Robin270 Год назад

      Technically nothing at all 😂 I find myself doing that from time to time, but the flex method replaced it in most of my codes. And I also guess it's easier. And ofc there are a few other solutions, and other than using a tag (which is has been outdated because of lost support from browser quite some time ago) and maybe a few other things, they all are working and up to you what you'll choose.

  • @aqumy
    @aqumy 7 месяцев назад

    that is not true. what if my div is position: fixed;? Then it ignores the outer flexbox container.

  • @KanxiPokhara
    @KanxiPokhara 7 месяцев назад

    What about display inline block

  • @devonchia5976
    @devonchia5976 Год назад +1

    What if my parent is the body(entire page) itself? What css should i add in order for this to work?

  • @indhurajc3814
    @indhurajc3814 Год назад

    Not working.,
    Bcz.,
    we must mention *height:100Vh* before align- items: center

  • @petra_bunic
    @petra_bunic Год назад +1

    Can we use it when we want to keep webpage centered while zooming out?

  • @Guy_Sandler
    @Guy_Sandler Год назад

    Im not good at css, is a center tag and position: relative, then lowering it 50% ok?

  • @with461
    @with461 Год назад

    You are from Italy?

  • @dnovus
    @dnovus Год назад

    what about calc() with hardcoded magic numbers?

  • @aswinp7323
    @aswinp7323 10 месяцев назад

    I thought it would be ask chatgpt

  • @urbangaming7334
    @urbangaming7334 Год назад +9

    Position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);

    • @aurorapaisley7453
      @aurorapaisley7453 Год назад

      this is the 'ol reliable and I still use when I feel icky using grid or flexbox out of nowhere

  • @Freprepp
    @Freprepp 26 дней назад

    what about text or span?

  • @Player-ux4ke
    @Player-ux4ke 9 месяцев назад

    After 1 hour of researching the only true answer was a YT short.... And all the answers from Google failed.

  • @deaddoomed2380
    @deaddoomed2380 Год назад

    I put alignt-items: center and nothing happened T.T

    • @semlon5266
      @semlon5266 11 месяцев назад +1

      The flex box must have height at 100% ofc

    • @deaddoomed2380
      @deaddoomed2380 11 месяцев назад

      @@semlon5266 thx man

  • @darknesskaneki
    @darknesskaneki Год назад

    what about ;-;

    • @chiggywiggy524
      @chiggywiggy524 Год назад

      Its no longer used. It's a very old html tag and html can remove It's support of old tags in future therefore it is not recommended to use center tag

  • @tsiolamo
    @tsiolamo 2 месяца назад

    Now this one it's outdated too😢

  • @KerenAywila
    @KerenAywila Год назад +2

    The element (div, body or article) must have a height for it to work.
    body{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    }

  • @hoomanramin
    @hoomanramin Год назад

    Height for parent is necessary

  • @ArewaMubarak
    @ArewaMubarak Год назад

    Please it not working

  • @wrongsyntax
    @wrongsyntax 2 года назад

    What if i have multiple Buttons inside parent and they need to be next to each other in center?

  • @Simping_is_Dead
    @Simping_is_Dead 2 года назад +5

    So sad… I still use margin: 0 auto 😂

    • @ogreeni
      @ogreeni 2 года назад +4

      That’s perfectly fine if you want to center a div horizontally. Better yet, use the logical property margin-inline: center;

  • @ruskibondes3509
    @ruskibondes3509 Год назад

    Thanks Jesus!

  • @DaniBee2900
    @DaniBee2900 Год назад

    daddy tho

  • @zapphoenixstudios2065
    @zapphoenixstudios2065 2 года назад

    So true

  • @toxicoxnt
    @toxicoxnt Год назад

    obg

  • @ISLAMIC_CHANNEL_0321
    @ISLAMIC_CHANNEL_0321 6 месяцев назад

    ✅✅

  • @yannry
    @yannry 11 месяцев назад

    didn't work

  • @KatDog-jl3fs
    @KatDog-jl3fs 11 месяцев назад

    this didn't work

  • @IceifritGaming
    @IceifritGaming Год назад

    Wtf is a div? 😆

  • @princeyoutuber1937
    @princeyoutuber1937 Год назад

    Glt

  • @Abir_Hossain111
    @Abir_Hossain111 Год назад

    Free Palestine 🇵🇸