居中一個具有100vh高度的背景圖上的div
P粉006847750
P粉006847750 2024-02-17 12:39:44
0
1
325

我正在學習一門課程,其中頂部有一個固定位置的導覽列。背景設定為100vh的高度,帶有文字內容的div位於背景的中心。使用 Flexbox 它幾乎居中,但為了真正使其到達背景中心,此 div 的高度必須設定為 100% 的高度。我對 Flexbox 和視窗高度有所了解,但是我對為什麼需要將 div 的高度設定為 100% 才能真正讓潛水居中感到困惑。我想你可以在這裡放置任何圖像作為背景來複製我所要求的內容。希望我在這裡說得通。

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  background: #fff;
  color: #333;
  line-height: 1.5;
}

ul {
  list-style: none;
}

a {
  color: #333;
  text-decoration: none;
}

h1,
h2 {
  font-weight: 300px;
  line-height: 1.5:
}

p {
  margin: 10px 0;
}

img {
  width: 100%;
  /*makes image width of the container */
}


/* navbar */

.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #333;
  color: #fff;
  width: 100%;
  height: 70px;
  /* apparently a common height for navbar */
  position: fixed;
  top: 0px;
  padding: 0 30px;
}

.navbar a {
  color: #fff;
  padding: 10px 20px;
  margin: 0 5px;
}

.navbar a:hover {
  border-bottom: 1px solid #28a745;
}

.navbar ul {
  display: flex;
}


/* Header */

.hero {
  background: url("/img/home/showcase.jpg") no-repeat center center/cover;
  height: 100vh;
  position: relative;
  color: #fff;
}

.hero .content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  height: 100%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Welcome to EdgeLedger</title>
  <link rel="stylesheet" href="/css/utilities.css">
  <!-- style.css is last in case utilities.css needs to be overwritten -->
  <link rel="stylesheet" href="/css/style.css">
  <script src="https://kit.fontawesome.com/6eab1538de.js" crossorigin="anonymous"></script>
</head>

<body id="home">
  <header class="hero">
    <div id="navbar" class="navbar">
      <h1 class="logo">
        <span class="text-primary"><i class="fas fa-book-open"></i>Edge</span>Ledger
      </h1>
      <nav>
        <ul>
          <li><a href="#home">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#cases">Cases</a></li>
          <li><a href="#blog">Blog</a></li>
          <li><a href="#contact">Contact</a></li>
        </ul>
      </nav>
    </div>

    <div class="content">
      <h1>The sky is the limit</h1>
      <p>We provide world class financial assistance</p>
      <a href="#about" class="btn">Read More</a>
    </div>
  </header>
</body>

</html>

P粉006847750
P粉006847750

全部回覆(1)
P粉953231781

如果我理解正確的話,你是說內容預設水平居中,但是div需要 height: 100% 來垂直居中內容。

Div 是區塊元素,這表示預設:

  1. 它們佔據了螢幕的整個寬度
  2. 它們僅佔據顯示內容所需的高度(它們的預設高度為 auto)。

如果您的 div 是內容居中的 Flexbox,即使內容垂直居中,div 仍只會根據需要向下擴展,以適應其中最高的元素。由於 div 仍然位於螢幕頂部,即使其內容在 div 內垂直居中,內容也會出現在螢幕頂部,因為 div 僅與內容一樣高,並且因為 div 位於螢幕頂部螢幕頂部。

但是,div 的 height: auto 預設屬性可以被覆寫。如果將高度設為 100%,則會強制 div 為其父元素(頁面)高度的 100%。然後,div 將為內容提供一堆額外的空間,並且由於 Flex 規則,它將內容定位在該額外空間的垂直中心。

要進一步了解這一點,您可以嘗試將border: 5px dashed black 添加到div 中,以便您可以看到其大小和位置,然後使用不同的高度值,例如 unset100%50% 等。嘗試並嘗試!

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!