How to dynamically adjust container height when flex items wrap in React with CSS?

I’m working on a React application where I have a container (answers-container) with multiple buttons (answer elements) inside it. These buttons are laid out using flexbox (display: flex) with flex-wrap: wrap to allow them to wrap onto the next line when the container width is exceeded.

The issue I’m facing is that the height of answers-container doesn’t dynamically adjust based on the number of buttons or when buttons wrap to the next line. Instead, it remains fixed, causing unexpected behavior:

As you can see from the image, it almost overlaps the container below (which is not desired), I want to keep a persistent space between the answers container and the container with the buttons below.

I think that if I manage to make the height to adjust dynamically, I can set something like a margin-top to the buttons container and this way will keep persistent space. When I do it now, it the margin-top is according to the first line with the answers only, and this way, the next lines still overlaps the buttons container.

Here’s a simplified version of my React component and CSS:

import { useState } from 'react';
import '../styles/Question.css';

export default function SecondQuestion() {
    const [answer, setAnswer] = useState('');

    const handleAnswer = (answer) => {
        setAnswer(answer);
        // Handle storing answer in localStorage or state as needed
    };

    return (
        <div className="container">
            <div className='question'>
                How often do you wash your hair?
            </div>
            <div className='answers-container'>
                <button onClick={() => handleAnswer('Daily')} className='answer'>
                    Daily
                </button>
                {/* Additional buttons follow */}
            </div>
            <div className='next-back'>
                <a href='/first' className='back-link'>Back</a>
                <button className='button-next'>
                    <Link to="/third">
                    <span className='button-text'>Next question</span>
                    </Link>
                    <FontAwesomeIcon className='arrow-icon' icon={faArrowRight} />
                </button>
            </div>
        </div>
    );
}

.answers-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    /* Other styles */
}

.answer {
    border-radius: 8px;
    border: 1px solid #5bc1ed;
    padding: 10px 20px;
    font-size: 16px;
    background-color: white;
    cursor: pointer;
    transition: background-color 0.3s;
    min-width: 189px;
    /* Other styles */
}

.next-back {
  width: 244px;
  height: 48px;
  top: 196px;
  left: 271px;
  gap: 20px;
  position: absolute;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  /* margin-top: 20px; */
}

What I’ve Tried:

  • Removing fixed height from .answers-container.
  • Ensuring flex-wrap: wrap is applied to .answers-container.
  • Ensuring justify-content: center is applied to .answers-container.
  • Checking for any fixed heights or other conflicting styles on parent elements.

Expected Behavior:

I expect the answers-container to dynamically adjust its height based on the number of buttons (answer elements) it contains and when they wrap onto the next line. This way, there shouldn’t be any unnecessary white space or overflow when buttons wrap.

Current Behavior:

The height of answers-container remains fixed, causing buttons to overflow.

Question:

How can I ensure that the height of answers-container dynamically adjusts based on its content (buttons) in a React application using CSS? What changes should I make to my CSS or React component to achieve this behavior?

New contributor

Bat Kolio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật