Aligning a pair of <div> elements within a container in a form field

As I attempt to recreate the survey form challenge from FreeCodeCamp.org Projects, I am facing an issue with aligning two <div> elements inside their parent <div> without the second child moving to a new line using CSS properties other than float.

I have experimented with setting the display property of the children <div> tags to inline elements but encountered issues with padding and margin not taking effect. Below is the code snippet that I used last which did not yield the desired outcome.

My goal is to achieve a layout similar to the one showcased in this CodePen example.

Answer №1

To set things in the right direction, a couple of quick adjustments were made. Eliminating several rows from the form simplified the code.

The .row-tab divs now have a display of flex, allowing them to span the entire width of the form and making the left/right divs flexible and sit side by side simultaneously.

.row-tab {
  display: flex;
}

Next, a rule was added for .left-tab:

.left-tab {
  flex-grow: 1; // ensures the left side expands to fill any remaining space
  text-align: right; // aligns the text
}

* {
    margin:0;
    padding:0;
    box-sizing: border-box;
}

body {
    font-family: Raleway,Helvetica,Arial,sans-serif;
    font-size: 16px;
    line-height: 1.2em;
}
.container {
    background-color: cornflowerblue;
}
#title {
    display: block;
    text-align: center;
    padding: 10px;
    margin-bottom: 10px;
    font-weight: bold;
}
#survey-form {
    background-color: #eeeeee;
    width: 80%;
    margin:0 auto;
    border-radius: 4px;
    padding: 10px;
}
#description {
    display: block;
    text-align: center;
    padding: 15px;
    font-size: 1em;
}

.row-tab {
    display: flex;
}

label {
    display: inline-block;
    text-align: right;
    width: 40%;
    padding: 5px;
    vertical-align: top;
    margin-top: 10px;

}
.left-tab {
  flex-grow: 1;
  text-align: right;
}
.right-tab {
    display: inline-block;
    width: 58%;
    height: 20px;
    text-align: left;
    padding: 15px;
    margin-left: 10px;
}

#submit-btn{
    width:100px;
    height: 50px;
    background-color: rgb(61, 138, 240);
    color: black;
    padding: 10px;
    margin-top: 15px;
    text-align: center;
    border:4px solid rgb(61, 58, 224);
    border-radius: 5px;
}
.submit-btn{
    text-align: center;
}
<body>
    <div class="container">
        <div id="title">
            <h1>Survey Form</h1>
        </div>
        <div>
            <form id="survey-form" action="get">
                <div id="description">
                    <p>Share your thoughts on enhancing FreeCodeCamp</p>
                </div>

                <div class="form-body">

                    <div class="row-tab"> 
                      <div class="left-tab">
                          <label for="name">*Name</label>
                      </div>
                         <div class="right-tab">
                            <input type="text" id="name" required placeholder="Your name">
                        </div>
                    </div>

                    <div class="row-tab">
                        <div class="left-tab">
                            <label for="email">*Email</label>
                        </div>
                        <div class="right-tab">
                        <input type="email" id="email" required placeholder="Your email">
                        </div>
                    </div>

                    <div class="row-tab">
                        <div class="left-tab">
                        <label for="age">*Age</label>
                        </div>
                        <div class="right-tab">
                        <input type="number" id="number" required placeholder="Your age">
                        </div>
                    </div>

                    

                    <div class="submit-btn">
                        <button id="submit-btn">Submit</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>

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

What is the best way to incorporate a background image that complements my content?

I'm currently working on revamping my friend's windsurf club website. The issue I'm facing is with trying to achieve a more elegant appearance by positioning a background image behind the <h1> and <h2> elements. Currently, I have ...

Issue with getting Bootstrap sticky-top to function on sidebar column

My goal is to implement a sticky sidebar on the right side of the page. The sidebar menu is contained within a grid column. I have attempted to utilize the sticky-top class, following the guidance provided in this question, but unfortunately, it does not s ...

A step-by-step guide on incorporating box-shadow for the jackColor in the Switchery Plugin

I am looking to add a box shadow to my iOS7 style switches for checkboxes when they are checked. Here is the code I have so far: var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch')); elems.forEach(function (html) { va ...

javascript tabs not functioning as expected in HTML

I am currently putting the finishing touches on a basic website for a project I'm involved in. The site features two tab controls - one on the right side of the page and the other on the left. Each control has 3 tabs that, when clicked, display differ ...

What are the steps for rearranging a table column?

I am trying to show a text file in a table and allocate some code to each entry. I can display the text file. I can assign the code for each record (though this part is still under development). However, I have encountered an issue. The columns of the t ...

What is the best way to incorporate the :after pseudo-element in JavaScript code

HTML: This is my current code snippet <div class="one"> Apple </div> I am looking to dynamically add the word "juice" using JavaScript with the .style property. Is there an equivalent of :: after in CSS, where I can set the content in JavaS ...

Issue with rendering <li> elements in Firefox browser detected

I have been working on a website and encountered an issue with the display of list elements in Firefox. The list is not showing consistently in Firefox, but it appears fine in IE, Opera, and Safari. I have attached an image below showing the difference bet ...

Make sure the navigation bar is fixed to the top of the page only

Looking for a solution to stick the navigation menu to the top of the screen only when the screen is wider than 782px. Progress so far: https://jsfiddle.net/ah4ta7xc/1/ Encountering an issue where there is an unintended gap at the top when applying the s ...

Is it possible to submit a form through a JavaScript hotkey?

Here's the current code that I'm working with: <select tabindex="2" id="resolvedformsel" name="resolved"> <option selected="selected" value="yes">resolved</option> <option value="no">not resolved</option> ...

The second tab on Bootstrap5's tab content feature seems to generate an endless amount of white

I am working on a project where I need to display statistics for both the current month and year. To organize this data, I created a card with tabs for each - one tab for the month and another for the year. By default, the month tab is loaded first. Howev ...

Retrieve the current height of the iFrame and then set that height to the parent div

Within a div, I have an iFrame that needs to have an absolute position for some reason. The issue is that when the iFrame's position is set to absolute, its content overlaps with the content below it. Is there a way to automatically adjust the height ...

Is there a method to give a webpage a subtle shimmering effect without utilizing CSS box-shadow?

Struggling to Develop a High-Performance Interface Feature I'm currently facing a challenge in coding an interface that requires a subtle and broad glow effect, similar to the example provided below: https://i.sstatic.net/E4ilD.jpg Exploration of ...

What can I do to keep my navbar on top of the slideshow?

https://i.sstatic.net/SfiLZ.jpg Is there a way to create a responsive navbar that sits in front of a slideshow containing images? I attempted to place the div within the main, but unfortunately it was unsuccessful. ...

Sketch a variety of numerical values in a circular formation

I was working on a number circle using the below fiddle, but I need it to always start from 0 at the top. How can I achieve this? Additionally, I would like to connect the numbers from the inner circle border to the number with a dotted line. How can I dr ...

Adjust Sidebar Height to Match Document Height (using React Pro Sidebar)

Having an issue with the height of a sidebar component in Next.js using React Pro Sidebar. It seems to be a JavaScript, HTML, and CSS related problem. I've tried several suggested solutions from Stack Overflow, but none of them seem to work. Surprisin ...

Is there a way to maintain the vertical alignment of spans within an HTML div after numerous drag and drop operations have taken place?

Recently, I created a JavaScript program (code provided below) to enable the dragging and dropping of <span> elements containing Greek characters within a basic <div> container. Upon initial page load, everything appears perfectly aligned vert ...

What is the simplest method to create a scroller for engaging narratives?

I am in the process of creating a static but responsive storytelling website using HTML. My objective is to create an effect similar to this: https://i.stack.imgur.com/zIEpU.gif The idea is to have text on the left and a *.jpg image fixed on the right. As ...

Selecting options from the Gmail dropdown menu

I'm attempting to create a dropdown menu similar to the one in Gmail using Bootstrap. https://i.sstatic.net/K1NCg.png However, I have encountered 2 issues: 1- I am unable to click on the input checkbox to select all 2 - I would like it so that if ...

Is it possible to have a div automatically scroll when hovered over, while also hiding the scroll bar specifically for that div?

Is there a way to autoscroll a specific element onmouseover or via an image map while also hiding the scrollbars for that div? I seem to be struggling with this task despite weeks of learning javascript and not being able to find the right solution online. ...

Struggling with organizing the layout of your HTML page? Let us lend

I am having difficulty with the layout of a page I'm working on. The lower page navigation is not positioning properly - it wraps below the left column instead of staying below the data columns. Additionally, the border for the navigation appears at t ...