好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

css两端对齐之div+css布局实现2端对齐的4种方法总结

div+css布局实现2端对齐是我们网页排版中经常会使用到的,这篇 文章 将总结一下可以实现的方法:

ht ML 结构

实现demo里面的div通过Css进行2端对齐。

<div class="box">
 <div class="demo">
     <div>1</div>
     <div>2</div> 
     <div>3</div>
 </div>
</div>

1.m arg in负值的方式

该方法需要外面多嵌套一层来实现,通过元素的间距,做为中间层的mar gin 溢出值来实现

<style>
.box{
     width:300px ;m argin:auto;overflow:hidden;border:1px solid  # ddd;
}
.box .demo{
    margin-left:-10px;width:310px
}
.box .demo div{
     width:93.333px;/*(计算:(300-10*2)/3)*/
     float:left;
     margin-left:10px;
}
</style>

2.dis play :inline-block/text-align:justify方式

justify方式比较 简单 方便。只要一个简单元素做了声明,里面的元素就自动等间距两端对齐布局啦!根本无需计算每个列表元素间的margin间距,更 不用 去修改父容器的 宽 度。

注意 一点 :demo结构内元素必须存在【换行符】或【空格符】,否则直接连着写将不会 生效

<style>
.demo{
     margin:0;padding:0;
     text-align:justify;
     text-align-last:justify;/*解决IE的支持*/
     line-h ei ght:0;/*解决标准浏览器容器底部多余的空白*/
}
@media all and (- webkit -min-device-pixel-ratio:0){
  .demo{
     font- Size: 0;/*webk IT 清除元素中使用[换行符]或[空格符]后,最后元素多余的空白*/
  }
}
.demo :after {/*text-align-last:justify只有IE支持,标准浏览器需要使用 .demo:after  伪类 模拟类似效果*/
     display:inline-block;
     overflow:hidden;
     width:100%;
     height:0;
     content:'';
     vert ical -align:to p; /* opera 浏览器解决底部多余的空白*/
}
.demo div{
     width:20%;
     display:inline-block;
     text-align: center ;/*取消上层元素的影响*/
     text-align-last:center;
     font -s ize:12px;
}
</style>

3.css3 属性 space-between

该方法基于webkit内核的webapp开发和winphone IE10及以上,常用于移动端布局。

<style>
.demo{
    display:-webkit-box;
    display:-webkit-flex;
    display:-ms- flexbox ;
    display:flex;
    -webkit-box-pack:justify;
    -webkit-justify-content:space-between;
    -ms-flex-pack:justify;
    justify-content:space-between;
}

.demo div{
     width:30%; 
}
</style>

4.css3属性column-count

column属性是多列布局,使用column来实现两端对齐只需要设置模块的个数跟column的列数一致即可,推荐使用于移动端布局

<style>
.demo{
     -webkit-column-count:3;-moz-column-count:3;column-count:3;
     -webkit-column-gap:10px;-moz-column-gap:10px;column-gap:10px; 
}

.demo div{
     
}
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

总结

以上是 为你收集整理的 css两端对齐之div+css布局实现2端对齐的4种方法总结 全部内容,希望文章能够帮你解决 css两端对齐之div+css布局实现2端对齐的4种方法总结 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于css两端对齐之div+css布局实现2端对齐的4种方法总结的详细内容...

  阅读:21次