Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Tạo màn hình chia đôi (split screen 50/50) với CSS


Split screen chia màn hình thành 2 nửa cố định, thường dùng cho landing page, login/register side-by-side.

Ví dụ

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      font-family: Arial;
      color: white;
      margin: 0;
      height: 100vh;
      overflow: hidden;
    }
    h1 { padding: 20px; }
    .left, .right {
      height: 100%;
      width: 50%;
      position: fixed;
      z-index: 1;
      top: 0;
      overflow-y: auto;
      padding-top: 20px;
      box-sizing: border-box;
    }
    .left {
      left: 0;
      background-color: rgb(36, 0, 95);
    }
    .right {
      right: 0;
      background-color: rgb(56, 1, 44);
    }
    .centered {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;
    }
    .centered img {
      width: 150px;
      border-radius: 50%;
    }
  </style>
</head>
<body>
  <div class="left">
    <h1>Một số văn bản ngẫu nhiên bên trái</h1>
  </div>
  <div class="right">
    <h1>Một số văn bản ngẫu nhiên bên phải</h1>
  </div>
</body>
</html>

Kết quả

Màn hình chia đôi cố định: nửa trái màu tím đậm, nửa phải màu tím đỏ, mỗi nửa chiếm 50% viewport, cuộn độc lập.

Cách hiện đại hơn (Flexbox)

.split-container {
  display: flex;
  height: 100vh;
}
.left, .right {
  flex: 1;
  overflow-y: auto;
}

Flexbox đơn giản hơn, không cần position:fixed, z-index.