在 Bootstrap 4 中對不同螢幕上的彈性專案從開始處對齊


在 Bootstrap 4 中使用 .align-self-*-start 類對不同螢幕上的彈性專案從開始處對齊。

對不同螢幕上的彈性專案對齊 −

小螢幕

<div class="d-flex bg-info" style="height:200px">
  <div class="p-2 border">Item 1</div>
  <div class="p-2 border">Item 2</div>
  <div class="p-2 border align-self-sm-start">Item 3</div>
  <div class="p-2 border">Item 4</div>
</div>

大螢幕

<div class="d-flex bg-primary" style="height:200px">
  <div class="p-2 border">Item 1</div>
  <div class="p-2 border">Item 2</div>
  <div class="p-2 border align-self-lg-start">Item 3</div>
  <div class="p-2 border">Item 4</div>
</div>

讓我們看一個完整的示例,演示如何在不同螢幕上對彈性專案從開始處對齊 −

示例

即時演示

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  </head>
<body>
<div class="container mt-3">
  <h1>Align Specific Flex Item from the start</h1>
  <div class="d-flex bg-info" style="height:200px">
    <div class="p-2 border">Item 1</div>
    <div class="p-2 border">Item 2</div>
    <div class="p-2 border align-self-start">Item 3</div>
    <div class="p-2 border">Item 4</div>
  </div>
  <h2>Small Screen Size</h2>
    <div class="d-flex bg-info" style="height:200px">
    <div class="p-2 border">Item 1</div>
    <div class="p-2 border">Item 2</div>
    <div class="p-2 border align-self-sm-start">Item 3</div>
    <div class="p-2 border">Item 4</div>
  </div>
  <h2>Medium Screen Size</h2>
  <div class="d-flex bg-primary" style="height:200px">
    <div class="p-2 border">Item 1</div>
    <div class="p-2 border">Item 2</div>
    <div class="p-2 border align-self-md-start">Item 3</div>
    <div class="p-2 border">Item 4</div>
  </div>
  <h2>Large Screen Size</h2>
  <div class="d-flex bg-info" style="height:200px">
    <div class="p-2 border">Item 1</div>
    <div class="p-2 border">Item 2</div>
    <div class="p-2 border align-self-lg-start">Item 3</div>
    <div class="p-2 border">Item 4</div>
  </div>
</div>

</body>
</html>

更新於: 2020 年 6 月 16 日

已檢視 87 次

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.