Tips for displaying clickable buttons and sub-buttons with the flex attribute

I'm a newcomer to the world of web development and I'm searching for code that can achieve a responsive design using CSS flex properties, similar to the one shown in the attached picture. When item1 is clicked, the sub items should become visible, and if the view is changed to mobile, they should be displayed in a list format. Does anyone have suggestions on how to accomplish this or can provide me with some guidance?

https://i.sstatic.net/YzImQ.png

Answer №1

This code snippet is designed to be responsive and work effectively on both mobile devices and desktops:

<div class='main'>
  <button id='mainbtn'>
    Click me
  </button>
  <div class='btns' id='pcbtn' style='display:none;'>
    <button id='1'>
      Option 1
    </button>
    <button id='2'>
      Option 2
    </button>
    <button id='3'>
      Option 3
    </button>
  </div>
  <div id='mobbtn' style='display:none'>
    <li>
      <ul>

        <button id='1'>
          Sub-option 1
        </button>
      </ul>
      <ul>

        <button id='2'>
          Sub-option 2
        </button>
      </ul>
      <ul>

        <button id='3'>
          Sub-option 3
        </button>
      </ul>
    </li>

  </div>
</div>

<script>
  document.getElementById('mainbtn').onclick = () => {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      document.getElementById('mobbtn').style.display = 'block';
    } else {
      document.getElementById('pcbtn').style.display = 'flex';
    }
  }

</script>

In this implementation, there is a main container div with two nested divs - one catering to desktop users and the other to mobile users. The buttons within these divs are initially hidden, but upon triggering the main button, the appropriate set of buttons will be displayed based on the device being used.

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

Is it possible to update a MobX state when a message is received from Firebase in the background?

I've set up Firebase in my project like this: import { initializeApp } from 'firebase/app'; import { getMessaging, getToken, onMessage, isSupported } from 'firebase/messaging'; import store from './flag'; ...

Using Angular ng-repeat to iterate over a mysteriously empty Array

To display a certain element multiple times in a row, I am looking for a way that does not involve using the do(x times) function in Angular or adding extra functions in the controller. If we consider ?Array.from({length:5}) (5) [undefined, undefined, un ...

Is there a way to make the hoverzoom effect only apply to a specific object instead of all objects?

I'm currently troubleshooting my code in order to implement a hover effect on Mui <cards> when a user hovers over the object. I have managed to make it work partially, but the issue is that the effect gets applied to all objects instead of just ...

Troubleshooting problem with passing props in React

I have implemented a React Kendo grid as shown below: <Grid sortable={true} filterable={true} onDataStateChange={onDataStateChange} groupable={true} reorderable={true} pageable={true} ...

Show a mpld3 graph in an HTML page using the Django framework

Incorporating mpld3 to showcase matplotlib charts within an HTML page through django has been my recent focus. I utilize the mpld3.fig_to_dict method to convert a matplotlib figure into a JSON string and store it in a variable. However, I am encountering ...

Using PHP and JavaScript to alter the text color within a table cell

I am working on a form where I want the days to change color at certain milestones. The JavaScript I have implemented only works for the first cell, but I am looking to apply the change to every cell labeled DAYS in the table. Any suggestions on how to loo ...

Fetch request encountered a 500 error due to the absence of the 'Access-Control-Allow-Origin' header on the requested resource

Currently, I am in the process of developing a front-end web application using create-react-app and need to make a request to the ProPublica API. The fetch call code snippet is as follows: export const fetchSenators = () => dispatch => { fetch(' ...

Display excerpts of code within an Angular application within the HTML segment

I'm currently developing a tutorial page using Angular where I intend to display code snippets in various programming languages such as 'Java', 'TypeScript', 'C#', and 'Html'. Although there is a snippet called ...

Enhancing form fields with informative tooltips

I found this helpful tutorial on how to create tooltips for form fields using jQuery: It appears that the jQuery function in the article uses ".tooltip" as the default CSS class name for styling the tooltips. Is there a way to customize or change the defa ...

Switch between webpage design for different devices (mobile/desktop) using javascript, jquery, and bootstrap

I am currently working on a webpage that contains various html elements with bootstrap classes applied to them. <div class="col-xs-12 col-lg-4"></div> My goal is to implement a button that, when clicked, will toggle the viewport/layout change ...

Remove a row from an ng-bootstrap table

I've managed to successfully implement the ng-bootstrap table full example. Deleting objects from the DOM and database works fine, but I'm struggling to figure out how to delete a row from the view without having to reload the page. It's i ...

Content Security Policy error due to Ruby on Rails integration with Stripe JS and Elements

I am currently developing a Ruby on Rails application and I am looking to incorporate Stripe payments using the stripe gem. My plan is to utilize Elements, as outlined in this documentation provided by Stripe. To include Stripe js, I have added the follow ...

Express not collaborating with Socket IO namespace

Recently, I attempted to establish a namespace on the backend of my project. Here is a snippet of the code: const server = require("http").createServer(app); const connectedUsers = {}; const io = require("socket.io")(server, { path: "/socket", serveC ...

utilize javascript to parse and access data from the xml.aspx file

After using F12 in Firefox, an error "NS_ERROR_FAILURE" appeared for html:16 (xml.send();). The opensource website suggests using HTTPS request method: GET. Why is there an error even when following these instructions? Any insight is appreciated. Thanks! ...

Click the Button in the Cypress table/grid

My table is created using DIVs which contain customer information. I am trying to implement a function where I can click on a delete button with the attribute nat=Delete, but the rows have the same name elements. Below is a snippet of my table: <div cla ...

Transferring items between different containers without using innerHTML

I've got an embedded <ul> within a (hidden) <aside id="idDetails">. How can I move the ul element from inside the aside and position it in a <div id="projectSide"> without using innerHTML? Any solutions in both plain JavaScript and j ...

Linking asynchronous functions does not function properly

I've been attempting to link two asynchronous functions together, but it appears that the second function is running before the first one. Below is the code snippet: function performAction(e) { const ZIP = document.getElementById('zip').valu ...

Looking for a seamless way to convert jsp code to html using sightly in AEM 6.1?

I need help transforming JSP code to HTML using Sightly in AEM. In the JSP code, we have a scriptlet that stores a value from the dialog into a JSP variable called "coltype" using the code pageContext.setAttribute("coltype", xssAPI.filterHTML(properties. ...

Having numerous sections condensed into one cohesive page

Currently, I am working with backbone.js to develop a single-page application that takes inspiration from the functionality of trello.com. I am interested in learning how to display multiple pages on top of the original page and effectively structure the ...

Preventing errors in a Node.js REST API is essential for maintaining smooth functionality

Hey there! I am currently working on updating the type value in my MongoDB database using a REST API, but I seem to be encountering an issue with an error message stating that it cannot be put (404 not found). Loading app.js app.put('api/types/:_id& ...