I need the text just right above the image, like a little title.
I am trying to position text above an image, but every time I use margin-top: ###px
the image moves down as well. They aren’t in the same div element, so why is this happening and how can I fix it?
I kept trying to move the text down using margin-top, but it also moved the image down and I don’t know where to go from there.
.column img {
width: 25%;
margin-top: 150px;
margin-left: 150px;
margin-right: 160px;
transition: transform .2s;
}
.column img:hover {
transform: scale(1.1);
}
.column h2 {
margin-left: 0px;
font-family: 'Montserrat', sans-serif;
margin-top: 100px;
}
.column img {
width: 25%;
transition: transform .2s;
}
.row {
display: flex;
align-items: center;
justify-content: center;
}
.column {
flex: 25%;
padding: 10px;
text-align: center;
}
.titles h2 {
margin-left: 300px;
}
<div class="titles">
<h2>Lonoke, AR</h2>
</div>
<div class="row">
<div class="column">
<img src="https://via.placeholder.com/300x100" alt="A field" style="width: 50%" />
</div>
<div class="column">
<img src="https://via.placeholder.com/300x100" alt="A forest" style="width: 50%" />
</div>
</div>
3
Answers
You have this rule:
margin-top: 150px;
is what’s separating your title from the images. Play with that value or just remove it .It’s a bit unclear what your goal is from your question, but if you want the resizing of the title to be separated from the resizing of the images under it, you can use
position: absolute;
as I’ve done below. On either the title, the images, or both.This will effectively layer the objects on top of each other, instead of having them flow one after the other, as you’ve observed they are doing now.
I think I understand your requirement. You need to give
margin-top
to the text but not the image to bring both of them together and.column h2
doesn’t select anything so I hide it and added the font to the correct selector which is.titles h2