Attempting to achieve horizontal alignment using flexbox

I have been attempting to create 2 menu bars, but I am facing difficulty aligning the menu buttons horizontally due to the uneven number of elements. I have tried various properties such as max-width, flex-basis, flex-grow, and flex-shrink, but I have not been able to achieve the desired outcome.

Below is a code snippet for reference :

body {
  background-color: black;
  text-align: center;
  color: white;
  font-family: Arial, Helvetica, sans-serif;
}
.row{
    display: flex;
    flex-direction: row;
    flex: 1;
    width: 100%;
   
}
  p, .myCustomButton {
    border-radius: 4px;
    height: 2.8em;
    font-size: 1.6rem;
    color: #fff;
    background-color: red;
    margin: 0.4rem 0.2rem;
    padding: 0 1em;
    flex: 1;
  }
  .row2 {
    justify-content: flex-end;
  }
  .myCustomButton {
    max-width: 20%;
    flex: 0.1 0.1 auto;
  }
<div class='row'>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
  <p>test</p>
</div>

<div class='row row2'>
  <p class='myCustomButton'>test</p>
</div>

I am looking to maintain consistent sizing between my custom button and test buttons in all situations (responsive design). What CSS property should I focus on to achieve this? I believe flex-basis might be the key, but I am unsure of how to properly implement it. Any guidance or suggestions would be greatly appreciated.

Thank you in advance.

Answer №1

The issue with the sizing discrepancy between the last element and the others is due to margins. The solution that worked for me was to wrap the <p> in an additional <div> within the row2 class and specify a maximum growth of 20%:

<div class='row row2'>
   <div>
    <p class='myCustomButton'>test</p>
   </div>
</div>

.row2 > div {
  flex: 0 1 20%;
}

.row {
  display: flex;
  width: 100%;
}

.row > div {
  flex: 1 1 20%;
}

p {
  border-radius: 4px;
  height: 2.8em;
  font-size: 1.6rem;
  color: #fff;
  background-color: red;
  margin: 0.4rem 0.2rem;
  padding: 0 1em;
  flex: 1;
}

.row2 {
  justify-content: flex-end;
}

.row2 > div {
  flex: 0 1 20%;
}
<div class='row'>
    <div>
      <p>test</p>
    </div>
    <div>
      <p>test</p>
    </div>
    <div>
      <p>test</p>
    </div>
     <div>
      <p>test</p>
    </div>
    <div>
      <p>test</p>
    </div>
</div>

<div class='row row2'>
    <div><p class='myCustomButton'>test</p></div>
</div>

If possible, I suggest utilizing CSS grids for a more streamlined approach:

https://codepen.io/sergiofruto/pen/zYqmKYW

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

Is it possible to establish a connection between Firebase Storage and HTML using TypeScript without Angular or React in IntelliJ?

My project goal is to create a functional login and register page using TypeScript. Currently, my code operates without a database, but I aim to implement Firebase for registering user credentials for easy login. I have only come across tutorials using F ...

Sending property via div

In my ReactJS project, I am working on creating a dynamic div board. To properly position the elements on the board, I need to pass props such as row and column IDs. componentDidMount() { let board = []; for(let i = 0; i < 30; i++){ for(let j = ...

I am attempting to display a JSON array in an angular application

How can I display a JSON Array on the UI using AngularJS? I have the JSON array in the controller, and I'm attempting to retrieve it in the view. Below is the code snippet of what I have implemented. Please provide feedback on whether my approach is ...

Exploring the grid system within bootstrap

As I venture into the world of web design, I have chosen to explore Angular. However, I find myself grappling with certain concepts in bootstrap, unsure of what is the correct approach. Being a novice in this field, I appreciate your patience as I seek gui ...

Issue with Node.js Request - Unable to decode HTML content

I'm attempting to decode this HTML page using Node.js and the Request module: The JavaScript console shows that the charset is windows-1252: document.characterSet = "windows-1252"; I've tried all available encodings in iconv-lite, but none of ...

Transform JSON array containing identical key-value pairs

My array is structured as follows: [ { "time": "2017-09-14 02:44 AM", "artist": "Sam", "message": "message 1", "days": 0 }, { "time": "2017-09-14 02:44 AM", " ...

Wheelnav.js implementing a dynamic bouncing animation

I'm currently in the process of working on a pie menu with wheelnav.js and everything is going smoothly so far. However, I can't seem to find any information in the wheelnav.js documentation on how to eliminate the bouncing effect when a menu cho ...

What is causing the unexpected expansion of the initial column in this table?

Can you figure out why the first column in this sample table is so much wider than the others? <html> <head> <style type="text/css"> table,th, td,{ width:100%; border: 4px solid; border-collapse:collapse; ...

HTML Option Absent from Blend VS2013

After recently installing VS2013 Pro in Office, I was excited to use Blend and its HTML Option. However, I am struggling to find a way to start an application with html as shown in videos. My goal was to utilize Blend for creating prototypes using html. D ...

Resetting form fields when clicking the "Next" button with the FormToWizard jQuery Plugin

I am in the process of developing a lengthy form using the jQuery plugin called formToWizard. Within one of the fieldsets located midway through the form, there are hidden fields that are displayed upon clicking a button. The strange issue I am encounterin ...

Ways to create a transparent parallax background

Currently, I'm tasked with developing an educational website using HTML and CSS for a school project. Unfortunately, due to restrictions on code.org, I cannot utilize JavaScript files or script tags. Despite successfully implementing a parallax effect ...

Issue with Materialize Sidenav: Not functional on iOS devices including iPhones, functions correctly on all other devices

My Materialize Sidenav is functioning on all devices except for iPad and iPhone. If you want to check out the code, here is the link to the repository: repo. Take a look at index.html (line 44 down) and js/onloadSetup.js. I attempted adding this in onload ...

How is it possible that the form already comes with data filled in that I didn't input myself

I am currently working on a website using cakephp. Strangely, when I load the login form, both the username and password fields are already prefilled. The interesting part is that this information is not hardcoded anywhere in the form or controller files. ...

Uninterrupted movement within a picture carousel

Seeking a way to create a smooth transition in an image slider with sliding dividers using continuous switching when clicking previous/next buttons. Currently, the dividers replace each other from far positions. Any ideas on achieving a seamless single mov ...

When the width of a tr tag is reduced, an <select> and <span> tag will split into two lines

I am working on creating a table that includes a field for each row along with a symbol to indicate mandatory fields. As the screen width decreases, I want the content to break into a new line at a specific point. Desired behavior: The field width should ...

Interactive game created using JavaScript input

I've scoured the internet for guidance on creating a text-based game that utilizes user input, but all I come across are tutorials focusing on button interactions. What I envision is triggering different responses based on specific user inputs. For i ...

Sending data from a select menu in one PHP file to another

I am facing a challenge that involves: A menu with options retrieved from a MySQL table. A clickable button. Another page written in PHP Here's what I need to accomplish: When the button is clicked, pass the selected option value from the menu ...

Site's design compromised by AJAX refresh intervention

Every time I run this code, the page ends up repeating itself. Adding to that, it doesn't refresh as required. Here is how it should work: Image Updated code segment below: <?php include 'config.php' ?> <script language="javascr ...

Including a variable in an array within a function

I'm encountering an issue with inserting a variable into my array. Below is the code snippet: var data = new Array(); google.load("feeds", "1"); function initialize() { var feed = new google.feeds.Feed("http://www.ntvmsnbc.com/id/24927681/device/r ...

Adjust parent div size based on image size increase

I am currently facing a situation where I have a page displaying an image, but sometimes it appears too small. In order to make the image larger, I have utilized CSS Transform and it is working well. However, the issue lies in the fact that the parent DIV ...