希望我的问题很容易理解。我创建了一个代码笔来更好地展示我想要实现的目标。
我有一个大菱形(实际形状是矩形),我使用 CSS 旋转它们以形成菱形。然后我将一个小图像(我也旋转)放在图像顶部并将第二个图像粘贴到主图像的顶部中心。
这适用于台式机和平板电脑,但我遇到的问题是在移动设备上查看时。由于有许多不同尺寸的移动宽度,第二张图片(橙色)在 iPhone 8 上查看时被 chop ,但在 iPhone X 上看起来很完美。
我知道我可以针对不同的设备进行媒体查询,但我认为这行不通,因为移动设备尺寸太多了。
我认为最合乎逻辑的是找到第二个图像 div 使其水平居中?但由于它是旋转的,我无法找到它们。
任何建议都会有所帮助。感谢你们。
<div class="box">
<div class="diamond-content space-default">
<div class="diamond-shape">
<div class="bg"><img src="https://i.postimg.cc/dQ2RMQ3m/sample.jpg" class="img-fluid"></div>
<div class="logo"><img src="https://i.postimg.cc/jSPtc1Z5/sample2.png" class="img-fluid"></div>
</div>
<div class="diamond-shape">
<div class="bg"><img src="https://i.postimg.cc/dQ2RMQ3m/sample.jpg" class="img-fluid"></div>
<div class="logo"><img src="https://i.postimg.cc/jSPtc1Z5/sample2.png" class="img-fluid"></div>
</div>
</div>
</div>
.box {
margin-top: 200px;
}
.diamond-content {
position: relative;
display: flex;
align-items: center;
max-width: 1200px;
}
.diamond-shape {
position: relative;
width: 200px;
height: 200px;
transform: rotate(-45deg);
transform-origin: 50% 50%;
margin: 0 auto;
overflow: hidden;
}
@media (min-width: 768px) {
.diamond-shape {
width: 350px;
height: 350px;
}
}
@media (min-width: 992px) {
.diamond-shape {
width: 420px;
height: 420px;
}
.diamond-shape.right {
margin: 0 auto 0 -100px;
}
}
@media (min-width: 1200px) {
.diamond-shape {
width: 480px;
height: 480px;
}
}
/*Content Inside Diamond*/
.diamond-shape img {
transform: rotate(45deg);
}
.diamond-shape .logo {
position: relative;
width: 80px;
z-index: 2;
margin: -12px 0 0 132px;
}
@media (min-width: 768px) {
.diamond-shape .logo {
width: 120px;
margin: -18px 0 0 248px;
}
}
@media (min-width: 992px) {
.diamond-shape .logo {
width: 130px;
margin: -20px 0 0 310px;
}
}
@media (min-width: 1200px) {
.diamond-shape .logo {
width: 150px;
margin: -22px 0 0 352px;
}
}
.diamond-shape .bg {
position: absolute;
width: 302px;
margin: -57px 0px 0 -57px;
}
@media (min-width: 768px) {
.diamond-shape .bg {
width: 510px;
margin: -100px 0px 0 -100px;
}
}
@media (min-width: 992px) {
.diamond-shape .bg {
width: 680px;
}
}
https://codepen.io/anon/pen/RXVjog
用这个替换整个 CSS,橙色菱形 (.logo) 将始终位于中心顶部,因为它被设置为绝对位置和右 0。
这是固定的 CodePen https://codepen.io/lakialex/pen/LwyeOm
.box {
margin-top: 200px;
}
.diamond-content {
position: relative;
display: flex;
align-items: center;
max-width: 1200px;
}
.diamond-shape {
position: relative;
width: 200px;
height: 200px;
transform: rotate(-45deg);
transform-origin: 50% 50%;
margin: 0 auto;
overflow: hidden;
}
@media (min-width: 768px) {
.diamond-shape {
width: 350px;
height: 350px;
}
}
@media (min-width: 992px) {
.diamond-shape {
width: 420px;
height: 420px;
}
.diamond-shape.right {
margin: 0 auto 0 -100px;
}
}
@media (min-width: 1200px) {
.diamond-shape {
width: 480px;
height: 480px;
}
}
/*Content Inside Diamond*/
.diamond-shape img {
transform: rotate(45deg);
}
.diamond-shape .bg {
position: absolute;
width: 302px;
margin: -57px 0px 0 -57px;
}
@media (min-width: 768px) {
.diamond-shape .bg {
width: 510px;
margin: -100px 0px 0 -100px;
}
}
@media (min-width: 992px) {
.diamond-shape .bg {
width: 680px;
}
}
.diamond-shape .logo {
position: absolute;
width: 80px;
z-index: 2;
margin: 0;
right: 0;
margin-right: -11px;
margin-top: -11px;
}
@media (min-width: 768px) {
.diamond-shape .logo {
width: 120px;
margin-right: -17px;
margin-top: -17px;
}
}
@media (min-width: 992px) {
.diamond-shape .logo {
width: 130px;
margin-right: -17px;
margin-top: -17px;
}
}
@media (min-width: 1200px) {
.diamond-shape .logo {
width: 150px;
margin-right: -21px;
margin-top: -21px;
}
}
我是一名优秀的程序员,十分优秀!