• 技术文章 >web前端 >html教程

    css+div水平居中的实例代码

    零下一度零下一度2017-06-24 11:45:28原创977
    实现div内容水平居中

    实现方案一:margin:0 auto;

    div{
         height:100px;
         width:100px;
         background:red;
         margin:0 auto;
      }

      

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>div水平居中</title>
    	</head>
    	<body>
    		<div></div>
    	</body>
    </html>

      

    实现div水平居中方案二:position:absolute;绝对定位

    div{height:100px;width:100px;background:red;position:absolute;
       left:50%;
       right:50%;
       margin: auto;
    }

    实现div水平垂直居中

    实现方案一:position:fixed;固定定位

    div{height:100px;width:100px;background:red;position:fixed;left:0;top:0;bottom:0;right:0;margin:auto;
                }

    实现方案二:position:absolute;绝对定位

    div{height:100px;width:100px;background:red;position:absolute;left:0;top:0;bottom:0;right:0;margin:auto;
                }

    实现方案二:css3+position;

    div{height:100px;width:100px;background:red;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);
                }

    transform为css3的新增属性,因此需要加上前缀,兼容手机和其他的浏览器。如

    -ms-transform:translate(-50%,-50%); /* IE 9 */-moz-transform:translate(-50%,-50%); /* Firefox */-webkit-transform:translate(-50%,-50%);/* Safari and Chrome */-o-transform:translate(-50%,-50%); /* Opera */

    以上就是css+div水平居中的实例代码的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:谈一谈我对css的认识 下一篇:github上项目发布成静态网页是什么情况?
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• css+div盒模型研究笔记_html/css_WEB-ITnose• 知名网站按钮 css 实现(含源码)_html/css_WEB-ITnose• 企业网站需要有些什么?_html/css_WEB-ITnose• codeforces Round #260(div2) D解题报告_html/css_WEB-ITnose• html实现跳转_html/css_WEB-ITnose
    1/1

    PHP中文网