Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I center the contents of a div that float: left?

Tags:

css

I found this article and I decided that I liked the way they styled links and buttons. So I took the CSS from the article...

.buttons a, .buttons button{
    display:block;
    float:left;
    margin:0 7px 0 0;
    background-color:#f5f5f5;
    border:1px solid #dedede;
    border-top:1px solid #eee;
    border-left:1px solid #eee;

    font-family:"Lucida Grande", Tahoma, Arial, Verdana, sans-serif;
    font-size:100%;
    line-height:130%;
    text-decoration:none;
    font-weight:bold;
    color:#565656;
    cursor:pointer;
    padding:5px 10px 6px 7px; /* Links */
}
.buttons button{
    width:auto;
    overflow:visible;
    padding:4px 10px 3px 7px; /* IE6 */
}
.buttons button[type]{
    padding:5px 10px 5px 7px; /* Firefox */
    line-height:17px; /* Safari */
}
*:first-child+html button[type]{
    padding:4px 10px 3px 7px; /* IE7 */
}
.buttons button img, .buttons a img{
    margin:0 3px -3px 0 !important;
    padding:0;
    border:none;
    width:16px;
    height:16px;
}

Then I have several buttons in a row I want to use like this...

<div class="buttons">
    <a href="/password/reset/"><img src="pict1.png" class="positive" alt=""/>Button 1</a>
    <a href="/password/reset/"><img src="pict2.png" alt=""/>Button 2</a>
    <a href="/password/reset/"><img src="pict3.png" class="negative" alt=""/>Button 3</a>
</div>

See this example: http://reljac.com/csstest.php

But that row of buttons may need to be aligned center, not all to the right or left. If I change the CSS to...

.buttons a, .buttons button{
    /*display:block;
    float:left;*/
    margin:0 7px 0 0;

The buttons no longer appear correctly when there is an image, specifically in IE 6,7 & 8.

See this example: http://reljac.com/csstest_wo.php

I can change the float to right to get the buttons to align right but I can't figure out what to do to get them centered (like in a <td></td>).

So the short of it is I want to use the style as it is but I also need to be able to center justify the buttons if necessary.

like image 417
Jason Avatar asked Jul 05 '09 19:07

Jason


People also ask

How do I center float left div?

You can set float div center by using margin property. you does not need to use float property but you can use margin property and set left or right margin value auto by this way.

How do you center an element with a float?

The CSS float property is used to set or return the horizontal alignment of elements. But this property allows an element to float only right or left side of the parent body with rest of the elements wrapped around it. There is no way to float center in CSS layout.

How do I center contents inside a div?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I make my div centered?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.


1 Answers

Try adding this to the CSS:

.buttons
{
    text-align:center;
    margin: 0px auto 0px auto;
}

The auto makes the margins equal on each side. The text-align is a bodge for older browsers.

EDIT:

Add an extra div around the buttons called buttonwrapper. then apply this CSS

.buttonwrapper
{
    position:relative;
    float:left;
    left:50%;
}

.buttons
{
    position:relative;
    float:left;
    left:-50%;
}

Method taken (but not tested) from http://www.pmob.co.uk/temp/centred-float4.htm

like image 106
Eric Avatar answered Sep 25 '22 06:09

Eric