Tips for maintaining the alignment of four buttons in a single row as the size of the screen changes

I'm creating a simple browser game as part of my web development learning process. I have a set of divs that represent a warrior, and at the head of the "div table" I have a group of 4 buttons arranged horizontally. However, when the screen size is reduced, the buttons do not stay in the same row. Additionally, there are white spaces between the buttons, but I want them to dynamically fill the entire button group even if there are fewer than 4 buttons. Can anyone provide assistance with this?

Check out my code on jsfiddle:

.div-TableHeadName {
  padding-top:6px;
  height:35px;
  background-color: darkgrey;
}
.div-TableHeadClass {
  padding-top:6px;
  background-color: darkgrey;
  height:35px;
}
.div-TableBodyImage{
  height:160px;
  background-color: lightgrey;
}
.div-TableBodyDescription{
    height: 160px;
    background-color: lightgrey;
}
.btn--actionType{
  background-color: lightgrey;
  border-radius: 0 !important;
  height: 35px;
  width: 25%
}

.btn--actionType:hover{
  background-color: red;
}
.btn--performAction{
  background-color: black;
  border-radius: 0 !important;
  color: white;
  height: 35px;
  width: 25%
}
.btn--group{
  cursor:pointer;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body>
  <div class="div-Table">
    <div class="col-md-2 col-sm-2 col-xs-2 div-TableHeadName">
      <span>Guldan</span>
    </div>
    <div class="col-md-4 col-sm-4 col-xs-4 div-TableHeadClass">
      <span>Warrior</span>
    </div>
    <div class="col-md-6 col-sm-6 col-xs-6">
      <btn-group data-toggle="buttons" class="btn--group">
        <div class="btn btn--actionType">
          <label>
            <input type="radio" name="action" value="sword"/>Sword
          </label>
        </div>
        <div class="btn btn--actionType">
          <label>
            <input type="radio" name="action" value="axe"/>Axe
          </label>
        </div>
        <div class="btn btn--actionType">
          <label>
            <input type="radio" name="action" value="bow"/>Bow
          </label>
        </div>
        <div type="button" class="btn btn--performAction">Attack
        </div>
      </btn-group>
    </div>
  </div>
  <div style="padding-top:10px" class="col-md-2 col-sm-2 col-xs-2 div-TableBodyImage">  
    <img src="https://upload.wikimedia.org/wikipedia/en/b/b1/Portrait_placeholder.png" width="100"/>
  </div>
  <div style="padding: 10px" class="col-md-10 col-sm-10 col-xs-10 div-TableBodyDescription">
    <span>This is a description for Guldan, an orc warrior.</span>
  </div>
</body>

Answer №1

.div-TableHeadName {
  padding-top:6px;
  height:35px;
  background-color: darkgrey;
}
.div-TableHeadClass {
  padding-top:6px;
  background-color: darkgrey;
  height:35px;
}
.div-TableBodyImage{
  height:160px;
  background-color: lightgrey;
}
.div-TableBodyDescription{
    height: 160px;
    background-color: lightgrey;
}
.btn--actionType{
  background-color: lightgrey;
  border-radius: 0 !important;
  height: 35px;
  width: 25%;
  float: left;
}

.btn--actionType:hover{
  background-color: red;
}
.btn--performAction{
  background-color: black;
  border-radius: 0 !important;
  color: white;
  height: 35px;
  width: 25%
}
.btn--group{
  cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<body>
  <div class="div-Table">
    <div class="col-md-6 col-sm-6 col-xs-6">
      <btn-group data-toggle="buttons" class="btn--group">
        <div class="btn btn--actionType">
          <label>
            <input type="radio" name="action" value="sword"/>Sword
          </label>
        </div>
        <div class="btn btn--actionType">
          <label>
            <input type="radio" name="action" value="axe"/>Axe
          </label>
        </div>
        <div class="btn btn--actionType">
          <label>
            <input type="radio" name="action" value="bow"/>Bow
          </label>
        </div>
        <div type="button" class="btn btn--performAction">Attack
        </div>
      </btn-group>
    </div>  
  </div>    
</body>

Sometimes simplifying your issue to the essential elements can lead to a solution. In the provided code, I have focused on those key elements. It appears that using the "float" property for the buttons instead of "inline-block" resolves both of your problems. Unlike inline-block, floating does not add additional space. The wrapping issue with your 25% buttons is caused by this extra space.

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

CSS Table Style Chaos

When styling a table using CSS, I encountered this line within the code: .pricing tr td:last-child { text-align: right; } This particular line ensures that the text in the last column of the table aligns to the right. The class "pricing" is applied to th ...

Is there a way to make text fade in and out smoothly instead of abruptly disappearing and reappearing after an animation ends?

I'm seeking a unique animation effect for my right-to-left scrolling text. Instead of the text teleporting back to its origin at the end of the animation, I would like it to smoothly disappear and reappear from the margins. .custom-animation { he ...

Ways to retain specific div elements following the execution of .html(data) command

Imagine I have a div structured in this way: <div id = "foo" style = "display: grid"> <div id = "bar"> keep me! </div> </div> If I use JQuery's .html(data) method like shown below: $('#foo').html(data); when m ...

Using useRef to update the input value

I utilized the useRef hook from React to capture an element. When I use console.log(this.inputRef), I receive this output: <input aria-invalid="false" autocomplete="off" class="MuiInputBase-input-409 MuiInput-input-394" placeholder="Type ItemCode or ...

Is it possible for an <input> tag in PHP AJAX to have an "action" attribute similar to a <form> tag?

Forms typically use an action attribute to specify where the data should be posted, such as a .php file. Do <input> elements have this action attribute as well? The answer is likely no, but I am curious to understand what concept I may be misunders ...

Utilize CSS to showcase the full-size version of a clicked thumbnail

I am working on a web page that features three thumbnails displayed on the side. When one of these thumbnails is clicked, the full-size image should appear in the center of the page with accompanying text below it. Additionally, I have already implemented ...

A problem arises when trying to showcase the content in a responsive manner on the hamburger menu, particularly when viewing on mobile

As a newcomer to web development, I decided to challenge myself by building an E-Commerce website to enhance my skills. To ensure mobile responsiveness, I opted for a hamburger menu to hide the navbar content. However, despite resizing working flawlessly, ...

Custom HTML select element not triggering the onchange event

I found a code snippet on https://www.w3schools.com/howto/tryit.asp?filename=tryhow_custom_select that demonstrates a custom select input with an onchange event. However, I am facing an issue where the onchange event does not get triggered when I change th ...

iframe: retrieve current iframe

Currently, I'm working on a page that contains multiple iframes. Only one iframe is active at a time, and I need to be able to determine which one is currently active. I attempted using document.activeElement.id, but it only returns the correct resul ...

Using jQuery to toggle the image of a button based on its id

I have a jQuery function that opens another section when clicking on a div with the class 'open', and changes the image of a button by switching its id for CSS. However, I now need to change the button's image back when it is clicked again. ...

Adding a gradient overlay to an image that has a see-through background

I am trying to achieve a unique effect where a gradient appears on the text instead of the background of an image. An example I created can be found here: https://codepen.io/BenSagiStuff/pen/BaYKbNj body{ background: black; } img{ padding: 30px; ...

Problems with Displaying Facebook Ads on Mobile Web

Currently, I am incorporating Facebook Audience Network (MobileWeb-beta) for advertisements on my mobile website. However, the 320x50 HTML source code is not displaying the ad across the width of the mobile screen as intended; instead, it appears smaller ( ...

Support for these CSS properties of left: 0; and right: 0; are compatible with

Just wondering if it's okay to utilize the CSS code below to ensure the element adjusts to its parent's width. .element { position: absolute; left: 0; right: 0; background-color: #ccc; border-top: 1px solid #ddd; } We don&ap ...

Maintain the aspect ratio of an image within a flex container when the height is adjusted

I'm currently facing an issue with an image in a flex container. My goal is to maintain the image's ratio when the height of the container or window is decreased or resized. Check out this sample fiddle html, body { height: 100%; } .wrappe ...

Apple's iPhone does not adhere to media query specifications

My responsive website was functioning perfectly on desktop, Android, and Windows Phone devices. However, when I asked my friends to check it on their iPhones running iOS 10, they informed me that the media queries were being ignored on iPhone models 5, 5s, ...

The combination of absolute positioning and percentage-based height measurements allows for

For more information, go to http://jsfiddle.net/A2Qnx/1/ <div id='w'> <div id='p'> <div id='c'> </div> </div> </div> With absolute positioning enabled, div P has a height ...

The art of placing images using CSS

I'm having trouble aligning the images into two rows of three, with the news section on the right side below the navigation bar. I've tried different methods but can't seem to get it right. Both the image and link should be clickable, but I& ...

Unable to retrieve the child value using getJSON

I am currently retrieving data from an API that provides information in a specific format. Here is an example of the JSON data I receive: { "total":1, "launches":[ { "name":"Falcon 9 Full Thrust | BulgariaSat-1", "net":"June 23, 2017 1 ...

Tips for maintaining consistent content length of nested divs as one div's content grows

I am looking for a solution to ensure that when the map is running on my MUI card, some cards with more text content will increase in length while maintaining equal text alignment. I have attached a screenshot of my actual app and a CodeSandbox example for ...

The console is displaying the array, but it is not being rendered in HTML format in AngularJS

Can you please review my App.js file and let me know if there are any mistakes? I have provided the necessary files index.html and founditemtemplate.html below. Although it is returning an array of objects in the console, it is not displaying them as inten ...