Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
React.js - Styled components - center image in direct div parent with other objects

I understand that question looks pretty easy, but I haven't found anything helping me.

I have an image inside a div, and I want that image to be centered in that div even if I add, for example, a text.

Here is some code and images to better understand that problem :

If I only have my image in my div (with solid border), all is well :

<div>
    <img height = '75px' width = '75px' src={USDCoin}/> 
</div>      

enter image description here

But adding a text, I can't figure out how to keep the image in the center of the div (well aligned with the grey lines) and the text below that :

<div>
    <img height = '75px' width = '75px' src={USDCoin}/> 
    test
</div>  

enter image description here

Here is a full example :

<div style = {{display: flex;
flex-direction: row;
height: 90%;
width: 100%;
justify-content: center;
align-items: center;}}>
   <Line/>
   <div>
      <img height = '75px' width = '75px' src={USDCoin}/>   
   </div>   
   <Line/>
   <Line/>
   <div>
      <img height = '75px' width = '75px' src={USDCoin}/>   
      test
   </div>   
   <Line/>
</div>

Thank you in advance for your precious help !

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use flexbox to do this very easily. I've created an example here for you (doesn't include styled-components but your issue is not the styled components anyway) https://codesandbox.io/s/solitary-night-7859bz

about 4 years ago · Juan Pablo Isaza Denunciar

0

you should use flex-direction: column;

import { React } from "react";
export default function Demo() {
  return (
    <>
      <div
        style={{
          display: "flex",
          flexDirection: "column",
          height: "90%",
          width: "100px",
          border: "2px solid",
          justifyContent: "center",
          margin: "0 auto",
          alignItems: "center"
        }}
      >
        <div>
          <img
            height="75px"
            width="75px"
            src={
              "https://images.unsplash.com/photo-1638913662252-70efce1e60a7?ixlib=rb-1.2.1&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80"
            }
          />
        </div>

        <div>test</div>
      </div>
    </>
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda