javascript - 图像溢出粘性 div 尺寸

标签 javascript jquery html css

我的页面中有一个粘性 div,它的高度基于屏幕尺寸(因此它可以在横向和纵向之间切换)和其中的图像。我遇到了图像无法在其父级 div 中正确调整自身大小的问题。我不能依赖于容器 div 的高度,因为它取决于屏幕尺寸。我也不能依赖图像上方文本的高度。这是问题的一个例子。

Codepen

<div class="textHolder">
    <div class="sticky">
        <h3>Java script</h3>
        <img src="https://placehold.it/350x150" />
    </div>
</div>

CSS:

.textHolder{
  margin:100px auto;
  width:700px;
  height:300px;
  background-color: #FFEEE7;
  padding:15px;
}
.sticky img{
  height: 100%;
  margin: auto;
}
.sticky h3 { 
  width: 100%;
  padding: 0;
  margin: 0;
}
.sticky{
  position:-webkit-sticky;  
  position: sticky;  
  background:rgb(50,50,250);
  width:100%;
  height:100px;
  border-radius:5px;
  text-align:center;
  line-height:46px;
  color:white;
  font-size:30px;
  top:10px;
}

最佳答案

将 .sticky 元素的显示设置为表格

.sticky{
  position:-webkit-sticky;  
  position: sticky;  
  background:rgb(50,50,250);
  width:100%;
  height:100px;
  border-radius:5px;
  text-align:center;
  line-height:46px;
  color:white;
  font-size:30px;
  top:10px;
  display:table;
}

演示:https://codepen.io/anon/pen/LxgOOq

关于javascript - 图像溢出粘性 div 尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42136778/

相关文章:

javascript - 如何在 javascript 中获取最接近的输入类型文本值?

javascript - Grails:twitter bootstrap 3 模式在每次迭代中都有相同的内容

javascript - 父组件如何将 HTML 传递给 Angular 中的子组件?

javascript - 如何在 ASP.NET MVC 中使用 Razor View 从对象列表中获取 Controller 中的单个对象

javascript - 使用渐变文本更改元素的 HTML 内容不会在 chrome 中重新呈现

javascript - 为什么单击此复选框后无法取消选中?

javascript - 如何使用 JavaScript 函数获取 HTML 值

检测到颜色时单击的 JavaScript 命令

javascript - 优化javascript数字功能

javascript - jQuery detach() vs remove() vs hide()