英文:
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>
这是第一张图片:
这是第二张图片:
英文:
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 -->
<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>
<!-- end snippet -->
Here is first image:
And here is second one:
答案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: flex
、align-items: center
和 justify-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)在父元素中居中,您应该:
- 为父元素设置正确的宽度和高度
- 对父元素(在您的情况下是.home-images)应用position: relative,display: flex,align-items: center和justify-content: center
- 对子元素应用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:
- Give your parent element a correct width and height
- Apply position: relative, display: flex, align-items: center and justify-content: center to your parent element (.home-images in your case)
- 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 -->
<section class="home">
<div class="container">
<div class="images">
<img class="big-img"/>
<img class="small-img"/>
</div>
</div>
</section>
<!-- 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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论