gpt4 book ai didi

css - 中心 react 比div

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:00 24 4
gpt4 key购买 nike

我想为我的家庭自动化系统创建网络应用程序,但我对我家的 img 有疑问。

我想要的是我家的居中(垂直和水平)图像,响应灵敏且保持图像的比例。

首先我尝试:

body {
color: #000;
background: #3A3D50;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.container {
width: 100%;
margin: 0 auto;
}
.rect {
width: 100%;
height: 0;
padding-bottom: 50%;
text-align: left;
background-image: url('http://www.bashihq.com/wp-content/uploads/2015/08/3d-house-floor-plans-modern-3d-floor-plans-are-also-a-great-way-for-architects-realtors-and-at-house-floor.jpg');
background-repeat: no-repeat;
background-size: contain;
background-position: center;
position:relative;
}
    <div class="container"> 
<div class="rect">
</div>
</div

效果很好,但没有垂直居中。

所以我尝试添加一些绝对定位:

body {
color: #000;
background: #3A3D50;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.container {
width: 100%;
height: 50%;
margin: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.rect {
width: 100%;
height: 0;
padding-bottom: 50%;
text-align: left;
background-image: url('http://www.bashihq.com/wp-content/uploads/2015/08/3d-house-floor-plans-modern-3d-floor-plans-are-also-a-great-way-for-architects-realtors-and-at-house-floor.jpg');
background-repeat: no-repeat;
background-size: contain;
background-position: center;
position:relative;
}
    <div class="container"> 
<div class="rect">
</div>
</div>

但是如何将 .rect 置于 .container 中心?

非常感谢。

编辑:

我忘记精确了,我需要在我的图像上放置 div 以保持位置。

body {
color: #000;
background: #3A3D50;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.container {
width: 100%;
height: 50%;
margin: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.rect {
width: 100%;
height: 0;
padding-bottom: 50%;
text-align: left;
background-image: url('http://www.bashihq.com/wp-content/uploads/2015/08/3d-house-floor-plans-modern-3d-floor-plans-are-also-a-great-way-for-architects-realtors-and-at-house-floor.jpg');
background-repeat: no-repeat;
background-size: contain;
background-position: center;
position:relative;
}
.rect2 {
width: 3%;
padding-bottom: 3%;
position: absolute;
top: 40%;
left: 31.5%;
background-color: blueviolet;
border-radius: 100px;
}
.rect4 {
width: 3%;
padding-bottom: 3%;
position: absolute;
top: 25%;
left: 60%;
background-color:rgba(0, 0, 0, 0.6);
border-radius: 100px;
}
.rect3 {
width: 50%;
height:50%;
background-image: url('images/light.png');
background-repeat: no-repeat;
background-size: contain;
position: absolute;
top: 25%;
left:32%;
}

代码

<body>
<div class="container">
<div class="rect">
<div class="rect2"><div class="rect3"></div></div>
<div class="rect4"><div class="rect3"></div></div>
</div>
</div>
</body>

最佳答案

您可以使用transform:translate (CSS3)

CSS

body {
color: #000;
background: #3A3D50;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
}
.container {
width: 100%;
height: 50%;
margin: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.rect {
position: relative;
display: block;
height: 350px; // Adjust your needs
width: 100%;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background-image: url('http://www.bashihq.com/wp-content/uploads/2015/08/3d-house-floor-plans-modern-3d-floor-plans-are-also-a-great-way-for-architects-realtors-and-at-house-floor.jpg');
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}

DEMO HERE

关于css - 中心 react 比div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779828/

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