skip to Main Content

Hello all, I am stuck right now making a quiz for my high school final and for some reason, the buttons flexbox refuses to place the buttons in a row across the screen. They are stuck in a column and I would also like the text to be right under the images. If someone can help me that would be great.

Here is what the webpage currently looks like

I tried to look for solutions elsewhere and relearned how to use flexbox, but nothing is working for this. Here is the code.

HTML:

<div class="TotalQuestions">
  <div class="question1">
    <div class="answer-choice">
        <button id="q1a1"><img class="button_images"src="lukeCombs.jpeg">  </button>
          
        <p>Luke Combs</p>
    </div>

   <div class="answer-choice">
        <button id="q1a2"><img class="button_images"src="TaylorSwift.webp">  </button>
          
        <p>Taylor Swift</p>
    </div>

    <div class="answer-choice">
        <button id="q1a3"><img class="button_images"src="Jcole.jpeg"></button>
        <p>J. Cole</p>
    </div>

    <div class="answer-choice">
        <button id="q1a4"><img   class="button_images"src="Franksinatra.jpg">  </button>
           
        <p>Frank Sinatra</p>
    </div>
  </div>
</div>

Here is the CSS:

.TotalQuestions{
  font-weight: bold;
  padding-bottom:100px;
  padding-left: 25px;
  padding-right: 25px;
  padding-top: 100px;
  text-align: center;
  display: flex;
  flex-direction:row;
  justify-content: space-around;
  align-items: baseline;
  flex-wrap:wrap;
}
.TotalQuestions>*{
  min-width:200px; 
}
.button_images{
  width: 100%;
  height:auto;
}

button{
  width:10%;
  border: none;
  background: none;
}
button:hover{
  padding:none;
  cursor:pointer;
  opacity: 0.8;
  transition: 0.3s;
}

Once again, any help is greatly appreciated.

2

Answers


  1. display: flex needs to be on the immediate parent on the elements you want to be placed using flexbox. This markup describes a flexbox container with 1 item placed in a row, which is the div with class question1. All the divs inside it are being placed in a column with the default display: block. Add a display: flex rule to .question1 and it will solve your issue.

    .question1 {
      display: flex;
    }
    

    EDIT: You probably actually want to generalize that to your other questions, so you would want to add it to all the children of .TotalQuestions:

    .TotalQuestions > * {
      display: flex;
    }
    
    Login or Signup to reply.
  2. you need to apply flex to .question1, when you apply it to .TotalQuestions it only applies to .question1

    .TotalQuestions{
      font-weight: bold;
      padding-bottom:100px;
      padding-left: 25px;
      padding-right: 25px;
      padding-top: 100px;
      text-align: center;
      display: flex;
      flex-direction:row;
      justify-content: space-around;
      align-items: baseline;
      flex-wrap:wrap;
    }
    
    .question1{
      font-weight: bold;
      padding-bottom:100px;
      padding-left: 25px;
      padding-right: 25px;
      padding-top: 100px;
      text-align: center;
      display: flex;
      flex-direction:row;
      justify-content: space-around;
      align-items: baseline;
      flex-wrap:wrap;
    }
    
    
    .TotalQuestions>*{
      min-width:200px; 
    }
    .button_images{
      width: 100%;
      height:auto;
    }
    
    button{
      width:10%;
      border: none;
      background: none;
    }
    button:hover{
      padding:none;
      cursor:pointer;
      opacity: 0.8;
      transition: 0.3s;
    }
    <div class="TotalQuestions">
      <div class="question1">
        <div class="answer-choice">
            <button id="q1a1"><img class="button_images"src="lukeCombs.jpeg">  </button>
              
            <p>Luke Combs</p>
        </div>
    
       <div class="answer-choice">
            <button id="q1a2"><img class="button_images"src="TaylorSwift.webp">  </button>
              
            <p>Taylor Swift</p>
        </div>
    
        <div class="answer-choice">
            <button id="q1a3"><img class="button_images"src="Jcole.jpeg"></button>
            <p>J. Cole</p>
        </div>
    
        <div class="answer-choice">
            <button id="q1a4"><img   class="button_images"src="Franksinatra.jpg">  </button>
               
            <p>Frank Sinatra</p>
        </div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search