Center a div with unknown width – CSS trick

To center a div, the typical  margin: 0 auto;  css attribute requires a known width for it to work, either a percentage or a fixed number of pixels. I came across this StackOverflow answer the other day that solves this perfectly. Sadly it’s not even the accepted answer 😕

#wrapper {
   position: relative;
   left: 50%;
   float: left;
}
#page {
   position: relative;
   left: -50%;
   float: left;
}

Note that in css, position: relative;  means relative to the block’s “normal” position, so it’s an offset to its original position.

Percentage values for left are interpreted as the percentages of the parent width. In this case, page div should be wrapper‘s only child, so their widths would be the same, hence the left: -50% for page moves the block to the left by half of the width of itself, along with the 50% offset of its parent, it’s centered! 😋

340 thoughts on “Center a div with unknown width – CSS trick”

  1. One more thing. I do believe that there are several travel insurance web pages of dependable companies than enable you to enter a trip details and obtain you the insurance quotes. You can also purchase the particular international travel cover policy on-line by using the credit card. All you should do should be to enter your current travel details and you can understand the plans side-by-side. Only find the system that suits your financial budget and needs and use your credit card to buy the item. Travel insurance online is a good way to start looking for a reliable company to get international holiday insurance. Thanks for revealing your ideas.

  2. Thank you, I’ve just been searching for information about this subject for a long time and yours is the best I have came upon till now. However, what about the bottom line? Are you positive about the supply?

  3. An interesting dialogue is value comment. I feel that it is best to write extra on this topic, it may not be a taboo topic however usually persons are not enough to talk on such topics. To the next. Cheers

  4. Excellent submit.. video youtube terfavorit I used to be reviewing continuously this website and I am influenced! Very useful details particularly the shutting down element 🙂 I contend with such information lots. I was in search of this type of data for a quite a while. Appreciate it and also connected with good luck.

  5. Do you mind if I quote a few of your articles as long as I provide
    credit and sources back to your site? My blog is in the exact same niche as
    yours and my visitors would definitely benefit from a lot of the information you provide here
    Center a div with unknown width – CSS trick – Bill Yu .
    Please let me know if this ok with you Center a div with unknown width –
    CSS trick – Bill Yu . Appreciate it!

  6. Dear you are comprehensive in your thoughts. I have made something for guys to find their life partner. Please check my blog and share with people who need this guide.

  7. I think I set the settings wrongly. Now that I changed the settings, there’s only 1 out of 8 posts visible to the public. Previously set to mature content, despite the lack of nudity. Should I double-post? It’s the blog part that confused me..

  8. Just desire to say your article is as astounding. The clarity to your post is just great
    and that i can assume you’re a professional in this subject.
    Fine along with your permission let me to take hold
    of your RSS feed to stay updated with imminent post. Thank you a million and please carry on the
    gratifying work.

  9. Hello there! This article couldn’t be written any better!

    Looking at this article reminds me of my previous roommate!
    He continually kept talking about this. I’ll forward this article
    to him. Fairly certain he will have a very good read. Thank
    you for sharing!

  10. Hi, I think your blog might be having browser compatibility issues.
    When I look at your website in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up! Other then that, wonderful blog!

  11. Oh my goodness! Amazing article dude! Many thanks, However
    I am going through troubles with your RSS. I don’t understand the reason why I cannot join it.
    Is there anybody else getting identical RSS issues? Anyone who knows the solution can you kindly respond?

    Thanks!!

  12. Good post. I learn something totally new and challenging on websites I stumbleupon every day.
    It will always be exciting to read through articles from other writers and practice something from
    other web sites.

  13. This is the right webpage for everyone who wants to understand this topic.
    You understand so much its almost hard to argue with you (not that
    I personally would want to…HaHa). You certainly put a brand new spin on a topic that’s
    been written about for many years. Excellent stuff, just wonderful!

  14. Hi there would you mind letting me know which web host you’re
    utilizing? I’ve loaded your blog in 3 different browsers and I must say this
    blog loads a lot quicker then most. Can you suggest
    a good hosting provider at a honest price? Kudos, I appreciate it!

  15. I just like the valuable information you provide to your articles.
    I will bookmark your blog and test once more here frequently.

    I am somewhat certain I’ll be informed many new stuff proper right here!
    Best of luck for the next!

  16. Hey there! Someone in my Myspace group shared this
    website with us so I came to check it out. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers!
    Exceptional blog and wonderful style and design.

  17. Greetings from Carolina! I’m bored at work so I decided to browse your site on my iphone during lunch break.
    I enjoy the info you present here and can’t wait to take a look when I get home.
    I’m amazed at how fast your blog loaded on my phone .. I’m
    not even using WIFI, just 3G .. Anyways, superb blog!

  18. I’m looking to spike my journalistic career and thought that a blog might be a good idea. But I also know that there are ways to set up a paypal account attached to the blog for payment to read it or donate. I guess I was inspired by the movie Julia and Julie and I want to do it on my own. Any ideas on how to get started?.

  19. My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.

    But he’s tryiong none the less. I’ve been using WordPress on several websites
    for about a year and am concerned about switching to another platform.
    I have heard great things about blogengine.net. Is there a way I can transfer all my wordpress posts into it?
    Any help would be really appreciated!

  20. What’s Taking place i am new to this, I stumbled upon this I have discovered
    It absolutely useful and it has helped me out loads.
    I’m hoping to contribute & help other customers like its helped me.
    Good job.

  21. Hi there would you mind letting me know which web host you’re working with?
    I’ve loaded your blog in 3 completely different internet browsers and I must
    say this blog loads a lot quicker then most. Can you recommend a good web hosting
    provider at a reasonable price? Many thanks, I appreciate it!

  22. I do agree with all the ideas you have presented to your post.
    They are very convincing and will definitely work.
    Nonetheless, the posts are very brief for starters. May you please extend them a little from next time?
    Thank you for the post.

  23. Howdy! I know this is sort of off-topic however I had to ask.
    Does managing a well-established website such as yours take a large amount of
    work? I am completely new to operating a blog however I do write in my journal everyday.
    I’d like to start a blog so I can share my personal experience and feelings online.
    Please let me know if you have any ideas or tips for new aspiring
    blog owners. Thankyou!

  24. I’m now not positive the place you are getting your information, however good topic.
    I needs to spend some time learning much more or working out more.
    Thanks for great info I used to be looking for this information for my mission.

  25. Hey just wanted to give you a quick heads up.
    The words in your content seem to be running off the screen in Internet explorer.
    I’m not sure if this is a format issue or something to do with internet browser
    compatibility but I figured I’d post to let you know.
    The style and design look great though! Hope you get the problem fixed soon. Kudos

  26. Today, I went to the beach front with my kids. I found a sea shell and gave it to my 4 year old
    daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear
    and screamed. There was a hermit crab inside and it pinched
    her ear. She never wants to go back! LoL I know this is completely
    off topic but I had to tell someone!

  27. 激光是單一波段的光能 , 在同一時間發射 , 光線能量較為集中 , 能深入皮膚打擊患處之異常色素或血管 , 溫和地刺激皮膚內的骨膠原更生, 新生的骨膠原令您的膚質光澤柔嫩 , 煥然一新 , 抗老效果顯著。有效治療色斑(雀斑 , 太陽斑 , 咖啡斑 , 太田痣 , 紋身) , 血管問題 (葡萄色斑 , 微絲血管擴張) , 膚色不均 , 皮膚鬆弛 , 毛孔粗大 治療後皮膚會出現短暫的紅腫 , 要注意防曬及遵照醫生指示護理皮膚

  28. I just want to mention I am just newbie to weblog and seriously liked your web site. Most likely I’m going to bookmark your site . You definitely come with awesome article content. Appreciate it for sharing your website page.

  29. Learning graphic editing software is even pricier. Chalkboard is a popular and always interesting graphic style that is often used in food and drink industry, for apparel, even company branding. There are many kinds of a chalkboard art – vintage, modern, grunge, black/white, color chalk, retro-hipster, art deco, etc. Any chalkboard style is interesting and attracting if done right – but don’t get your chalk artwork from someone who will butcher it, use cheap fonts and make it look cheezy. There are many online portals, forums and galleries dedicated to this “art phenomenon” that came back some 10 years ago and is here to stay for a long haul. You can also check many pinterest tags, websites like chalkboardstyle.com, etc. It’s easy to find good chalkboard artwork online. There are even sellers on Fiverr who will create you amazing chalk artwork for a price of a beer or two. You don’t have to learn photoshop and other graphic programs just to design your own chalkboard menus and flyers. But if you’re into DIY, there are also many chalkboard kits, sets and premade templates that will make your life much easier:) Happy Chalkboarding!

  30. CLINIQUE 倩碧線上購物官網。瀏覽Clinique倩碧官方網站,了解更多線上購物、護膚、彩粧、香氛及禮品詳情。通過過敏性測試,百分百不含香料。

  31. Do you mind if I quote a couple of your articles as long as I provide credit and sources back to youjr weblog?
    My blog is in the very samme niche aas yours and my users woulod definitely bnefit from a
    lot of the information you provide here. Please let me
    know if this okay with you. Regards!

  32. I spent four years trying EVERYTHING in Online Dating, and through a huge amount of trial and error, I produced a system that I will share for you. This book will take you, step by step, through everything you need to know to double, triple or even quadruple the number of women you meet online.

  33. Undeniably consider that that you stated. Your favorite reason appeared to be on the
    web the easiest thing to have in mind of. I say to you,
    I definitely get irked while people consider worries that they
    plainly don’t understand about. You managed to hit the nail
    upon the top and also outlined out the whole thing without having side-effects , folks can take a signal.

    Will probably be back to get more. Thank you

  34. Howdy! I simply would like to give you a huge thumbs up
    for your great info you have got here on this post.

    I am coming back to your web site for more soon.

  35. Howdy would you mind stating which blog platform you’re working with?
    I’m going to start my own blog in the near future
    but I’m having a hard time making a decision between BlogEngine/Wordpress/B2evolution and
    Drupal. The reason I ask is because your design and style seems different then most blogs and
    I’m looking for something unique. P.S My apologies
    for being off-topic but I had to ask!

  36. Hello my family member! I want to say that this post is awesome, nice written and come
    with almost all vital infos. I’d like to see extra posts like this .

  37. Do you mind if I quote a couple of your posts as long as I provide credit and
    sources back to yur weblog? My blog is in the exact same niche as yours and my users would certainly benefit from some of
    the information you present here. Please let
    me know if this alright with you. Thank you!

  38. Have you ever thought about publishing an ebook or guest authoring on other blogs?
    I have a blog based on the same ideas you discuss and would really like to have
    you share some stories/information. I know my subscribers
    would value your work. If you are even remotely interested, feel
    free to send me an e-mail.

  39. My brother recommended I might like this website.

    He was totally right. This post actually made my day.

    You cann’t imagine simply how much time I had spent for this information! Thanks!

  40. The other day, while I was at work,my sister stole my apple ipad
    and tested to seee if it can survive a thirty foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views.
    I know this is completely off topic but I had tto sharee it with someone!

  41. Greetings! Quick question that’s entirely off topic.
    Do you know how to make your site mobile friendly? My weblog looks
    weird when viewing from my apple iphone. I’m trying to
    find a theme or plugin that might be able to correct this issue.
    If you have any recommendations, please share. Many thanks!

  42. A love horoscope may be just what is needed to help someone get through their love love issues. When a person is dating people or in a relationship that is current, a special horoscope designed in the area of love may be able to help. It could provide the information that is needed to make hard choices and decisions. Some people could get the clues they need to either move on or settle down with the person they are with.

  43. That is really interesting, You are an overly skilled blogger.

    I’ve joined your feed and stay up for in search of extra
    of your excellent post. Also, I’ve shared your site in my social networks

  44. 我們彩用國際及美國食品及藥物管理局FDA認可的CO2 激光儀 Lutronics® Spectra SPR, 具安全性, 準確度高 . 二氧化碳激光可安全地去除皮膚上的癦痣、肉粒、疣、老人斑等問題。此激光的幼細光束可準確及直接地將要去除的組織氧化,過程快捷,傷口細小及乾淨,對周圍的皮膚傷害減至最少。一般1-2次就可永久去除。

  45. Cool one! Amazing informations over here. It is pretty worth enough for me. In my opinion, if all site owners and bloggers made good content as you did, the Inet will be much more helpful than ever before. I couldn’t refrain from commenting. I have spent 3 hours looking for such tips. I will also share it with some friends interested in it. I’ve just bookmarked this website. Now with the search done, I going to find some free online blonde cams. Thanks!! Greetings from Clovis!

  46. Thanks on your marvelous posting! I seriously enjoyed reading it,
    you happen to bbe a great author. I wil always bookmark your blog and will ofren come back
    at some point. I want to encourage you to definitely continue your great writing, havge a nice holiday weekend!

  47. With havin so much content do you ever run into any
    problems of plagorism or copyright infringement? My
    website has a lot of exclusive content I’ve either written myself
    or outsourced but it looks like a lot of it is popping it
    up all over the internet without my agreement.
    Do you know any techniques to help stop content
    from being ripped off? I’d definitely appreciate it.

  48. Simply want to say your article is as amazing.
    The clarity in your post is simply great and i can assume you’re an expert on this subject.
    Well with your permission let me to grab your
    feed to keep updated with forthcoming post. Thanks a million and please continue the rewarding
    work.

Leave a Reply

Your email address will not be published. Required fields are marked *