views:

240

answers:

4

Hi, How can I define a define float as center in css? actually i need a layout between right and left and also i tried "text-align" but it doesn't work and the "float" property just working.

Thank you

A: 

You can only float an element to the left or the right.

Maybe describe what kind of layout you are trying to achieve.

Palo Verde
+1  A: 

There is no float: center. but when you want a div to be centered to its parent this should do the trick:

#parent-element{
    position: relative;
}
#element
{
  width: 500px; 
  left: 50%;
  margin-left: -250px; /* - (width/2)  */
  position: absolute;
}

but, what exactly are you trying to achieve?

yopefonic
CSS like this is never a good idea
David Caunt
@dcaunt, I'd downvote your comment if I could. While this technique can lead to problems on small screens, sometimes it's exactly what's needed to solve a problem, especially with complex layered layouts. Yours is the more common solution, but yopefonic's isn't *wrong*.
Gabriel Hurley
What's the best use case for this over the text-align approach? My comment might have been overly harsh!
David Caunt
@dcaunt, no problem. I agree that it is not THE solution but as indicated the text-align was causing some problems.
yopefonic
+9  A: 

You want something like this:

<div style="width: 500px; text-align: center;">

    <div style="margin: 0 auto; width: 100px;">I am centered</div>

</div>

The key is text-align: center on the parent, and a margin: 0 auto on the inner element

David Caunt
the best way ^o^
Stephanie Luther
the most applicable and most common way.
Gabriel Hurley
A: 
Ali