Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

415
Visualizações
Dynamically set variable padding based on image-height css angular

I have some angular html code that renders a list of images (some of which may be repeats of other images) like so:

<div *ngFor="let content of contentList>
  <img class="image" src="{{ content.image }}"/>
</div>

The css for image:

.image {
  height: 100%;
  max-height: 72px;
}

I'd like to set the padding-bottom for each image based on the height of the image. The padding-bottom should be 72px - height of image--basically, the padding should encompass any leftover pixels of the max-height of 72px.

Something like:

.image {
  height: 100%;
  max-height: 72px;
  padding-bottom: (72 - the image's height)px;
}

How can this be accomplished?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can create a component with a template like the following:

<img
  [src]="path"
  (load)="onLoad()"
  [style.paddingBottom.px]="paddingBottom"
  #image
/>

where path represents the image source link and paddingBottom defines the image bottom padding. The image is flagged with #image identifier to access to the element (ElementRef). As soon as the image is loaded (in the onLoad() function) you can access to the image height and calculate the bottom padding. Hereby the ImageComponent:

export class ImageComponent implements OnInit {

  @Input() paddingBottom = 72;
  @Input() maxPaddingBottom = 72;
  @Input() path = '';
  @ViewChild('image') image?: ElementRef;

  ...

  onLoad(): void {
    if (this.image) {
      const h = (this.image?.nativeElement as HTMLImageElement).height;
      this.paddingBottom = this.maxPaddingBottom - h;
    }
  }
}

Of course you can call this component multiple times inside your component container.

<ng-container *ngFor="let p of paths">
  <app-image [path]='p'></app-image>
</ng-container>
over 4 years ago · Santiago Trujillo Relatório

0

You could keep all the logic in the template so it will work in an ngFor:

<div *ngFor="let content of contentList">
  <img
    class="image"
    (load)="image.style.paddingBottom = 72 - image.height + 'px'"
    [src]="content.image"
    #image
  />
</div>
over 4 years ago · Santiago Trujillo Relatório

0

If the effect you want is a max image height of 72px, could you try wrapping the img in a div and setting the height on that div? Will that give you the visual effect you are looking for? It won't need varying padding, the "padding" will be the space left inside the imgHolder div.

.imgHolder {
  height: 72px;
}

.image {
   max-height: 100%;
   max-width: 100%;
}


<div *ngFor="let content of contentList>
   <div class="imgWrapper"
     <img class="image" src="{{ content.image }}"/>
   </div>
</div>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda