Attempting to align several div elements in the middle while ensuring they all stay in a single row

I've been attempting to center a contact form and a feedback form side by side. I tried using float: left; for both to align them horizontally, but I also want them to be centered on the page.

Whenever I manage to display them together horizontally, they stay stuck to the left side. And when I try to center them, they stack on top of each other.

I'm striving to achieve both of these goals.

I've experimented with:

    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 500px;

I've also attempted using float: left; to position them next to each other horizontally, but I can't seem to center them that way either.

text-align: center; 

It appears that this only centers the text within the div tags, not the full div itself.

Please lend a hand, thank you!

Answer №1

Enclose all elements within a centralized parent container.

<div id="container"><!--your content goes here --></div>

Set a specific width for the parent container and apply margin: 0 auto;:

#container{
margin: 0 auto;
width:800px;
}

Answer №2

I believe this is exactly what you've been searching for, and it's quite responsive too.

The pair of divs are nicely centered inside a container div.

Here's the code snippet:

<div id="mainContainer">
<div class="leftSideDiv"></div>
<div class="rightSideDiv"></div>
</div>

#mainContainer{
  width:auto; 
  margin:none;
  height:200px;
  background-color:#3f3f3f;
  align-content:center;
  padding:1em 0;
}
.leftSideDiv{
  background-color:#696969;
}
.rightSideDiv{
  background-color:#939393;
}
.halfDiv{
  display:inline-block;
  height:100px;
  margin: 0 2%;
  width:46%;
  float:left;
}

Answer №3

Opt for using the display property with a value of table rather than block.

display: table;
    margin: 0 auto;
    width: 500px;

Answer №4

To achieve a horizontal line-up of elements, replace display: block with flex* and add justify-content: center*

*

If elements are stacking vertically, they likely have display: block. For a horizontal layout, use display: inline or display: inline-block†. Most form elements default to display inline.

I opted for input tags instead of divs as they are inline. If using divs, give them display: inline-block:

div { display: inline-block; }

The "div" selector can vary based on your code (which is curiously absent) -_-

Also created a demo with 2 fieldsets #contact and #feedback.

http://plnkr.co/edit/pET6ZsG3IKQITXqnXBZY?p=preview

body {
  width: 100vw;
}
#bar {
  display: flex;
  justify-content: center;
  margin-left: auto;
  margin-right: auto;
  width: 500px;
  border: 2px solid black;
}
input {
  width: 40px;
  text-align: center;
}
<section id="bar">
  <input value="INPUT">
  <input value="INPUT">
  <input value="INPUT">
  <input value="INPUT">
  <input value="INPUT">
  <input value="INPUT">
  <input value="INPUT">
  <input value="INPUT">
  <input value="INPUT">
  <input value="INPUT">
</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

Methods for updating an HTML-select without a page reload in PHP

I am in need of a solution to update a dropdown list without refreshing the page. The scenario is that I have a form where I can add elements, and another form with a dropdown list connected to a database. If the element I want is not available in the drop ...

Issue with AngularJS causing HTML input field to limit the display to only three digits

Whenever I input a number with 4 digits or more (excluding decimals) into the designated box, it mysteriously disappears once I click away (blur). Could it be due to the currency filter causing this issue? Even though the model retains the value when logg ...

Is it possible to increase the delay in the nth-child selector?

I'm attempting to create a compact checklist that displays in a single row with items appearing sequentially. To achieve this, I utilized the :nth-child() selector in CSS as shown below: .animated { -webkit-animation-duration: 0.8s; animation-d ...

What is the process for altering fonts in a Chrome OS application?

After extensive research on the Internet, I have been unable to find clear instructions on how to modify the text font in a Chrome OS application. Most search results pertain to adjusting the font for the OS user rather than the developer of the app. My i ...

Create a specific part of a webpage that can be scrolled through

In a unique way, I have designed a page where the only way to navigate is by simply clicking. To achieve this, I have implemented the following custom CSS: body{ overflow:hidden; } However, I have encountered a problem where I am unable to scroll to ...

Learn how to dynamically set the "selected" option in Vue based on object data

I've done some digging on SO but haven't found exactly what I need. So, here's the situation - I've got a sorting function in progress. I have an array of date ranges (PayPeriods) that I want to render into a select with option compone ...

Assess html code for Strings that include <% %> tags along with embedded scripts

Having a small issue with my code where I am receiving an HTML response from a web service as a java String. I need to display this String as HTML on my webpage, but the problem is that there are some script tags in the form of <% ... %> which are sh ...

The content is not wrapped when transferred from App.js through props, although it is being wrapped in other scenarios

As I work on developing a basic invoice generator application, I encounter an issue with overflowing content in the "notes" section when passing data through props from the App.js file. Instead of wrapping the text, it overflows its container. import "./Ap ...

What is the best way to center a navbar vertically inside a div?

I've come across some posts mentioning the need for a container with h-100 to align items using the align-self class. However, I'm facing issues aligning a navbar at the bottom of a div. When I place the navbar within another container and row, i ...

What is the process for creating a hover linear wipe transition using CSS/JS?

It's worth noting that I can't simply stack one image on top of the other because I'll be dealing with transparent images as well. preview of linear wipe ...

Adjust background image size to fit the dimensions of the foreground content

I have a div where an image and content are inside. I am trying to make the background image adjust its size based on the content within it, rather than showing the full image size with empty space. My current styling uses flexbox: .section { height: ...

The sub-menu is being obscured by a PDF element in Internet Explorer, causing visibility issues

I have an object type tag on my page that displays a PDF file. Everything is functioning correctly, but in Internet Explorer, my sub-menu is being hidden behind the object tag. var objTag = $('<object></object>') .attr({ data: source ...

Can non-mobile browsers handle touch events effectively?

Touch and gesture functionalities are no longer limited to mobile devices. When can we expect to see these features integrated into web apps for devices like the MacBook Pro or any other device with a touchpad or trackpad? I'm particularly interested ...

Instructions for swapping out the default drop-down icon in my select box with the <i class="icon-xyz-chevron down-rotation"></i> symbol

Is there a way to replace the default drop down arrow with a new one without creating a new class and adding it to the label? <i class="icon-xyz-chevron down-rotation"></i> Any suggestions on achieving this? <label class="col-md-8 col-sm- ...

Schedule Master: A sophisticated tool for time management

I have been following the instructions to implement a date time picker from this tutorial. I downloaded the necessary js and css files and placed them in the respective directories. However, when I click on the calendar icon, the calendar does not pop up. ...

Styling images and text in CSS within a jQuery UI autocomplete widget

I am currently using an autocomplete widget that displays text along with images. The results I have right now are not meeting my requirements. I want to customize the appearance of my results so that the words 'test' and either 'Federico&a ...

IE9 - The suitable alternative for framesets

Currently, I am experiencing an issue with framesets in my website. Ever since IE9 (HTML5), framesets have been deprecated which triggers quirks mode and causes a slowdown in the loading of my page. I am seeking suggestions for an appropriate HTML5 compa ...

Designate the preferred location for requesting the desktop site

Is there a way to specify the location of the 'Request desktop site' option? Here is the code I am currently using: var detector = new MobileDetect(window.navigator.userAgent); if(detector.mobile() != null || detector.phone() != null || det ...

What is the process of displaying text within a link?

I have a basic webpage where I want the text to display from a link. Here is my website: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Encyclopedia</title> <link href="test.css" re ...

The stylesheet's URL reference to the image is not functioning properly

I'm attempting to showcase images inside a table cell using CSS. Here's what I currently have: HTML: <td class="activity-status status_not_started">Not Started</td> CSS: td.activity-status { font: 0/0 a !important; color: transpar ...