css居中与布局

作者: fanison | 来源:发表于2019-02-22 20:07 被阅读18次

- 水平居中

1.行内元素:text-align:center
  <p class="cen">居中</p>     //html代码
  .cen{                      //css代码
  text-align:center;
  border:1px solid red;
  }
2.块级元素

让块级元素居中的方法就是设置 margin-leftmargin-rightauto
margin:0 auto

  <div class="first"></div>          //html
  .first{                            //css
        width:20px;
        height:20px;
        margin:0 auto;
        border:1px solid red;
  }

- 垂直居中

1.设置padding-top与padding-bottom相等

  <div class="first">first</div>      //html
  .first{                             //css
        width:200px;
        padding-top:20px;
        padding-bottom:20px;
        border:1px solid red;
  }

2.设置height与line-height相等

 <div class="first">first</div>      //html
  .first{                            //css
  width:100px;
  height: 50px;
  line-height: 50px;
  border:1px solid red;
  }

- 左右布局

通过float浮动实现,float:left与float:right

<div class="first">leftcontent</div>
<div class="second">rightcontent</div>
.first{                       
  width:100px;
  height: 100px;
  float:left;
  border:1px solid red;
}
.second{                       
  width:100px;
  height: 100px;
  float:right;
  border:1px solid green;
}

- 左中右布局

思路:使用div将左中左浮,右侧右浮

<div class="out">
    <div class="clearfix inner" style="float:left"> 
        <div class="first">left</div>
        <div class="first">middle</div>  
     </div>   
     <div class="third">right</div>
</div> 

.out{
    width:354px;
    height:100px;
    border:1px solid red;
}
.clearfix::after{
    content:'';
    display:block;
    clear:both;
}
.first{                       
  width:100px;
  height: 100px;
  float:left;
  border:1px solid red;
}
.second{                       
  width:100px;
  height: 100px;
  float:left;
  border:1px solid green;
  margin-left:25px;
}
.third{                       
  width:100px;
  height: 100px;
  float:right;
  border:1px solid green;
}

其他技巧
google关键字:
  • css shadow generator 生成阴影
  • css gradient generator 渐变背景
  • webpage free psd 免费psd文件
  • dribbble 网站
  • css tricks shape css形状代码
  • iconfont.cn 图标库
  • wallhaven 高清壁纸

相关文章

  • 页面布局居中问题

    css页面布局水平垂直居中问题 居中问题

  • html编程技巧

    字体外部描边 Css 基于flex布局的盒子上下居中 Css 基于flex布局的盒子左右居中 Css 基于flex...

  • CSS常用布局实现

    该系列用于整理记录常用的CSS布局实现。 CSS常用布局实现01-水平居中 CSS常用布局实现02-垂直居中 CS...

  • CSS布局(不完全)总结

    CSS布局(不完全)总结 实现水平居中布局的几种方式 方法一: 通过以下CSS代码实现水平居中布局 方法二: 通过...

  • web前端教程:CSS 布局十八般武艺都在这里了

    CSS布局 布局是CSS中一个重要部分,本文总结了CSS布局中的常用技巧,包括常用的水平居中、垂直居中方法,以及单...

  • CSS布局

    HTML CSS + DIV实现整体布局必备知识利用HTML和CSS实现常见的布局 单列布局 css 实现竖直居中...

  • CSS布局与居中

    这篇文章会介绍本人已学会的CSS中常用的左右布局、左中右布局、水平居中方法、垂直居中方法和一些CSS小技巧,如有错...

  • CSS 布局与居中

    一、常见布局 1. 浮动布局 可以通过盒模型的 float 属性实现浮动布局,使元素脱离文档流,浮动起来。(1)使...

  • css布局与居中

    默认情况下,元素是如何布局的? 独立元素布局默认的,一个块级元素的内容宽度是其父元素的100%,高度与其内容高度一...

  • CSS 布局与居中

    一、左右布局 1. inline-blockdisplay:inline-block属性是介于行级元素(displ...

网友评论

    本文标题:css居中与布局

    本文链接:https://www.haomeiwen.com/subject/uscyyqtx.html