当前位置:Gxlcms > html代码 > button按钮居中的方法

button按钮居中的方法

时间:2021-07-01 10:21:17 帮助过:16人阅读

今天在写页面时,发现给button按钮设置居中时,css页面写了text-align="center",但是不起作用,用了display属性也无作用,试了好多次发现要给button按钮添加个p,然后让p居中就可以了。以下写个test来说明下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <style type="text/css">
  7. p,input,button{
  8. text-align: center;
  9.   }
  10. </style>
  11. </head>
  12. <body>
  13. <p>你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊</p>
  14. <input type="button" value="点我握手哦"/><br/>
  15. <button type="button">点我呀</button>
  16. </body>
  17. </html>

页面效果(p标签居中了,但是按钮不居中,创建按钮可以用input和button标签):

button按钮居中的方法

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <style type="text/css">
  7. /*p,input,button{
  8. text-align: center;
  9. }*/
  10. p{
  11. text-align: center;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <p>
  17. <p>你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊你好啊</p>
  18. <input type="button" value="点我握手哦"/><br/>
  19. <button type="button">点我呀</button>
  20. </p>
  21. </body>
  22. </html>

页面效果:

button按钮居中的方法

第一次写博客,也是前端新手,如有不当之处,欢迎指正,谢谢

更新下:因为下面第一条评论的小伙伴的指教,再加点,text-align: 属性规定元素中的文本的水平对齐方式, 该属性通过指定行框与哪个点对齐,从而设置块级元素内文本的水平对齐方式,对于input/button/img等标签也可以设置display:block属性使其变成块级元素,再加上width,margin:0 auto;来实现居中。

以上就是button按钮居中的方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行