1

My question is simple, how can I center these two elements using the css "button" class dynamically? -> http://jsfiddle.net/MyVN4/ (I don't want to specify the width of the elements)

.button {
   background-color: black;
   border-radius: 10px;
   color: white;
   display: inline-block;
   padding: 10px;
 }

I've been looking other similar questions but none of the solutions work for this problem.

Thanks in advance for your help!

DreaMTT
  • 341
  • 2
  • 4
  • 11

2 Answers2

4

put text-align: center; on its parent (whatever its parent will be).

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
Esser
  • 540
  • 1
  • 4
  • 16
3

Given that they are inline elements, just set text-align:center on a parent element, in this case, the body element:

UPDATED EXAMPLE HERE

#parent_element {
    text-align:center;
}

You usually only have to set a width on the element when it is a block level element and will be unaffected by text-align:center, (it would need margin:0 auto). Using text-align:center is probably the best method for centering, even if you have to change the display of an element to inline-block. Just be aware of the fact that inline elements respect whitespace in the markup.

Community
  • 1
  • 1
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304