Я пытаюсь выровнять 2 div по вертикали, как показано на рисунке ниже, с помощью гибкого блока: как это должно быть
А вот второй div с описанием картинки всегда слева: как это отображается в настоящее время
Я что-то упустил в отношении выравнивания 2 div с flexbox или есть лучший способ.
Заранее спасибо!
Клузо
<!DOCTYPE html>
<html lang = "en">
<head>
<style>
.thumb {
width: 300px;
margin-bottom: 50px;
display: flex;
justify-content: center;
align-items: center;
flexbox-direction: column;
}
.thumb img {
max-width: 300px;
max-height: 300px;
transition: all 0.5s;
margin-bottom: 1rem;
}
.thumb .museum-label {
padding: 1em;
background: white;
box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.50);
width: 200px;
color:#2E3225;
}
.thumb .museum-label .artist{
font-weight: bold;
display: block;
}
.thumb .museum-label .title{
font-style: italic;
}
</style>
</head>
<body>
<div class = "thumb">
<a href = "framing.html">
<img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg/800px-Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg" alt = "Venice, from the Porch of Madonna della Salute">
<div class = "museum-label">
<span class = "artist">Pieter Bruegel the Elder</span>
<span class = "title">The Harvesters</span>,
<span class = "date">1565</span>
</div>
</a>
</div>
</body>
</html>
Вам нужно поместить div с классом museum-label
вне тега привязки (a). Это должно решить проблему с выравниванием.
Полный рабочий фрагмент кода:
.thumb {
width: 300px;
margin-bottom: 50px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.thumb img {
max-width: 300px;
max-height: 300px;
transition: all 0.5s;
margin-bottom: 1rem;
}
.thumb .museum-label {
padding: 1em;
background: white;
box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.50);
width: 200px;
color: #2E3225;
}
.thumb .museum-label .artist {
font-weight: bold;
display: block;
}
.thumb .museum-label .title {
font-style: italic;
}
<div class = "thumb">
<a href = "framing.html">
<img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg/800px-Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg" alt = "Venice, from the Porch of Madonna della Salute">
</a>
<div class = "museum-label">
<span class = "artist">Pieter Bruegel the Elder</span>
<span class = "title">The Harvesters</span>,
<span class = "date">1565</span>
</div>
</div>
parent {
display : flex;
justify-content: center;
align-items: center;
}
ИЛИ
parent {
display: grid;
place-items: center;
}
После добавления этого к родительскому элементу дочерний элемент или дочерние элементы будут правильно центрированы во всех измерениях.
вам нужно поставить margin:0 auto; чтобы центрировать его. Попробуйте этот код
.thumb {
width: 300px;
margin-bottom: 50px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
margin:0 auto;
}
.thumb img {
max-width: 300px;
max-height: 300px;
transition: all 0.5s;
margin-bottom: 1rem;
}
.thumb .museum-label {
padding: 1em;
background: white;
box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.50);
width: 200px;
color:#2E3225;
margin:0 auto;
}
.thumb .museum-label .artist{
font-weight: bold;
display: block;
}
.thumb .museum-label .title{
font-style: italic;
}
<!DOCTYPE html>
<html lang = "en">
<body>
<div class = "thumb">
<a href = "framing.html">
<img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg/800px-Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg" alt = "Venice, from the Porch of Madonna della Salute">
<div class = "museum-label">
<span class = "artist">Pieter Bruegel the Elder</span>
<span class = "title">The Harvesters</span>,
<span class = "date">1565</span>
</div>
</a>
</div>
</body>
</html>
Следуйте селекторам .thumb и .thumb a
<!DOCTYPE html>
<html lang = "en">
<head>
<style>
.thumb{
display: flex;
justify-content: center;
}
.thumb a{
width: 300px;
margin-bottom: 50px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.thumb img {
max-width: 300px;
max-height: 300px;
transition: all 0.5s;
margin-bottom: 1rem;
}
.thumb .museum-label {
padding: 1em;
background: white;
box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.50);
width: 200px;
color:#2E3225;
}
.thumb .museum-label .artist{
font-weight: bold;
display: block;
}
.thumb .museum-label .title{
font-style: italic;
}
</style>
</head>
<body>
<div class = "thumb">
<a href = "framing.html">
<img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg/800px-Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg" alt = "Venice, from the Porch of Madonna della Salute">
<div class = "museum-label">
<span class = "artist">Pieter Bruegel the Elder</span>
<span class = "title">The Harvesters</span>,
<span class = "date">1565</span>
</div>
</a>
</div>
</body>
</html>
Установите flex
вместо a
вместо .thumb
.
<!DOCTYPE html>
<html lang = "en">
<head>
<style>
.thumb {
width: 300px;
margin-bottom: 50px;
}
a{
display: flex;
align-items: center;
flex-direction: column;
}
.thumb img {
max-width: 300px;
max-height: 300px;
transition: all 0.5s;
margin-bottom: 1rem;
}
.thumb .museum-label {
padding: 1em;
background: white;
box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.50);
width: 200px;
color: #2E3225;
}
.thumb .museum-label .artist {
font-weight: bold;
display: block;
}
.thumb .museum-label .title {
font-style: italic;
}
</style>
</head>
<body>
<div class = "thumb">
<a href = "framing.html">
<img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg/800px-Pieter_Bruegel_the_Elder-_The_Harvesters_-_Google_Art_Project.jpg" alt = "Venice, from the Porch of Madonna della Salute">
<div class = "museum-label">
<span class = "artist">Pieter Bruegel the Elder</span>
<span class = "title">The Harvesters</span>,
<span class = "date">1565</span>
</div>
</a>
</div>
</body>
</html>