What is the best way to align div elements side by side while using a flex direction of column?

I need help aligning the text input next to the h1 tag, inline, and then displaying it as a flex-direction of column. Currently, all inputs are being set in a line with the h1 on top which is not the intended layout.

Here is an illustration:

This is how I want it to be displayed:

.contactuscontent{
          border: 1px solid pink;
          display: flex;
      }
    
    
    .contactusinput {
        display: inline-flex;
        flex-direction: column;
        border: 1px solid purple;
    }
<div class="contactuscontent">
          <div class="contactusinput">
            <div class="name"><h1>Name</h1> <input type="text"> </div>
            <div class="email"><h1>Email</h1> <input type="text"> </div>
            <div class="refer"><h1>How did you find us</h1> <input type="text"> </div>
          </div>
        </div>

Answer №1

Exploring the semantics of HTML is essential; use <h1> for headlines.

For labeling input fields, opt for

<label for="...">
. Remember, you have the freedom to style any tag as you desire, so default styling should not dictate your choice of tags.

.contactuscontent {
  border: 1px solid purple;
  display: flex;
  justify-content: center;
}

.contactusinput {
  width: 400px;
  border: 1px solid teal;
}

.contactusinput>div {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
}

.contactusinput label {
  width: 200px;
}

.contactusinput input,
.contactusinput textarea {
  width: 200px;
  padding: 3px;
  }
<div class="contactuscontent">
  <div class="contactusinput">
    <div class="name"><label for="name">Name</label> <input type="text" id="name" name="name"> </div>
    <div class="email"><label for="email">Email</label> <input type="text" id="email"> </div>
    <div class="refer"><label for="howtofind">How did you find us</label> <textarea id="howtofind"> </textarea> </div>
  </div>
</div>

Answer №2

It's happening because the h1 element is a block element, which causes it to push the input onto a new line since it's inside an unstylized div.

If you turn the div containing both the h1 and the input into a flexbox, it will appear similar to the image:

.contactusinput div {
  display: flex;
}

You don't necessarily need to apply flexbox to any of the parent elements for this solution to work.

To keep the inputs on the same line, consider adding a min-width property to the h1:

h1 {
  min-width: 200px;
}

For smaller screens, you might need to adjust the styling by removing the min-width property and displaying the h1 as a column instead of a row.

Feel free to check out this example on jsFiddle

By the way, using heading elements like h1-h6 in this context isn't ideal. It's recommended to have only one h1 throughout the entire website. Consider using a label instead for better structuring.

Answer №3

In order to achieve the desired design, you must apply the flexbox property to the div that contains both the input and h1 elements.

Therefore, in this scenario, there will be three divs with flexbox design, all of which are direct children of the .contactusinput selector.

For styling purposes, you can set the style of the direct child div of .contactusselector to utilize flexbox, as shown below.

.contactuscontent {
  border: 1px solid pink;
  display: flex;
}

.contactusinput {
  display: inline-flex;
  flex-direction: column;
  border: 1px solid purple;
}

.contactusinput > div {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
<div class="contactuscontent">
          <div class="contactusinput">
            <div class="name"><h1>Name</h1> <input type="text"> </div>
            <div class="email"><h1>Email</h1> <input type="text"> </div>
            <div class="refer"><h1>How did you find us</h1> <input type="text"> </div>
          </div>
        </div>

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

If the iframe's CSS source is updated, the parent's CSS source will also change

I'm currently working on a unique school project that involves creating multiple CSS styles for different views. <link rel="stylesheet" type="text/css" href="css/main.css" title="main" media="screen"> <link rel="stylesheet" type="text/css" h ...

Issue encountered while generating dynamic Routes using the map function

When attempting to dynamically use Route from an array, I encounter an error. Warning: Incorrect casing is being used. Use PascalCase for React components, or lowercase for HTML elements. The elements I am utilizing are as follows: const steps = [ { ...

Utilizing Laravel 5.2 to showcase a video

I am currently developing in Laravel 5.2 and facing an issue with displaying a video on my view page. While I can successfully store the video in my database table, I am encountering difficulties when trying to fetch and display it. In my database table, I ...

Is there a way to incorporate CSS or tables when converting HTML to PDF using JavaScript?

While working on a project, I successfully converted an HTML file into a PDF. However, the output did not display the CSS design. Can anyone provide suggestions on how to include CSS design in the PDF file? Below is the JavaScript function code: $(funct ...

Is there a method similar to insertBefore that works with arrays and/or HTMLCollections?

Is there a vanilla JavaScript or jQuery function that works like Node.insertBefore(), but for arrays and/or HTMLCollections? An example of what I'm looking for: var list = document.getElementsByClassName("stuff"); var nodeToMove = list[0]; var other ...

Customizing Material UI Themes: A Guide to Globally Overriding Children Styles

I am currently in the process of developing an application using the Material UI library for ReactJS. I am exploring the capabilities of the Theme Overrides API to apply global styles to a component, specifically when it is a child of another specific comp ...

I am encountering errors when running NPM start

After setting up my webpack, I encountered an error in the terminal when attempting to run the code npm start. The specific error message was related to a module not being found. Can someone please assist me with resolving this issue? > <a href="/c ...

Accessing and displaying a randomly selected PDF file from a directory using HTML coding

Welcome to my recipe collection website where I document all the delicious recipes I love to eat. One feature I would like to add is a 'random' button, so that a recipe will be selected at random for me without having to make a decision. Althou ...

Failures in React-router on the server side

I am currently working on making my client-side webapp isomorphic and came across a helpful pattern on Client Routing (using react-router) and Server-Side Routing. Here is what I have implemented: server.js: var React = require('react'), expre ...

The form action now triggers the download of the PHP file instead of simply loading

I'm encountering an issue with the form on my website: <div class="searchBar"> <form action="searchDB.php" method="get"> <input type="text" class="searchBarInput" bdo dir="rtl" name="k"> <input type="image" cl ...

What is a more efficient method for obtaining the current URL in isomorphic React applications that works on both the client and server side?

Currently, I am working on developing an app utilizing a React Redux boilerplate that can be found here. One of the components requires access to the current URL upon mounting in order to generate a shareable link for social media. This component is acces ...

"Enhance your React app with stunning typography and

Is it possible to achieve a text underline effect in Typography like this? https://i.stack.imgur.com/5Eukj.png Here is the code I am currently using: <Typography variant="p" sx={{ letterSpacing: '1.5px', ...

<ol><li>Arranges elements in a peculiar way if the image is aligned to the left</li></ol>

Explore this comprehensive presentation on combining PDFs online. In this blog post, I have encountered two instances of <ol><li> formatting. The first one is styled properly using the following CSS: .post_content ul, .post_content ol ...

I want to deploy all my React files, including the source code, on Google App

I'm currently utilizing Google App Engine to host and serve my website, however, I've run into a peculiar issue. When browsing my website, I am able to access and retrieve all the source code that I have written through the developer tools: Belo ...

The proper way to update MUI styles

click here to see an image I am looking to customize the background style in this code snippet: MuiAccordion: { styleOverrides: { root: { boxShadow: 'none', position: 'inherit', '& .Mui-expanded': ...

Removing styling from a table header and specific table data cells with CSS

I am trying to create an HTML table where I only want to select cells that are not part of the thead section and do not have a specific class. I am having trouble with the selector for this particular case. table :not(thead):not(.cell-class) { backgro ...

What's the best way to ensure my website's footer spans the entire width of the webpage?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Elena Brown | Graphic Designer</title> <link rel="stylesheet" href="CSS/normalize.css"> <link href=' ...

The use of pattern fill in fabric.js is causing a decrease in rendering speed

I recently attempted to create a clip mask effect on a large image by using the picture as a pattern and setting it to the svg paths that support the desired shape. You can view the slow-loading jsfiddle example here: http://jsfiddle.net/minzojian/xwurbw ...

Having trouble with sending a POST request to a URL?

I'm attempting to communicate with an API by including the parameters in the URL. I am uncertain whether it will return XML or JSON, but it will be one of the two. Unfortunately, I keep encountering an error message. Here's an example of my requ ...

Using the PMT function in PHP allows for easy calculation of

I have been attempting to integrate the PMT function into my PHP code for a loan calculator. Unfortunately, the PHP code seems to be malfunctioning and I am unsure of the reason, especially since I am still at the novice level in programming. PHP(Get.php ...