Bootstrap元素水平居中方法

 时间:2024-10-12 14:12:08

1、打开WebStorm集成开发工具,新建‘test.html’网页,在网页的同级目录新建bootstrap文件夹,并在文件夹内放入bootstrap.min.css文件,版本3.7。

Bootstrap元素水平居中方法

2、在‘test.html’引入bootstrap.min.css文件,并在body标签内放一个div,代码如下:<!DOCTYPE html><稆糨孝汶;html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link ref="stylesheet" href="bootstrap/bootstrap.min.css"></head><body> <div style="width: 100px;height: 200px;background-color:red">hhhh</div></body></html>

Bootstrap元素水平居中方法

3、查看网页效果如下图,在整个页面左上角。

Bootstrap元素水平居中方法

4、修改‘test.html’代码如下:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link ref="stylesheet" href="bootstrap/bootstrap.min.css"></head><body> <div style="width: 100px;height: 200px;background-color:red;margin: 0 auto">hhhh</div></body></html>仅增加了一个样式margin: 0 auto

Bootstrap元素水平居中方法

5、再次刷新网页查看效果,div水平居中了

Bootstrap元素水平居中方法

6、修改代码,把div标签换乘button,代码如下:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link ref="stylesheet" href="bootstrap/bootstrap.min.css"></head><body> <button style="width: 100px;height: 200px;background-color:red;margin: 0 auto">hhhh</button></body></html>

Bootstrap元素水平居中方法

7、再次刷新网页查看效果,button回到了左上角

Bootstrap元素水平居中方法

8、修改代码如下:<!DOC哌囿亡噱TYPE html><html lang="en"><head> <meta ch锾攒揉敫arset="UTF-8"> <title>Title</title> <link ref="stylesheet" href="bootstrap/bootstrap.min.css"></head><body> <button style="width: 100px;height: 200px;background-color:red;margin: 0 auto;display: block;">hhhh</button></body></html>增加了display:block属性

Bootstrap元素水平居中方法

9、再次刷新网页查看效果,button水平居中了,这就是bootstrap元素水平居中的方法,注意文字水平居中使用text-align:center属性

Bootstrap元素水平居中方法
  • 怎么设置a标签的垂直居中
  • css如何实现背景图片等比例缩放
  • 怎么让div换行
  • HTML网页怎样获取input的值
  • PLSQL如何查看数据表的sql源码
  • 热门搜索
    关于春节的手抄报资料 小学安全知识手抄报 关于元旦节的手抄报 唐诗宋词手抄报 七年级英语手抄报 依法治校手抄报 古诗配画手抄报 无烟校园手抄报 初中小组手抄报 诚信手抄报花边