# CSS - centering block types

**URL:** https://boards.straightdope.com/t/css-centering-block-types/625877
**Category:** Factual Questions
**Created:** [June 21, 2012, 7:27pm UTC](https://boards.straightdope.com/t/css-centering-block-types/625877 "2012-06-21T19:27:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![SlickRoenick](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@SlickRoenick](https://boards.straightdope.com/u/SlickRoenick)
#### Post date: [June 21, 2012, 7:27pm UTC](https://boards.straightdope.com/t/css-centering-block-types/625877/1 "2012-06-21T19:27:32Z")

</div>

I am not a web developer so hopefully i can explain this the right way. Let’s say i have a div that will have several links centered within it. The links visually look like “buttons” that are CSS boxes with a small background image 120x50 px. No matter where i enter align=“centered” whether in the div, the body, etc, the blocks always always always move to the far left! Here is the code:

HTML:

```auto

<div id="centercontent" align="center">
    <div id="maincontentcentered" align="center"><a href="#">link4</a> <a href="#">link5</a> <a href="#">link6link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a></div>
  </div>

```

CSS:

```auto

#centercontent {
	text-align: center;
	margin-right: auto;
	margin-left: auto;
}
#maincontentcentered {
	width: 500px;
	float: none;
	font-family: Arial, Helvetica, sans-serif;
	font-size: 18px;
}
#maincontentcentered a {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	font-weight: bold;
	text-decoration: none;
	background-image: url(../images/buttonbg2.gif);
	background-repeat: no-repeat;
	background-position: left top;
	float: left;
	height: 50px;
	width: 120px;
	display: block;
	line-height: 15px;
	margin: 2px;
	text-align: center;
	color: #002D62;
}
#maincontentcentered a:hover {
	background-image: url(../images/MainFrameButtons/m2.gif);
	background-repeat: no-repeat;
	background-position: left bottom;
	text-decoration: underline;
}

```

---

<div class="post-metadata">

### Author: ![Sparklo](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Sparklo](https://boards.straightdope.com/u/Sparklo)
#### Post date: [June 21, 2012, 7:50pm UTC](https://boards.straightdope.com/t/css-centering-block-types/625877/2 "2012-06-21T19:50:54Z")

</div>

In order for the margin-left: auto and margin-right: auto to work, the div needs to have a specified width. So it looks like you can either set #centercontent to a width of 500px or move the margin-left and margin-right stuff into #maincontentcentered.
