What methods can be used to wrap buttons within a div located inside a row? (utilizing flexbox and Angular)

Currently, I have a header with a title and several buttons that can be dynamically added. It's crucial to me that these buttons wrap properly within the container.

The challenge is that these buttons are injected through Angular as an external component, likely placed inside a div.

In the demonstration on this codepen, I display the desired outcome (blue) compared to the current result (red). For a clearer view, you can access the codepen here. Specifically, I want each button to wrap individually instead of wrapping as a group.

.flex-row {
  display: flex;
  flex-flow: row wrap;
}

.red {
  background: red;
}

.blue {
  background: blue;
}
<div class="flex-row red">
  <h1>Really long annoying header goes here</h1>
  <div>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
  </div>
</div>

<div class="flex-row blue">
  <h1>Really long annoying header goes here</h1>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
</div>

Additionally, here is the content of selection-buttons.component.html:

<button class="selectionbtn selectbtn" *ngFor="let btn of input.items;let idx = index" (click)="onSelect(idx)" [ngClass]="{'first': idx === 0, 'last': idx === input?.items?.length -1, 'active': currIndex === idx }" value="idx">{{btn.label | translate}}</button>

I made sure to eliminate the surrounding div where the buttons were previously being created, assuming that when called, they would simply appear within the existing div. However, it doesn't seem to work as expected.

Answer №1

When dealing with block-like elements such as flex items, browsers will typically wrap the outer elements first and the inner elements last.

To address this, one potential solution is to change all children to inline elements, which means that the display: flex property should be removed from the outermost parent.

Check out the updated CodePen here

Here's a stack snippet showcasing the changes:

.flex-row {
  /*display: flex;
  flex-flow: row wrap;*/
}

.flex-row * {
  display: inline;
}

.red {
  background: red;
}

.blue {
  background: blue;
}
<div class="flex-row red">
  <h1>Really long annoying header goes here</h1>
  <div>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
  </div>
</div>

<div class="flex-row blue">
  <h1>Really long annoying header goes here</h1>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
</div>

Answer №2

Utilizing flexbox allows for this design, though it requires the use of a less supported property on the inner div.

display: contents;

These elements do not create a specific box on their own. Instead, they are replaced by their pseudo-box and child boxes.

MDN

Browser support is mainly limited to Chrome (58+) & FF (37+)

Note: Prior to Chrome 63 ("In Chrome, the contents value is currently disabled by default, but can be enabled with the "Experimental Web Platform features" flag.")

.flex-row {
  display: flex;
  flex-flow: row wrap;
  align-items: center;
}

.red {
  background: red;
}

.red div {
  display: contents;
}
<div class="flex-row red">
  <h1>Really long header goes here</h1>
  <div>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
  </div>
</div>

Answer №3

To achieve a flex display for the button wrapped in a div with the class "red," use the following CSS code:

.red div{
  display:flex;
}

Note: Make sure to view this in full page.

.flex-row {
  display: flex;
  flex-flow: row wrap;
}
.red {
  background: red;
}
.blue {
  background: blue;
}
.red div{
  display:flex;
}
<div class="flex-row red">
  <h1>Really long annoying header goes here</h1>
  <div>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
  </div>
</div>

<div class="flex-row blue">
  <h1>Really long annoying header goes here</h1>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
    <button>button</button>
</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

The options in the Dropdown choices-js remain hidden beneath a div within a bootstrap table-responsive element

When utilizing the choices-js drop-down menu within a Bootstrap 5 responsive table, I am encountering display issues. Specifically, when ID 1 is selected, it appears incorrectly while ID 3 displays correctly. It is crucial that overflow-x incorporates a s ...

Bespoke animated angular material tabs

Having an issue with Angular Material tabs. I have managed to remove the "slide" effect, but I can't figure out how to modify or remove the black effect behind my tab. Can anyone offer some assistance? black effect 1 black effect 2 Here is a snippe ...

Adjust size of logo in navigation bar header

Having trouble resizing a logo within a navbar-header? Check out the code below: <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <spa ...

Using dynamic variables in Angular 2 to update innerHTML

I am seeking guidance on how to insert variables into a string and display it using Angular2. Below is my code : HTML : <div [innerHTML]="testHTML"></div> The variable : public testHTML: string = "<h1>test - {{ variable[0] }}</h1& ...

Modify certain parameters in the current route using Angular 6 router

Within my Angular 6 setup, the URLs are structured as follows: /{language}/{app_section}/{object_id}/{view}?queryparams... All sections of the application utilize a language selector component, which is included in the template of a parent route to ensur ...

Adjust the element based on hover over the pseudo element

Looking to modify an element based on the hover state of a pseudo-element. Here's what I've tried so far, but it isn't working as expected: nav .logo { display: none; } nav:before{} nav:hover:before .logo { display: block; } I want th ...

Bootstrap form validation solution

Utilizing bootstrap validation to validate a jsp page. The folder structure is as follows: WebContent ├── bootstrap-form-validation ├── js └── pages All three folders are under the web content. If I create another folder called teacher ...

Tips for Modifying the Width of a Scrollbar

Is it possible to temporarily adjust the scrollbar width in Firefox or Internet Explorer while testing some layout code? I vaguely recall reading about this being tied to resolution, but I can't quite remember. I attempted changing the computer' ...

Dynamic resizing of grids using React and TypeScript

I'm attempting to create a dynamic grid resizing functionality in React and TypeScript by adjusting the lgEditorSize value on onClick action. Setting the initial lgEditorSize value const size: any = {}; size.lgEditorSize = 6; Adjusting the lgEditorS ...

What is the best way to spin an element around its center?

I'm faced with a challenge where I have an element that needs to be rotated using JavaScript. The rotation is currently functional, but it's rotating around points other than its center. My goal is to rotate the element around its own center. ...

A slew of problems arises from this horizontal list

Honestly, I've been struggling with this problem for hours - the submenu just won't style properly, and no matter how many lists I compare it to, I can't seem to get it right. The website: - you have to click "enter" to see the list, my ap ...

Issues with Css custom properties on certain webhosting platforms

Seeking assistance with a CSS custom properties issue. Recently purchased a web hosting service from Hostinger and using the default Wordpress template. Our web designer created a website in teleporthq.io, exported the files as HTML and CSS. Deleted the ...

Strange gap between element and border on chrome

I noticed some strange spacing between the div borders and elements when I wrapped a few divs inside a container. This issue is only happening on Chrome and Edge, while Mozilla seems to display it correctly. My code includes the usage of Bootstrap along w ...

Material-UI's JSS does not adhere to the justify content property

Having trouble aligning elements in my React app using material-ui 1.0 and its JSS solutions. The code I've written below should center justify, but it's not working no matter how I configure it. I suspect there might be a naming issue since mat ...

Aligning UL LI elements vertically for multi-line textORHow to vertically

I am dealing with a ul list that contains both single and multi-line li text. The issue is that the second line of the multi-line text starts at a different indentation than the first line. I need a simple solution to resolve this problem that works seaml ...

Tips for aligning text to the right and button to the left while ensuring responsive design with CSS

Trying to align text on the left and the button on the right with space between them in a responsive card design. The button has a background image added to it. Here is my attempted CSS, but the design is not yet fully responsive: .component { disp ...

Embedding an image by inserting the URL

I have been attempting to implement functionality in Typescript where an image preview appears after a user enters a URL, but I have only been successful in achieving this in JavaScript. My goal is to display a preview of the image and enable the user to u ...

What are effective ways to design a dialogue or conversation with CSS styling?

Looking to create a design similar to this one, but unsure of the terminology: https://i.sstatic.net/4QRcw.png I am open to different approaches for the layout, but possibly something along these lines: https://codepen.io/nachocab/pen/LaBwzw .containe ...

When building with Angular using the "ng build" command, the JavaScript file names are altered

I'm currently learning Angular and I've noticed that when creating a new project with Angular CLI, files like runtime.js, polyfills.js, main.js, styles.css are generated. However, after running the ng build command, similar files can be found in ...

Issue with applying className to ToggleButton component in React with Material-UI

When utilizing React and Material UI, my goal is to efficiently apply styles to a group of ToggleButtons. At the moment, I am only able to specify the style prop for each individual ToggleButton in order to achieve the desired styling. I am attempting to ...