【问题标题】:How to position an image in the bottom of a side(left) div?如何将图像定位在侧(左)div的底部?
【发布时间】:2016-04-27 13:01:24
【问题描述】:

我的标题并不是描述问题的最佳方式。

我有两个 div。左 div 和右 div,我知道如何使它们具有相同的高度,但问题是我必须将图像放在左 div 的底部,如果高度在右侧,图像必须始终位于底部div 变化。

希望你能理解我的问题。

【问题讨论】:

标签: html css image position


【解决方案1】:

在左侧 div 上使用背景图片并将其放置在左下方。

div.left-div {
  background: url(image.jpg) bottom left no-repeat;
  padding-bottom: 200px; /* change this to the height of the image */
}

【讨论】:

    【解决方案2】:

    您可以使用绝对定位。但更现代的方法是将flexboxmargin-top: auto; 一起使用。

    #example
    {
      display: flex;
    }
    
    #one
    {
      display: flex;
    }
    
    #one img
    {
      margin-top: auto;
    }
    <div id="example">
    <div id="one">
      <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150" />
    </div>
    <div id="two">
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height<br />
    This div has a big height
    </div>
    </div>

    【讨论】:

      【解决方案3】:

      如果您在 html 中使用图像,则可以使用position 如下代码,如果您使用背景图像,则使用@Aaron 回答。

      *{margin:0;padding:0;}
        .wrap{
          min-height:300px;
          width:450px;
          position:relative;
          border:1px solid #ccc;
          padding-bottom:150px;
          
        }
        .innerBox{
          margin-bottom:10px;
        }
        figure{
          position:absolute;
          bottom: 0;
          right:0;
        }
        figure img{
          display:block;
        }
      <div class="wrap">
          <div class="innerBox">
           Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br/>
           Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br/>
           exercitationepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
          </div>
          
          <figure>
            <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150">
          </figure>
        </div>

      希望对你有帮助。

      【讨论】:

      • 是的,我明白这一点,但是当我创建一个新的 html 页面时,我总是必须调整左侧 div 的高度,以便图像位于底部。我需要一个总是将图像放在底部的东西,所以我不需要每次都调整高度。
      猜你喜欢
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多