饥人谷技术博客

css杂记

2018-11-05  本文已影响1人  simon_李玉兵

css的引入方式

嵌入式,内联样式style标签、外部css文件link、在a.css样式中引入b.css的样式: @import url(./b.css)

块级元素高度

由其内部文档流元素的高度总和决定的。

行内块级元素的高度

line-height可以确定行内块级元素的高度。

文档流

文档内元素的流动方式。内联元素从左往右流,如果宽度不够就换行。每个块级元素独占一行,从上往下。

脱离文档流

position:absolute position:fixed float都能使元素脱离文档流。

清除浮动

.clearfix::after{
  content:'';
  display:block;
  clear:both;
}

box-sizing的content-box和border-box区别

content-box 的 width 不包括 padding 和 border
border-box 的 width 包括 padding 和 border

非空标签才有伪类,伪类就是::before ::after

css居中的多种方式

  1. flex布局实现垂直居中。推荐flex阮一峰
  2. grid布局实现居中。(兼容还不太好)
  3. display:table-cell实现垂直居中。使用到vertical-align: middle

vertical-align只对行级元素display:table-cell生效。行级元素包括inline-block.

vertical-align && display:table-cell

//html
<div class="father">
  <div class="container2">
     <span class="child2">
       hello
     </span>
  </div>
</div>

//css

.father{
  display:table;
  width:200px;
  height:200px;
  margin-top:20px;
  border:1px solid red;
  text-align:center;
}
.container2{
  display:table-cell;
  vertical-align:middle;
}
.child2{
  display:inline-block;
  padding:10px;
  border:1px solid;
  box-shadow:0 0 2px 2px rgba(0,0,0,0.2);
}

//html
<div class="container">
  hello
</div>

//css

.container{
  width:200px;
  height:200px;
  border:1px solid red;
  text-align:center;
}
.container::before {
  content: '';
  display: inline-block;
  width: 0;
  height: 100%;
  vertical-align: middle;
}
  1. 使用绝对定位。
.container {
    position: relative;
}
.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
  1. 简单的居中


    1.png

加上下padding,就可以实现垂直居中;

  1. 使用line-height实现垂直居中。text-align:center实现水平居中。

左边固定宽度,右侧自适应

position:fixed

//html
<div class="left"></div>
<div class="right">
  <span>hello</span>
</div>

/*css*/
body{
  margin:0;
}
.left{
  width:200px;
  background:#ccc;
  height:100%;
  position:fixed;
}
.right{
  background:#f06060;
  margin-left:200px;
  height:1500px;
}
span{
  display:block;
  border:1px solid;
}

利用float

//html
<div class="father"></div>
<div class="mother">
  <div class="child"></div>
</div>
//css
.father{
  width:100px;
  height:500px;
  border:1px solid;
  float:left;
  margin-right:1px;
}
.mother{
  height:500px;
  overflow:hidden; 
  background-color:#ccc;
}
.child{
  width:100px;
  height:100px;
  background-color: #f06060;
}

table方式

//html
<div class="pather">
  <div class="left"></div>
  <div class="right"></div>
</div>

//css
.pather{
  display:table;
  width: 100%;
  table-layout: fixed;
  background-color: #333;
  border:1px solid;
  background-color: #fff;
}
.left,.right{
   display: table-cell;
}
.left{
    width: 100px;
    margin-right: 20px;
    height:500px;
    background-color: #000;
}
.right{
  background-color: #f06060;
}

flex方式

//html
<div class="pather">
 <div class="left"></div>
 <div class="right"></div>
</div>

//css
.pather{
  display:flex;
  height:200px;
}
.left{
  margin-right:0x;
  width:100px;
  background-color: #000;
}
.right{
  flex:1;
  background-color: #f06060
}

还可以使用calc()实现左右布局。

中间自适应,左右固定。

取自圣杯布局

//html
<div class="father">
  <div class="container col">container</div>
  <div class="left col">left</div>
  <div class="right col">right</div>
</div>

//css
.col{
  color:#fff;
  height:500px;
  float:left;
  position:relative;
}
.left{
  width:200px;
  background-color: #333;
  margin-left:-100%;
  left:-200px;
}
.container{
  width:100%;
  background-color: #f06060;
}
.right{
  width:200px;
  background-color: #ddd;
  margin-left:-200px;
  right:-200px;
}
.father{
  padding:0 200px 0 200px;
}

position

//html
<div class="container col">container</div>
<div class="left col">left</div>
<div class="right col">right</div>
//css
.col{
  color:#fff;
  height:500px;
  position:absolute;
}
.left{
  width:200px;
  background-color: #333;
  top:0;
  left:0;
}
.container{
  width:100%;
  background-color: #f06060;
  top:0;
  left:0;
  margin:0 200px 0 200px;
}
.right{
  width:200px;
  background-color: #ddd;
  right:0;
  top:0;
}

calc()

//html
<div class="left col">left</div>
<div class="container col">container</div>
<div class="right col">right</div>
//css
.col{
  color:#fff;
  height:500px;
  float:left;
}
.left{
  width:200px;
  background-color: #333;
}
.container{
  width:calc(100% - 400px);
  background-color: #f06060;
}
.right{
  width:200px;
  background-color: #ddd;
}

圣杯布局

//html
<div class="container">
 <div class="header">header</div>
 <div class="wrapper clearfix">
    <div class="main col">main</div>
    <div class="left col">left</div>
    <div class="right col">right</div>
 </div>
 <div class="footer">footer</div>
</div>

//css
.container {
  width: 600px;
  margin: 50px auto;
  border:1px solid red;
}
.header,.footer{
  height:50px;
  color:#fff;
  background-color: #333;
}
.col{
  float:left;
  height:200px;
  position:relative;
}
.clearfix::after{
  content:'';
  display:block;
  clear:both;
}
.main{
  width:100%;
  background-color: #eee; 
}
.left{
  width:200px;
  background-color: #ddd;
  margin-left:-100%;
  left:-200px;
}
.right{
  width:200px;
  background-color: #ddd;
  margin-left:-200px;
  right:-200px;
}
.wrapper{
  padding:0 200px 0 200px;
}

等高

//html
<div class="father">
  <div class="left">
     <div class="child">
       <p>yiersan</p>
       <p>siwuliu</p>
     </div>
  </div>
  <div class="right"></div>
  <div class="other"></div>
</div>
//css
.left{
  width:200px;
  float:left;
  background-color: #eee;
}
.right{
  overflow:hidden;
  background-color: #ddd;
  float:left;
  width:200px;
}
.left,.right,.other{
   padding-bottom: 9999px;
   margin-bottom: -9999px;
}

.other{
  overflow:hidden;
  background-color: #333;
}
.father{
  overflow:hidden;
}

table实现

//html
<div class="father">
  <div class="left">
     <div class="child">
       <p>yiersan</p>
       <p>siwuliu</p>
     </div>
  </div>
  <div class="right"></div>
</div>
//css
.father{
  width:100%;
  display:table;
}
.left{
  width:200px;
  margin-right:20px;
  display:table-cell;
  background-color: #ddd;
}
.right{
  display:table-cell;
  background-color: #eee;
}

flex实现

//html和上方的相同
//css
.father{
  display:flex;
  width:100%;
}
.left{
  width:100px;
  background-color: #ddd;
}
.right{
  flex:1;
  background-color: #eee;
}
上一篇下一篇

猜你喜欢

热点阅读