CSS:我不能在DIV元素的中间放置一个按钮

时间:2022-03-05 19:33:23

I'm using CSS buttons from this tutorial:

我正在使用本教程中的CSS按钮:

http://www.oscaralexander.com/tutorials/how-to-make-sexy-buttons-with-css.html

I need to put a button in the middle of a DIV so it's centered. But I can't!

我需要在DIV中间放一个按钮,使其居中。但我不能!

Here's the code of the button:

这是按钮的代码:

<a class="button" href="#"><span>Bring world peace</span></a>  

And here's CSS:

这是CSS:

.clear { /* generic container (i.e. div) for floating buttons */
    overflow: hidden;
    width: 100%;
}

a.button {
    background: transparent url('bg_button_a.gif') no-repeat scroll top right;
    color: #444;
    display: block;
    float: left;
    font: normal 12px arial, sans-serif;
    height: 24px;
    margin-right: 6px;
    padding-right: 18px; /* sliding doors padding */
    text-decoration: none;
}

a.button span {
    background: transparent url('bg_button_span.gif') no-repeat;
    display: block;
    line-height: 14px;
    padding: 5px 0 5px 18px;
} 

Here's the code I'm trying to use:

这是我正在尝试使用的代码:

<div align="center"><a class="button" href="#"><span>Bring world peace</span></a></div>

3 个解决方案

#1


27  

the align attribute for the div element is deprecated. You're better off defining a class for that div, like so:

不推荐使用div元素的align属性。你最好为那个div定义一个类,如下所示:

<div class="centerize">
  <a class="button" href="#"><span>Bring world peace</span></a>
</div>

And the CSS:

而CSS:

.centerize { 
    text-align: center;
}

Note however that setting the text-align will only affect the content inside the div. The div itself (should be) a block element, and depending on where it sits in the document structure, may not be centered itself.

但请注意,设置text-align只会影响div内的内容。 div本身(应该是)一个块元素,并且取决于它在文档结构中的位置,可能不会居中。

Just to make a little more certain, you can do something like this:

为了更加确定,你可以这样做:

.centerize {
    display: block;
    margin: 0 auto;
    text-align: center;
}

Now you can apply centerize to any element, and that element should take up the entire browser's width and center-align its content.

现在,您可以将centerize应用于任何元素,并且该元素应占用整个浏览器的宽度并居中对齐其内容。

#2


3  

The a.button is floated to the left. You could try float: none; on that element. margin: 0 auto; is also useful for center-aligning elements.

a.button浮动到左边。你可以试试float:none;在那个元素上。保证金:0自动;对于居中对齐元素也很有用。

Does that help?

这有帮助吗?

#3


3  

Modify the button class for these properties:

修改这些属性的按钮类:

.button{
  margin-left:50%;
  margin-right:50%;
  position: relative;
}

And wrap your link in the div like this:

并将您的链接包装在div中,如下所示:

<div align="center">
  <a class="button" href="#"><span>Bring world peace</span></a>  
</div>

#1


27  

the align attribute for the div element is deprecated. You're better off defining a class for that div, like so:

不推荐使用div元素的align属性。你最好为那个div定义一个类,如下所示:

<div class="centerize">
  <a class="button" href="#"><span>Bring world peace</span></a>
</div>

And the CSS:

而CSS:

.centerize { 
    text-align: center;
}

Note however that setting the text-align will only affect the content inside the div. The div itself (should be) a block element, and depending on where it sits in the document structure, may not be centered itself.

但请注意,设置text-align只会影响div内的内容。 div本身(应该是)一个块元素,并且取决于它在文档结构中的位置,可能不会居中。

Just to make a little more certain, you can do something like this:

为了更加确定,你可以这样做:

.centerize {
    display: block;
    margin: 0 auto;
    text-align: center;
}

Now you can apply centerize to any element, and that element should take up the entire browser's width and center-align its content.

现在,您可以将centerize应用于任何元素,并且该元素应占用整个浏览器的宽度并居中对齐其内容。

#2


3  

The a.button is floated to the left. You could try float: none; on that element. margin: 0 auto; is also useful for center-aligning elements.

a.button浮动到左边。你可以试试float:none;在那个元素上。保证金:0自动;对于居中对齐元素也很有用。

Does that help?

这有帮助吗?

#3


3  

Modify the button class for these properties:

修改这些属性的按钮类:

.button{
  margin-left:50%;
  margin-right:50%;
  position: relative;
}

And wrap your link in the div like this:

并将您的链接包装在div中,如下所示:

<div align="center">
  <a class="button" href="#"><span>Bring world peace</span></a>  
</div>