美文网首页
CSS常用布局

CSS常用布局

作者: 陈泽chanzed | 来源:发表于2018-08-27 22:57 被阅读0次

一、左右布局

HTML部分:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>JS Bin</title>
</head>
<body>
<div class="wrapper">
 <div class="left"></div>
 <div class="right"></div>
</div>
</body>
</html>
1. float+margin-left实现
.wrapper{
  content: "";
  display: block;
  clear: both;
}
.left{
  background-color: yellow;
  width: 100px;
  height: 300px;
  float: left;
}
.right{
  background-color: green;
  margin-left:120px;
  height:400px; 
2. position+margin-left实现
.wrapper{
  position: relative;
}
.left{
  width:100px;
  height:300px;
  background-color: yellow;
  position: absolute;
}
.right{
  height: 500px;
  background-color: green;
  margin-left: 120px;
}
3. flex实现
.wrapper{
  display: flex;
}
.left{
  background-color: yellow;
  width: 200px;
  height: 500px;
}
.right{
  background-color: green;
  flex: 1;
}

二、左中右布局

HTML部分:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div class="wrapper">
  <div class="left"></div>
  <div class="right"></div>
  <div class="main"></div>
</div>
</body>
</html>
1. position+margin实现
.wrapper{
  position: relative;
}
.left{
  height: 400px;
  width: 100px;
  background-color: yellow;
  position: absolute;
  top: 0;
  left: 0;
}
.right{
  height: 400px;
  width: 100px;
  background-color: yellow;
  position: absolute;
  top: 0;
  right: 0;
}
.main{
  height: 500px;
  background-color: green;
  margin-left: 110px;
  margin-right: 110px;
}
2. float+margin实现
.warpper{
  content:"";
  display: block;
  clear: both;
}

.left{
  float: left;
  width: 100px;
  height: 400px;
  background-color: yellow;
}

.right{
  float: right;
  width: 100px;
  height: 400px;
  background-color: yellow;
}

.main{
  background-color: green;
  height: 400px;
  margin-left: 110px;
  margin-right: 110px;
}

三、水平居中

1. inline 或 inline-* (像链接、文本)元素水平居中

可以在其display为block的父元素上加上text-align: center;属性

<style>
  .parent{
    text-align: center;
}
.child{
  display: inline-block;
  border: 1px solid red
}
</style>

 <div class="parent">
    这是居中的
    <div class="child">你好</div>
    <span>你的名字</span>
    <a href="#">百度一下</a>
 </div>
2. block元素

在该元素上加margin: 0 auto;属性

<style>
.center-me{
  margin: 0 auto;
  background-color: pink;
  width: 100px;
}
</style>
<div class="center-me">你的名字</div>
3. 多个块元素水平居中

将元素的display属性设置为inline-block,并且把父元素的text-align属性设置为center即可:

.parent {
    text-align:center;
}
4.多个块状元素解决方案 (使用flexbox布局实现)

使用flexbox布局,只需要把待处理的块状元素的父元素添加属性display:flex及justify-content:center即可:

.parent {
    display:flex;
    justify-content:center;
}

四、垂直居中

1. 单行的行内元素解决方案
.parent {
    background: #222;
    height: 200px;
}
 
/* 以下代码中,将a元素的height和line-height设置的和父元素一样高度即可实现垂直居中 */
a {
    height: 200px;
    line-height:200px; 
    color: #FFF;
}
2. 多行的行内元素解决方案

组合使用display:table-cell和vertical-align:middle属性来定义需要居中的元素的父容器元素生成效果,如下:

.parent {
    background: #222;
    width: 300px;
    height: 300px;
    /* 以下属性垂直居中 */
    display: table-cell;
    vertical-align:middle;
}
3. 已知高度的块状元素解决方案
.item{
    top: 50%;
    margin-top: -50px;  /* margin-top值为自身高度的一半 */
    position: absolute;
    padding:0;
}

相关文章

  • CSS常用布局实现

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

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

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

  • ##深入学习CSS布局系列(一)布局常用属性

    @(CSS)[CSS, 布局] 深入学习CSS布局系列(一)布局常用属性 一直感觉自己对CSS的各个属性很了解,可...

  • css布局和居中

    本文主要介绍了css常用的布局,居中等其他小技巧。 css布局 左右布局 利用float实现左右布局 左右模块设置...

  • Css float属性的一些特点

    Css float属性的一些特点 css布局中float布局是常用的布局方式,用于实现横向多列布局。这个时候我们就...

  • Good Luck

    概念 HTML以及CSS篇,集中在CSS 说下你常用的几种布局方式,集中往盒模型、flex布局说(至于grid布局...

  • 前端技能

    HTML 常用标签/分类/属性 标签的分类 CSS 布局:position、float 响应式布局:rem布局(移...

  • 006_布局任务与答案第一期(四川师范大学JavaWeb)

    试验目的 了解页面常用布局结构; 掌握 DIV+CSS 布局的基本方法; 3) 掌握用 CSS 改变页面样式的方法...

  • CSS布局常用

    margin : 顺序:顺时针,上右下左 padding : 顺序:顺时针,上右下左。内边距,在宽度和高度之外绘制...

  • 常用 CSS 布局

    一、 垂直水平居中 定位方式实现设置 父元素 样式position: relative;设置 子元素元素 样式wi...

网友评论

      本文标题:CSS常用布局

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