html - 如何使 CSS 图像容器适应不同的浏览器尺寸

标签 html css image scaling

我目前正在和一些 friend 一起做一个网站元素,我们已经(在 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/

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

最佳答案

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

* {
  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;
}
<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>

关于html - 如何使 CSS 图像容器适应不同的浏览器尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39332298/

相关文章:

html - 居中堆叠绝对元素+居中 flex 列垂直于窗口高度

html - CSS 表格响应格式

html - 水平线的 HR 标签不起作用

html - 防止内容扩展或溢出列

c# - 将查询字符串参数传递给第二页

css - 根据条件为 Angular 选择器的外部 div 添加样式

android - 背景颜色/图像不覆盖移动设备上 div 的宽度

css - 图像在最小化的 Firefox 窗口中无法正确调整大小

html - Bootstrap 3 .img 响应图像不是链接内的响应图像

javascript - 使用 jQuery attr 检查 img src 是否为空