Ways to specify distinct css styles according to varying number of elements

I need the Button element to always take up 100% of the container, no matter how many elements are inside.

Check out my Fiddle link here: Js Fiddle Link

<div class="inputFieldBoolean buttonSeries" data-type="Boolean"><button class="true" data-value="1"><div class="sign"></div></button><button class="false" data-value="2"><div class="sign"></div></button><button class="notapplicable selected" data-value="3"><div class="sign"></div></button></div>

<div class="inputFieldBoolean buttonSeries" data-type="Boolean"><button class="true" data-value="1"><div class="sign"></div></button><button class="false" data-value="2"><div class="sign"></div></button></div>

<div class="inputFieldBoolean buttonSeries" data-type="Boolean"><button class="true" data-value="1"><div class="sign"></div></button></div>

Answer №1

To accomplish this task, you have the option to use a script or simply apply display: flex to ensure that all the buttons fit perfectly within the container:

Your CSS code would look something like this:

.inputFieldBoolean.buttonSeries {
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
}

.inputFieldBoolean.buttonSeries button {
    -webkit-box-flex: 1;
    -moz-box-flex: 1;
    -webkit-flex: 1;
    -ms-flex: 1;
    flex: 1;
    height: 42px;
    border: 1px solid #cdd6e9;
    background: #fcfcfc;
    background: -moz-linear-gradient(top, #fcfcfc 0%, #e6eef2 100%);
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fcfcfc), color-stop(100%, #e6eef2));
    background: -webkit-linear-gradient(top, #fcfcfc 0%, #e6eef2 100%);
    background: -o-linear-gradient(top, #fcfcfc 0%, #e6eef2 100%);
    background: -ms-linear-gradient(top, #fcfcfc 0%, #e6eef2 100%);
    background: linear-gradient(to bottom, #fcfcfc 0%, #e6eef2 100%);
}

You can view the updated jsFiddle here.

For more detailed information on flexbox, visit MDN.

Flexbox is widely supported by most modern browsers but not by Internet Explorer versions 9 and below. I have included prefixing to ensure compatibility across different platforms.

Check the CanIUse page here for more on flexbox support.

Answer №2

Initially, it is not permissible to include a div within a button. If a similar structure is required, one can utilize a pseudo element such as ::before, illustrated in my response below.

flexbox is undoubtedly the optimal solution; however, for those of us needing to accommodate IE9, an alternative method utilizing display: table (although linear gradient support may necessitate SVG) is presented below.

.inputFieldBoolean.buttonSeries {
  display: table;
  width: 100%;
}
.inputFieldBoolean.buttonSeries div {
  display: table-cell;
  width: 1%;
  height: 42px;
}
.inputFieldBoolean.buttonSeries button {
  width: 100%;
  height: 42px;
  border: 1px solid #cdd6e9;
  background: #fcfcfc;
  ...
}
<div class="inputFieldBoolean buttonSeries" data-type="Boolean">
  <div>
    <button class="true sign" data-value="1"></button>
  </div>
  <div>
    <button class="false sign" data-value="2"></button>
  </div>
</div>


In addition, another approach without extra markup, using :nth-child, is detailed below:

.inputFieldBoolean.buttonSeries button {
  float: left;
  height: 42px;
  border: 1px solid #cdd6e9;
  background: #fcfcfc;
  ...
}
<div class="inputFieldBoolean buttonSeries" data-type="Boolean">
  <button class="true sign" data-value="1">
  </button>
  <button class="false sign" data-value="2">
  </button>
  <button class="notapplicable selected sign" data-value="3">
  </button>
</div>

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

How to display an unordered list horizontally in HTML

I'm working on a screen that has filters displayed vertically, but I want to align them horizontally and have the filter options arranged in two columns. I've tried using CSS properties like spacing, clear, and display block, but the checkboxes/l ...

Using JQuery to send a post request to an iframe and receiving a

Currently, I am utilizing a form on a webpage to send data to an iFrame. This process can occur multiple times based on user requests. You can see an example of this process here: Target an iframe with a HTML Post with jQuery My goal is to implement speci ...

Issue with Javascript functionality not persisting after page reload initiated with a href = '#'

I am facing an issue with a button on my webpage that is meant to redirect and reload the home page. However, after redirection to '#', my JavaScript seems to stop functioning correctly. Currently, my JavaScript code is enclosed within window.on ...

If there is no content present, the html element with contenteditable will have an <br>

I'm encountering an issue with a <div contenteditable="true"></div>. Whenever I enter content into it and then delete that content, the browser seems to automatically insert a <br> element into the div. Has anyone else experienced th ...

What is the method for identifying the selected radio button that has been dynamically generated after clicking the submit button?

Despite this question being asked multiple times before, I am struggling to find a solution that fits my specific scenario. Initially, I have a static radio button that I create. The HTML code is as follows: <body> &l ...

What is the best way to create a variable within this loop that remains constant and does not update?

I'm having some trouble phrasing this question, but I'm working on creating a loop to display dates for a calendar. So far, everything seems to be working well, except for one thing. My goal is to allow users to click on a date within the calenda ...

The primary objective of utilizing margin

Just embarked on my journey to mastering HTML and CSS. Regarding the 'margin' property: Does its primary function revolve around centering elements horizontally on a webpage? I've crafted a navigation menu for a website, yet I struggle to ...

Auto-scrolling text box cursor movement

My query is quite similar to the topic discussed in this thread on automatic newline in textarea. However, my situation involves multiple textareas with a 1-row attribute, making it seem like writing on air due to the absence of visible borders (I've ...

Storing persistent JSON data in a mobile app built with HTML5 involves utilizing the local storage capabilities of the

I am currently working on a mobile app using PhoneGap that is based on HTML technology. When the app is opened for the first time, my goal is to have it download a zip file that includes a JSON file and media files such as images or audio. Once the zip f ...

Displaying server errors in an Angular componentIn this tutorial, we

As I work on creating a registration page, my focus has been on posting data to the server. I have successfully implemented client-side and server-side validation mechanisms. Managing client-side errors is straightforward using code such as *ngIf="(emailAd ...

What is the best way to strip the text from a link that encompasses both text and an image using jquery?

I have a website with a list of items. Each item in the list has a link that includes both text and an image. Here is an example of the HTML structure: <li class="li-one" style=""> <a href="#" onclick="return ...

Transferring css to an external file within an angular 4 component by utilizing webpack

I encountered this specific error message: Error: Anticipated 'styles' to be a collection of strings. Despite the fact that I have clearly defined the styles as an array of strings: styleUrls: ['./app.component.css'] Can anyone pinp ...

Modifying SAS ODS contentitem in PROC SQL SELECT query

When using SAS to generate ODS output in HTML format, you have the option to include a table of contents with the "CONTENTS" and "FRAME" settings: ODS HTML PATH="C:\Path\" (url=none) BODY="body.html" CONTENTS="contents.html" ...

Accordions housing Bootstrap 3 pills in a sleek design

I'm working on integrating pills and an accordion in Bootstrap 3. Everything seems to be functioning correctly, but there are a couple of issues I am encountering: When the accordion is open, clicking on another tab closes the accordion. I would li ...

Attempting to build a table within a table structure

My goal is to create a nested table structure similar to this image: https://i.sstatic.net/v6lZo.png The number of months, topics, and arguments for each topic can vary as they are retrieved from a database. I have attempted to implement this functionali ...

Implementing selective image loading with HTML and angularJS

This image reveals the issue at hand Within this HTML code snippet, some of my images are displaying on the webpage while others are not. How can I go about resolving this? <img src="img/uploadFiles/{{room.imageLocation.split('//')[6]}}/{{ro ...

The innerHTML inside Angular components appears scrambled, with only the final innerHTML rendering correctly

When working on my Angular project (version 8), I encountered an issue where a list of static HTML content retrieved from a database is not rendering correctly in the parent HTML. Strangely, only the last div with innerHTML is being rendered correctly, whi ...

I require a way to decelerate the speed of the roulette wheel's rotation

For my assignment, I'm tasked with creating a roulette spin wheel code in JavaScript without using any plugins. I need to incorporate some conditions before executing the code, particularly for slowing down the speed of the roulette spin. Additionally ...

Is it possible to automatically adjust the text color based on the dynamic background color?

While browsing Palantir.com, I noticed that the logo color always changes to be the inverse of the background color behind it as the background scrolls or changes. https://i.sstatic.net/BYvZa.png I'm currently working on a website using Wordpress an ...

Determine whether the browser is being used on an Android or iOS device

Is there a dependable method to alter buttons and text on a mobile site based on whether the user is using an Android or iOS browser? ...