Flexbox isn't lining up items horizontally as expected

I have been researching flexbox and it seems like it should display in a horizontal row by default. However, no matter what I try, I can't seem to get it to work as expected. I've included the code below. When I remove "display: flex;" from the CSS, it looks closer to how I want it to appear, but I need the heights of the 3 divs to be equal, which is why I want to utilize flexbox. Any guidance on this would be greatly appreciated.

/*PARENT*/
form fieldset{
    display: flex;
    flex-direction: row;
    padding: 5px 0px;
    border: 0;
}

/*CHILDREN*/
form fieldset > div{
    display: inline-block;
    width: 25%;
    text-align: center;
    border: solid thin black;
}


/*STYLES*/
form fieldset:first-of-type > div > p{
    text-decoration: underline;
    font-size: 1.2em;
}

form fieldset:first-of-type > div > div{
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
    font-size: 0.9em;
}

form fieldset:first-of-type div p{
    text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>
    <link rel="stylesheet" href="temp_css.css">
</head>
<body>
    <form action="temp.html">
        <fieldset id="fieldset">
            <div id="movie">
                <p>Movie or TV Show?</p>
                <div>
                    <label for="movie">Movie</label>
                    <input type="radio" name="tv_or_movie" id="movie"><
                    <label for="tv">TV Show</label>
                    <input type="radio" name="tv_or_movie" id="tv">
                </div>
            </div>
            <div id="animated">
                <p>Is It Animated?</p>
                <div>
                    <label for="animated_yes">Yes</label>
                    <input type="radio" name="animated" id="animated_yes">
                    <label for="animated_no">No</label>
                    <input type="radio" name="animated" id="animated_no">
                </div>
            </div>
            <div id="favorites">
                <p>Would You Consider it One of Your Favorites?</p>
                <div>
                    <label for="favorites_yes">Yes</label>
                    <input type="radio" name="favorite" id="favorites_yes">
                    <label for="favorites_no">No</label>
                    <input type="radio" name="favorite" id="favorites_no">
                </div>
            </div>
        </fieldset>
    </form>
</body>
</html>

Answer №1

There is an issue with the fieldset tag not responding to the flex container. To make flex work, you can try using a different element instead. In the example below, I have used the section tag to demonstrate how to solve the issue, but feel free to experiment with other tags as well.

/*PARENT*/
form section{
    display: flex;
    flex-direction: row;
    padding: 5px 0px;
    border: 0;
}

/*CHILDREN*/
form section > div{
    display: inline-block;
    width: 25%;
    text-align: center;
    border: solid thin black;
}


/*STYLES*/
form fieldset:first-of-type > div > p{
    text-decoration: underline;
    font-size: 1.2em;
}

form fieldset:first-of-type > div > div{
    display: inline-block;
    margin-left: auto;
    margin-right: auto;
    font-size: 0.9em;
}

form fieldset:first-of-type div p{
    text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>s
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test</title>
    <link rel="stylesheet" href="temp_css.css">
</head>
<body>
    <form action="temp.html">
        <section id="fieldset">
            <div id="movie">
                <p>Movie or TV Show?</p>
                <div>
                    <label for="movie">Movie</label>
                    <input type="radio" name="tv_or_movie" id="movie">
                    <label for="tv">TV Show</label>
                    <input type="radio" name="tv_or_movie" id="tv">
                </div>
            </div>
            <div id="animated">
                <p>Is It Animated?</p>
                <div>
                    <label for="animated_yes">Yes</label>
                    <input type="radio" name="animated" id="animated_yes">
                    <label for="animated_no">No</label>
                    <input type="radio" name="animated" id="animated_no">
                </div>
            </div>
            <div id="favorites">
                <p>Would You Consider it One of Your Favorites?</p>
                <div>
                    <label for="favorites_yes">Yes</label>
                    <input type="radio" name="favorite" id="favorites_yes">
                    <label for="favorites_no">No</label>
                    <input type="radio" name="favorite" id="favorites_no">
                </div>
            </div>
        </section>
    </form>
</body>
</html>

Answer №2

  1. flex and fieldset don't mix well together. It's best to use a wrapper instead, as flex will handle everything else smoothly.
  2. Get rid of any unnecessary elements, as justify-content will manage things effectively in your case.

Keep things simple rather than making them unnecessarily complex.

.MainDiv {
  display: flex;
  width: 100%;
  justify-content: space-around;
}

.MainDiv>div {
  width: 25%;
  text-align: center;
  border: 1px solid black;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>test</title>
  <link rel="stylesheet" href="temp_css.css">
</head>

<body>
  <form action="temp.html">
    <fieldset id="fieldset">
      <div class="MainDiv">
        <div id="movie">
          <p>Movie or TV Show?</p>
          <div>
            <label for="movie">Movie</label>
            <input type="radio" name="tv_or_movie" id="movie">
            <label for="tv">TV Show</label>
            <input type="radio" name="tv_or_movie" id="tv">
          </div>
        </div>

        <div id="animated">
          <p>Is It Animated?</p>
          <div>
            <label for="animated_yes">Yes</label>
            <input type="radio" name="animated" id="animated_yes">
            <label for="animated_no">No</label>
            <input type="radio" name="animated" id="animated_no">
          </div>
        </div>

        <div id="favorites">
          <p>Would You Consider it One of Your Favorites?</p>
          <div>
            <label for="favorites_yes">Yes</label>
            <input type="radio" name="favorite" id="favorites_yes">
            <label for="favorites_no">No</label>
            <input type="radio" name="favorite" id="favorites_no">
          </div>
        </div>
      </div>

    </fieldset>
  </form>
</body>

</html>

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

App.jsx line 11 is throwing an error due to an undefined property 'TodoComponent'

While attempting to style a ReactJS component, I encountered an error in my browser's development console: App.jsx:11 Uncaught TypeError: Cannot read property 'TodoComponent' of undefined at App.render (App.jsx:11) I have tried imp ...

Issue with Bootstrap collapsible menu not expanding when clicked

I want to make my collapsed navbar menu expand when clicked on a small screen. I've customized a Bootstrap navbar to split my links at each end of the bar. Right now, the links collapse when the screen size decreases, but clicking on the accordion but ...

The UI does not display the html code (specifically svg) added through jquery appending

I am working with the following HTML code: <svg> <g> <rect></rect> <text> abc </text> </g> <g> <rect></rect> <text> def </text> </g& ...

Boosting Your Theme - Eliminating Redundant Styles

Currently, I am facing more of a best practice issue rather than a coding one. I am in the process of creating a custom bootstrap theme inspired by https://github.com/HackerThemes/theme-kit. Although I have a functional theme that I am satisfied with, I am ...

Retrieving text snippet from an HTML document using .NET

Users input HTML content using a richtext editor, allowing for a variety of elements. Here is an example of such content: <h1>Header 1</h1> <p>Some text here</p><p>Some more text here</p> <div align=right><a hr ...

Experiencing an issue while rendering due to the presence of display: table-cell;

I am currently in the process of developing a website. The HTML Header section is structured as follows: <div id="page"> <header> <a href="#" class="redsquare">&nbsp;</a> <div class="head-wrapper"> ...

Pull data from one array of objects using the id from another array to display in a list using AngularJS ng-repeat

After retrieving a list of options, I make an API call to validate each option. The goal is to display whether each option is valid or not. My starting array looks like: $scope.preValidationArray = [ { id: 1, description: 'Item 1' }, { id: 2 ...

What is the best way to shift a list to the right within my navigation bar using ReactJS and CSS?

I'm looking to align a list to the right in my navbar, as shown in the image below Image Here's my navbar.js code: import React from "react"; import "./Navbar.css"; const Navbar = ({ isScrolling, information }) => { ...

Exploring the distinction between absolute elements nested within other absolute elements and relative elements nested within absolute elements

My latest CSS experiment involved creating a flag of a country. Check out my code: div.flag { width: 900px; height: 600px; background-color: red; position: relative; } div.flag > div { position: absolut ...

What steps can be taken to avoid my Bootstrap banner wrapping to a new line?

I am facing an issue while designing a top banner using Bootstrap. The condensed menu always wraps to a new line for the resolution of 1024x768. It works fine for resolutions greater than 1024x768. I need help with correcting my HTML so that the banner rem ...

Why does box-shadow only display on the bottom border and not the right and bottom border?

I want to add a shadow effect on the bottom and right sides of a specific div. #navigation-and-slideshow { overflow: hidden; background-color: #fff; padding: 10px 1%; box-shadow: 2px 2px 5px #222; } However, I am only seeing the shadow ef ...

Tips for embedding Javascript code within the window.write() function

I have a JavaScript function that opens a new window to display an image specified in the data variable. I want the new window to close when the user clicks anywhere on it. I've tried inserting some JavaScript code within window.write() but it doesn&a ...

Export Image as Json File

I am currently working on creating a browser extension and I am trying to figure out how to save images locally. My plan is to store the images in a json file, but I'm feeling quite lost on how to accomplish this. Below is the code I have so far, alth ...

Uncover the hidden message within the unique special character "ì"

My JSON file contains a specific character, such as ì; Here is the service I am using: $http({ url: jsonUrl, method: "GET" }).success(function (data) { // data is an array that contains a list of elements (f ...

Restoring hover functionality after resetting color using jQuery - tips and tricks

My jQuery code is working well, but I am facing one issue. After clicking on my infocontent menu, the hover effect on h1 is no longer working. How can I bring back the hover function? Here is the HTML: <div class="infocontent"><h1>What?</h ...

Issue with rendering of Outlined select label in Material UI not functioning correctly

According to the demonstration, the position of the label for a Material UI outlined select input should be at the top border of the select box. However, in my application, it seems like the z-index of the label is causing it to appear behind the top bord ...

Steps to dynamically reveal a table row within another row when clicked:

I have a table and I want to show the contents of another row inside the current row when the plus icon is clicked. I'm not sure which HTML element to use for this. Please help me figure it out. Thank you in advance. <div class="pro ...

Clicking the popup form causes it to blur out

Having trouble with a blurry pop-up form when clicking the button? Check out the code I've used below: <button class="btn btn-default" data-toggle="modal" data-target="#myModal">ENQUIRE NOW</button> ...

Full-screen Bootstrap burger navigation menu

Check out this code snippet on boot ply: I'm trying to create a full-screen menu with the same effect as the burger menu for non-mobile screens. Currently, the burger menu only shows up on mobile devices. Does anyone know how I can achieve this? Than ...

The content is being pushed down by the responsive navigation bar

While the navbar is in normal non-responsive mode (I'm not sure if that's the right terminology) on Chrome and you scroll down, the logo image stays behind the menu. However, when the screen width shrinks and the menu collapses, clicking the icon ...