Display Bootstrap radios horizontally when the screen size is small or larger, and vertically when the screen

After reviewing the Bootstrap documentation for styling checkboxes and radios, I am searching for a solution to have my radios displayed horizontally on small screens or larger, and vertically on extra small screens. The approach described in the Bootstrap documentation does not seem to offer a clear solution for this specific requirement. The structural differences between horizontally and vertically aligned radios in the HTML code might necessitate the use of jQuery for manipulation, although I would prefer to avoid this method. Any suggestions on how to achieve this would be greatly appreciated.

Answer №1

Have you considered enclosing the checkboxes and radios within an additional container?

<div class="row">
  <div class="col-xs-12 col-sm-4">
    <label class="checkbox">
      <input type="checkbox" name="checkbox1" id="checkbox1" value="option1">1
    </label>
  </div>
  <div class="col-xs-12 col-sm-4">
    <label class="checkbox">
      <input type="checkbox" name="checkbox2" id="checkbox2" value="option2">2
    </label>
  </div>
  <div class="col-xs-12 col-sm-4">
    <label class="checkbox">
      <input type="checkbox" name="checkbox3" id="checkbox3" value="option3">3
    </label>
  </div>
</div>

Please be cautious with the use of col-sm-4 - it depends on the number of checkboxes/radios you need.

Revised as per the feedback provided below.

If you really want to avoid using JavaScript, you can try implementing this approach, but keep in mind that duplicating content is generally not recommended.

<div class="visible-xs">
    <div class="checkbox">
        <label>
            <input type="checkbox" name="CheckboxXS-1" id="CheckboxXS-1" value="option1">xs-1
        </label>
    </div>
    <div class="checkbox">
        <label>
            <input type="checkbox" name="CheckboxXS-2" id="CheckboxXS-2" value="option2">xs-2 very lengthy text here
        </label>
    </div>
    <div class="checkbox">
        <label class="checkbox">
            <input type="checkbox" name="CheckboxXS-3" id="CheckboxXS-3" value="option3">xs-3
        </label>
    </div>
</div>
<div class="hidden-xs col-md-12">
    <label class="checkbox-inline">
        <input type="checkbox" name="inlineCheckboxOptions" id="inlineCheckbox1" value="option1">1
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" name="inlineCheckboxOptions" id="inlineCheckbox2" value="option2">2 very lengthy text here
    </label>
    <label class="checkbox-inline">
        <input type="checkbox" name="inlineCheckboxOptions" id="inlineCheckbox3" value="option3">3
    </label>
</div>

Answer №2

/* This code snippet toggles between displaying elements horizontally and vertically */
$(document).ready(function() {
  $("#horizVertChange").on("click", function() {
    $(".radios-hv > div").each(function() {
      if ($(this).hasClass("radio")) {
        $(this).removeClass("radio");
        $(this).css("display", "inline");
      } else {
        $(this).addClass("radio");
        $(this).css("display", "block");
      }
    });
    $(".radios-hv label").each(function() {
      if ($(this).hasClass("radio-inline")) {
        $(this).removeClass("radio-inline");
      } else {
        $(this).addClass("radio-inline");
      }
    });
  });
});
/* CSS code for the layout and theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
 body {
  margin: 10px;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-xs-12">
      <button id="horizVertChange">Toggle between horizontal and vertical</button>
    </div>
  </div>
  <div class="row">
    <div class="col-xs-12 radios-hv">
      <div class="radio">
        <label>
          <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked="checked" />Option one</label>
      </div>
      <div class="radio">
        <label>
          <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2" />Option two</label>
      </div>
      <div class="radio disabled">
        <label>
          <input type="radio" name="optionsRadios" id="optionsRadios3" value="option3" />Option three</label>
      </div>
    </div>
  </div>
</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

Adjust the minimum height and width when resizing the window

Apologies in advance if this has already been addressed, but I have tried solutions from other sources without success. My Objective: I aim to set the minimum height and width of a div based on the current dimensions of the document. This should trigger ...

Tips for successfully transferring an image through an XMLHttpRequest

I found this helpful resource at: I decided to test out the second block of code. When I made changes in the handleForm function, it looked like this: function handleForm(e) { e.preventDefault(); var data = new FormData(); f ...

Turn off automatic spelling correction and predictive text in a content-editable section

I'm currently working on a cross-browser application using Script#. I've incorporated a contenteditable div where users can input text. However, I am facing an issue with the auto correct/auto completion feature altering the user's text. Co ...

The jQuery function .on('input') does not trigger in IE8 by itself

Encountering a specific issue with IE8. The event isn't firing in IE8, but functions normally in IE9, Firefox, and Chrome. $('#myId').on('input', function () { //perform some action. } If anyone has a workaround for this probl ...

Having trouble with Ajax POST and CodeIgniter integration?

I have encountered an issue while trying to echo back code received from Ajax. It seems that when I use contentType:false and processData:false, the functionality stops working. Below is the snippet of my ajax code. The url is correct. Interestingly, if I ...

Redirecting to a new page in JavaScript as the clock nears the top of the hour after 5 minutes

I am looking to automatically redirect a user to another page when it is 5 minutes until the start of the next hour. In military time (24-hour clock), this means I want the redirect to occur at times like... 11:55 12:55 13:55 14:55 15:55 etc So far, I ...

How can you prevent JQuery AJAX from automatically redirecting after a successful form submission?

Encountered an issue with loading http://example.com/signup.ashx: The redirect from 'http://example.com/signup.ashx' to '' was blocked by the CORS policy. This is due to the absence of 'Access-Control-Allow-Origin' header on t ...

Are you looking to add some flair to your Flexbox layout using Media

I am in the process of developing a website using flexbox, and one key aspect of the design is to hide the navigation bar, specifically the left panel in this JSFiddle, when the viewport width is between 480px and 1023px. The current implementation achieve ...

Discover updates within a JQuery Ajax call

I am sorry if this question sounds simple, but I would like to know how to set up a function that triggers when the Ajax data changes from the previous request. window.setInterval(function(){ $.get("feed", function(data){ if (data.changed ...

Modify the hover color of the FontAwesome span icon

Here's a span that I have: <span class="fa fa-green fa-arrow-circle-o-right" aria-hidden="true"></span> I decided to change the color to #008d4c like this: .fa-green { color: #008d4c; } But when I try to hover over it, the color do ...

Using Ajax and PHP to upload an image

I'm looking to implement an image upload feature triggered by a button click with the id of #myid.save. Below is the code I have so far: HTML Code: <canvas id="cnv" width="500" height="100"></canvas> <input id="myid_save" type="submit ...

What causes the $_FILES superglobal to consistently appear empty?

Check out my page Take a look at my form: <h2>Upload Photo</h2> <form name="photo" enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post"> Photo <input type="file" name="image" size="30" /> & ...

I'm having trouble changing the background color on my HTML Bootstrap website. Is there a way to easily add social network buttons as well?

I'm encountering a problem here. One of my friends assisted me with Bootstrap elements for my website, but after switching my site to Bootstrap, I am unable to change the background color on my website. Can someone lend a hand? Much appreciated! Also, ...

Unable to assign a basic HTML class to a Blazor component

Upon attempting to implement the following code in my .NET 5 Blazor project within a .razor file: <SignedLabel class="some-css-class" Price=123 Currency=Currency.Usd /> I encountered an issue where Blazor interpreted the class attribute as ...

Creating dynamic web pages using Smarty templating engine to render HTML

I'm attempting to display my unprocessed HTML content using Smarty. {if !empty($brand.description)} {$brand.description} {/if} Although the initial text includes spaces and line breaks, when viewed, the HTML appears as plain text. I also experimen ...

Wrapping a list vertically with ASP.NET using a repeater for generation

I have a repeater that is creating a list of links in no particular order. I want the list to be displayed as follows: -Item1 -Item4 -Item2 -Item5 -Item3 Most solutions I've found require you to know the items in advance and set specific classes for ...

Conditional jQuery actions based on the selected radio button - utilizing if/else statements

This task seemed simple at first, but I quickly realized it's more challenging than expected. Apologies in advance, as Javascript is not my strong suit. My goal is to have the main button (Get Your New Rate) perform different actions based on whether ...

Using varied colors to style list items

Is there a way to apply three different colors to my list items? What is the best method for achieving this? This is what I have in my code: li { width: 33.333%; float: left; padding: 15px; list-style: none; } .light { background-color: #0 ...

Certain HTML code on a webpage is currently inaccessible to me

Struggling with web scraping on a webpage due to inaccessible code. The part of the page's code that remains out of reach is only accessible through an anchor tag, with the following html: <a class="MTLink" href="#d192633539-47" title="example" &g ...

Tips on resolving JavaScript's failure to adjust to the latest HTML inputs

I'm working on a homepage where users can choose their preferred search engine. The issue I'm facing is that even if they switch between search engines, the result remains the same. I've experimented with if-then statements, but the selecti ...