中心响应率div

时间:2015-09-25 10:08:40

标签: css

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

我想要的是拥有我家的中心(verticaly和horizo​​ntaly)图像,响应并仍然保持图像的比例。

首先我尝试:

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>

但是如何直接进入.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>

1 个答案:

答案 0 :(得分:1)

您可以使用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