'How can i align a image link to the middle with css
So I have this image I'm trying to align to the middle of the web frame with my CSS file style.css but I'm not sure how to, how can I do this?
body {
background-color: black;
}
h1 {
text-align: center; font-family: sans; color: blue;
}
p {
text-align: center; font-family: sans; color: white;
}
a {
text-align: center;
}
<h1>Pages</h1>
<p><a href="html/page2.html">Page Two</a></p>
<p><a href="html/page3.html">Page Three</a></p>
<a href="#"><img src="https://via.placeholder.com/100.jpg" alt="Discord" style="width:42px;height:42px;"></a>
Solution 1:[1]
Wrap your a tag in a flex container.
.container {
display: flex;
justify-content: center;
}
.container a img {
width: 300px;
height: 300px;
}
<div class="container">
<a href="https://en.wikipedia.org/wiki/Universe">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/69/NASA-HS201427a-HubbleUltraDeepField2014-20140603.jpg" />
</a>
</div>
Alternatively you can wrap it in a grid container and place items center and set the width and height to 100vw/vh respectively.
.container {
display: grid;
place-items: center;
height: 100vh;
width: 100vw;
}
.container a img {
width: 200px;
height: 200px;
}
<div class="container">
<a href="https://en.wikipedia.org/wiki/Universe">
<img src="https://upload.wikimedia.org/wikipedia/commons/6/69/NASA-HS201427a-HubbleUltraDeepField2014-20140603.jpg" />
</a>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | alechristopher |
