CSS Related question

  • Thread starter Thread starter Barry
  • Start date Start date
Barry said:
How do i center-align a items within a <div></div> tag

If the item is a block element, on the item use:

margin-left: auto;
margin-right: auto;

If the item is an inline element, on the div tag use:

text-align: center;
 
if i use

<div style="margin-left: auto;margin-right: auto;text-align: center;">
gridview is here</div>

the grid view within the div is centered alright, but all the Text in the
columns is also centered, which i need left-aligned (by default)
 
Barry said:
if i use

<div style="margin-left: auto;margin-right: auto;text-align: center;">
gridview is here</div>

the grid view within the div is centered alright,

That is only because you are looking at it in Internet Explorer, which
applies text-align to block elements although it shouldn't. If you look
at it in a more standards compliant browser, the grid isn't centered.

You applied the margin to the div, not to the item as I said. However,
as the div doesn't have a specific size, the margin will still be zero.
If you want to center a block, it has to have a specific width.
but all the Text in the
columns is also centered, which i need left-aligned (by default)

That's because you tried to use both methods. An element can't be both a
block element and an inline element.
 
Back
Top