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

前端之家收集整理的这篇文章主要介绍了CSS:我不能在DIV元素的中间放一个按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用本教程中的CSS按钮:

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

我需要在DIV的中间放一个按钮,以便集中.但我不能!

这是按钮的代码

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

这里的CSS:

  1. .clear { /* generic container (i.e. div) for floating buttons */
  2. overflow: hidden;
  3. width: 100%;
  4. }
  5.  
  6. a.button {
  7. background: transparent url('bg_button_a.gif') no-repeat scroll top right;
  8. color: #444;
  9. display: block;
  10. float: left;
  11. font: normal 12px arial,sans-serif;
  12. height: 24px;
  13. margin-right: 6px;
  14. padding-right: 18px; /* sliding doors padding */
  15. text-decoration: none;
  16. }
  17.  
  18. a.button span {
  19. background: transparent url('bg_button_span.gif') no-repeat;
  20. display: block;
  21. line-height: 14px;
  22. padding: 5px 0 5px 18px;
  23. }

这是我试图使用的代码

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

解决方法

不推荐使用div元素的align属性.你最好为这个div定义一个类,就像这样:
  1. <div class="centerize">
  2. <a class="button" href="#"><span>Bring world peace</span></a>
  3. </div>

和CSS:

  1. .centerize {
  2. text-align: center;
  3. }

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

只要多一点确定,你可以这样做:

  1. .centerize {
  2. display: block;
  3. margin: 0 auto;
  4. text-align: center;
  5. }

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

猜你在找的CSS相关文章