【问题标题】:How to fix position image on another image如何将位置图像固定在另一个图像上
【发布时间】:2022-02-14 16:23:26
【问题描述】:

我有以下code(也粘贴在下面),我想在其中制作两列的布局。在第一个中,我放了两张图片,在第二个中显示了一些文字。

在第一列中,我希望第一张图片带有width:70%,第二张图片带有position:absolute。最终结果应该是this

如您所见,第二张图片部分位于上方每个屏幕的第一张图片中,768px

我可以在第一张图片上部分定位第二张图片,但这不是动态的,如果您更改屏幕尺寸,您可以看到它是如何折叠的。

但无论我如何努力,我都无法达到这个结果。

.checkoutWrapper {
  display: grid;
  grid-template-columns: 50% 50%;
}

.coverIMGLast {
  width: 70%;
  height: 100vh;
}

/* this .phone class should be dynamically  located partially on first image */
.phone {
  position: absolute;
  height: 90%;
  top: 5%;
  bottom: 5%;
  margin-left: 18%;
}

.CheckoutProcess {
  padding: 5.8rem 6.4rem;
}

.someContent {
  border: 1px solid red;
}

/* removing for demo purposes
@media screen and (max-width: 768px) {
  .checkoutWrapper {
    display: grid;
    grid-template-columns: 100%;
  }
  img {
    display: none;
  }
  .CheckoutProcess {
    padding: 0;
  }
}
*/
<div class="checkoutWrapper">
  <img src="https://via.placeholder.com/300" class="coverIMGLast" />
  <img src="https://via.placeholder.com/300/ff0000" class="phone" />

  <div class="CheckoutProcess">
    <Content />
  </div>
</div>

【问题讨论】:

  • 上面的demo基本上和你的图片很像。它在什么方面是错误的?请修改以更具体地了解问题。如果有一些文本代替 Content 组件可能会有所帮助。
  • 请查看我在问题中提供的link。我认为 sn-p 在所有屏幕上都有静态宽度和高度,这是它没有显示问题的方式
  • 你得到了什么?你可以截图并添加它吗?您发布的那个链接的屏幕宽度很小,因此默认情况下图像是隐藏的,增加尺寸会显示您所描述的图像。你遇到的实际问题是什么?从目前的内容很难判断
  • @someone 如果您已经尝试过我在下面粘贴的回复,我已对其进行了编辑,所以请接受新的回复。
  • 不清楚您的问题是什么,请编辑您的问题以澄清您的代码与您想要的结果不匹配的原因。

标签: html css css-grid


【解决方案1】:

您可以使用transformaspect-ratio 来实现结果:

.checkoutWrapper {
  display: grid;
  grid-template-columns: 30% 70%;
  height: 100vh;
}

.left {
  background-color: #baafa0;
  position: relative;
  
  background-image: url('https://picsum.photos/id/1028/200/300');
  background-size: cover;
}

.phone {
  height: 60%;
  aspect-ratio: 1/2;
  
  position: absolute;
  top: 50%;
  left: 100%;
  transform: translate(-50%, -50%);
  
  border-radius: 15%;
}

.right {
  padding-left: calc(15vh + 1rem);
}
<div class="checkoutWrapper">
  <div class="left">
    <img class="phone" src="https://via.placeholder.com/300/444" />
  </div>
  <div class="right CheckoutProcess">
    <Content>
      <h1>Check Out</h1>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda consectetur ducimus officiis dicta vero distinctio odio sit explicabo illum dolorem quasi facilis repellendus sapiente fuga corporis, iure dolore ad, quis quisquam? Dolorum nostrum, veritatis
        molestiae culpa maiores exercitationem cumque qui harum, unde est ratione doloremque necessitatibus et numquam itaque neque!
      </p>
    </Content>
  </div>
</div>

【讨论】:

    【解决方案2】:

    我编辑了你的code

    你写的都是正确的。

    您只是忘记添加margin-left: 25%。无论您使用哪个屏幕,它总是部分位于第一个屏幕上。 (按中心)

    【讨论】:

      【解决方案3】:

      使用下面的代码,您就拥有了您想要的结构。您所要做的就是使用widthheight 等来制作您需要的东西。

      .checkoutWrapper {
        display: grid;
        grid-template-columns: 50% 50%;
      }
      
      .checkoutImgs {
        position: relative;
        border: 2px solid red;
      }
      
      img{
        object-fit:cover;
        display:block;
      }
      
      .coverIMGLast {
        width: 70%;
        height: 100vh;
        
      }
      
        
      .phone {
        position: absolute;
        height: 80%;
        width:40%;
        top: 10%;
        right: 0;
      
      }
      
      .CheckoutProcess {
        padding: 5.8rem 6.4rem;
        border: 2px solid blue;
      }
      
      
      
      /* removing for demo purposes
      @media screen and (max-width: 768px) {
        .checkoutWrapper {
          display: grid;
          grid-template-columns: 100%;
        }
        img {
          display: none;
        }
        .CheckoutProcess {
          padding: 0;
        }
      }
      */
      <div class="checkoutWrapper">
        <div class="checkoutImgs">
          <img src="https://via.placeholder.com/300" class="coverIMGLast" />
          <img src="https://via.placeholder.com/300/ff0000" class="phone" />
        </div>
      
        <div class="CheckoutProcess">
          <Content />
        </div>
      </div>

      【讨论】:

        【解决方案4】:

        查看此代码,我为您创建了此设计,希望对您有所帮助...

        enter image description here

            body {
                padding: 0;
                margin: 0;
            }
        
            .parent {
                display: flex;
                position: relative;
                align-items: center;
            }
        
            .left {
                width: 37%;
                background-color: blue;
                height: 100vh;
            }
        
            .right {
                width: 63%;
                background-color: red;
                height: 100vh;
        
                display: flex;
                justify-content: center;
                align-items: center;
            }
        
            .image-container {
                position: absolute;
                height: 60%;
                width: 100%;
                display: flex;
                align-items: center;
            }
        
            .image {
                height: 500px;
                width: 250px;
                background-color: grey;
                margin-left: 30%;
                border-radius: 20px;
            }
        <body>
        
        
            <div class='parent'>
                <div class='left'></div>
                <div class='right'>
                    <div>
                        <h3>THis is a heading</h3>
                        <p>this is a para</p>
                        <p>this is a para</p>
                        <p>this is a para</p>
                    </div>
                </div>
        
                <div class="image-container">
                    <div class="image"></div>
                </div>
            </div>
        
        
        </body>

        【讨论】:

          【解决方案5】:

          您可以将第一张图片作为背景图片,然后指定您想要的背景尺寸。然后在&lt;div&gt; 元素中添加第二张图片。

          #bg-img{
            background-image:url('https://images.pexels.com/photos/36029/aroni-arsa-children-little.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500');
            position: absolute;
            width: 100%;
            height: 100%;
            background-repeat: no-repeat;
            background-size: 50% 100%;
            background-color: #999;
          }
          #img-inside{
            position:absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width:15%;
          }
          <div id="bg-img">
            <img id="img-inside" src="https://images.pexels.com/photos/36029/aroni-arsa-children-little.jpg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="second-img" />
          </div>

          我刚刚在sn-p中左div

          【讨论】:

            【解决方案6】:
            * {
              margin: 0;
            }
            
            .checkoutWrapper {
              display: grid;
              grid-template-columns: 35% 25% 40%; /* create three columns to fit both images  */
            }
            
            .coverIMGLast {
              width: calc(100% - 195px); **/* give that illution of real overlapping */**
              height: 100vh; /* please don't delete this line */
              grid-column: 1 / 3; /* overlapping */
              grid-row: 1;
              object-fit: cover;
            }
            /* this .phone class should be dynamically  located partially on first image */
            .phone {
              height: 90vh;
              width: 380px;
              grid-row: 1; /* setting the images on one track / row */
              grid-column: 1/3; /* overlaping */
              justify-self: end;
              transform: translateY(5vh);
            }
            
            .CheckoutProcess {
              padding: 5.8rem 0.5rem 6.4rem; /* Add normal padding to give that effect */
            }
            
            .someContent {
              border: 1px solid red;
            }
            
            @media screen and (max-width: 768px) {
              .checkoutWrapper {
                display: grid;
                grid-template-columns: 100%;
              }
            
              img {
                display: none;
              }
            
              .CheckoutProcess {
                padding: 0;
              }
            }
            
            <div class="checkoutWrapper">
              <img src="https://via.placeholder.com/300" class="coverIMGLast" />
              <img src="https://via.placeholder.com/300/ff0000" class="phone" />
              <div class="CheckoutProcess">
                <div class="someContent">
                  <h1>Some heading</h1>
                  <p> 
                    Some text to go with the paragraph 
                  </p>
                </div>
              </div>
            </div>
            

            https://react-2djy5g-tsheob.stackblitz.io/

            从上面的代码和你编辑的代码的链接可以看出。

            您只需要通过使用grid-column 来制造重叠的错觉并使用模板列来创造一种书本般的感觉。

            与:

            1. grid-template-columns: 35% 25% 40%; /* 创建三列以适应两个图像 */
            2. grid-row: 1; /* 将图像设置在一个轨道/行上 */
            3. grid-column: 1/3; /* 重叠 */
            4. justify-self: end; /* 将图像放在网格的右侧 */

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-07-28
              • 1970-01-01
              • 2018-03-17
              • 2016-09-16
              • 1970-01-01
              • 1970-01-01
              • 2015-06-06
              • 2011-07-05
              相关资源
              最近更新 更多