Ensuring that the justify button's widths align correctly within a container that spans 100% of the

Is there a way to achieve this layout similar to the Button Groups in Bootstrap? Check out the example here

This is my current markup:

<div class='button-group'>
  <button>1</button>
  <button>2</button>
  <button>3</button>
</div>

If the parent container (.button-group) has a CSS property of width: 100%;, how can I make the buttons automatically take up 33.33% of the width each, or 25% if there are 4 buttons, and so on...
Is there a way to achieve this using CSS?

I have already tried implementing it on JSFiddle

Answer №1

UPDATE:

If you want to maintain the existing HTML structure, one option is to utilize display: flex (although this may not be supported in older browsers): http://codepen.io/pageaffairs/pen/GLIhc

<!DOCTYPE html>
<html lang="en>
<head>
<meta charset="utf-8>
<style>

.button-group {display: flex;}
button {flex: 1 100%;}

</style>
</head>
<body>

<div class="button-group">
    <button>1</button>
    <button>2</button>
    <button>3</button>
</div>

</body>
</html>

Alternatively, if you're open to adding more markup, you can achieve a similar layout like this: http://codepen.io/pageaffairs/pen/wznGK

<!DOCTYPE html>
<html lang="en>
<head>
<meta charset="utf-8>
<style>

.button-group {display: table; table-layout: fixed; width: 100%;}
.button-group div {display: table-cell;}
button {width: 100%;}

</style>
</head>
<body>

<div class="button-group">
    <div>
        <button>1</button>
    </div>
    <div>
        <button>2</button>
    </div>
    <div>
        <button>3</button>
    </div>
</div>

</body>
</html>

Answer №2

If you are looking to create a button group, one way to achieve this is by using a table. It may seem a bit daunting, but it's really not that complicated. Take a look at the example below:

HTML:

<table class="button-group">
<tr>
<td><button class="bt">1</button></td>
<td><button class="bt">2</button></td>
<td><button class="bt">3</button></td>
</tr>
</table>

CSS:

table.button-group {
    width: 100%;
    border: 0;
    border-collapse: collapse;
}
table.button-group td {
    margin: 0;
    padding: 0;
}
.bt {
    width: 100%;
}

If you prefer not to use a table, you can replace the table tags with div elements that have the style display: table;, and the td tags with divs styled with display: table-cell;

In this setup, the tr tag is not needed.

Check out the updated JSFiddle HERE



Apologies if my English is not perfect...

Answer №3

To create a series of buttons wrapped in a justified button group, simply enclose them in the following HTML structure: .btn-group.btn-group-justified.

Check out the demo here

Here is the HTML code:

2 buttons
<br>
<div class="group justified">
    <div class="group">
        <button type="button" class="btn default">1</button>
    </div>
    <div class="group">
        <button type="button" class="btn default">2</button>
    </div>
</div>
<br>3 buttons
<br>
<div class="group justified">
    <div class="group">
        <button type="button" class="btn default">1</button>
    </div>
    <div class="group">
        <button type="button" class="btn default">2</button>
    </div>
    <div class="group">
        <button type="button" class="btn default">3</button>
    </div>
</div>
<br>

And here is the CSS code:

.justified {
    display: table;
    width: 100%;
    table-layout: fixed;
    border-collapse: separate;
}
.group {
    position: relative;
    display: inline-block;
    vertical-align: middle;
}
.justified>.btn, .justified>.group {
    display: table-cell;
    float: none;
    width: 1%;
}

.justified>.group .btn {
    width: 100%;
}

.group>.btn {
    position: relative;
    float: left;
}
.default {
    color: #333;
    background-color: #ccc;
}
.btn {
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    cursor: pointer;
    border: 1px solid;
    border-radius: 4px;
}

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

What is the best way to adjust the mobile screen size to display the most optimized version of a web app's

Currently, I am utilizing react, Bootstrap, and some custom sass to construct and style the front end of my web application. While the design appears good on mobile devices, there is an issue where users need to pinch the screen due to it looking slightly ...

What is the best way to align these two divs centrally within a container set to display as inline-block?

I can't seem to get these two floated left divs centered within a container that has a height set to auto and a display of inline-block. The container has a max-width of 1600px, but when the window is stretched wider than that, it stays aligned to the ...

The Flowbite CSS plugin seems to be malfunctioning when attempting to include it within the tailwind.config.js file

Incorporating Flowbite (both JS and CSS) into my application has been a bit of a challenge. I managed to successfully insert the JS using Webpack and import 'flowbite' in the entry point JS file. As for the CSS, I followed the documentation and ...

Utilizing HTML5 history instead of hash URLs to preserve the user's browsing history

Our application is a single page that dynamically loads results based on query strings. The query string format we use is as follows: ?city=Delhi&pn=1 Within the SPA, there are different sections displayed on the same page. As users navigate through ...

Ensure that only one checkbox is checked at a time and that unchecking one does not remove the

https://i.sstatic.net/EEC2U.png I am facing an issue with two checkboxes in my code. When I click on the first checkbox in the featured playlist section, another popular genre checkbox should be unchecked. However, only the value changes and the checked m ...

Avoiding a constantly repeating video to prevent the browser from running out of memory

Using HTML5, I created a video that loops and draws the frames to canvas. I decided to create multiple canvases and draw different parts of the video on each one. However, after some time, I encountered an issue where Google Chrome would run out of memory. ...

Using HTML to execute a JavaScript function that transforms images

The script cutImageUp has been previously discussed on SE in a thread about Shattering image using canvas. However, I have encountered a different issue while attempting to use it. The HTML code I implemented seems to be ineffective, and despite my efforts ...

A step-by-step guide on showcasing the content from a textfield onto a dynamic column chart

How can I show the value from a text field in a column chart? I found the code for the chart on this website(). I tried using the code below, but nothing happens. Can someone please assist me? <script> window.onload = function () { ...

In a jQuery conditional statement, selecting the second child of the li element for targeting

I'm encountering an issue where I can't target the second child of an li element and use it in a conditional statement. Are jQuery conditionals not compatible with li:nth-child(2)? if($(".steps ul li:first-child").attr('aria-selected') ...

Utilize .NET MAUI Blazor's customizable light and dark themes by loading unique stylesheets through the headcontent feature

I am currently working on a .net MAUI Blazor Hybrid app and I am looking to create both a dark and light theme using CSS stylesheets, without relying on MudBlazor. My approach involves using two separate CSS files, lightMode.css and darkMode.css, each con ...

Java: Issue with JLabel Text Alignment

At the bottom of my panel, I have a JLabel that provides text instructions to the user. When some of the text exceeded the screen width, I used tags to fix this issue. However, after implementing the code shown below, the text is no longer centered an ...

utilizing a background image with a specific div identifier

I am attempting to achieve the same effect shown in this example: http://www.w3schools.com/cssref/tryit.asp?filename=trycss_background-position I want to set an image as the background of a div using an id. #wrapper { height: 900px; width: 900px; bord ...

Python HTMLParser: Extracting HTML tag content made easy!

After working through an HTML page, I have now reached a point where I have lines that look like this: <td class="border">AAA</td><td class="border">BBB</td> I am trying to extract AAA and BBB into variables using HTMLParser, but ...

Display a remote page on hover using Javascript, CSS, and HTML without providing any clickable link

How can I display a preview of another page within a div container without actually making the text clickable? I want the preview to show when the mouse hovers over specific text, but I need the original text to stay on the main page. I've experiment ...

Centering the scroll bar with Flexbox

Is it possible to center the scroll bar of a flex box in the middle of the screen? Currently, when using the code below I am seeing the UI as shown below, with the scroll bar at the bottom of the container DIV https://i.sstatic.net/XakpC.png Instead, is ...

Having trouble with your GitHub Pages site displaying properly on desktop but working fine on mobile?

I am facing an issue with my GitHub pages site on a custom domain. It works perfectly fine on mobile, but when I try to access the same URL from desktop, I encounter a DNS_PROBE_FINISHED_NXDOMAIN error. Any suggestions on why this might be happening? You c ...

What is the best way to achieve maximum clarity in a div element while adjusting opacity?

Is there a way to have a div with an opacity of 40%, but make the text inside the div show with an opacity of 100%? #box { Background-color:#000; Opacity:40%; } ...

Issues with Bootstrap columns not displaying correctly in versions 5 with col-sm-6 and col-md-4

When using Bootstrap 5, the col-sm-6 and col-sm-4 classes do not function correctly on small and medium screens, but work fine on large screens. The goal is to display 2 images on small screens and 3 images on medium screens. However, only 4 images are dis ...

What is the best way to transform the HTML retrieved from an AJAX GET request into an object using JQuery?

$.ajax({ method:"get", url:"/wall", data:"ajax=1", beforeSend:function(){}, success:function(html){ $("#grid_mason").append(html); //Inserting additional co ...

Achieving Center Alignment for Material-UI's <Table> within a <div> using ReactJS

Currently, I am working with a Material-UI's <Table> embedded within a <div>. My goal is to center the <Table> while maintaining a fixed width. I want the table to remain centered in the browser, but if the browser window is minimize ...