Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blog] 七种方法实现左侧固定右侧自适应布局 #29

Open
lvqq opened this issue May 27, 2020 · 0 comments
Open

[Blog] 七种方法实现左侧固定右侧自适应布局 #29

lvqq opened this issue May 27, 2020 · 0 comments
Labels

Comments

@lvqq
Copy link
Owner

lvqq commented May 27, 2020

左侧固定宽度,右侧自适应的布局在网页中比较常见,像一些文档或者是后台管理系统都是这样的布局,那么实现它的方法有哪些呢?这里我归纳总结了以下七种方法实现这一布局

页面dom结构如下:

<div class="box">
  <div class="aside"></div>
	<div class="main"></div>
</div>

基础的css样式:

body {
  padding: 0;
  margin: 0;
}
.aside {
  width: 300px;
  height: 200px;
}
.main {
  height: 200px;
}

那么要实现左侧定宽(300px)右侧自适应的布局效果,有以下七种方法:

① float + margin-left

.aside {
  float: left;
}
.main {
  margin-left: 300px;
}

方法很简单,但确实实现了这个效果。

② float + overflow

.aside {
  float: left;
}
.main {
  overflow: auto;
}

左侧盒子浮动,右侧利用overflow: auto形成了BFC,因此右侧盒子不会与左侧盒子重叠。

③ absolute + margin-left

.box {
  position: relative;
}
.aside {
  position: absolute;
  left: 0;
}
.main {
  margin-left: 300px;
}

常见的方法之一。

④ float + calc

.aside {
  float: left;
}
.main {
  float: left;
  width: calc(100% - 300px);
}

让左右两个盒子都浮动,然后给通过动态计算宽度使右侧自适应。

⑤ inline-block + calc

.aside {
  display: inline-block;
}
.main {
  display: inline-block;
  width: calc(100% - 300px);
}

设置两个盒子为行内块元素,同样通过动态计算宽度使右侧自适应。

⑥ flex

.box {
  display: flex;
}
.main {
  flex: 1;
}

设置父盒子为flex布局,然后让右侧自动占满剩余宽度。

⑦ grid

.box {
  display: grid;
  grid-template-columns: 300px 1fr;
}

设置父元素为grid,第二个网格的自动占满剩余宽度。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant