Aligning two elements both vertically and horizontally inside a flex-row parent div

Utilizing Bootstrap 4...

If there is a flex-row inside of a div with font-awesome icons in each port-item and a small text underneath, the goal is to center both the icon and text vertically and horizontally within their parent port-item div. Despite hours of searching, a solution has not been found.

Approximately 15 topics have been explored on this site without success. The unique aspect of the setup is attributed to the flex-row layout.

<div>
  <div class="d-flex flex-row text-white">
    <div class="port-item p-4 bg-primary" data-toggle="collapse" data-target="#thing1">
      <i class="fa fa-home d-block"></i>
      Thing 1
    </div>
    <div class="port-item p-4 bg-success" data-toggle="collapse" data-target="#thing2">
      <i class="fa fa-graduation-cap d-block"></i>
      Thing 2
    </div>
    <div class="port-item p-4 bg-warning" data-toggle="collapse" data-target="#thing3">
      <i class="fa fa-folder-open d-block"></i> 
      Thing 3
    </div>
    <div class="port-item p-4 bg-twilight" data-toggle="collapse" data-target="#thing4">
      <i class="fa fa-pencil d-block"></i>
      Thing 4
    </div>
    <div class="port-item p-4 bg-citrus" data-toggle="collapse" data-target="#thing5">
      <i class="fa fa-bolt d-block"></i>
      Thing 5
    </div>
    <div class="port-item p-4 bg-danger" data-toggle="collapse" data-target="#thing6">
      <i class="fa fa-envelope d-block"></i>
      Thing 6
    </div>
  </div>
</div>

CSS affecting this area:

.port-item{
        width: 16.6666667%;
        .fa{
            font-size: 2rem;
        }
    }

The 16.66667% ensures equal width distribution for each of the 6 port items in the flex row. The font-size adjustment is for responsiveness improvement. Custom background classes and data attributes are included but can be disregarded.

Issue visualized:

https://i.sstatic.net/DKyaZ.png

Both the font-awesome icon and the text appear misaligned horizontally and vertically.

EDIT: Following ZNaneswar's updated response using the actual bar:

https://i.sstatic.net/6by3P.png

Answer №1

It seems like the best approach here would be to convert the port-item into a flex container with a column direction. This will arrange the icon and text vertically, with the text transitioning into an anonymous flex item. Then, you can use align-items and justify-content to center them both horizontally and vertically.

.port-item {
  width: 16.6666667%;
  
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div>
  <div class="d-flex flex-row text-white">
    <div class="port-item p-4 bg-primary" data-toggle="collapse" data-target="#thing1">
      <i class="fa fa-home d-block"></i> Home
    </div>
    <div class="port-item p-4 bg-success" data-toggle="collapse" data-target="#thing2">
      <i class="fa fa-graduation-cap d-block"></i> CV
    </div>
    <div class="port-item p-4 bg-warning" data-toggle="collapse" data-target="#thing3">
      <i class="fa fa-folder-open d-block"></i> Research
    </div>
    <div class="port-item p-4 bg-primary" data-toggle="collapse" data-target="#thing4">
      <i class="fa fa-pencil d-block"></i> Teaching
    </div>
    <div class="port-item p-4 bg-success" data-toggle="collapse" data-target="#thing5">
      <i class="fa fa-bolt d-block"></i> Lifting
    </div>
    <div class="port-item p-4 bg-danger" data-toggle="collapse" data-target="#thing6">
      <i class="fa fa-envelope d-block"></i> Contact
    </div>
  </div>
</div>

Answer №2

Here is a suggestion:

Try adding word-wrap: break-word;

.port-item{
        width: 16.6666667%;
         word-wrap: break-word;
        text-align:center;
        .fa{
            font-size: 2rem;
        }
        
    }
    p{
         
        }
    
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div>
  <div class="d-flex flex-row text-white">
    <div class="port-item p-4 bg-primary" data-toggle="collapse" data-target="#thing1">
      <i class="fa fa-home d-block"></i>
      Thing 1
    </div>
    <div class="port-item p-4 bg-success" data-toggle="collapse" data-target="#thing2">
      <i class="fa fa-graduation-cap d-block"></i>
      Thing 2
    </div>
    <div class="port-item p-4 bg-warning" data-toggle="collapse" data-target="#thing3">
      <i class="fa fa-folder-open d-block"></i> 
      Thing 3
    </div>
    <div class="port-item p-4 bg-twilight" data-toggle="collapse" data-target="#thing4">
      <i class="fa fa-pencil d-block"></i>
      Thing 4
    </div>
    <div class="port-item p-4 bg-citrus" data-toggle="collapse" data-target="#thing5">
      <i class="fa fa-bolt d-block"></i>
      Lifting
    </div>
    <div class="port-item p-4 bg-danger" data-toggle="collapse" data-target="#thing6">
      <i class="fa fa-envelope d-block"></i>
      Contact
    </div>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

looping through the iteration

Here is a link to my original plunker demonstration: http://plnkr.co/edit/9UBZ9E4uxAo1TXXghm1T?p=preview. In the case of div 4 (ng-if="show==4"), I am looking for a way to hide the particular div when the list is empty. Currently, each div is displayed fo ...

Merge the date within every individual element

I am trying to combine two dates together. I thought my code was correct, as I first created the current year and then added 1 month to get to 2019. However, for some reason, the result is not displaying. function combineDates() { var currentDate = ...

Fixing the vertical centering of content when printing HTML

My bootstrap 4-based HTML page is looking good on most screens, but when I try to print it, the content is vertically centered in the print layout. How can I ensure that the content is displayed at the top of the paper? https://i.sstatic.net/kfjrx.png Be ...

Why Isn't This JPG Image Functioning Properly in HTML/CSS?

I am facing an issue with a JPG image that I am attempting to use in CSS as a background-image for a div container. For some reason, this particular image refuses to render in the viewport. Interestingly, when I inspect the element, I can see its thumbnail ...

Assign different classes to each element sequentially

Is there a way to display DIVs with the same class, initially hidden using CSS, one after another when a button is clicked? If you have any hints or directions on how to achieve this, it would be greatly appreciated. Thank you! $(".add_another").click ...

Add fresh inline designs to a React high-order component creation

Applying a common HOC pattern like this can be quite effective. However, there are instances where you may not want a component to be wrapped, but rather just extended. This is the challenge I am facing here. Wrapper HOC const flexboxContainerStyles = { ...

Connecting a saved link to a picture located in another document using HTML

Hey there! I'm currently working on including an image with the following code: <p class="b1"> <img id="cakes" src="~/images/cakesBanner.png" alt="cakes"> </p> In a different file, I have the following code: <div class="nav ...

Ways to activate a hover effect on an external element

Hello everyone! This is my first time posting here and I've been struggling to find a solution with my limited knowledge. I could really use some help on this: Is there a way to trigger a hover effect on an element that is not related (no parent, ch ...

Having trouble getting the background image to display full screen in ReactJS

Currently, I am venturing into the world of reactjs with an aim to create a seamless user experience by implementing login, register, and home screens through routing. Below is a snippet of my code. import React from 'react'; import { render } f ...

Fixing Errors in Cordova Android Build

I am in need of assistance with creating an android application using Cordova. I have encountered some obstacles at this stage, as shown in the image below. Thank you in advance for your help. https://i.sstatic.net/8bQVk.jpg ...

The Twitter widget is not staying in sync with the rest of the page when scrolling on

Twitter provides a widget that is widely used by many people. Below is the code provided by Twitter: <script src="http://widgets.twimg.com/j/2/widget.js"></script> <script> new TWTR.Widget({ version: 2, type: 'profile', ...

Determine the width of invisible images using JavaScript/jQuery

Please take a look at the jsFiddle Every time I run it, I am trying to retrieve the width of the images, but the values I get are as follows (check in the JS console) 400 0 575 533 0 ... Some values are zero, and I can't figure out why. It seem ...

Is there a way to incorporate a forward email button in an HTML email newsletter?

I'm working on sending an HTML newsletter for my website using a combination of PHP and HTML. I am looking to include a forward email link in the newsletter. Can anyone guide me on how to achieve this? Thanks in advance! ...

Updating an already opened HTML page from a Java application

I am working with an application that generates an HTML page using Freemarker. Once the webpage is generated, I use Desktop to open it from the application as follows: public void openPage() { if (Desktop.isDesktopSupported()) { try { ...

What causes the difference in behavior when selecting multiple elements in CSS?

My attempt to create a responsive menu resulted in unexpected outcomes when selecting specific elements. For instance, choosing "nav ul li" for list styles at the default size and using "ul li" for the breakpoint did not produce the desired effect. The is ...

Nested tabs in Bootstrap are not visible until they are re-selected

For my application, I am working on designing an admin panel that utilizes nested tabs. To give you a clear picture, here is a simple diagram illustrating how the tabs are structured: Initially Active and Visible -1a Initially Active and Visible -1b In ...

When running a local server with Node.js, the file index.js cannot be found if it is located one folder above the

I am relatively new to node.js and have encountered an issue while running a local server with index.js located one directory up from index.html. The browser is giving me an error message in this scenario. However, when I run the server with index.js in t ...

Display and conceal information upon tweeting

Can anyone help me troubleshoot an issue with hiding the Twitter button after displaying new content? I tried adding a script to make it work, but it's still not functioning properly. Any insights on what I might be doing wrong would be greatly apprec ...

The top scrolling behavior on click applies exclusively to the first set of Bootstrap 5 tabs

I am experiencing an issue with my HTML webpage that contains multiple BS5 Nav Tabs. The first tab is not functioning properly, while all the other tabs are working smoothly. When I click on the first BS5 Nav Tab, the page scrolls to the top with the addre ...

What is the process for validating the CSS background-image URL using Javascript?

The concept here is to verify the URL of the CSS element's id (.boot). If the URL matches, which it does, then it should display "hello world." However, despite everything seeming correct, the expected behavior is not occurring, and the reason behind ...