简体   繁体   English

如何在元素的底部对齐图像?

[英]How can I align an image right at the bottom of an element?

I want to put the image (which is for now just a placeholder) right at the bottom of its leftColumn, how can I achieve this? 我想将图像(现在只是一个占位符)放在它的leftColumn的底部,如何实现呢?

在此处输入图片说明

I've tried adjusting the margin for the <img> class. 我试过调整<img>类的边距。 I thought auto on the top margin (seeing as I've set its height) would work the same way auto works for centering items. 我以为auto在顶部边缘(看到我已经设置了高度)将与auto用于居中项的方式相同。 No change. 没变。

Do I need to specify the margin for the top of the image (dependant on its height), or can I do it another way? 是否需要指定图像顶部的margin (取决于图像的高度),还是可以用其他方式指定?

I've also tried vertical-align: bottom; 我也尝试过vertical-align: bottom; , but again no change. ,但仍然没有变化。

Here's my HTML: 这是我的HTML:

<div class="pageArea">
  <div class="rowHalfImages">
    <div class="rowFirstPad"> <img width="100%" height="300px" /> </div>
  </div>
  <div class="rowHalf">
    <div class="rowSecondPad">
      <h1>DSC</h1>
      <p class="introPara">Hello.</p>
      <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p>
      <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p>
    </div>
  </div>
  <div class="clearBoth"></div>
</div>

My CSS: 我的CSS:

.pageArea {
    max-width: 1200px;
    margin: 0px auto;
    padding: 0;
}
.rowHalf {
    width: 50%;
    float: right;
    margin: 0;
    padding: 0;
}   
.rowHalfImages {
    width: 50%;
    float: left;
    height: 100%;
    padding: 0;
}
.rowFirstPad {
    padding: 0 2% 0 2%;
}
.rowFirstPad img {
    vertical-align: bottom;
    margin: auto 0;
}
.rowSecondPad {
    padding: 0 0 0 2%;
}

The easy way is to give .rowHalfImages a position:relative, then give the image inside it a position:absolute, this will allow you to position the image at the bottom and still have other things on top if you want. 最简单的方法是给.rowHalfImages一个position:relative,然后给它里面的图像一个position:absolute,这将使您可以将图像放置在底部,如果需要,还可以在顶部放置其他内容。

.rowHalfImages{
    position:relative;
    height:auto;
    /* other style you want */
}
.rowHalfImages img {
    position:absolute;
    left:10px;
    bottom:10px;`
    /* other style you want */
}

change this class like bellow : 像下面这样更改此类:

.rowFirstPad {
    padding: 0 2% 0 2%;    
    padding-top: 100%;
}

By changing percentage you are able to set it as you want! 通过更改百分比,您可以根据需要进行设置!

Another approach would be to use display:table if you wanted to use vertical-align. 如果要使用垂直对齐,则另一种方法是使用display:table。 Using display: table has some side effects especially on older browsers but in your given setup it works fine. 使用display:table有一些副作用,尤其是在旧版浏览器上,但是在给定的设置下它可以正常工作。

.pageArea {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0;
    display: table;
}
.rowHalf {
    margin: 0;
    padding: 0;
}   
.rowHalfImages {
    display:table-cell;
    vertical-align: bottom;
    padding: 0;
}
.rowFirstPad {
    padding: 0 2%;
}
.rowFirstPad img {
    display: block;
}

Basically, you could also position absolute and remove some of your html: 基本上,您还可以定位绝对位置并删除一些html:

 .pageArea { max-width: 1200px; margin: 0 auto; padding: 0; } .rowHalf { position: relative; padding: 0 1% 0 51%; } .rowHalf img { position: absolute; bottom: 0; left: 1%; width: 300px; height: 300px; } 
 <div class="pageArea"> <div class="rowHalf"> <h1>DSC</h1> <img src="" alt="failed to load" style="position: absolute;border: 1px solid;left: 1%;width: 300px;height: 300px;bottom: 0;" width="" height=""> <p class="introPara">Hello.</p> <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p><p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p><p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p><p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p> <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p> </div> </div> 

The flex solution is also quite nice so you see: there are lots of ways, it all depends on what you need. flex解决方案也非常不错,因此您会看到:有很多方法,这完全取决于您的需求。

if you used display instead float, things would be much easier (see answer about table). 如果使用display而不是float,事情会容易得多(请参阅有关表的答案)。

Nowdays flex is quiet powerfull:( flex-fow:column + justify-content:space-between did it) 如今,flex安静而强大:( flex-fow:column + justify-content:space-between做到了)

 .pageArea { max-width: 1200px; margin: 0px auto; padding: 0; display: flex; } .rowHalfImages, .rowHalf { flex: 1; margin: 1em 0; padding: 0 1em; background: lightgray; } .rowHalfImages { position: relative; display: flex; flex-flow: column; justify-content: space-between; } 
 <div class="pageArea"> <div class="rowHalfImages"> <img src="http://lorempixel.com/800/300/people" width="100%" /> <img src="http://lorempixel.com/800/300/nature" width="100%" /> </div> <div class="rowHalf"> <div class="rowSecondPad"> <h1>DSC</h1> <p class="introPara">Hello.</p> <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p> <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p> <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p> <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p> <p>Lorem upsum diosks d amei sfor. More dojrn nbutforge absirns sie, for the siemssmsie a s. During which time the enovleo, will apela, hibt bdeue nf o the dirj, sisnr rufndf. Soerm iejrj ssleoe, orf risms, prod, ddvs sos anmd. Recent doejd aba dor the ssie dimjsiuend duekopmnvuer dmuie doe. Maolske, nfi sikeie. Adie dows. Osleifspd fi tje sos, gjsdijchfu.</p> </div> </div> </div> 

Note: i have removed a couple of div 注意:我删除了几个div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM