前端开发随笔-生活工作点滴前端学习笔记

CSS布局

2019-07-08  本文已影响5人  _ClariS_
  1. 左右布局
    方法一:浮动float
  <div class="left-right">
    <div class="left">左</div>
    <div class="right">右</div>
  </div>
.left-right {
  position: relative;
  max-width: 1000px;
  height: 200px;
  border: 3px solid yellow;
}
.left {
  float: left;
  background: red;
  width: 30%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}
.right {
  float: right;
  background: blue;
  width: 50%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}

效果图:


左右布局

方法二:绝对定位absolute

.left-right {
  position: relative;
  max-width: 1000px;
  height: 200px;
  border: 3px solid yellow;
}
.left {
  position: absolute;
  left: 0;
  top: 0;
  background: red;
  width: 30%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}
.right {
  position: absolute;
  top: 0;
  right: 0;
  background: blue;
  width: 50%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}

效果同上

  1. 左中右布局
    方法一:浮动float
  <div class="left-center-right">
    <div class="left">左</div>
    <div class="center">中</div>
    <div class="right">右</div>
  </div>
.left-center-right {
  max-width: 1000px;
  height: 200px;
  border: 3px solid yellow;
}
.left {
  float: left;
  background: red;
  width: 30%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}
.center {
  float: left;
  background: green;
  width: 20%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}
.right {
  float: right;
  background: blue;
  width: 50%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}

效果图:


左中右布局

方法二:绝对定位absolute

.left-center-right {
  position: relative;
  max-width: 1000px;
  height: 200px;
  border: 3px solid yellow;
}
.left {
  position: absolute;
  top: 0;
  left: 0;
  background: red;
  width: 30%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}
.center {
  position: absolute;
  top: 0;
  left: 30%;
  background: green;
  width: 20%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}
.right {
  position: absolute;
  top: 0;
  right: 0;
  background: blue;
  width: 50%;
  height: 100%;
  text-align: center;
  line-height: 200px;
}

效果同上

  1. 水平居中
  1. 垂直居中
    父元素设置:
position: relative;

要居中的元素设置:

position: absolute;
margin: auto;
top: 0;
left: 0;
bottom:0;
right:0;
上一篇下一篇

猜你喜欢

热点阅读