gpt4 book ai didi

javascript - 使用 CSS 在另一个图像之上调整图像

转载 作者:行者123 更新时间:2023-11-28 09:54:02 24 4
gpt4 key购买 nike

我希望图像 B 像图像 A 一样流畅,同时将其置于图像 A 的中心位置。

这是我的示例代码:

<div class="container">
<img id="imgA" src="A.png" />
<img id="imgB" src="B.png" />
</div>

CSS:

#imgA{
display: block;
width: 100%;
height: auto;
position: relative;
}




#imgB{
position: absolute;
top: 50%;
left: 50%;
margin-left: -200px;
margin-top: -100px;
width: 400px;
height: 200px;
}

使用上面的代码,我只能在分辨率足够高的情况下使图像 B 成为图像 A 的中心,但是当它变低时,图像 A 会调整到它的屏幕,但图像 B 保持不变。我需要添加什么才能使图像 B 流畅并在任何分辨率下成为图像 A 的中心?

最佳答案

您需要以 % 而非像素为单位计算第二个图像的偏移量,并确保它们的大小是相对于容器 div 的。我用一个例子创建了一个 js fiddle :http://jsfiddle.net/BX6u7/

<div class="container">
<img id="imgA" src="http://placehold.it/350x150" />
<img id="imgB" src="http://placehold.it/150x50" />
</div>

和CSS:

.container {
margin: 0 auto;
position:relative;
width: 50%;
background-color:yellow;
}

#imgA{
display: block;
width:100%;
max-width:100%;
height:auto;
border:none;
}

#imgB{
top: 28.6%;
left: 21.6%;
width: 57%;
max-width: 57%;
border:1px solid white;
position: absolute;
}

关于javascript - 使用 CSS 在另一个图像之上调整图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24972468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com