Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!