Is there a way to ensure that all my "+" buttons are aligned vertically?

I am currently working on a Frequently Asked Questions section for a project I am developing. My goal is to compile a list of common questions that potential buyers may have. I envision a setup where, upon clicking the + button next to the question, a new paragraph will expand below to provide the answer. One issue I am facing is ensuring that all the + buttons are aligned vertically with each other. Currently, the position of each + button varies based on the length of the question. Although I have implemented justify-content: space-between to add some spacing between the question and the +, it results in a messy look as the + buttons are not uniformly aligned.

section {
  background-color: #fff;
  height: 100vh;
  height: fill-available;
  min-height: -moz-available;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow-x: hidden;
}

.faq {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.container {
  display: flex;
  width: 60vw;
}

.left {
  width: 100%;
}

.get-started {
  font-family: "Poppins", sans-serif;
  font-size: 28px;
}

.question {
  display: flex;
  justify-content: space-between;
}
<section class="faq">
        <div class="container">
          <div class="left">
            <div class="get-started">
              <p>Learn How To Get Started</p>
            </div>
            <div class="question">
              <p>How do I create a website?</p>
              <button>+</button>
              <div>
                <p></p>
              </div>
            </div>
            <div class="question">
              <p>Is this website right for me?</p>
              <button>+</button>
              <div>
                <p></p>
              </div>
            </div>
           <div class="question">
             <p>How much will web storage cost me?</p>
             <button>+</button>
             <div>
               <p></p>
             </div>
           </div>
          </div>
        </div>
      </section>

Answer №1

/* A simple demonstration with minimal effort put into aesthetics */

// Getting all elements that match the selector:
const questions = document.querySelectorAll('.question'),
    buttons = document.querySelectorAll('.question button'),
    // Defining the toggle() function using Arrow syntax:
    toggle = (evt) => {
      let {currentTarget} = evt,
          answer = currentTarget.closest('.question').querySelector('div');

      answer.hidden = !answer.hidden;

      currentTarget.textContent = !answer.hidden ? '-' : '+';
      
    }
 
 questions.forEach((q) => q.querySelector('div').hidden = true);

 buttons.forEach(
  (btn) => btn.addEventListener('click', toggle)
 );
/* Rearranged CSS for better readability */
section {
  background-color: #fff;
  height: 100vh;
  height: fill-available;
  min-height: -moz-available;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow-x: hidden;
}

.faq {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.container {
  display: flex;
  width: 60vw;
}

.left {
  width: 100%;
}

.get-started {
  font-family: "Poppins", sans-serif;
  font-size: 28px;
}

.question {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
}

.question button {
  flex-basis: 2rem;
}

.question div {
  flex-basis: 100%;
}
<section class="faq">
  <div class="container">
    <div class="left">
      <div class="get-started">
        <p>Learn How To Get Started</p>
      </div>
     <!-- Question and Answer sections -->
    </div>
  </div>
</section>

Link to JS Fiddle demo.

Suggested improvement:

/* Updated CSS for improved clarity */
section {
  /* Styles for section element */
}
.faq {
  /* Styles for faq element */
}
.container {
  /* Styles for container element */
}
.left {
  /* Styles for left element */
}

/* Additional CSS styles go here */
HTML content goes here...

Another JS Fiddle demo link.

For more information, refer to the following resources:

Answer №2

To align the button to the right using justify-content: space-between;, it is important to only have 2 elements in the container. Otherwise, there will be equal spacing between all elements within that container (as seen after the button in your example). In the code snippet below, I created a div (.label) to encompass the question paragraph and the button, and then applied flexbox to achieve the desired layout.

section {
    background-color: #fff;
    height: 100vh;
    height: fill-available;
    min-height: -moz-available;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
}

.faq {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.container {
    display: flex;
    width: 60vw;
}

.left {
    width: 100%;
}

.get-started {
    font-family: "Poppins", sans-serif;
    font-size: 28px;
}

/* Apply flex styling to the label div */
.label {
    display: flex;
    justify-content: space-between;
}
<section class="faq">
    <div class="container">
        <div class="left">
            <div class="get-started">
                <p>Learn How To Get Started</p>
            </div>
            <div class="question">
                <div class="label">
                    <p>How do I create a website?</p>
                    <button>+</button>
                </div>
                <div>
                    <p></p>
                </div>
            </div>
            <div class="question">
                <div class="label">
                    <p>Is this website right for me?</p>
                    <button>+</button>
                </div>
                <div>
                    <p></p>
                </div>
            </div>
            <div class="question">
                <div class="label">
                    <p>How much will web storage cost me?</p>
                    <button>+</button>
                </div>
                <div>
                    <p></p>
                </div>
            </div>
        </div>
    </div>
</section>

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

Move your cursor over an image within a div container

After not finding any helpful answers on this topic, I've decided to ask for help again. My issue involves having an image inside a div. <div id ="gratterlendis"> <a href ="?page_id=8"><img src="img/gratterlendis.png" align="right" wid ...

How can I position a div element inside another fixed div element?

I'm working on a layout with a fixed gradient div that contains all my content. However, I want the content to be scrollable instead of fixed. How can I make this happen? Here is the current structure: <body> <div class="wrappe ...

Is it possible to modify the shrinking behavior of Flexbox?

My goal is to mimic the layout of a specific webpage using flexbox in my version. However, I've noticed that my version, utilizing flexbox, adjusts to smaller screen sizes differently than the original. The original layout I'm trying to replicate ...

Having trouble with implementing a lightbox form data onto an HTML page using the load() function? Let me help

In my project, I have set up a lightbox containing a form where user inputs are used to populate an HTML file loaded and appended to the main HTML page with the load() function. While I can successfully load the data onto the page, I am facing challenges i ...

CSS - Curved background image in the top left corner

Currently in the process of creating a website here. There is a postcode search div on the right side, and I am looking to round the top corner of it. I have decided to use images to achieve the rounded corners effect. My requirement is that any method us ...

Bootstrap 5 dropdown toggle displaying incorrect menu

I am currently working on a website project using Bootstrap 5. In the navigation bar of my site, I have implemented a dropdown menu with multiple submenus that dropend. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" ...

Move the location of the mouse click to a different spot

I recently received an unusual request for the app I'm developing. The requirement is to trigger a mouse click event 50 pixels to the right of the current cursor position when the user clicks. Is there a way to achieve this? ...

Unexpected behavior with Bootstrap classes

I have come across the following HTML code which appears to be using bootstrap, however, it is not responsive. It is supposed to be responsive and adapt to mobile and tablet devices, but it still displays the desktop view on mobile and tablet screens. < ...

Can Angular Flex support multiple sticky columns at once?

I am trying to make the information columns in my angular material table stay sticky on the left side. I have attempted to use the "sticky" tag on each column, but it did not work as expected. <table mat-table [dataSource]="dataSource" matSort class= ...

Ensuring that hover remains active despite mousedown in JavaScript, CSS, and HTML

It appears that in browser-javascript hover events are deactivated when the mouse button is pressed down, as demonstrated by the following example: (Please run the code snippet to witness what I am referring to.) * { user-select: none; } #click, #drag, ...

Updating JQuery function after window is resized

click here Currently, I am using slick-slider to showcase content in 3 columns by utilizing flexbox. The aim is to have the slider activated only on mobile view. This means that when the screen size shrinks down to mobile view, the 3 column flexbox transf ...

Adjust the height of a div using jQuery once the AJAX call retrieves the data

There is a div that always matches the height of the adjacent div, depending on the content loaded in it. This setup was functioning properly until I implemented a search feature using jQuery. Now, when I perform a search, the element used in the jQuery ca ...

Using jQuery to remove all inline styles except for a specific one

Just a quick inquiry: Our Content Management System (CMS) utilizes CKEditor for clients to modify their websites. The front-end styles include the use of a pre tag, which we have customized to our desired appearance. However, when their team members copy a ...

What is the best way to generate a tilted slant with text positioned next to it?

.image { min-height: 100vh; } .bg-image { background-image: url('https://picsum.photos/1440/900'); background-size: cover; background-position: center; } <!--About Us --> <div class="container-fluid"> <div class="row no- ...

Concluding remarks can be found at the end of the article

Is there a way to keep text aligned next to an image without it dropping down and disrupting the layout of the article? * { margin: 0; box-sizing: border-box; } * a:link { text-decoration: none; ...

Disable automatic playback of HTML video

There is an HTML video with an image that loads initially and then disappears to play the video. I would like the image to always be visible until I click on it. Once clicked, the video should start playing. You can view the code on JSFiddle: http://jsf ...

Issue: In Firefox, resizing elements using position:absolute does not work as expected

My resizable div code looks like this: #box { height: 10rem; width: 10rem; resize: both; overflow: hidden; border: solid black 0.5rem; position: absolute; pointer-events: none; } <div id="item"></div> While it works perfectly ...

Disabling the hover effect of a material-ui button within a styled component

I tried adding the css hover property to disable the button's hover effect, but it doesn't seem to be working for my situation. How can I go about resolving this issue? import Button from 'material-ui/Button' import styled from 's ...

Connecting a picture to a web address using the <img class> tag

I'm facing a major issue with the "fade script" I added to my portfolio. It's designed to fade between thumbnails using jQuery and CSS, but it relies on 2 img classes that I can't seem to link properly. Basically, I want to link the image g ...

What steps can I take to prevent Javascript/JQuery from repositioning other elements on the webpage?

Is there a way for the animation code to only affect the show/hide toggle buttons and not interfere with the other jQuery code I have implemented for hiding and showing div text? When I included the animate code in my js page, it caused issues with the exi ...