CSS的两种经典布局

  • 左右布局

  • 一栏定宽,一栏自适应
    <!-- html -->
<div class="left">定宽</div>
<div class="right">自适应</div>
<!-- css -->
.left{
width: 200px;
height: 600px;
float: left;
display: table;
text-align: center;
line-height: 600px;
}
.right{
margin-left: 210px;
height: 600px;
background: yellow;
text-align: center;
line-height: 600px;
}

  • 利用绝对定位实现
    <!-- html -->
<div class= "left">
</div>
<div class= "right">
</div>
<!-- css-->
.left{
position:absolute;
left:0;
width:200px;
}
.right{
margin-left:200px;
}

  • 左中右布局

  • 利用绝对定位实现
    <!-- html-->
<div class= "left">
</div>
<div class= "main">
</div>
<div class= "right">
</div>
<!-- css-->
.left{
width:200px;
background-color:yellow;
position:absolute;
top:0;
left:0;
}
.main{
margin-left:200px;
margin-right:300px;
}
.right{
width:300px;
background-color:orange;
position:absolute;
top:0;
right:0;
}

  • 利用浮动定位实现
    <!-- html-->
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
<!-- css-->
.left{
width:300px;
background-color:yellow;
float:left;
}
.right{
width:200px;
background-color:orange;
float:right;
}
.main{
margin-left:300px;
margin-right:200px;
}

  • 圣杯布局,两边定宽,中间自适应
    <!-- html-->
<div class="container">
<div class="main col">Main</div>
<div class="left col">Left</div>
<div class="right col">Right</div>
</div>
<!-- css-->
.col{
float: left;
position:relative;
}
.container{
padding:0 200px 0 100px;
}
.left{
left:-100px;
width: 100px;
height:100%;
margin-left: -100%;
background: red;
}
.main{
width:100%;
height: 100%;
}
.right{
right:-200px;
width:200px;
height:100%;
margin-left: -200px;
background: yellow;
}

  • 双飞翼布局
    <!-- html-->
<div class="container">
<div class="left col ">Left</div>
<div class="main col ">
<div class="main_inner">Main</div>
</div>
<div class="right col ">Right</div>
</div>
<!-- css-->
.col{
float: left;
}
.main{
width:100%;
height:100%;
}
.main_inner{
margin:0 200px 0 100px;
}
.left{
width: 100px;
height: 100%;
margin-left: -100%;
background: red;
}
.right{
height:100%;
width:200px;
margin-left: -200px;
background: yellow;
}

CSS居中问题

  • 水平居中

  • 对于行内元素(inline):text-align: center;
    <!-- html -->
<div>
<span >kaka</span>
</div>
<!-- css -->
div {
text-align:center
}

  • 对于块级元素(block):
    1.给此块级元素设置宽度
    2.margin:0 auto;
    <!-- html -->
<div class="parent">
<div class="child">kaka</div>
</div>
<!-- css -->
.parent {
width:1002px;
}
.child {
width:50%;//也可以是固定像素
margin:0 auto;
}

  • 垂直居中

  • 行高与高度一致使其居中,适用于只有一行文字的情况
    <!-- html -->
<div class="parent">
<div class="child">kaka</div>
</div>
<!-- css -->
.parent {
height:1002px;
line-height:1002px;
}

  • 水平垂直均居中

  • 已知宽高,给负margin
    <!-- html -->
<div class="parent">
<div class="child">kaka</div>
</div>
<!-- css -->
.parent {
position: relative;
}
.child {
position: absolute;
width:1002px;
height:828px;
top: 50%;
left: 50%;
margin-top:-414px;
margin-left:-501px;
}

  • 未知宽高,transform方案
    <!-- html -->
<div class="parent">
<div class="child">kaka</div>
</div>
<!-- css -->
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

CSS的一些小技巧

  • 请写出「姓名」与「联系方式」两端对齐的例子
<!-- html -->
<span>姓名</span>
<span>联系方式</span>
<!-- css -->
span{
line-height:20px;
font-size:20px;
height:20px;
overflow:hidden;
}
span::after{
content: '';
display: inline-block;
width: 100%;
}
  • 文本内容过长如何变成省略号?
    1 一行文本过长,只需要对该div作以下操作:
<!-- css -->
div{
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
}
2 多行文本超出,如:在第二行后省略:
<!-- css -->
div{
display:-webkit-box;
-webkit-line-clamp:2;
-webkit-box-orient:vertical;
overflow:hidden;
}
  • 如何使固定高度的div里面的文字垂直居中?

1.先确定行高 2.再用padding补全高度。这种写法的好处是在文字增减过程中不会出现bug。
例:一个高 40px 的 div,里面的文字垂直居中

<!-- css -->
div{
line-height:20px;
padding:10px 0;
}
  • 使该元素变大1.2倍
    transform: scale(1.2);
  • 动画过渡效果
    transition: all 0.3s;

关于css布局、居中的问题以及一些小技巧的更多相关文章

  1. CSS布局——居中

    参考文章1.CSS布局奇淫技巧之--各种居中 2.http://www.imooc.com/article/2235 1.行内元素水平居中text-align:center对图片,按钮,文字等行内元素 ...

  2. css学习の第六弹—样式设置小技巧

    一.css样式设置小技巧>>1.行内元素水平居中是通过给父元素设置 text-align:center 来实现的.html代码:<body> <div class=&qu ...

  3. CSS布局---居中方法

    在web页面布局中居中是我们常遇到的情况,而居中分为水平居中与垂直居中 文本的居中 CSS中对文本的居中做的非常友好,我们是需要text-align, line-height 两个属性就可以控制文本的 ...

  4. CSS布局居中

    1.把margin设置为auto,此方法只能进行水平的居中,且对浮动元素或绝对定位元素无效.

  5. 【转】css布局居中和CSS内容居中区别和对应DIV CSS代码

    原文地址:http://www.divcss5.com/jiqiao/j771.shtml css布局居中和CSS内容居中区别和对应DIV CSS代码教程与图文代码案例篇 对于新手来说DIV CSS布 ...

  6. CSS中的一下小技巧2之CSS3动画勾选运用

    使用CSS3实现动画勾选 相信大家在项目中会经常遇到这种需求:勾选框.现在用CSS3来实现一个动画勾选,只需要一个标签即可完成: 这次需要用到CSS中伪类 after,这个小技巧也是很容易忘记的,所以 ...

  7. CSS 布局实例系列(一)总结CSS居中的多种方法

    使用 CSS 让页面元素居中可能是我们页面开发中最常见的拦路虎啦,接下来总结一下常见的几种居中方法吧. 1. 首先来聊聊水平居中: text-align 与 inline-block 的配合 就像这样 ...

  8. css布局之居中

    CSS布局之居中 本文主要是介绍水平居中,垂直居中,还有水平垂直居中的方法 水平居中 1.行内元素水平居中 使用text-align:center;就可以实现行内元素的水平居中,但是记得要在父元素中设 ...

  9. 主流 CSS 布局(水平居中、垂直居中、居中 )

    什么是布局 html 页面的整体结构或骨架 布局不是某个技术内容 而是一种设计思想 [ 布局方式 ] 水平居中布局 垂直居中布局 居中布局( 水平 + 垂直 ) 什么是水平居中布局 水平居中布局 元素 ...

随机推荐

  1. Qt:QListWidgetItem

    0.说明 一个QListWidgetItem是QListWidget中的一项(一行). 每个Item都可以持有多部分的信息,并将它们在适当时候展示出来. 在构造一个Item时指明它所在的List Wi ...

  2. Python:Scrapy(一) 基础知识与实例

    学习自: Scrapy爬虫框架教程(一)-- Scrapy入门 - 知乎 Scrapy爬虫框架,入门案例(非常详细)_ck784101777的博客-CSDN博客_scrapy爬虫案例 爬虫框架Scra ...

  3. websocket原理和基于c/c++实现的websocket协议栈(更新中)

    参考: 博客1:http://blog.sina.com.cn/s/blog_bf397e780102w25k.html https://www.cnblogs.com/barrywxx/p/7412 ...

  4. 解决 Page 'http://localhost:63342/v3/js/math/math.map' requested without authorization页面未授权问题

    用webstorm调试页面时,老是弹出对话框说页面未授权,如下图: 解决方法尝试了两种都有效,感觉第一种是正解通用,第二种大家也可以了解一下作为参考 第一种: File--Settings如下图 第二 ...

  5. oracel数据库ORA-28001: the password has expired

    调试c#项目时登录用户不成功ORA-28001: the password has expired错误 密码过期失效 网上查了一下,是Oracle11g密码过期的原因 Oracle提示错误消息ORA- ...

  6. List、Set、Map有什么异同(详解)

    引言:Java集合框架提供了一套性能优良.使用方便的接口和类,它们位于java.util包中 Java集合框架(常用接口):       Collection 接口存储一组不唯一,无序的对象(父类接口 ...

  7. Linux CentOS 7.X-关机、重启命令

    一.命令操作 1.退出命令 退出登陆命令:logout: 2.关闭命令 立即关机:shutdown -h now(root用户)    halt poweroff 延时关机:shutdown -h m ...

  8. Laravel 报错: Dotenv values containing spaces must be surrounded by quotes.

    报错信息如下: 原因: .env文件配置中欧冠包含空格的配置信息,用双引号""引起来即可

  9. egg-multipart + el-upload 实现带参图片上传至阿里云OSS

    egg-multipart有两种模式:file和stream el-upload参数传递有两种方式:利用自带参数data和手动添加参数 egg-multipart介绍 一.file 模式下的带参传递 ...

  10. 初探JVM字节码

    作者: LemonNan 原文地址: https://juejin.im/post/6885658003811827725 代码地址: https://github.com/LemonLmNan/By ...