1.css实现居中
缺点:需要提前知道元素的宽度和高度。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .box{ width: 600px; height: 400px; position: absolute; left: 50%; top: 50%; border:1px solid #000; background:red; margin-top: -200px; /* 高度的一半 */ margin-left: -300px; /* 宽度的一半 */ } </style> </head> <body> <div class="box"> </div> </body> </html>
2、css3实现水平垂直居中
注意:无论元素的尺寸是多少,都可以居中。不过IE8以上才兼容这种写法,移动端可忽略。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .box{ width: 600px; height: 400px; position: absolute; left: 50%; top: 50%; border:1px solid #000; background:red; transform: translate(-50%, -50%); /* 50%为自身尺寸的一半 */ } </style> </head> <body> <div class="box"> </div> </body> </html>
3、margin:auto实现居中
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .box{ width: 600px; height: 400px; position: absolute; left: 0; top: 0; right: 0; bottom: 0; border:1px solid #000; background:red; margin: auto; /* 有了这个就自动居中了 */ } </style> </head> <body> <div class="box"> </div> </body> </html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
什么是.ART? 如果有个网址egg.COM,我们看到了脑海里大概率会出现养鸡...
背景 本篇收录于 《进阶不完全指南》 专栏 按照以往的系列文章的惯例, 第一篇一...
在ie下设置 css 样式 style="cursor:hand;" 可以正常显示 但是在firefox下就不行...
热门版本类型推荐 下载地址 Dreamweaver MX 2004 官方简体中文专业版 点击进入下...
先看页面效果。下面四个按钮分别表示开始、结束、暂停、继续 下面是帧动画图片素...
从后台做到前端,悲剧...
浏览器中input输入光标位置错位详解 在工作中遇到的问题,尝试着找找解决方法,...
首先看代码和效果 style .main { outline: 1px solid; display: flex; justify-c...
实现思路 最外面是一个大圆(渐变色) 内部里面绘制两个半圆,将渐变的圆遮住(为...
大家好,我是卡颂。 当聊到 Chrome ,你第一反应是啥? 市占率第一的浏览器?鼎...