0

In a very simple webpage, I would like to create a div containing a sequence of tweets, with the code from Twitter.

This div should be horizontally centered in the page; I tried with

<div style="text-align:center">

and with

<div style="width: 900px;  display: block;  margin-left: auto;  margin-right: auto;">

as in this post. The first solution does not center the div; the second moves the div progressively to the left of the page, when the width is increased (and the div is centered with low widths, say 200, 300).

Are there any other solutions? If possible without specifying a fixed width or percentage.

Community
  • 1
  • 1
BowPark
  • 1,340
  • 2
  • 21
  • 31

2 Answers2

0

The best way to center a div horizontally is to use margin: auto like you did. But you have to make sure the parent div is larger than the child. It will be centered in the parent.

<div style="width: 1000px;">
  <div style="width: 500px; margin: 0 auto">I'm align in the middle of my parent</div>
</div>
alexmngn
  • 9,107
  • 19
  • 70
  • 130
0

margin: 0 auto;

Is what you need as long as the parent stretches the whole width, apply this to the style of that div, or use it as in a class.

Zach Leighton
  • 1,939
  • 14
  • 24