How to center a div with 2 photos ,one covering another in mobile screen resolution

huangapple go评论72阅读模式
英文:

How to center a div with 2 photos ,one covering another in mobile screen resolution

问题

我尝试将一个包含两张照片的 div 置于正中央,但效果不如预期。尝试了不同的定位方式、使用 flex 布局、align-items 和 justify-content 属性等,但都无法将图像容器移到中心位置。

以下是我的代码:

.grid {
  display: grid;
  gap: 1.5rem;
}

.section {
  padding: 4.5rem 0 2rem;
}

.home_container {
  position: relative;
  align-items: center;
  justify-content: center;
}

.home_images {
  position: absolute;
  display: flex;
  left: 50%;
  top: 50%;
}

.home_big-img {
  z-index: 12;
}

.home_small-img {
  transform: translateX(-9rem);
}
<section class="section home grid">
  <div class="home_container container">
    <div class="home_images">
      <img src="./img/man.png" alt="" class="home_big-img">
      <img src="./img/plane.png" alt="" class="home_small-img">
    </div>
  </div>
</section>

这是第一张图片:

How to center a div with 2 photos ,one covering another in mobile screen resolution

这是第二张图片:

How to center a div with 2 photos ,one covering another in mobile screen resolution

英文:

I tried to center a div with 2 photos exactly in the center. However it didn't go well. Tried different types of positioning, display flex, align items justify content, etc, nothing allows me to
move image container to the center.

Here is my code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.grid {
  display: grid;
  gap: 1.5rem;
}

.section {
  padding: 4.5rem 0 2rem;
}

.home_container {
  position: relative;
  align-items: center;
  justify-content: center;
}

.home_images {
  position: absolute;
  display: flex;
  left: 50%;
  top: 50%;
}

.home_big-img {
  z-index: 12;
}

.home_small-img {
  transform: translateX(-9rem);
}

<!-- language: lang-html -->

&lt;section class=&quot;section home grid&quot;&gt;
  &lt;div class=&quot;home_container container&quot;&gt;
    &lt;div class=&quot;home_images&quot;&gt;
      &lt;img src=&quot;./img/man.png&quot; alt=&quot;&quot; class=&quot;home_big-img&quot;&gt;
      &lt;img src=&quot;./img/plane.png&quot; alt=&quot;&quot; class=&quot;home_small-img&quot;&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

Here is first image:

How to center a div with 2 photos ,one covering another in mobile screen resolution

And here is second one:

How to center a div with 2 photos ,one covering another in mobile screen resolution

答案1

得分: 0

这会有帮助吗?

.home_images{
  position: absolute;
  display: flex;
  left: 50%;
  transform: translateX(-50%);
}

如果您想垂直居中,可以使用translateY而不是translateX,并使用top: 50%而不是left: 50%

英文:

Will this help?

.home_images{
  position: absolute;
  display: flex;
  left: 50%;
  transform: translateX(-50%);

}

In case you want to make it vertically center, use translateY instead of translateX and top: 50% instead of left: 50%

答案2

得分: 0

为了使图像居中,您可以使用 flexbox。

.grid {
  display: grid;
  gap: 1.5rem;
}
    
.section {
  padding: 4.5rem 0 2rem;
}
    
.home_container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
    
.home_images {
  display: flex;
  align-items: center;
  justify-content: center;
}
    
.home_big-img {
  z-index: 12;
}

.home_small-img {
  transform: translateX(-9rem);
}

在修改后的代码中,我从 .home_images 类中移除了 position: absolute 并将其替换为 .home_container 类中的 display: flexalign-items: centerjustify-content: center,以使整个图像容器居中在屏幕上。我还将容器的高度设置为 100vh,以确保它占满整个视口高度。

保存更改,现在图像应该在移动屏幕分辨率上居中显示。

英文:

To center the images, you can use flexbox.

.grid {
  display: grid;
  gap: 1.5rem;
}
    
.section {
  padding: 4.5rem 0 2rem;
}
    
.home_container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
    
.home_images {
  display: flex;
  align-items: center;
  justify-content: center;
}
    
.home_big-img {
  z-index: 12;
}

.home_small-img {
  transform: translateX(-9rem);
}

In the modified code, I removed the position: absolute from the .home_images class and replaced it with display: flex, align-items: center, and justify-content: center in the .home_container class to center the entire image container on the screen. I also set the height of the container to 100vh to make sure it takes up the full viewport height.

Save the changes, and the images should now be centered on the mobile screen resolution.

答案3

得分: 0

要使子元素(在您的情况下为img)在父元素中居中,您应该:

  1. 为父元素设置正确的宽度和高度
  2. 对父元素(在您的情况下是.home-images)应用position: relativedisplay: flexalign-items: centerjustify-content: center
  3. 对子元素应用position: absolute
section {
  width: 200px;
  height: 200px;
}

.container {
  width: 100%;
  height: 100%;
}

.images {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.big-img {
  position: absolute;
  width: 100px;
  height: 100px;
  background-color: red;
}

.small-img {
  position: absolute;
  width: 50px;
  height: 50px;
  background-color: blue;
}
<section class="home">
  <div class="container">
    <div class="images">
      <img class="big-img"/>
      <img class="small-img"/>
    </div>
  </div>
</section>

祝您有一个愉快的一天!

英文:

To center child elements (img in your case) in a parent element, you should:

  1. Give your parent element a correct width and height
  2. Apply position: relative, display: flex, align-items: center and justify-content: center to your parent element (.home-images in your case)
  3. Apply position: absolute to your child elements

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

section {
  width: 200px;
  height: 200px;
}

.container {
  width: 100%;
  height: 100%;
}

.images {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.big-img {
  position: absolute;
  width: 100px;
  height: 100px;
  background-color: red;
}

.small-img {
  position: absolute;
  width: 50px;
  height: 50px;
  background-color: blue;
}

<!-- language: lang-html -->

&lt;section class=&quot;home&quot;&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;images&quot;&gt;
      &lt;img class=&quot;big-img&quot;/&gt;
      &lt;img class=&quot;small-img&quot;/&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

Have a good day!

答案4

得分: 0

Sure, here is the translated code:

.grid {
  display: grid;
  gap: 1.5rem;
}

.section {
  padding: 4.5rem 0 2rem;
}

.home_container {
  position: relative;
  align-items: center;
  justify-content: center;
}

.home_images {
  position: absolute;
  display: flex;
  left: 50%;
  top: 50%;
}

.home_big-img {
  z-index: 12;
}

.home_small-img {
  transform: translateX(-9rem);
}
<section class="section home grid">
  <div class="home_container container">
    <div class="home_images">
      <img src="./img/man.png" alt="" class="home_big-img">
      <img src="./img/plane.png" alt="" class="home_small-img">
    </div>
  </div>
</section>
英文:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

.grid {
  display: grid;
  gap: 1.5rem;
}

.section {
  padding: 4.5rem 0 2rem;
}

.home_container {
  position: relative;
  align-items: center;
  justify-content: center;
}

.home_images {
  position: absolute;
  display: flex;
  left: 50%;
  top: 50%;
}

.home_big-img {
  z-index: 12;
}

.home_small-img {
  transform: translateX(-9rem);
}

<!-- language: lang-html -->

&lt;section class=&quot;section home grid&quot;&gt;
  &lt;div class=&quot;home_container container&quot;&gt;
    &lt;div class=&quot;home_images&quot;&gt;
      &lt;img src=&quot;./img/man.png&quot; alt=&quot;&quot; class=&quot;home_big-img&quot;&gt;
      &lt;img src=&quot;./img/plane.png&quot; alt=&quot;&quot; class=&quot;home_small-img&quot;&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月31日 21:07:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定