What is the best way to contain several elements in a flexbox with a border that doesn't span the entire width of the page?

Just dipping my toes in the waters of web development and I've come across a challenge. I'm attempting to create a flexbox container for multiple elements with a border that doesn't stretch across the entire width of the page, but instead encloses the elements no matter their 'flex-basis' value. Here's the code snippet:

body {
        color: rgb(0, 0, 0);
        font-size: 1.2rem;
      }

      .table {
        display: flex;
        flex-direction: row;
        border: 5px solid rgb(75, 100, 182);
      }

      .table > * {
        flex-basis: 100px;
      }

      .bronze {
        background-color: rgb(116, 97, 44);
      }
      .silver {
        background-color: silver;
      }
      .gold {
        background-color: gold;
      } 
      .platinum {
        background-color: rgb(69, 179, 173);
      }
      .diamond {
        background-color: rgb(175, 81, 175);
      }
<body>
    <div class="table">
      <div class="bronze">Bronze</div>
      <div class="silver">Silver</div>
      <div class="gold">Gold</div>
      <div class="platinum">Platinum</div>
      <div class="diamond">Diamond</div>
    </div>
  </body>

I'm aiming to adjust the 'flex-basis' value for all items within the container while ensuring the border wraps around them without extending to cover the full page width.

If I modify display: flex; to display: inline-flex and utilize width over flex-basis, it functions as intended. However, my preference is solely on using flex-basis. I don't wish to alter the flex-direction setting. Despite attempting to switch the display to inline-flex without incorporating width, this causes the flex-basis to be completely disregarded. My knowledge is still developing, and despite experimenting with various solutions, I haven't found one that works yet. Feeling puzzled!

Answer №1

It seems like you are looking to modify the flex-basis property of flex items within a container while maintaining the item borders and preventing them from expanding to the full width of the page. You mentioned that switching the display property to inline-flex and using width instead of flex-basis has been effective for your design, but you prefer to stick with flex-basis.

To achieve this, you can adjust the flex-basis property of the flex items. However, it is crucial to ensure that the combination of flex-basis, flex-grow, and flex-shrink does not exceed the space available in the flex container. If this total surpasses the available space, the items will no longer fit within the container, causing the borders to extend to the entire width of the page.

Here are the steps you need to follow:

<body>
    <div class="table">
      <div class="bronze">Bronze</div>
      <div class="silver">Silver</div>
      <div class="gold">Gold</div>
      <div class="platinum">Platinum</div>
      <div class="diamond">Diamond</div>
    </div>
  </body>
 body {
        color: rgb(0, 0, 0);
        font-size: 1.2rem;
      }

      .table {
        display: flex;
        flex-direction: row;
        border: 5px solid rgb(75, 100, 182);
      }

    
    

      .table > * {
        flex-basis: 100px; /* Adjust the flex-basis as required */
        flex-grow: 0;
        flex-shrink: 0;
        border: 1px solid red; /* borders around the items */
      }

      .bronze {
        background-color: rgb(116, 97, 44);
      }
      .silver {
        background-color: silver;
      }
      .gold {
        background-color: gold;
      } 
      .platinum {
        background-color: rgb(69, 179, 173);
      }
      .diamond {
        background-color: rgb(175, 81, 175);
      }

I trust these instructions will address your needs, feel free to reach out if you have any more questions!

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

Issue encountered when assigning a CSS class to Angular component

My module contains a table structure with specific components. Here is the source code for table.component.html: <table border=1> <theader></theader> </table> The source code for theheader.component.html is as follows: <thea ...

Display a new div with its content every 5th item

I am currently working with a Smarty template that contains the following code output: Check out my other question on Stackoverflow My problem lies in the fact that the provided code does not repeat the inserted HTML after every 5 elements... Could some ...

Press on the menu <li> item to create additional submenus

A group of friends is facing a challenge. They want to create a dropdown sub-menu that appears directly below the selected item when clicking on a link from a menu. So far, they have been able to use ajax to send requests and generate a sub-menu. However, ...

Can someone explain why my button icons are not aligning to the center properly?

I have a small issue with the icons I pulled from react-icons - they appear slightly raised within the buttons. How can I position them in the middle? That's my only query, but I can't post it alone due to mostly having code in my post. What an ...

Scraping Websites with SimpleHTMLDom in PHP

I am struggling to extract specific data from a table on a website page, particularly the columns name, level, and experience. The table's alternating row colors (zebra pattern) are complicating this task. I have implemented SimpleHTMLDom for this pu ...

Is it still beneficial to incorporate image sprites in the design of a mobile web app?

As I work on developing a mobile web app, I find myself debating whether to use individual images or stick with image sprites similar to what I do for my desktop site. My main concern is the potential increase in file size from consolidating all the images ...

get the value of the last selected element using jQuery

I am facing an issue where I cannot retrieve the selected value from my second select element. Even though I have selected the second option in the dropdown, it keeps returning the value of the first option as empty. Here is a snippet of the HTML code: & ...

Obtaining table data and HTML elements using the correct code - Selenium and Python

I've been attempting to extract the ticker symbol, stock name, price, sector, and market cap columns from this website. I'm facing challenges in identifying the correct HTML elements using the appropriate code. Although I've used Selector Ga ...

Enhancing visual aesthetics with Vuetify on v-slot label components

I am working with Vuetify code that appears like this <v-radio-group v-model="gender" column class="radio-group-full-width"> <v-radio value="Boy"> <template v-slot:label> <v-textarea v-model="answer" v-vali ...

What steps can be taken to activate the remember password feature when using an AJAX login system?

I've developed a web application with a basic login box, utilizing jQuery AJAX for the login process. However, I'm facing an issue with prompting the browser to remember the password. How can I trigger the "Do you want the browser to remember th ...

Increase visibility, decrease visibility by utilizing ng-hide/ng-show and functions

As I implement the show more/show less feature, I am uncertain if achieving a smooth effect is feasible. However, I believe it's worth reaching out to this community for some creative ideas on how to make it possible. I have a list of dynamic links w ...

Reducing div size when clicked - Using JQuery version 1.9

I am looking to make a specific div shrink in size, while keeping all the data visible, each time a user clicks on a certain icon with the class legend-icon. For example, I want the div with the ID #Chart to shrink when clicked. This is the HTML code: &l ...

Borders are absent on several div elements

There is a strange issue I'm facing where some borders on identical divs are not showing up. I have spent hours trying to troubleshoot this problem, and it seems like zooming in or out sometimes makes the border appear or disappear. My hunch is that i ...

Multiple instances of jQuery file being loaded repeatedly

Having an issue with the jQuery file (jquery-1.11.3.min.js) loading multiple times. It seems that other jQuery files in the index.html page might be causing this. Any advice on how to resolve this would be greatly appreciated. <script type="text/javasc ...

Java Applet for capturing specific sections of a webpage to create a screenshot

Does anyone know of a way (applet or something else) to capture a screenshot of the content within a specific div on a webpage? I came across this Java code for capturing a screenshot of the entire screen: import java.awt.AWTException; import java.awt.Re ...

Lose yourself in the horizontal beauty of the CSS menu

Currently, I am working on an application using ASP.Net MVC4, jquery 1.9, and CSS 2.1. However, I have encountered an issue with the horizontal menu display. Whenever a form with a jquery component is shown, some buttons seem to disappear behind the menu, ...

Activate the preview mode by transmitting information between two controllers

I'm trying to set up a preview mode in one controller to show an image in another controller using an angular service. However, I'm struggling with the final step of getting the URL from the passed parameter into the ng-src in SideMenuCtrl dynami ...

Console not displaying any logs following the occurrence of an onClick event

One interesting feature I have on my website is an HTML div that functions as a star rating system. Currently, I am experimenting with some JavaScript code to test its functionality. My goal is for the console to log 'hello' whenever I click on ...

What is the best way to arrange 4 divs with 2 on each line?

Consider the divs provided below: <body> <div id="f1">{{ f1|safe }}</div> <div id="f2">{{ f2|safe }}</div> <div id="f3">{{ f3|safe }}</div> <div id="f4"> ...

Create a toggle effect using attribute selectors in CSS and JavaScript

I am looking to switch the "fill: #000;" from the CSS property "svg [id^='_']". Usually, I would use a method like this, but it seems that I can't do so because "svg [id^='_']" is not an element, correct? pub.toggleClass = functi ...