Give it a width (less than 100%), and set its margin(s) to auto
:
article {
width: 800px;
margin: auto;
}
By default the <article>
takes up all available horizontal space of its containing element. This means it's just as wide as the parent (generally speaking). In order to center it, we'll have to reduce its width so that it's less than the containing element. In the example above, I went with 800px
, but you could just as easily use an em
, rem
, or %
value.
By setting the margin
to auto
we ask the browser to determine an equal set of margin values for the sides of the element. The browser, in turn, determines what is needed to place the element in the middle of the containing element.
You might even consider giving it different styles for different widths:
/* When the viewport is 600px+ wide, article is 500px */
@media ( min-width: 600px ) {
article {
width: 500px;
margin: auto;
}
}
/* When the viewport is 800px+ wide, article is 600px */
@media ( min-width: 800px ) {
article {
width: 600px;
}
}