如何将CSS背景图设置在左下角?现有代码无法实现求解决方案
背景图固定在左下角的实现方案
原代码里的float: left只能让元素靠左排列,没法固定在底部,而且#Bg,img选择器会选中所有img标签,容易干扰其他元素。可以用以下两种方法实现需求:
方法一:针对img元素定位
给目标元素设置绝对定位,配合父容器的相对定位来固定位置:
/* 给父容器添加(如果父容器是body可省略,但建议设置min-height确保占满视口) */ body { position: relative; min-height: 100vh; margin: 0; } #Bg { width: 150px; margin: 0; padding: 0; position: absolute; left: 0; bottom: 0; }
方法二:作为背景图使用
如果是把这张图作为元素的背景,直接用background-position属性:
#Bg { width: 100%; min-height: 100vh; margin: 0; padding: 0; background-image: url('导航用六边形背景图'); background-position: left bottom; background-repeat: no-repeat; background-size: 150px; }
内容的提问来源于stack exchange,提问作者Disa Soni
相关产品推荐
相关产品推荐

