如何在所有图像中间放置一个播放按钮

how to place a play button in the middle of all the images

index.html - 下面是 index.html 文件代码,我在其中编写了所有 html 代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Streaming</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
    <link rel="stylesheet" href="style.css" />
    <script src="script.js" defer></script>
</head>
<body>
    <nav>
        <div class="logo"><i class="fas fa-play"></i></div>
        <div class="stream">
            <ul>
                <li><a href="#">View All</a></li>
                <li>
                    <h1>Net Steam</h1>
                </li>
                <li><a href="#">Subscribe</a></li>
            </ul>
        </div>
    </nav>
    <div class="container">
        <div class="row">
            <div class="col">
                <div class="img-container"><img src="./img-1.jpg" alt="img-1"></div>
                <div class="centered"><i class="fas fa-play"></i></div>
                <div class="text">
                    <h1>Take Mantrix</h1>
                    <p>Take the Yellow Pill to get into the zone</p>
                    <a href="#">Watch Now</a>
                </div>
            </div>
            <div class="col">
                <div class="img-container"><img src="./img-2.jpg" alt="img-2"></div>
                <div class="centered"><i class="fas fa-play"></i></div>
                <div class="text">
                    <h1>2 Fast 2 Soon</h1>
                    <p>Take the Yellow Pill to get into the zone</p>
                    <a href="#">Watch Now</a>
                </div>
            </div>
            <div class="col">
                <div class="img-container"><img src="./img-3.jpg" alt="img-3"></div>
                <div class="centered"><i class="fas fa-play"></i></div>
                <div class="text">
                    <h1>Take Outback</h1>
                    <p>Take the Yellow Pill to get into the zone</p>
                    <a href="#">Watch Now</a>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

style.css - 下面是 style.css 文件代码 我已经写完了我所有的 CSS 代码

@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700;900&display=swap");
* {
    box-sizing: border-box;
}
body {
    font-family: "Roboto", sans-serif;
    margin: 0;
    background-color : #000;
    color : #fff;
    padding : 1rem 4rem;
}
nav {
    display : flex;
    flex-direction : column;
    align-items: center;
    border-bottom : 2px solid #fff;
    margin : 0;
    padding : 0;
}
.stream{
    max-width : 1000px;
    width : 100%;
}
.fa-play {
    font-size : 3rem;
}
ul {
    list-style-type: none;
    display : flex;
    justify-content: space-around;
    align-items: center;
    margin : 0;
    padding : 0;
}
li h1{
    font-size : 3rem;
}
a{
    color : black;
    text-decoration: none;
    padding : 1rem 2rem;
    background-color : white;
}
.row {
    display : flex;
}
.col {
    padding : 1rem;
    flex : 1;
}
.col img{
    max-width : 100%;
}
.img-container {
    position : relative;
}
.centered {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
.text {
    display : flex;
    flex-direction : column;
    align-items: center;
}

我想使用 CSS 将播放图标放置在所有三张图片的中央,但我只获得了中间图片的播放图标。如何让播放图标位于所有三个图像的中心。 我想把 play font-awesome 图标放在图片的中央

您可以为 .centered.img-container 类 使用 flexbox 属性。如下图所示:

img {
    width: 200px;
    height: 200px;
}
.img-container {
    display: flex;
    justify-content: center;
    align-items: center;
}
.centered {
    position: absolute;
    top: 30%;
    left: 50%;
    z-index: 1111;
    transform: translate(-50%, -50%);
    display: flex;
    justify-content: center;
    align-items: center;
}

完整工作片段: -(我使用随机 gif 作为图像只是为了这个答案,您可以将它们替换为您的图像路径)

@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700;900&display=swap");
* {
  box-sizing: border-box;
}

body {
  font-family: "Roboto", sans-serif;
  margin: 0;
  background-color: #000;
  color: #fff;
  padding: 1rem 4rem;
}

nav {
  display: flex;
  flex-direction: column;
  align-items: center;
  border-bottom: 2px solid #fff;
  margin: 0;
  padding: 0;
}

.stream {
  max-width: 1000px;
  width: 100%;
}

.fa-play {
  font-size: 3rem;
}

ul {
  list-style-type: none;
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin: 0;
  padding: 0;
}

li h1 {
  font-size: 3rem;
}

a {
  color: black;
  text-decoration: none;
  padding: 1rem 2rem;
  background-color: white;
}

.row {
  display: flex;
}

.col {
  position: relative;
  padding: 1rem;
  flex: 1;
}

.col img {
  max-width: 100%;
}

img {
  width: 200px;
  height: 200px;
}

.img-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.centered {
  position: absolute;
  top: 30%;
  left: 50%;
  z-index: 1111;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.text {
  display: flex;
  flex-direction: column;
  align-items: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" rel="stylesheet"/>
<nav>
  <div class="logo"><i class="fas fa-play"></i></div>
  <div class="stream">
    <ul>
      <li><a href="#">View All</a></li>
      <li>
        <h1>Net Steam</h1>
      </li>
      <li><a href="#">Subscribe</a></li>
    </ul>
  </div>
</nav>
<div class="container">
  <div class="row">
    <div class="col">
      <div class="img-container"><img src="https://c.tenor.com/Lvhj4QVL8-4AAAAM/server-is-for-javascript.gif" alt="img-1"></div>
      <div class="centered"><i class="fas fa-play"></i></div>
      <div class="text">
        <h1>Take Mantrix</h1>
        <p>Take the Yellow Pill to get into the zone</p>
        <a href="#">Watch Now</a>
      </div>
    </div>
    <div class="col">
      <div class="img-container"><img src="https://c.tenor.com/Lvhj4QVL8-4AAAAM/server-is-for-javascript.gif" alt="img-2"></div>
      <div class="centered"><i class="fas fa-play"></i></div>
      <div class="text">
        <h1>2 Fast 2 Soon</h1>
        <p>Take the Yellow Pill to get into the zone</p>
        <a href="#">Watch Now</a>
      </div>
    </div>
    <div class="col">
      <div class="img-container"><img src="https://c.tenor.com/Lvhj4QVL8-4AAAAM/server-is-for-javascript.gif" alt="img-3"></div>
      <div class="centered"><i class="fas fa-play"></i></div>
      <div class="text">
        <h1>Take Outback</h1>
        <p>Take the Yellow Pill to get into the zone</p>
        <a href="#">Watch Now</a>
      </div>
    </div>
  </div>
</div>

希望这就是您想要的样子

试试这个。

@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700;900&display=swap");
* {
    box-sizing: border-box;
}
body {
    font-family: "Roboto", sans-serif;
    margin: 0;
    background-color : #000;
    color : #fff;
    padding : 4rem 4rem;
}
nav {
    display : flex;
    flex-direction : column;
    align-items: center;
    border-bottom : 2px solid #fff;
    margin : 0;
    padding : 0;
}


.stream{
    max-width : 1000px;
    width : 100%;
}
.fa-play {
    font-size : 3rem;
    position:absolute;
    text-align:center;
    color:red;
    bottom:30px;

    
}
ul {
    list-style-type: none;
    display : flex;
    justify-content: space-around;
    align-items: center;
    margin : 0;
    padding : 0;
}
li h1{
    font-size : 3rem;
}
a{
    color : black;
    text-decoration: none;
    padding : 1rem 2rem;
    background-color : white;
}
.row {
    display : flex;
}
.col {
    padding : 1rem;
    flex : 1;
    justify-content:center;
}
.col img{
    max-width : 100%;
}
.img-container {
    position : relative;
    display:flex;
    justify-content:center;

}
.centered {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
.text {
    display : flex;
    flex-direction : column;
    align-items: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Streaming</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
    <link rel="stylesheet" href="style.css" />
    <script src="script.js" defer></script>
</head>
<body>
    <nav>
        <div class="logo"><i class="fas fa-play" style="position:relative"></i></div>
        <div class="stream">
            <ul>
                <li><a href="#">View All</a></li>
                <li>
                    <h1>Net Steam</h1>
                </li>
                <li><a href="#">Subscribe</a></li>
            </ul>
        </div>
    </nav>
    <div class="container">
        <div class="row">
            <div class="col">
                <div class="img-container"><i class="fas fa-play" ></i><img src="https://dictionary.cambridge.org/images/thumb/circle_noun_001_02738.jpg" alt="img-1"></div>
               
                <div class="text">
                    <h1>Take Mantrix</h1>
                    <p>Take the Yellow Pill to get into the zone</p>
                    <a href="#">Watch Now</a>
                </div>
            </div>
            <div class="col">
                <div class="img-container"><i class="fas fa-play" ></i><img src="https://dictionary.cambridge.org/images/thumb/circle_noun_001_02738.jpg" alt="img-2"></div>
                
                <div class="text">
                    <h1>2 Fast 2 Soon</h1>
                    <p>Take the Yellow Pill to get into the zone</p>
                    <a href="#">Watch Now</a>
                </div>
            </div>
            <div class="col">
                <div class="img-container"><i class="fas fa-play" ></i><img src="https://dictionary.cambridge.org/images/thumb/circle_noun_001_02738.jpg" alt="img-3"></div>
                
                <div class="text">
                    <h1>Take Outback</h1>
                    <p>Take the Yellow Pill to get into the zone</p>
                    <a href="#">Watch Now</a>
                </div>
            </div>
        </div>
    </div>
</body>
</html>