Rumah > hujung hadapan web > tutorial css > Reka letak - cabaran CSS

Reka letak - cabaran CSS

Barbara Streisand
Lepaskan: 2024-11-07 05:33:03
asal
325 orang telah melayarinya

Layout - CSS challenges

Anda boleh menemui semua kod dalam siaran ini di repo Github.

Anda boleh menyemak visual di sini:

  • Navigasi Tetap - Reka Letak - CodeSandbox
  • Dua lajur - Reka Letak - CodeSandbox
  • Tiga lajur - Reka Letak - CodeSandbox
  • Holy Grail - Reka Letak - CodeSandbox

Reka letak biasa melalui CSS


Susun atur navigasi tetap

<!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>
    <nav>This is a navbar</nav>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
    <h1>Test</h1>
  </body>
</html>
Salin selepas log masuk
nav {
  position: fixed;
  top: 0;
  z-index: 1000;
  width: 100%;
}
Salin selepas log masuk

Reka letak dua lajur

<!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>
    <!--
    <div>





<pre class="brush:php;toolbar:false">.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;
}
Salin selepas log masuk

Susun atur tiga lajur

<!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>
    <div>





<pre class="brush:php;toolbar:false">.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;
}
Salin selepas log masuk

Holy Grail

<!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>Header</header>
    <div>





<pre class="brush:php;toolbar:false">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;
}
Salin selepas log masuk

Rujukan

  • Holy grail (reka bentuk web) - Wikipedia.org
  • Susun atur grail suci - Web.dev

Atas ialah kandungan terperinci Reka letak - cabaran CSS. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan