Enable one button within a div to expand to accommodate the text

We are facing an issue with modals where the text of the submit button is wrapping in some cases, which is not desired. The problem can be seen in the image below.

Even though the buttons are the same size, the "Create User" text is getting wrapped.
I want to make sure that the submit button (create user) can expand to accommodate the text without wrapping, while keeping the Cancel button at its current size. Here is the code snippet for reference:

const ModalFooter = styled.div<{ numberOfButtons?: number }>`
  display: flex;
  justify-content: flex-end;
  flex-flow: row wrap;
  gap: ${theme.spacing.pixel[100]} ${theme.spacing.pixel[100]};
  margin-top: ${theme.spacing.pixel[200]};
  width: 100%;

  :empty {
    display: none;
  }

  & > * {
    display: flex;
    flex: 1 0
      ${({ numberOfButtons = 2 }) =>
        `calc(100% / ${numberOfButtons} - ${theme.spacing.pixel[100]} * ${
          numberOfButtons - 1
        })`};
  }
`;

I believe this behavior is due to the flex: 1 0 property. Is there a way to modify this so that only the submit button expands to prevent text wrapping, leaving the cancel button unaffected?
[1]: https://i.sstatic.net/ys7eU.png

Answer №1

To ensure that your button content is seen as a single word and fits in one line, you can utilize non-breaking space by inserting &nbsp; HTML entity between the words.

<button>Create&nbsp;user</button>

This approach forces the element to have a minimum width, preventing flex items from shrinking below their content size.

Check out this demo: Click here for the demo

An alternative solution involves applying flex-grow: 0; to all child elements of your <ModalFooter> component, and then specifying flex-grow: 1; specifically to your "Create user" submit button:

const ModalFooter = styled.div<{ numberOfButtons?: number }>`
  & > * {
    display: flex;
    flex: 0 0 // flex-grow 0 (first number)
      ${({ numberOfButtons = 2 }) =>
        `calc(100% / ${numberOfButtons} - ${theme.spacing.pixel[100]} * ${
          numberOfButtons - 1
        })`;
  }

  // Target the submit button
  & > button[type=submit] {
    flex-grow: 1; // Override the flex-grow property
  }
`;

<ModalFooter>
  <button>Cancel</button>
  <button type="submit">Create user</button>
</ModalFooter>

View the demo here: Demo link

Keep in mind that flex items grow with their container rather than their own content, except for their minimum size as shown in the first solution.


A combination of solutions would be to add a rule of white-space: nowrap; on the button to prevent line breaks:

This suppresses text wrapping and ensures the content stays on one line.

const ModalFooter = styled.div<{ numberOfButtons?: number }>`
  & > * {
    display: flex;
    flex: 1 0 
      ${({ numberOfButtons = 2 }) =>
        `calc(100% / ${numberOfButtons} - ${theme.spacing.pixel[100]} * ${
          numberOfButtons - 1
        })`;
  }

  // Target the submit button
  & > button[type=submit] {
    white-space: nowrap; // Prevent line breaks on white spaces like &nbsp;
  }
`;

See the mixed solution in action: Link to demo

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

Modal display malfunctioning

Recently, I've been experimenting with Modals on my website. However, I've encountered a small issue - when I try to add multiple modals, they all seem to be connected in some way. This means that I can't have different content in each modal ...

Transform coordinated universal time to the corresponding local time using Angular

Looking to transform the UTC format date into an India date and time format using Angular 2019-02-18T17:31:19-05:00 I am looking for it to be in the following format: DD/MM/YYYY HH:MM(eg: 02/19/2019 04:01 AM). Any guidance on how to accomplish this would ...

SonarQube seems to be overlooking TypeScript files with the .ts extension

I'm currently setting up SonarQube on Jenkins for an Angular 2 project, but I'm facing an issue where Sonar isn't recognizing the TypeScript files with the .ts extension. Here's the current configuration: sonar.projectKey=App Name- An ...

When multiple checkboxes are selected, corresponding form fields should dynamically appear based on the checkboxes selected. I attempted to achieve this functionality using the select option method

Require checkboxes instead of a selection option and have multiple checkbox options. Depending on the checked checkboxes, different form fields should appear. A submit button is needed. I have included some CSS code, but a more detailed CSS code is requir ...

Find all elements located in between two headers with varying ids

I am trying to select a specific range of elements in between two headers, excluding everything after the second header. For example, I want to select elements 1-5 from the following code snippet: <!DOCTYPE html> <html> <head> <link r ...

Troubleshooting: Unable to Trigger Click Events Outside Angular Component

Check out the code I've put together on Stackblitz. Here, you'll find my custom component that allows for toggling between two ng-content elements based on click events. import {Component, ElementRef, EventEmitter, OnDestroy, OnInit ...

Challenges with API arrays

Here is the response from the API: { "meals": [ { "strTags":"Fish,Breakfast,DateNight", } ] } I am aiming to achieve the following outcome: https://i.sstatic.net/Y7B8B.png This is my current code setup: &l ...

Looking to retrieve the value of an input element within an ng-select in Angular 6?

Currently, I am working on a project where I aim to develop a customized feature in ng-select. This feature will enable the text entered in ng-select to be appended to the binding item and included as part of the multiselect function. If you want to see a ...

Encountered a Metro Bundler issue while trying to install a custom npm package

Ever since I developed this npm package and installed it in a different application, I keep encountering the following error during runtime: An internal error has been encountered by Metro Bundler Surprisingly, this issue occurs even if the project is br ...

Can an abundance of helper functions lead to a larger bundle size being sent to the client?

Currently, I’m involved in a project that involves a significant amount of data parsing on the frontend. We have a variety of helper functions that help us manipulate data structures and more. The frontend is built with React (create-react-app) while t ...

The clear button in Material UI Autocomplete is not removing the selected value as expected

I am experiencing an issue with the Autocomplete component. It is functioning correctly, except when I try to clear my selection (i.e. change my mind and want to unselect, leaving the field empty) before submitting the form and making the state empty. The ...

Creating a 2x2 grid layout with flexbox styling

Having trouble creating a customized 2 by 2 grid using flexbox at the moment. Here is my goal: https://i.sstatic.net/uiGOD.png The height of 1 and 2 should be smaller than 3 and 4. And the width of 1 and 3 should be wider than 2 and 4. I attempted to a ...

Internet Explorer not recognizing attribute selector in jQuery

My JavaScript code utilizes the css selector [style="display:none"], and it functions correctly in Chrome, Firefox, Opera, and Safari on Windows. However, in Internet Explorer 11, it behaves unexpectedly. To test this issue, simply click on the buttons (e ...

Using jQuery and HTML to create numerous links that direct to a solitary iframe

I created a basic overlay setup using css, html, and jQuery. Here's how it looks: <a class="activator" id="activator" style="" href="#">hello</a> <div class="overlay" id="overlay" style="display:none;"></div> <div style="d ...

Error: unable to locate the react-redux context value; make sure the component is enclosed in a < Provider > tag

import React, { Component } from 'react' import { createStore } from 'redux' import { Provider, connect, useSelector } from 'react-redux' function rootReducer(state = { name: 'store' }, action) { return state ...

Guide on filtering images within a collection of files using django-rest-framework

I am currently working with a Django rest server that serves a list of files to my React frontend. I am interested in filtering these files by images only, so that I can display only the images on my frontend. Despite searching extensively, I have not be ...

Tailwind causes HTML to malfunction at certain widths

Can someone please provide insight into why this issue is occurring? I have set the width to 100%, but for some reason, it breaks at this resolution. I am not seeking a solution handed to me, just an explanation of the root cause. Thank you in advance! ht ...

Moving object sideways in 100px intervals

I'm struggling to solve this issue. Currently, I have multiple div elements (each containing some content) that I need to drag and drop horizontally. However, I specifically want them to move in increments of 100px (meaning the left position should b ...

Sticky Position Not Functioning Properly in Flexbox Layout

I've attempted to use align-self: flex-start, but it had no effect. The main flex container is on the right side of my page (flex-direction: column) and I want one block of flex columns to stick once I scroll down to them. I enclosed a container aroun ...

Switch up the box-shadow color with ColorThief!

Is there a way to adjust this script to change the box-shadow color of #player1? <script type="text/javascript> $(window).ready(function(){ var sourceImage = document.getElementById("art"); var colorThief = new ColorThief(); var color = ...