美文网首页
布局居中

布局居中

作者: ZombieBrandg | 来源:发表于2018-06-04 22:55 被阅读0次

两栏布局

左侧固定宽度右边自适应

负margin方法:

<div class="container">
  <div class="left"></div>
  <div class="right"></div>
</div>
<style>
.left{
    width:200px;
    height:300px;
    margin-right:10px;
    background:red;
    float:left;
}
.right{
    width:100%;
    height:400px;
    float:right;
    margin-right:-210px;
    background:blue;
}
</style>

overflow方法:

<div class="container">
  <div class="left"></div>
  <div class="right"></div>
</div>
<style>
.left{
    width:200px;
    height:300px;
    margin-right:10px;
    background:red;
    float:left;
}
.right{
    height:400px;
    background:blue;
    overflow:hidden;
}
</style>
    

position方法:

<div class="container">
  <div class="left"></div>
  <div class="right"></div>
</div>
<style>
.container{
    position:relative;
}
.left{
    position:absolute;
    top:0;
    left:0;
    width:200px;
    height:300px;
    background:red;
}
.right{
    position:absolute;
    top:0;
    left:210px;
    right:0;
    height:400px;
    background:blue;
}
</style>

flex方法:

<div class="container">
  <div class="left"></div>
  <div class="right"></div>
</div>
<style>
.container{
   display:flex;
}
.left{
    width:200px;
    height:300px;
    background:red;
    margin-right:10px;
}
.right{
    flex:1;
    height:400px;
    background:blue;
}
</style>

三栏布局

margin方法

<div class="container">
  <div class="left"></div>
  <div class="right"></div>
  <div class="center"></div>
</div>
<style>
.left{
  width:200px;
  height:300px;
  background:red;
  margin:0 10px;
  float:left;
}
.center{
  height:400px;
  background:pink;
  overflow:auto;
}
.right{
  width:200px;
  height:300px;
  background:blue;
  float:right;
  margin:0 10px;
}
</style>

overflow方法

<div class="container">
  <div class="left"></div>
  <div class="right"></div>
  <div class="center"></div>
</div>
<style>
.left{
  width:200px;
  height:300px;
  background:red;
  margin:0 10px;
  float:left;
}
.center{
  height:400px;
  background:pink;
  overflow:auto;
}
.right{
  width:200px;
  height:300px;
  background:blue;
  float:right;
  margin:0 10px;
}
</style>

position方法

<div class="container">
  <div class="left"></div>
  <div class="center"></div>
  <div class="right"></div>
  
</div>
<style>
*{
  box-sizing:border-box;
  margin:0;
  padding:0;
}
.container{
  position:relative;
}
.left{
  width:200px;
  height:300px;
  background:red;
  margin:0 10px;
  position:absolute;
  top:0;
  left:0;
}
.center{
  width:calc(100% - 440px);
  height:400px;
  background:pink;
  position:absolute;
  top:0;
  left:50%;
  transform:translateX(-50%);
}
.right{
  width:200px;
  height:300px;
  background:blue;
  position:absolute;
  top:0;
  right:0;
  margin:0 10px;
}
</style>

flex方法

<div class="container">
  <div class="left"></div>
  <div class="center"></div>
  <div class="right"></div>
</div>
<style>
*{
  box-sizing:border-box;
  padding:0;
  margin:0;
}
.container{
 display:flex;
}
.left{
  width:200px;
  height:300px;
  background:red;
  margin:0 10px;
  
}
.center{
  height:400px;
  background:pink; 
  flex:1;
}
.right{
  width:200px;
  height:300px;
  background:blue;
  margin:0 10px;
}
</style>

块级元素居中

margin方法

.container{
  background:grey;
}
.item{
  width:300px;
  height:200px;
  background:red;
  margin:0 auto;
}

position方法

.container{
  background:grey;
  position:relative;
}
.item{
  width:300px;
  height:200px;
  background:red;
  position:absolute;
  top:0;
  left:50%;
  transform:translateX(-50%)
}

display:flex

.container{
  background:grey;
  display:flex;   
  justify-content:center;
}
.item{
  width:300px;
  height:200px;
  background:red;
}

text-align:center

.container{
  background:grey;
  text-align:center;
}
.item{
  width:300px;
  height:200px;
  background:red;
  display:inline-block;
}

多行文字水平垂直居中

<section>
  <div class="container">
     <h1 class="title">这是标题</h1>
     <p class="content">这是一大段文字内容这是一大段文字内容这是一大段文字内容这是一大段文字内容这是一大段文字内容这是一大段文字内容</p>
  </div>
</section>
<style>
section{
  margin:0 auto;
  width:600px;
}
.container{
  padding:0 100px;
  width:600px;
  height:600px;
  background:grey;
  text-align:center;
  display:table-cell;
  vertical-align:middle;
}
</style>

image水平垂直居中

 <div class="container">
   <img src="" alt="图片">
 </div>
.container{
  width:300px;
  height:300px;
  border:1px solid red;
  text-align:center;
}
.container::after{
  content:'';
  height:100%;
  display:inline-block;
  vertical-align:middle;
}
img{
  vertical-align:middle;
}

浏览器固定水平垂直居中

.box{
  position:fixed;
  top:50%;
  left:50%;
  transform:translate(-50%,-50%);
  background:#ccc;
  text-align:center;
}

相关文章

  • 居中布局

    水平居中 垂直居中 垂直水平居中 已知元素的宽高的居中布局 定位居中布局 盒模型居中布局 图片的垂直水平居中(利用...

  • 布局

    布局主要分为布局和居中,布局一般指的是多个div的左右布局或者左中右布局。居中就是水平居中和垂直居中 布局 只要记...

  • 页面布局的几种方式

    居中布局 一、水平居中布局 水平居中:absolute + transform: translateX(-50%)...

  • CSS布局与水平垂直居中

    左右布局 左中右布局 水平居中 垂直居中 水平垂直居中 左右布局 float实现左右布局 子元素设置float: ...

  • 页面架构

    布局解决方案 水平居中布局 垂直居中布局 水平垂直都居中的布局 多列布局 多列等分布局 多列等高布局 在多列布局的...

  • CSS边用边学(一):一站式各种布局实践

    目录 布局相关属性displaypositionfloat 各种布局两栏布局三栏布局水平居中垂直居中 总结 概述 ...

  • 无标题文章

    css左右布局 两个块级元素实行左右布局. 左中右布局 水平居中 块级元素水平居中 内联元素居中 垂直居中 行内元...

  • CSS水平居中布局、垂直居中布局、垂直水平居中布局

    本章将介绍父子元素宽高不定的CSS水平居中布局、垂直居中布局、垂直水平居中布局。学习如何写出布局不是内容关键,解决...

  • CSS常用居中方法

    在使用CSS对页面进行样式布局的时候,居中是我们最经常用到的布局之一,而居中布局又分为水平居中和垂直居中。本文将要...

  • [2020-08-10]css3中的常用几种布局

    div的水平垂直居中 flex布局,使用display: flex实现水平垂直居中 grid布局 使用grid布局...

网友评论

      本文标题:布局居中

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