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

347
Views
Why won't this second item center in the div?

HTML

<div id="flexbox-container">
  <h1 id="test1">test1</h1><h1 id="test2">test2</h1><h1 id="test3">test3</h1>
</div>

CSS

#flexbox-container {
      display:inline-flex;
    }
#test1 {
      float:left;
    }
    #test2 {
      justify-content:center;
      align-items:center;
      text-align:center;
      align-self:center;
      align-content:center;
    }
    #test3 {
      position:relative;
      left:1000px;
    }

Why does test2 not center itself in the flex? I would prefer not to have to set px or margin to get it to centre. I tried all sorts of aligning stuff on it yet it still sticks to the left. I need the three items to be inline, so setting it to flex wouldn't work (though it does center align if I make it flex), PLEASE HELP IVE BEEN TRYING FOR DAYS

https://codepen.io/throwaway123/pen/mdpJJKY

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Only this much code is enough. No need for all those styles for separate h1 tags. You have to give the aligning styles to the parent div.

#flexbox-container {
  width: 100%;
  display:inline-flex;
  justify-content: space-between;
}
<div id="flexbox-container">
  <h1 id="test1">test1</h1>
  <h1 id="test2">test2</h1>
  <h1 id="test3">test3</h1> 
</div>

over 4 years ago · Santiago Trujillo Report

0

Using IDs for css is bad practice. I'd suggest you to start using class selectors

Anyway, here is solution to your problem :

<style>
    #flexbox-container {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
   }
</style>
over 4 years ago · Santiago Trujillo Report

0

Basically that isn't how flex works.

You don't want the contents of the second item to be justified within itself, you want the container to have that element centered.

If you scrap all the positioning of the three items you can get flex to do the work for you. There are several ways of telling it how you want the items set out in the line. For example justify-content: space-between.

From MDN:

The items are evenly distributed within the alignment container along the main axis. The spacing between each pair of adjacent items is the same. The first item is flush with the main-start edge, and the last item is flush with the main-end edge.

#flexbox-container {
  display: inline-flex;
  justify-content: space-between;
  width: 100vw;
}
<div id="flexbox-container">
  <h1 id="test1">test1</h1>
  <h1 id="test2">test2</h1>
  <h1 id="test3">test3</h1>
</div>

over 4 years ago · Santiago Trujillo 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!