【问题标题】:Responsive image, fixed ratio. Can't get code working响应式图像,固定比例。无法让代码工作
【发布时间】:2014-08-19 23:20:23
【问题描述】:

对不起,我在网上尝试了很多教程,试图让这个图像响应/缩放固定宽高比,但没有任何效果。

这是一张 900x400 像素的图片,我只是想让它在较小的屏幕等上都能看到。

这是密码笔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 */
}

【问题讨论】:

  • 嗨!我已经在我的手机上尝试过你的 codepen,背景图像似乎反应灵敏。能否请您提供有关您的目标的更多细节?
  • 你确定吗?有一个带有背景图像的黑色部分,上面写着 900x400 并且有白色边角。我想在黑色部分显示整个图像。这目前不起作用。谢谢。

标签: html css image responsive-design


【解决方案1】:

别担心,我最终自己想通了。如果其他人需要,这是固定宽高比响应背景图像的代码...

<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) */
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 2014-01-25
    相关资源
    最近更新 更多