Can not put a div in center (margin:0 auto; doesn't work)

KingkongP

I want to put a div in center (horizontally). At the same time, I want to put that div margin-top:40px and margin-bottom:20px. But once I assigned margin:0 auto, margin-top and bottom does not work.

#circle_price {
  background-color: red;
  width: 100px;
  height: 100px;
  background: red;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  margin-top: 40px;
  margin-bottom: 20px;
  margin: 0 auto;
}

Anyone has an idea about this? Thanks.

m4n0

margin: 0 auto which is equal to margin: 0 auto 0 auto will override your earlier set properties i.e. margin-top and margin-bottom is reset to 0.

You need to use margin: 40px auto 20px auto which is short hand for margin-top margin-right margin-bottom and margin-left in clockwise order.

.center-me {
  margin: 40px auto 20px auto;
  width: 300px;
  background: lightblue;
}
<div class="center-me">Centered block</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Div doesn't align to center with margin:auto

From Dev

div won't center with margin: 0 auto;

From Dev

div won't center with margin: 0 auto;

From Dev

Why doesn't footer center with margin:0 auto?

From Dev

Why doesn't footer center with margin:0 auto?

From Dev

Can't center a fixed-positioned flexbox with margin: 0 auto

From Dev

margin:auto doesn't seem to work, can't figure it out

From Dev

Center div using margin 0 auto

From Dev

Why doesn't margin: auto 0px; vertically center a block as margin: 0px auto; horizontally centers it?

From Dev

Automatic margin doesn't center elements in div

From Dev

How do I get my divs to align at the center if margin:auto doesn't work?

From Dev

How do I get my divs to align at the center if margin:auto doesn't work?

From Dev

Why doesn't "margin: auto" center an element vertically?

From Dev

margin:0 auto; does not work

From Dev

`margin:auto;` doesn't work on inline-block elements

From Dev

margin 0 auto centers an element, why doesn't margin 1 auto do the same?

From Dev

float center or margin: 0 auto; some problems

From Dev

BootStrap CSS margin 0 auto not center

From Dev

float center or margin: 0 auto; some problems

From Dev

Margin: 0px auto; will not center element

From Dev

Center floating div with margin auto not working

From Dev

Auto-scroll on div doesn't work

From Dev

Why the margin on css doesn't work properly for div

From Dev

why does't my title center when I use margin: 0 auto?

From Dev

Why won't my image center when using 'margin: 0 auto'

From Dev

css margin:0 auto does not work in firefox

From Dev

Margin 0 auto wont work as intended

From Dev

css) margin doesn't work

From Dev

Margin 0 auto to a span within div not working

Related Related

  1. 1

    Div doesn't align to center with margin:auto

  2. 2

    div won't center with margin: 0 auto;

  3. 3

    div won't center with margin: 0 auto;

  4. 4

    Why doesn't footer center with margin:0 auto?

  5. 5

    Why doesn't footer center with margin:0 auto?

  6. 6

    Can't center a fixed-positioned flexbox with margin: 0 auto

  7. 7

    margin:auto doesn't seem to work, can't figure it out

  8. 8

    Center div using margin 0 auto

  9. 9

    Why doesn't margin: auto 0px; vertically center a block as margin: 0px auto; horizontally centers it?

  10. 10

    Automatic margin doesn't center elements in div

  11. 11

    How do I get my divs to align at the center if margin:auto doesn't work?

  12. 12

    How do I get my divs to align at the center if margin:auto doesn't work?

  13. 13

    Why doesn't "margin: auto" center an element vertically?

  14. 14

    margin:0 auto; does not work

  15. 15

    `margin:auto;` doesn't work on inline-block elements

  16. 16

    margin 0 auto centers an element, why doesn't margin 1 auto do the same?

  17. 17

    float center or margin: 0 auto; some problems

  18. 18

    BootStrap CSS margin 0 auto not center

  19. 19

    float center or margin: 0 auto; some problems

  20. 20

    Margin: 0px auto; will not center element

  21. 21

    Center floating div with margin auto not working

  22. 22

    Auto-scroll on div doesn't work

  23. 23

    Why the margin on css doesn't work properly for div

  24. 24

    why does't my title center when I use margin: 0 auto?

  25. 25

    Why won't my image center when using 'margin: 0 auto'

  26. 26

    css margin:0 auto does not work in firefox

  27. 27

    Margin 0 auto wont work as intended

  28. 28

    css) margin doesn't work

  29. 29

    Margin 0 auto to a span within div not working

HotTag

Archive