您可以在 github 仓库中找到这篇文章中的所有代码。

您可以在这里查看视觉效果:

  • 固定导航 - 布局 - codesandbox
  • 两列 - 布局 - codesandbox
  • 三列 - 布局 - codesandbox
  • 圣杯 - 布局 - codesandbox

通过 css 实现通用布局


固定导航布局

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>fixed navigation</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    
    

test

test

test

test

test

test

test

test

test

test

test

test

test

test

test

test

test

</body> </html>
登录后复制
nav {
  position: fixed;
  top: 0;
  z-index: 1000;
  width: 100%;
}
登录后复制

两列布局

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>two columns layout</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <!--
    

left side

right side

--> <!--

left side

right side

--> <!--

left side

right side

--> <!--

left side

right side

--> <!--

left side

right side

-->

left side

right side

</body> </html>
登录后复制
.float-container .left {
  width: 200px;
  float: left;
  background-color: tomato;
}

.float-container .right {
  margin-left: 200px;
  background-color: aqua;
}

.absolute-container {
  position: relative;
}

.absolute-container .left {
  width: 200px;
  position: absolute;
  top: 0;
  left: 0;
  background-color: tomato;
}

.absolute-container .right {
  margin-left: 200px;
  background-color: aqua;
}

.bfc-container .left {
  width: 200px;
  float: left;
  background-color: tomato;
}

.bfc-container .right {
  overflow: hidden;
  background-color: aqua;
}

.flex-container {
  display: flex;
}

.flex-container .left {
  width: 200px;
  background-color: tomato;
}

.flex-container .right {
  flex: 1;
  background-color: aqua;
}

.grid-container {
  display: grid;
  grid-template-columns: 200px 1fr;
}

.grid-container .left {
  background-color: tomato;
}

.grid-container .right {
  background-color: aqua;
}

.table-container {
  display: table;
}

.table-container .left {
  display: table-cell;
  background-color: tomato;
}

.table-container .right {
  display: table-cell;
  background-color: aqua;
}
登录后复制

三列布局

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>three columns layout</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    
left
middle
right
left
middle
right
left
middle
right
left
middle
right
</body> </html>
登录后复制
.flex-container {
  display: flex;
}

.flex-container .left {
  width: 200px;
  background-color: tomato;
}

.flex-container .middle {
  flex: 1;
  background-color: blanchedalmond;
}

.flex-container .right {
  width: 200px;
  background-color: aqua;
}

.grid-container {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
}

.grid-container .left {
  background-color: tomato;
}

.grid-container .middle {
  background-color: blanchedalmond;
}

.grid-container .right {
  background-color: aqua;
}

.absolute-container {
  position: relative;
}

.absolute-container .left,
.absolute-container .right {
  position: absolute;
  width: 200px;
  top: 0;
}

.absolute-container .left {
  left: 0;
  background-color: tomato;
}

.absolute-container .right {
  right: 0;
  background-color: aqua;
}

.absolute-container .middle {
  margin-left: 200px;
  margin-right: 200px;
  background-color: blanchedalmond;
}

.float-container .left {
  width: 200px;
  float: left;
  background-color: tomato;
}

.float-container .right {
  width: 200px;
  float: right;
  background-color: aqua;
}

.float-container .middle {
  margin-left: 200px;
  margin-right: 200px;
  background-color: blanchedalmond;
}
登录后复制

圣杯

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <title>holy grail</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    
header
main
footer
</body> </html>
登录后复制
body {
  min-height: 100vh;
}

#root {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

header,
nav,
main,
aside,
footer {
  text-align: center;
  padding: 12px;
}

header {
  height: 60px;
  background-color: tomato;
}

.columns {
  display: flex;
  flex-grow: 1;
}

nav {
  flex-shrink: 0;
  width: 100px;
  background-color: coral;
}

main {
  flex-grow: 1;
  background-color: moccasin;
}

aside {
  flex-shrink: 0;
  width: 100px;
  background-color: sandybrown;
}

footer {
  height: 100px;
  background-color: slategray;
}
登录后复制

参考

  • 圣杯(网页设计)- wikipedia.org
  • 圣杯布局 - web.dev

以上就是布局 - CSS 挑战的详细内容,更多请关注慧达安全导航其它相关文章!

点赞(0)

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部