如何使CSS图像容器适应不同的浏览器大小

时间:2016-09-05 14:02:38

标签: html css image scaling

我目前正在与一些朋友合作开展一个网站项目,我们已经(在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/

我们应该如何调整代码以使其适应最常见的屏幕尺寸?我们是否必须为每个不同的屏幕执行不同的代码?

1 个答案:

答案 0 :(得分:1)

这是一个jsFiddle,但我建议你在单独的文件中试一试,这样你就可以调整屏幕大小,这对我有用,祝你好运。

      /*
       * IF
       *   we were invoked as `bash -c' (startup_state == 2) AND
       *   parse_and_execute has not been called recursively AND
       *   we're not running a trap AND
       *   we have parsed the full command (string == '\0') AND
       *   we're not going to run the exit trap AND
       *   we have a simple command without redirections AND
       *   the command is not being timed AND
       *   the command's return status is not being inverted
       * THEN
       *   tell the execution code that we don't need to fork
       */
* {
  margin: 0;
  padding: 0;
}


.accordian ul{ width:auto;} 
.accordian {    
width: auto;
overflow:hidden;
    height: 100%;
    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);
}


.accordian li { 

    width: calc(95% / 3);
 position: relative;
    display: inline-block;
	height:100%;
    border-left: 1px solid #888;
    transition: all 0.5s ease 0s;

  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);
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
	see the basic accordian*/
}


.accordian ul:hover li{
  width:10%;
}
.accordian ul li:hover {

  width:calc( 95% - (10%*3));
  background-color:rgba(0,0,0,0.5);
}

.accordian li img {
  display: block;
  width:auto;
  height:100%;
}


.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: 1%;
  font-size: 30px;
}