Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7726533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:14:22+00:00 2026-06-01T05:14:22+00:00

Say you have the following CSS applied to a div tag .divtagABS { position:

  • 0

Say you have the following CSS applied to a div tag

.divtagABS {
    position: absolute;
    margin-left: auto;  
    margin-right: auto;
}

the margin-left and margin-right does not take effect

but if you have relative, it works fine
i.e.

.divtagREL {
    position: relative;
    margin-left: auto;  
    margin-right: auto;
}

Why is that? I just want to center an element.

Can someone explain why setting margins to auto in absolute position does not work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:14:24+00:00Added an answer on June 1, 2026 at 5:14 am

    EDIT : this answer used to claim that it isn’t possible to center an absolutely positioned element with margin: auto;, but this simply isn’t true. Because this is the most up-voted and accepted answer, I guessed I’d just change it to be correct.

    When you apply the following CSS to an element

    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    

    And then give the element a fixed width and height, such as 200px or 40%, the element will center itself.

    Here’s a Fiddle that demonstrates the effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say i have the following code: <div class=wrapper> <div class=title></div> <div></div> </div> <div
lets say i have a tree of the following structure: <div>node level1 <div>node level2
Let's say we have the following schema: root/ application/ -public/ index.php css/ img/ javascript/
Say I have the following css: .cls {} .cls ul {list-style-type:none;} .cls ul li
Say I have the following code: ... <div id=originalContent>Foo</div> ... How can I use
Say I have the following DIVs: <div id=top>Page header</div> <div id=main>Main content</div> <div id=sidebar>Sidebar
Say I have the following: <ul> <li><img src='test.png'> <div id=parentDiv> <div><img='click-me-icon.png'></div> </div> </li> </ul>
Let's say you have the following chunk of code: <div id=container> <someelement>This is any
Let's say you have the following HTML: <html> <div style=text-align: center;> <img src=http://www.ps.uci.edu/~tomba/ants/sugar.jpg width=50
I have the following HTML/CSS: #scrollpanel{height:100px;overflow-x:hidden;overflow-y:scroll;width:200px} <div id=scrollpanel> <div class=listing ref_1>...</div> <div class=listing ref_2>...</div>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.