【问题标题】:Background size is half of div背景大小是 div 的一半
【发布时间】:2021-11-18 14:37:32
【问题描述】:

我需要一个 div 宽度为 50% 的背景(从左边缘到中心)。并且背景图像在屏幕改变时不应该改变比例。现在我有了css:

.bg {
  display: flex;
  justify-content: center;
  align-items: stretch;
  flex-direction: column;
  background: url(../img.png);            
  background-position: left;
  background-size: 50vw 100%;
  -webkit-background-size: 50vw 100%; /* browser support */
  -moz-background-size: 50vw 100%; /* browser support */
  -o-background-size: 50vw 100%; /* browser support */
  background-repeat: no-repeat;
}
<div class="bg container-fluid px-0">
  <div class="container-xl">
    <div class="left"></div>
    <div class="right"></div>
  </div>
</div>

img的尺寸:1043*735

但图像在不同屏幕上会改变比例(压缩或拉伸)。有没有办法做图像“覆盖”但仍然是 div 的一半?

【问题讨论】:

  • 您能提供照片的尺寸或绝对参考吗?
  • 1043x735px

标签: css background-image


【解决方案1】:

添加一个覆盖一半宽度的左侧填充并将背景剪辑到内容框,然后您可以使用封面:

.bg {
  height:100vh;
  padding-left:50%;
  background: url(https://picsum.photos/id/237/200/300);
  background-position:center;
  background-size: cover;
  background-origin:content-box;
  background-clip:content-box;
}


body {
 margin:0;
}
&lt;div class="bg"&gt;&lt;/div&gt;

【讨论】:

  • 它对我不起作用,我认为 padding-left 与背景无关
  • @Newta 它在 sn-p 中工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 2019-03-05
  • 2012-04-10
  • 2011-03-18
  • 2023-03-23
  • 2019-10-16
相关资源
最近更新 更多