【发布时间】:2017-01-12 22:12:08
【问题描述】:
我目前正在和一些朋友一起做一个网站项目,我们已经(在 SO 的帮助下)为我们的主页制作了一个图像容器,但我们希望它适应大多数屏幕尺寸(如 1920x1080, 1366x768 等,所以我猜是 16:9,还没有太多关于平板电脑和智能手机的知识)。
我们已经尝试使用此处提到的解决方案 (Automatically resize images with browser size using CSS),但它只是非常奇怪地缩放图像而不是基于浏览器,只是将它们缩小到容器内。
我们也尝试了css get height of screen resolution,但也得到了一些奇怪的结果。
* {
margin: 0;
padding: 0;
}
body {
background: #ccc;
font-family: arial, verdana, tahoma;
}
/*Time to apply widths for accordian to work
Width of image = 1840px
total images = 3
so width of hovered image = 1840px
width of un-hovered image = 40px - you can set this to anything
so total container width = 1840 + 40*2 = 1920px;
default width = 1920/3 = 640px;
*/
.accordian {
width: 1920px;
height: 1000px;
overflow: hidden;
/*Time for some styling*/
margin: 0px auto;
box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.35);
-webkit-box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.35);
-moz-box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.35);
}
/*A small hack to prevent flickering on some browsers*/
.accordian ul {
width: 2000px;
/*This will give ample space to the last item to move
instead of falling down/flickering during hovers.*/
}
.accordian li {
position: relative;
display: block;
width: 640px;
float: left;
border-left: 1px solid #888;
box-shadow: 0 0 25px 10px rgba(0, 0, 0, 0.5);
-webkit-box-shadow: 0 0 25px 10px rgba(0, 0, 0, 0.5);
-moz-box-shadow: 0 0 25px 10px rgba(0, 0, 0, 0.5);
/*Transitions to give animation effect*/
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
/*If you hover on the images now you should be able to
see the basic accordian*/
}
/*Reduce with of un-hovered elements*/
.accordian ul:hover li {
width: 40px;
}
/*Lets apply hover effects now*/
/*The LI hover style should override the UL hover style*/
.accordian ul li:hover {
width: 1840px;
}
.accordian li img {
display: block;
}
/*Image title styles*/
.image_title {
background: rgba(0, 0, 0, 0.5);
position: absolute;
left: 0;
bottom: 0;
width: 1840px;
}
.image_title a {
display: block;
color: #fff;
text-decoration: none;
padding: 60px;
font-size: 30px;
}
<!doctype html5>
<head>
<title>CdS Home</title>
<link rel='stylesheet' href='index.css' />
</head>
<div class="accordian">
<ul>
<li>
<div class="image_title">
<a href="#">Event 1</a>
</div>
<a href="#">
<img src="https://images.duckduckgo.com/iu/?u=http%3A%2F%2Fwww.wallpaperup.com%2Fuploads%2Fwallpapers%2F2015%2F01%2F30%2F606369%2F5510b472cf9126e305bb941e8ec8d9e8.jpg&f=1" />
</a>
</li>
<li>
<div class="image_title">
<a href="#">Event 2</a>
</div>
<a href="#">
<img src="https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.betaseries.com%2Fimages%2Ffonds%2Foriginal%2F9296_1415901083.jpg&f=1" />
</a>
</li>
<li>
<div class="image_title">
<a href="#">Event 3</a>
</div>
<a href="#">
<img src="https://images.duckduckgo.com/iu/?u=http%3A%2F%2Fwww.wallpaperup.com%2Fuploads%2Fwallpapers%2F2015%2F01%2F30%2F606369%2F5510b472cf9126e305bb941e8ec8d9e8.jpg&f=1" />
</a>
</li>
</ul>
</div>
And here is the CSS file /*Now the styles*/
这是一个运行代码的 JSFiddle 链接 - https://jsfiddle.net/qvpy07vd/
我们应该如何调整代码以适应最常见的屏幕尺寸?我们是否必须为每个不同的屏幕编写不同的代码?
【问题讨论】:
-
看看响应式设计,你想尝试让事情变得流畅,以便它们可以很好地扩展。您还可以查看媒体查询以在某些断点处隐藏某些内容。我目前使用一种自适应方法(提供给手机和平板电脑的完全不同的模板),它在不同的设计宽度之间是响应式的。基本上,您需要将绝对测量值(即像素)更改为相对测量值(即百分比)
-
@Pete 我也看到了这一点,但由于某种原因,答案被否决了。我会看看我能用百分比做什么:) 谢谢。
-
先纠正你
html:) -
@MoolsBytheway 你什么意思?它工作正常,但我不习惯 HTML 约定,所以我看不出有什么要纠正的。
-
没关系,我的错