如何創建:分屏(1/2)

學習如何使用 CSS 創建分屏(50/50)效果。

親自試一試

如何創建分屏

第一步 - 添加 HTML:

<div class="split left">
  <div class="centered">
    <img src="img_avatar2.png" alt="Avatar woman">
    <h2>Jane Flex</h2>
    <p>Some text.</p>
  </div>
</div>
<div class="split right">
  <div class="centered">
    <img src="img_avatar.png" alt="Avatar man">
    <h2>Bill Gates</h2>
    <p>Some text here too.</p>
  </div>
</div>

第二步 - 添加 CSS:

/* 將屏幕一分為二 */
.split {
  height: 100%;
  width: 50%;
  position: fixed;
  z-index: 1;
  top: 0;
  overflow-x: hidden;
  padding-top: 20px;
}
/* 控制左側 */
.left {
  left: 0;
  background-color: #111;
}
/* 控制右側 */
.right {
  right: 0;
  background-color: red;
}
/* 如果您想讓內容在水平和垂直方向上都居中 */
.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}
/* 如果需要,設置居中容器內的圖片樣式 */
.centered img {
  width: 150px;
  border-radius: 50%;
}

親自試一試