Guide on building a personalized button layout in Bootstrap 4

I have been working on a React component that I want to stick to the bottom of the screen no matter what content is above it. Even if there is minimal content, I want it to remain at the bottom.

To achieve this, I added

style={{position: 'fixed', bottom: 0}}
to the main div.

Now, my goal is to create a grid layout like the one shown in this image. However, no matter what I try, the elements end up overlapping each other and making no sense at all.

I've attempted using Bootstrap, grouping them in divs, and playing with flex directions, but nothing seems to work as intended.

Here is the code snippet:

render() {
        return(
            <div style={{position: 'fixed', bottom: 0}}>
                {/*Fix elements as footer*/}

                <div style={{flexDirection:'row'}}>
                    <Button variant="raised" color="default" style={{fontSize:20, minHeight: 200}}> A </Button>
                </div>
                <div style={{flexDirection:'column'}}>
                    <Button variant="raised" color="default" style={{fontSize:20}}> A </Button>
                    <Button variant="raised" color="default" style={{fontSize:20}}> A </Button>
                </div>
                <div style={{flexDirection:'column'}}>
                    <Button variant="raised" color="default" style={{fontSize:20}}> A </Button>
                    <Button variant="raised" color="default" style={{fontSize:20}}> A </Button>
                    <Button variant="raised" color="default" style={{fontSize:20}}> A</Button>
                </div>
                <div style={{flexDirection:'column'}}>
                    <Button variant="raised" color="default" style={{fontSize:20}}> A </Button>
                    <Button variant="raised" color="default" style={{fontSize:20}}> A </Button>
                </div>
            </div>
        );
    }

CSS has always been a challenge for me despite reading extensively about styling. If anyone has any tips on creating custom layouts, I would greatly appreciate the guidance. While Bootstrap makes simple layouts easy, mastering CSS remains elusive and time-consuming for me.

Answer №1

If you're looking to grasp the inner workings of CSS, take a look at this code snippet that excludes Bootstrap:

<div class="container row">
   <div class="column flex-1">
     <button class="flex-1"> A </button>
   </div>
   <div class="column flex-1">
      <button class="flex-1"> B </button>
      <button class="flex-1"> B </button>
   </div>
   <div class="column flex-2">
      <button class="flex-1"> C </button>
      <button class="flex-1"> C </button>
      <button class="flex-1"> C </button>
   </div>
   <div class="column flex-2">
      <button class="flex-1"> D </button>
      <button class="flex-2"> D </button>
   </div>
</div>

.container {
  position: fixed;
  bottom: 0;
  width: 100%;
}

.row {
  display: flex;
  flex-direction: row;
}

.column {
  display: flex;
  flex-direction: column;
}

.flex-1 {
  flex: 1;
}

.flex-2 {
  flex: 2;
}

Check out the jsfiddle link for a visual representation: https://jsfiddle.net/dnotkL5w/16/

flex: 1 essentially means: "Utilize all available space in the specified container direction." So, in a row-oriented container, it will expand to occupy all available width.

On the other hand, flex: 2 signifies: "Fill up all available space within the container's direction. If there is a sibling item with flex: 1, then be twice its size."

This concept can be likened to a barycenter: the numeric value assigned to the flex property dictates the component's size ratio relative to its siblings.

Answer №2

To fully utilize Bootstrap, it's essential to implement Bootstrap classes in your project.

Below is a method to create the desired grid layout for buttons:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="container-fluid fixed-bottom">
    <div class="row no-gutters">
        <div class="col-sm-4">
            <div class="row no-gutters h-100">
                <div class="col-6">
                    <button type="button" class="btn btn-sm btn-block h-100 btn-outline-primary">A</button>
                </div>
                <div class="col-6">
                    <button type="button" class="btn btn-sm btn-block h-50 btn-outline-secondary">B</button>
                    <button type="button" class="btn btn-sm btn-block h-50 mt-0 btn-outline-success">C</button>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <button type="button" class="btn btn-sm btn-block btn-outline-danger">D</button>
            <button type="button" class="btn btn-sm btn-block mt-0 btn-outline-warning">E</button>
            <button type="button" class="btn btn-sm btn-block mt-0 btn-outline-info">F</button>
        </div>
        <div class="col-sm-4">
            <div class="row">
                <div class="col">
                    <button type="button" class="btn btn-sm btn-block btn-outline-dark">G</button>
                    <button type="button" class="btn btn-sm btn-block h-100 mt-0 btn-outline-primary">H</button>
                </div>
            </div>
        </div>
    </div>
</div>

The btn-lg or btn-sm classes can be utilized to adjust button sizes. The btn-block class makes buttons span the full width of the column. By adding no-gutters to the row, padding/gutters inside columns are removed, and using h-100 or h-50 sets height to 100% or 50%, respectively.

Here is an example of nesting the entire row within another Bootstrap column:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3">
            col-sm-3 content
        </div>
        <div class="col-sm-9 d-flex flex-column" style="min-height: 100vh">
            col-sm-9 content
            <div class="row no-gutters mt-auto">
                <div class="col-sm-4">
                    <div class="row no-gutters h-100">
                        <div class="col-6">
                            <button type="button" class="btn btn-sm btn-block h-100 btn-outline-primary">A</button>
                        </div>
                        <div class="col-6">
                            <button type="button" class="btn btn-sm btn-block h-50 btn-outline-secondary">B</button>
                            <button type="button" class="btn btn-sm btn-block h-50 mt-0 btn-outline-success">C</button>
                        </div>
                    </div>
                </div>
                <div class="col-sm-4">
                    <button type="button" class="btn btn-sm btn-block btn-outline-danger">D</button>
                    <button type="button" class="btn btn-sm btn-block mt-0 btn-outline-warning">E</button>
                    <button type="button" class="btn btn-sm btn-block mt-0 btn-outline-info">F</button>
                </div>
                <div class="col-sm-4">
                    <div class="row">
                        <div class="col">
                            <button type="button" class="btn btn-sm btn-block btn-outline-dark">G</button>
                            <button type="button" class="btn btn-sm btn-block h-100 mt-0 btn-outline-primary">H</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

By applying the d-flex flex-column classes to the column, it becomes a flex column, while mt-auto pushes a row to the bottom of that column. Including style="min-height: 100vh" ensures the column height is at least equal to the viewport height, keeping the buttons at the bottom even with minimal content in the parent column.

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

Why are the buttons on my HTML/JavaScript page not functioning properly?

I have been struggling with a code for a 5 image slideshow where the NEXT and PREVIOUS buttons are supposed to take me to the next and previous slides. However, when I press them, nothing happens. Can anyone provide some assistance? I need additional detai ...

Caution: A value of "true" was provided for the attribute "exact" which is not a boolean

I'm currently utilizing version "6.16.0" of the "react-router-dom" library in my project. Upon running the program, I encountered a warning message stating: react-dom.development.js:86 Warning: Received true for a non-boolean attribute exact. If you i ...

"The onkeydown event dynamically not triggering for children elements within a parent element that is contentEditable

Can anyone offer some insights into why this code isn't functioning as expected? My goal is to attach the listener to the children elements instead of the body so that I can later disable specific keystrokes, such as the return key. <!DOCTYPE html ...

Dynamically adjusting the width of an HTML element with ng-style using percentage values in AngularJS

I am facing a challenge where I need to display a progress bar in my UI based on a percentage value stored in a JSON response object. Here is an example of the JSON object: { completionPercent: 42 } The desired UI outcome should look like this: ┌ ...

Switching Bootstrap Navbar Active State with JavaScript

I have encountered an issue with using the "active" class on my navbar navigation items in Bootstrap 4. When I click on the links, the active state does not switch as intended. I have tried incorporating JavaScript solutions from similar questions but have ...

Is there a way to determine the quantity of items within a sortable div?

In my HTML document, there are multiple divs containing smaller divs that can be rearranged within each other. Upon loading the document, a random assortment of these smaller divs are added to #boxtop. Below is a snippet of my HTML code: <html> &l ...

Why isn't this working? I'm attempting to trigger a sound when I hover with my cursor, but it only plays when I click instead

When I click on it, it works fine. But I can't seem to get it to work on hover. Can someone help me out? This is the HTML code: <body> <audio autoplay id="HAT2" > <source src="OOOOO_1_HAT.mp3" > Your browser doesn't support t ...

Why is it that I am getting a promise when using React with Fetch and async/await?

I'm currently facing some issues with react, fetch, and async/await. Here is a snippet of code I have as a fetch wrapper: export const fetcher = (url, options = null) => { const handle401Response = error => { throw error; } const is ...

Implement Material UI Textfield with 'error' and 'helper text' for items within a repeated loop

I am currently working on developing an application that involves dynamic text field input using MUI textfield. This application consists of two fields - From and To. The functionality includes generating two new fields when the user clicks on the "Add New ...

Encountering an issue with MUI x-data-grid: "Module '../InputBase' not found"

The code snippets used in the project are: import React from "./testDetails.css" import { DataGrid, GridToolbar } from "@mui/x-data-grid"; import { useParams } from "react-router-dom"; import { useState, useEffect } from &quo ...

Organize the structure of a JSON object in AngularJS to generate a hierarchical unordered list

Greetings! I am working with a RESTful API that returns a JSON object structured like this: { data: [ { id: 4, name: "Naranja", cant_portion: 2, portion_name: "Piezas", foodgroup_name: "Frutas" } ...

Persistent column menu in ag-grid

Is there a way to include a menu for each row within a sticky column in Ag-grid? I couldn't find any information about this feature in the official documentation, so I'm unsure if it's even possible. I've attempted several methods, but ...

Upgrading from Mui migration DatePicker version 5 to version 6 and customizing the input

Could someone assist with the process of migrating from MUI v5 to v6 and styling the TextField in a MUI DatePicker? This is how my DatePicker looked in v5: <DatePicker value={fromDate} onChange={onFromDateChange} minDate={props.minDate} ...

When hovering, transition occurs unless the cursor is over a specific element

I have created a small box element with a close button that looks like this: ___ | X| ‾‾‾ In order to make it more interactive, I applied some CSS so that when hovered, the box expands like this: ___________________ | X| ‾ ...

There are various IDs in the output and I only require one specific ID

I have a JSON fetcher that is functioning properly. However, whenever I request an ID, it returns all the IDs present in the JSON data. Is there a way to retrieve only the latest ID? This is my first time working with JSON so I am still learning. $(docu ...

Numerous approaches to updating state instead of just one

Currently, I am in the process of building a React application without utilizing Redux or any other state manager. Assume I want to execute three tasks when a button is clicked: Activate another button Delete a label Display a confirmation toaster Thes ...

Can a personalized user permission prompt be designed for HTML5 APIs?

Can a consistent custom user permission request dialog be designed for HTML5 APIs, such as geolocation or getUserMedia, to ensure uniform appearance on all browsers? ...

Retrieve a result following an AJAX post request to utilize the obtained value in subsequent code functionalities

Below is the code snippet where an "if" statement is used to check if a query has been executed correctly. If it has, a part of the script is stored in a variable called $output. This variable is then immediately read by the notification script included in ...

Node.js file successfully establishes a database connection, but script tags fail to do so

Having some trouble connecting to my MongoDB database (or MongoLab server). It works perfectly fine when the code is in the name.js file, but for some reason it fails when placed inside <script> tags within an HTML file. My folder contains only thes ...

Is there a way to trigger a custom event from a Web Component and then intercept it within a React Functional Component for further processing?

I'm facing an issue with dispatching a custom event called "select-date" from a custom web component date picker to a React functional component. Despite testing, the event doesn't seem to be reaching the intended component as expected. Below is ...