html - 如何制作带有媒体标签的响应式图片?

标签 html css twitter-bootstrap

我的响应式 div 标签有这些 ul 和 li。我有一个 CSS 来设置标签的背景。

<ul class="list-unstyled">
    <li><a href="#" id="Feature1"  ></a></li>
    <li><a href="#" id="Feature2"  ></a></li>
</ul>

CSS:

 @media screen and (max-width: 480px) {      
    #Feature1{background: url('../img/Feature1.png');
       background-repeat: no-repeat;
       background-size: 60px auto;
       display: inline-block;width:60px;height:30px;}
    }

标签背景图像未调整大小。

我尝试过使用标签的 100% 宽度和高度,但这并没有解决问题。

最佳答案

与此处的评论不同,我认为您的问题不在于背景大小,而在于您的 a 元素。我认为您的预期结果是根据宽度自动调整高度。

你可以这样解决这个问题:

@media screen and (max-width: 480px) {      
  #Feature1{
    background: url('../img/Feature1.png');
    background-repeat: no-repeat;
    background-size: 100% auto;
    display: inline-block;
    width:100%;
    padding-bottom: 50%;
  }
}

因为垂直填充是相对于元素宽度的。所以 60x30 比率表示 padding-bottom: 50%如果你有一个方形图像,你会使用 padding-bottom: 100% ;

就我个人而言,我会选择 <img>在这里标记,因为它更容易让父元素自动调整它(它甚至是默认行为)。

关于html - 如何制作带有媒体标签的响应式图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31956706/

相关文章:

css -::before 元素在没有 display:flex 的情况下不会显示

javascript - jQuery 一页滚动不起作用

html - 如何在 glyphicon Bootstrap 周围创建正方形?

javascript - jQuery动态调整div的大小

html - Bootstrap 的 Div 背景缩放

css - I在同一页面中创建 2 种不同的布局/设计

javascript - 创建 float 菜单

database - 我如何看待 "IndexedDB"?

javascript - 触发引导模式隐藏 (hide.bs.modal) 事件后,$location.path 需要一些时间才能工作

javascript - 使用html按钮用Javascript修改img src