gpt4 book ai didi

html - 如何居中此图像和悬停效果?

转载 作者:行者123 更新时间:2023-11-28 00:45:58 24 4
gpt4 key购买 nike

我是 HTML 和 CSS 的新手。请帮助我对齐我在下面的代码片段中使用的图像和悬停效果(叠加和文本)。我使用的是 FlexBox,但有点困惑对齐可能是我做错了。

.container {
position: relative;
width: 50%;
display: flex;
justify-content: flex-end;
align-items: center;
align-content: center;
}

h1 {
text-align: center;
font-size: 50px;
color: Grey;
}

.image {
display: block;
width: 100%;
height: auto;
}

.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #008CBA;
border-radius: 10px;
}

.text {
color: white;
background-color: #008CBA;
opacity: 0;
border-radius: 25px;
font-size: 100px;
position: absolute;
top: 50%;
left: 50%;
padding: 10px;
transition: 0.8s ease;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}

.container:hover .overlay {
opacity: 0.5;
}

.container:hover .text {
opacity: 1;
}
<h1>Overlay Effect</h1>
<div class="container">
<img src="http://www.freepngimg.com/download/technology/7-2-technology-picture.png" alt="Avatar" class="image">
<div class="overlay"></div>
<div class="text">Hey Sup!</div>
</div>

这是代码笔 Link同样

最佳答案

您需要添加 margin :0 auto;到你的容器类。这将使容器和其中的图像居中。

.container {
margin: 0 auto;
position: relative;
width: 50%;
display: flex;
justify-content: center;
align-items: center;
align-content: center;
}

h1{
text-align: center;
font-size: 50px;
color: Grey;
}

.image {

width: 100%;
height: auto;
}

.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #008CBA;
border-radius: 10px;
}

.text {
color: white;
background-color: #008CBA;
opacity: 0;
border-radius: 25px;
font-size: 100px;
position: absolute;
top: 50%;
left: 50%;
padding: 10px;
transition: 0.8s ease;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
text-align: center;
}

.container:hover .overlay {
opacity: 0.5;
}

.container:hover .text {
opacity: 1;
}
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Overlay</title>
</head>
<body>
<h1>Overlay Effect</h1>
<div class="container">
<img src="http://www.freepngimg.com/download/technology/7-2-technology-picture.png" alt="Avatar" class="image">
<div class="overlay"></div>
<div class="text">Hey Sup!</div>
</div>
</body>

关于html - 如何居中此图像和悬停效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com