我是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>
&#13;
以下是相同
的codepen Link答案 0 :(得分:1)
您需要添加保证金:0自动;到您的容器类。这将使容器和图像集中在其中。
.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>
答案 1 :(得分:0)
您没有正确使用flex。我已经以正确的方式应用了这些变化。
.container {
display: flex;
justify-content: center;
align-items: center;
align-content: center;
}
.container > div {
width: 50%;
position: relative;
}
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 > div:hover .overlay {
opacity: 0.5;
}
.container > div: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">
<div>
<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>
</div>
</body>