Design a responsive layout featuring a div containing four buttons that incorporate text and logos using Bootstrap 5

Looking for help with a web application built using HTML/CSS/Bootstrap 5/JS and Rust/Actix-web? Check out the code here.

The index page of the app features a navbar and a div containing four buttons in the center (vertically and horizontally aligned). The current method for resizing the buttons involves calculating the size based on the text above and dividing it by 4 to determine the width of each button. However, there are two main issues: when the user's name is short, the buttons end up being small and on medium-sized devices like tablets, the font size remains large compared to laptops due to difficulty in configuring @media screen accurately.

As a result, there is overflow within the buttons.

let divElement = document.getElementById("welcome");
let buttons = document.getElementsByClassName('button-group-element')
let buttons_array = Array.from(buttons)
let elemWidth = divElement.offsetWidth / buttons_array.length;
for (i = 0; i < buttons_array.length; i++) {
  if (i != 0) {
    buttons_array[i].style.marginLeft = elemWidth * i + "px"
  }
  buttons_array[i].style.width = elemWidth + "px"
}
html,
body {
 position: relative;
 height: 100%;
 width: 100%;
}

body {
 display: flex;
 position: absolute;
 align-items: center;
 bottom: 40%;
 justify-content: center;
 background-color: #0000;
}

#welcome {
 text-align: center;
 }

.button-group-element {
  text-align: center;
  position: absolute;
  height: 40%;
}

@media screen and (min-width: 707px) {
  .button-group-element {
    font-size: 2em;
  }
}

@media screen and (max-width: 706px) {
  .button-group-element {
    font-size: 1.3em;
  }
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font/bootstrap-icons.css">

<h1 id="welcome">Welcome John Doe!</h1>
<div class="button-group">
<button onclick="window.location.href='/add_question'" type="button" class="btn btn-outline-primary button-group-element"><i class="bi bi-plus-circle"></i> <br>New Question</button><button type="button" class="btn btn-outline-primary button-group-element"
  onclick="window.location = '/modify_question'"><i class="bi bi-pen"></i> <br>Modify Question</button><button type="button" class="btn btn-outline-primary button-group-element" onclick="window.location = '/student_question'"><i class="bi bi-mortarboard"></i> <br>Student Questions</button>
<button
  type="button" class="btn btn-outline-primary button-group-element" onclick="window.location = '/statistics'"><i class="bi bi-bar-chart"></i> <br>Statistics</button> </div>

Answer №1

In response to your request to stack the elements on smaller devices to prevent overflow, I have revised the code to address this issue.


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d4f4242595e595f4c5d6d18031f031e">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="791b16160d0a0d0b180954101a16170a3948574849574c">[email protected]</a>/font/bootstrap-icons.css">

<h1 id="welcome">Bienvenido John Doe!</h1>
<!-- By adding the `row` class, you can utilize Bootstrap's grid system to stack elements on smaller devices -->
<div class="button-group row">
  <button type="button" class="btn btn-outline-primary button-group-element col-md-3">
    <i class="bi bi-plus-circle"></i><br>
    New Question
  </button>
  <button type="button" class="btn btn-outline-primary button-group-element col-md-3">
    <i class="bi bi-pen"></i><br>
    Edit Question
  </button>
  <button type="button" class="btn btn-outline-primary button-group-element col-md-3">
    <i class="bi bi-mortarboard"></i><br>
    Student Questions
  </button>
  <button type="button" class="btn btn-outline-primary button-group-element col-md-3">
    <i class="bi bi-bar-chart"></i><br>
    Statistics
  </button>
</div>

To make this revision, I have removed any unnecessary CSS and JS code.

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 with color display inconsistency in webgrid across various segments

https://i.sstatic.net/UW17M.png My goal is to display section status colors in a webgrid and divide them percentage-wise, but I am facing issues with some of the rows. Here is the cshtml script for the webgrid section status: webGrid.Column(header: "Sec ...

Steps to create an iframe that opens in a new window

I'm facing an issue with the iframe sourced from flickr. My website includes an embedded flickr iframe to showcase a gallery without the hassle of creating a slider, resizing images, and extracting thumbnails. Given that this site belongs to a frien ...

Authentication using a singular input

Struggling to design an input for a verification code with just one input instead of four. However, I am facing some challenges: The input is not responding correctly when clicked, it's a bit buggy and requires clicking at the end of the input to typ ...

Incorporate content from HTML into various sections

Is there a way to dynamically extract the h4 headers and the first sentence from each section of this HTML, and then add them to a new div? function summarize() { let headings = document.getElementsByTagName("h4"); // Get all H4 elements let newsText = do ...

The attempt to change the header's background color has proven to be unsuccessful

Hello this is my first question on stackoverflow so please forgive my lack of experience. I am attempting to modify the background color of an entire HTML document using the background-color property on the element. Despite changing some parts of the docu ...

AngularJS encountered an unhandled syntax error

My current approach involves utilizing the code below to display data fetched from Parse API into a table using AngularJS and Bootstrap. However, the JavaScript section where I have defined the controller doesn't seem to be running as expected. Below ...

Is the background color extending the entire width of the page?

Hey there, I'm currently trying to determine how to set the background-color on a paragraph or h1 so that it only appears behind the words and not across the entire page with blank space. I'm still a beginner when it comes to coding. I'm wor ...

Responsive React Page Design with Swiper's Breakpoints (Bootstrap Integrated)

I have integrated Swiper React with my React project. It seems that I cannot manipulate the 'slides per view' using Bootstrap directly. If it were possible, I would define the necessary columns and insert the swiper slides within them. Currently ...

Ways to subtly adjust the edges of text in a design

https://i.stack.imgur.com/sr4PF.pngIs there a way to adjust the border of text that already has a border applied? I have successfully created the text with the border using the following CSS: h1 { font-size: 35pt; text-align: center; font-family: ...

Tips on displaying hyperlinks within a text area in an Angular application

In my Angular application, I am facing an issue with displaying hyperlinks within a text area box for dynamic content. The hyperlinks are not recognized and therefore cannot be clicked. Can someone please advise on how to properly display hyperlinks with ...

Tips for synchronizing the movement of a nested div with its parent during zoom operations

I have noticed that most posts address the issue of elements resizing or moving, but I am experiencing the opposite problem with my element. The div named headLogo, which contains the logo as a background, remains fixed and does not resize or move along wi ...

Concealing a Div on Click Outside of Child Div in ReactJS

I have a parent div with a child div in the center. I am trying to figure out how to close the parent div only when clicking outside of the child div. My current code is structured like this, using triggerParentUpdate to control whether the div should be d ...

creating center-focused gradients in react native using LinearGradient

Hey there, I'm currently facing an issue with applying a box shadow to one of the buttons in my React Native application. Unfortunately, the box shadow property is not supported in Android. To work around this limitation, I am experimenting with creat ...

Does jQuery's click event not successfully hide an element on Internet Explorer?

In my dropdown menu, I have the following options: <div class="my_div"> <select name="selection" id="select_id"> <option value="0">A</option> <option value="5">B</option> ...

Grid system that adapts without distortion

My goal is to create a grid that maximizes the number of elements in its width without stretching them to fit the entire window. That's why I'm utilizing CSS grid with the auto-fill property, which is almost achieving the desired outcome. I want ...

Stacking SVG elements can lead to distortion effects

Currently experimenting with the innovative SVG stacking method to integrate multiple icons into a single file, reducing the browser's HTTP requests. The details of this technique can be found in depth here. In essence, it involves placing numerous S ...

Why isn't this code for hiding the animation and displaying it not functioning properly?

Why isn't this animation from display none to block working properly? The initial code appears as follows, and it functions correctly: $(".box_outer").stop().animate({top: '25px' , opacity: 1}, 100); When I add display: none; to the class ...

Using a diverse class for enhancing the PrimeVue dialog's maximizable feature

I'm currently working with the PrimeVue Dialog component. My goal now is to apply different styles depending on whether the dialog is maximized or not. Let's keep it simple by saying I want to change the text to bold or red when the dialog is max ...

Setting a custom background image in HTML using the designated file PATH

My current issue involves the inability to set my background photo using a CSS file. I am utilizing NetBeans for this project. Inside a folder named css, there is a file called global.css which contains the following code: body{ background-image: url ...

Utilizing Bootstrap 5: Executing JavaScript exclusively upon successful form validation

I've created a form with multiple input fields that are being validated using Bootstrap 5 validation. <form class="needs-validation asset-test" action="{{ url_for('asset_test') }}" method="post" novalidate> ...