标题 | HTML对于元素水平垂直居中的探讨 |
内容 | 我们在设计页面的时候,经常要把DIV居中显示,而且是相对页面窗口水平和垂直方向居中显示,如让登录窗口居中显示。 到现在为止,探讨了很多种方法。 HTML: XML/HTML Code 1.<body> 2. <div> 3. <div></div> 4. </div> 5.</body> 6. 效果图(下面几种方法效果图一样): ![]() 第一种: CSS绝对定位 主要利用绝对定位,再用margin调整到中间位置。 父元素: CSS Code 1..maxbox{ 2. position: relative; 3. width: 500px; 4. height: 500px; 5. margin: 5px; 6. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); 7.} 8. 子元素: CSS Code 1..minbox{ 2. width: 200px; 3. height: 200px; 4. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); 5.} 水平垂直居中对齐: CSS Code 1..align-center{ 2. position: absolute; 3. left: 50%; 4. top: 50%; 5. margin-left: -100px; /*width/-2*/ 6. margin-top: -100px; /*height/-2*/ 7.} 第二种: CSS绝对定位 + Javascript/JQuery 主要利用绝对定位,再用Javascript/JQuery调整到中间位置。相比第一种方法,此方法提高了class的灵活性。 父元素: CSS Code 1..maxbox{ 2. position: relative; 3. width: 500px; 4. height: 500px; 5. margin: 5px; 6. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); 7.} 8. 子元素: CSS Code 1..minbox{ 2. width: 200px; 3. height: 200px; 4. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); 5.} 水平垂直居中对齐: CSS Code1..align-center{ 2. position: absolute; 3. left: 50%; 4. top: 50%; 5.} 6. JQuery: JavaScript Code 1.$(function(){ 2. $(".align-center").css( 3. { 4. "margin-left": ($(".align-center").width()/-2), 5. "margin-top": ($(".align-center").height()/-2) 6. } 7. ); 8.}); 9. 第三种: CSS3绝对定位 + 位移 使用绝对定位与CSS3的 transform: translate同样也可以达到效果。 父元素: CSS Code 1..maxbox{ 2. position: relative; 3. width: 500px; 4. height: 500px; 5. margin: 5px; 6. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); 7.} 8. 子元素: CSS Code 1..minbox{ 2. width: 200px; 3. height: 200px; 4. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); 5.} 6. 水平垂直居中对齐: CSS Code 1..align-center{ 2. position: absolute; 3. top: 50%; 4. left: 50%; 5. -webkit-transform: translate(-50%, -50%); 6. -moz-transform: translate(-50%, -50%); 7. transform: translate(-50%, -50%); /*向左向上位移*/ 8.} 9. 第四种: Flexbox: [伸缩布局盒模型] 要让元素水平垂直,对于Flexbox模型来说太容易了。 这里得改变一下HTML: XML/HTML Code 1.<div> 2. <div></div> 3.</div> 4. 父元素: CSS Code 1..maxbox{ 2. position: relative; 3. width: 500px; 4. height: 500px; 5. margin: 5px; 6. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); 7.} 8. 子元素: C# Code 1..minbox{ 2. width: 200px; 3. height: 200px; 4. box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); 5.} 水平垂直居中对齐: CSS Code 1..align-center{ 2. display: flex; 3. display: -webkit-flex; /*兼容问题*/ 4. justify-content: center; 5. align-items: center; 6.} 几种方法的比较: 第一种CSS绝对定位margin调整,兼容性很好但是欠缺灵活性。如果有很多box里需要水平垂直居中,因其width,height不同而需要写不同的 .align-center 。 第二种使用脚本语言,兼容性很好且弥补了第一种的缺点。不因width,height的改变而影响水平垂直居中的效果。 第三种使用CSS3的一些新的属性,兼容IE10, Chrome, Firefox, 和 Opera。兼容性不太很好,不因width,height的改变而影响水平垂直居中的效果。 使用Flexbox模型,兼容Firefox、Opera 和 Chrome,IE全军覆没。也是不因width,height的改变而影响水平垂直居中的效果。 以上就是本文的全部内容,希望对大家的学习有所帮助。 |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。