Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a block of content when you don't know its width in advance

Tags:

html

css

After lots of attempts and search I have never found a satisfactory way to do it with CSS2.

A simple way to accomplish it is to wrap it into a handy <table> as shown in the sample below. Do you know how to do it avoiding table layouts and also avoiding quirky tricks?

table {
  margin: 0 auto;
}
<table>
  <tr>
    <td>test<br/>test</td>
  </tr>
</table>

What I want to know is how to do it without a fixed width and also being a block.

like image 677
Santiago Corredoira Avatar asked Sep 05 '08 17:09

Santiago Corredoira


People also ask

How do I center a div without the width?

Using the Position, Top, Left, and Transform Properties To horizontally and vertically center a div within a div whose height and width is unknown, you can use the transform property instead of the margin property. You'll still use the CSS position, top, and left properties.

How do you center a block item?

Centering a block or image The way to do that is to set the margins to 'auto'. This is normally used with a block of fixed width, because if the block itself is flexible, it will simply take up all the available width. Here is an example: P.blocktext { margin-left: auto; margin-right: auto; width: 8em } ...

How do I center my content align?

By using flex layout model: Set the display of the parent div to display: flex; and the you can align the child elements inside the div using the justify-content: center; (to align the items on main axis) and align-items: center; (to align the items on cross axis).

Which elements do we want to Centre a block of content or position?

The <center> HTML element is a block-level element that displays its block-level or inline contents centered horizontally within its containing element.


3 Answers

@Jason, yep, <center> works. Good times. I'll propose the following, though:

body {
  text-align: center;
}

.my-centered-content {
  margin: 0 auto; /* Centering */
  display: inline;
}
<div class="my-centered-content">
  <p>test</p>
  <p>test</p>
</div>

EDIT @Santi, a block-level element will fill the width of the parent container, so it will effectively be width:100% and the text will flow on the left, leaving you with useless markup and an uncentered element. You might want to try display: inline-block;. Firefox might complain, but it's right. Also, try adding a border: solid red 1px; to the CSS of the .my-centered-content DIV to see what's happening as you try these things out.

like image 161
Joe Liversedge Avatar answered Oct 05 '22 20:10

Joe Liversedge


This is going to be the lamest answer, but it works:

Use the deprecated <center> tag.

:P

I told you it would be lame. But, like I said, it works!

*shudder*

like image 35
Jason Bunting Avatar answered Oct 05 '22 19:10

Jason Bunting


I think that your example would work just as well if you used a <div> instead of a <table>. The only difference is that the text in the <table> is also centered. If you want that too, just add the text-align: center; rule.

Another thing to keep in mind is that the <div> will by default fill up all the available horizontal space. Put a border on it if you aren't sure where it starts and ends.

like image 20
Neall Avatar answered Oct 05 '22 20:10

Neall