Steven Paslawsky
Steven Paslawsky

Reputation: 13

Using flex box how can I center text under a div

I am trying to center text under a circular div using flexbox but I cannot figure it out. My goal is to have icons in the circle with text underneath as a title but the h2 ends up beside the circle and I can't figure out how to place it at the bottom of the circle. Thank you for your help!

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
}

.flex-container {
  display: flex;
  flex-wrap: wrap;
  width: 1000px;
  align-content: space-between;
  align-tems: center;
  justify-content: center;
  position: absolute;
}

.circle {
  border-radius: 50%;
  background: #252525;
  width: 150px;
  height: 150px;
  border: 10px solid #69bf4a;
  margin: 20px;
}

.flex-container>div:hover {
  background-color: #69bf4a;
}

h2 {
  position: absolute;
  display: inline;
  flex-direction: column;
  justify-content: flex-start;
}
<div class="flex-container">
  <div class="circle"></div>
  <div>
    <h2>T-SHIRTS</h2>
  </div>
  <div class="circle"></div>
  <div class="circle"></div>
  <div class="circle"></div>
  <div class="circle"></div>
  <div class="circle"></div>
  <div class="circle"></div>
  <div class="circle"></div>
</div>

Upvotes: 1

Views: 3373

Answers (3)

James
James

Reputation: 11

I think this is about how I would approach this issue. Hope it helps.

<div class="flex-container">
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
               <div class="flex-item">
                    <div class="circle"></div>
                    <h2>T-SHIRTS</h2>
               </div>
            </div>




html, body {
    margin: 0;
    padding: 0;
    width: 100%;
}
.flex-container {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    align-content: space-between;
    align-items: center;
    justify-content: center;
}
.flex-item {
    -webkit-flex: 1 1 100px;
    flex: 1 1 100px;
    padding: 25px 45px;
    text-align: center;
}

.circle {
   border-radius: 50%;
   background: #252525;
   width: 150px;
   height: 150px;
   border: 10px solid #69bf4a;
   margin: 20px auto;
}

.circle:hover {
   background-color: #69bf4a;
}

Upvotes: 0

Punit Sachan
Punit Sachan

Reputation: 593

Try this one.

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
}


.flex-container {
    display: flex;
    flex-wrap: wrap;
    width: 1000px;
    align-content: space-between;
    align-tems: center;
    justify-content: center;
    position: absolute;

}

.circle {
   border-radius: 50%;
   background: #252525;
   width: 150px;
   height: 150px;
   border: 10px solid #69bf4a;
   margin: 20px;
   text-align: center;
}

.flex-container >div:hover {
   background-color: #69bf4a;
}

h2 {
    margin-top: 170px;
}

<div class="flex-container">
                <div class="circle"><h2>T-SHIRTS</h2></div>             
                <div class="circle"></div>
                <div class="circle"></div>
                <div class="circle"></div>
                <div class="circle"></div>
                <div class="circle"></div>
                <div class="circle"></div>
                <div class="circle"></div>
            </div>

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

You cannot have an arbitrary element inside the flex-box view. So I suggest you to give each flex element, a wrapper:

<div class="flex-wrap">
  <div class="circle"></div>
  <div><h2>T-SHIRTS</h2></div>
</div>

Now, getting back to the styles, if you want them to have the equal size, use this way:

html, body {
  margin: 0;
  padding: 0;
}

.flex-container {
  display: flex;
  flex-wrap: wrap;
  width: 1000px;
  align-content: space-between;
  align-items: top;
  justify-content: center;
}

.circle {
  border-radius: 50%;
  background: #252525;
  width: 150px;
  height: 150px;
  border: 10px solid #69bf4a;
  margin: 20px;
}

.flex-container > div:hover {
  background-color: #69bf4a;
}

h2 {
  text-align: center;
}
<div class="flex-container">
  <div class="flex-wrap">
    <div class="circle"></div>
    <div><h2>T-SHIRTS</h2></div>
  </div>
  <div class="flex-wrap">
    <div class="circle"></div>
  </div>
  <div class="flex-wrap">
    <div class="circle"></div>
  </div>
  <div class="flex-wrap">
    <div class="circle"></div>
  </div>
  <div class="flex-wrap">
    <div class="circle"></div>
  </div>
  <div class="flex-wrap">
    <div class="circle"></div>
  </div>
  <div class="flex-wrap">
    <div class="circle"></div>
  </div>
  <div class="flex-wrap">
    <div class="circle"></div>
  </div>
</div>

Preview

preview

To illustrate that the text is centred, see:

border

Upvotes: 2

Related Questions