css_14 | CSS——把“能够动的盒子”更优雅地展现:③ 经常使用的“布局”

本文推荐 PC 端阅读~

本文版权归 “公众号 | 前端一万小时” 全部,未经受权,请勿转载!
复制代码

获取编号.png

css_14
复制代码

涉及面试题.png

响应式布局原理?
复制代码


前言: 拿到一张设计稿,咱们首要的就是从宏观上考虑这整个页面的“布局”。随着前端技术的不断更替,之前不少老的布局方式如今也慢慢淡化了,那哪些是咱们最基本最经常使用的布局方式呢?css

本篇给出答案,属于必掌握篇。html



1 什么是布局?

现有样式不能知足人们的需求:前端

  • 文档流
  • 浮动
  • 定位

人们须要:面试

  • 导航栏+内容
  • 导航栏+内容+广告栏
  • 从上到下、从左到右、定宽、自适应...

2 最经常使用的布局有哪些?

2.1 单列布局

现方式——定宽 + 水平居中。bash

2.1.1 非通栏

🔗源码及效果展现
htmlide

<div id="header"  class="layout">头部</div>
<div id="content" class="layout">内容</div>
<div id="footer" class="layout">尾部</div>
复制代码

css布局

.layout {
  width: 960px;
/*❗️实际网站中经常用的都是 width ,而没有用 max-width 。 用 max-width 的好处就是它不会由于用户屏幕的变小而出现左右滚动条。 反之用 width 就会出现这种状况。 可是,因为如今的网页都很复杂,信息不少。若是用 max-width , 虽然它会按照用户的屏幕大小来显示网页,但很难让它去作合适的布局。 那与其这样,咱们开发者更愿意用 width ,即便有个滚动条,但至少里边的内容不会乱。 */ 


  margin: 0 auto;
}
#header {
  height: 60px;
  background: red;
}
#content {
  height: 400px;
  background: blue;
}
#footer {
  height: 50px;
  background: yellow;
}
复制代码

15-01.png

2.1.2 通栏

🔗源码及效果展现
htmlflex

<div id="header">
  <div class="layout">头部</div>
</div>
<div id="content" class="layout">内容</div>
<div id="footer">
  <div class="layout">尾部</div>
</div>
复制代码

css网站

.layout {
  width: 960px;
  margin: 0 auto;
}

body {
  min-width: 960px;
/*❗️❗️❗️对比加这个 min-width 和不加的区别,拉动屏幕大小, 咱们能够看见给 body 设置 min-width 能够去掉滚动背景色 bug。 */

}

#header {
  height: 60px;
  background: red;
}
#content {
  height: 400px;
  background: blue;
}
#footer {
  height: 50px;
  background: yellow;
}
复制代码

15-02.png

2.2 双列布局

一列固定宽度,另一列自适应宽度。
实现方式——浮动元素 + 普通元素 margin。spa

2.2.1 侧边栏在左

🔗源码及效果展现
html

<div id="content">
  <div class="aside">aside</div>
  <div class="main">content</div>
</div>
<div id="footer">footer</div>
复制代码

css

#content:after {
  content: '';
  display: block;
  clear: both;
}
.aside {
  width: 200px;
  height: 500px;
  background: yellow;
  float: left;
}
.main {
  margin-left: 210px;
  height: 400px;
  background: red;
}
#footer {
  background: #ccc;
}
复制代码

15-03.png

2.2.2 侧边栏在右

时刻记着页面元素的渲染顺序!

🔗源码及效果展现
html

<div id="content">
  <div class="aside">aside</div>
  <div class="main">content</div>
</div>
<div id="footer">footer</div>
复制代码

css

#content:after {
content: '';
display: block;
clear: both;
}
.aside {
  width: 200px;
  height: 500px;
  background: yellow;
  float: right;    
}    
.main {      
  margin-right: 210px;      
  height: 400px;      
  background: red;    
}    
#footer {      
  background: #ccc;    
}
复制代码

2.3 三列布局

两侧两列固定宽度,中间列自适应宽度。
⚠️注意顺序!

🔗源码及效果展现
html

<div id="content">

<!-- 注意为何不是 main 在前面 -->

  <div class="menu">aside</div>
  <div class="aside">aside</div>
  <div class="main">content</div>
</div>
<div id="footer">footer</div>
复制代码

css

#content:after {
  content: '';
  display: block;
  clear: both;
}
.menu {
  width: 100px;
  height: 500px;
  background: pink;
  float: left;
}
.aside {
  width: 200px;
  height: 500px;
  background: yellow;
  float: right;
}
.main {
  margin-left: 110px; 
  /*注意为何要加 margin-left*/

  margin-right: 210px;
  height: 400px;
  background: red;
}

#footer {
  background: #ccc;
}
复制代码

15-04.png

2.4 水平等距排列

主要关注“负 margin ”的运用!

🔗源码及效果展现
html

<div class="ct">
  <ul>
    <li class="item">1</li>
    <li class="item">2</li>
    <li class="item">3</li>
    <li class="item">4</li>
    <li class="item">5</li>
    <li class="item">6</li>
    <li class="item">7</li>
    <li class="item">8</li>
  </ul>
</div>
复制代码

css

ul,li {
  margin: 0;
  padding: 0;
  list-style: none;
}
.ct{
  overflow:hidden;
  width: 640px;
  border: 1px dashed orange;
  margin: 0 auto;
}

.ct .item {
  float:left;
  margin-left: 20px;
  margin-top: 20px;
  width:200px;
  height:200px;
  background: red;
}
.ct>ul {
  margin-left: -20px;  

}
复制代码

15-05.png



后记: 对于“布局”,咱们还有一些其余更新、更强大的方式:弹性布局 flex、grid 布局等等,咱们随后会在“前端综合”系列文章中再一一探讨,这篇咱们先掌握基础。

前端知识突飞猛进,迭代很快,但最基本的知识是永远都不会变的。因此,夯实好基础,以不变应万变!

祝好,qdywxs ♥ you!

相关文章
相关标签/搜索