<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rotate and Center with Proportional Div2</title>
</head>
<body>
<div class="p-body">
<img class="div1" src="./img/blue-tit-8024809_1280.png" alt="" width="1280" height="640" />
<img class="div2" src="./img/img2.webp" alt="">
</div>
<style>
body {
margin: 0;
}
.p-body {
position: relative;
height: 100vh;
display: flex;
justify-content: center;
}
.div1 {
height: auto;
width: calc(100vh - 72px);
object-fit: contain;
transform: translateY(36px) rotate(-90deg);
}
.div2 {
position: absolute;
top: calc(50% + 36px);
left: 50%;
width: 33vw;
transform: translate(-50%, -50%);
background-color: rgb(10, 42, 42);
}
</style>
<script>
document.addEventListener("DOMContentLoaded", function() {
const div1 = document.querySelector('.div1');
const div2 = document.querySelector('.div2');
function updateDiv2Width() {
const div1Width = div1.naturalWidth;
const div1Height = div1.naturalHeight;
const aspectRatio = div1Height / div1Width;
const div1RotatedHeight = window.innerHeight - 72
const div1RotatedWidth = div1RotatedHeight * aspectRatio;
div2.style.width = `${div1RotatedWidth}px`;
}
div1.onload = updateDiv2Width;
// If the image is already loaded (from cache), trigger the onload event manually
if (div1.complete) {
updateDiv2Width();
}
window.addEventListener('resize', updateDiv2Width);
});
</script>
</body>
</html>
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme