响应图像,固定比例。无法使代码正常工作

时间:2014-08-18 11:23:17

标签: html css image responsive-design

抱歉,我一直在尝试使用网络上的大量教程来尝试使用固定的宽高比使图像响应/缩放,但没有任何工作。

它是一张900x400px的图像,我只是想在较小的屏幕上保持所有图像等。

这里是codepen link

<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>

<div id="aboutsec1">
<div class="parallaxbg">
<div class="parallaxcontainer">
<div class="parallaxcontent">
<h1>header</h1>
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
</div>
</div>
</div>
</div>

<section id="aboutsec2">
<div class="parallaxbg"></div>
<div class="parallaxbg2"></div>
</section>

</body>
</html>

CSS

html,body{
    width:100%;
    height:100%;
  margin: 0;
  padding: 0;
}

/* Basic Parallax Background */
.parallaxbg {
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    height: 100%;
    width: 100%;
    position: relative;
}

/* Basic Parallax Container */
.parallaxcontainer {
    display: table;
    table-layout: fixed;
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: relative;
}

/* Basic Parallax Content */
.parallaxcontent {
    max-width: 450px;
    display: table-cell;
    vertical-align: middle;
    padding-left: 10%;
    padding-right: 10%;
    text-align: center;
}

#aboutsec1{
    height: 100%;
}

/* About Page Section 1 */
#aboutsec1 .parallaxbg {
    background: none;
    background-color: #eeeeee;
    text-align: center;
}

/* About Page Section 2 */
#aboutsec2 {
}

#aboutsec2 .parallaxbg2 {
    background-color: #000000;
    background-image: url('http://s16.postimg.org/k5xrwvp7p/900x400.png');
    z-index: 1;
    background-position: center center;
   background-repeat: no-repeat;
    background-attachment: fixed;
    width: 100%;
    background-size: cover;
  padding-bottom: 44.444444444444%; /* 400px/900px */
}

1 个答案:

答案 0 :(得分:0)

不用担心,我最终弄明白了。这里是固定宽高比响应背景图像的代码,如果有其他人需要它...

<div class="imagecontainer">
    <div class="responsiveimage"></div>
</div>

.imagecontainer {
    margin: 0px auto;
    max-width: 900px; \* For a 900px wide image *\
}

.responsiveimage {
    background-image: url('');
    background-position: center center;
    background-repeat: no-repeat;
    background-size: contain;
    padding-bottom: 44.444444444444%; /* 400px/900px x 100 (for a 900x400 image) */
}

使用总和将填充底部设置为正确的比例,这意味着图像将始终保持相同的宽高比。