I'm currently working on a responsive design, and I'm facing an issue when trying to place an image inside a circle without it overflowing. I have a circular container, and I want the image to fit within the circle without any overflow, especially when the screen size changes.
#inner {
position: absolute;
left: 10vw;
max-width: 30%;
}
.circled {
width: 30vw;
height: 30vw;
}
.container {
width: 80vw;
}
#outer {
position: relative;
left: -10vw;
bottom: 20vh;
width: 400px;
height: 400px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Circle</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<div class="container">
<div class="circled"><img id="inner" src="https://i.postimg.cc/yNNG527Q/pfp.png"><img id="outer" src="https://i.postimg.cc/K8bS99Kw/circle.gif"></div>
</div>
</body>
</html>
I've tried using CSS properties like max-width: 100% and max-height: 100% to ensure the image doesn't exceed the dimensions of the circle, but it doesn't seem to work as expected. The image still overflows sadly. I want to achieve a responsive design without relying too heavily on media queries.
I believe this is what you're asking. If you want two images to stack on top of each other perfectly every time, one method is this:
position: relative;
and the other position: absolute;
position
, which here is set to position: relative
. This gives the two images a shared common starting point.As mentioned in the comments, there are multiple ways to achieve what you're trying to do. Using background-image
is another method, although I find it easier to use another HTML div so I don't have to recall all of the CSS background image properties to accomplish the same thing.
.container {
width: 80vw;
position: relative;
}
.circled {
width: 30vw;
height: 30vw;
position: relative;
}
#inner {
position: relative;
width: 100%;
height: auto;
}
#outer {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Circle</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<div class="container">
<div class="circled">
<img id="inner" src="https://i.postimg.cc/yNNG527Q/pfp.png">
<img id="outer" src="https://i.postimg.cc/K8bS99Kw/circle.gif">
</div>
</div>
</body>
</html>