Attempting to center two divs to start at the same point

I've been attempting to center align two divs and have them start at the same position using CSS.

Below is the inline CSS code. The first div is named container and the second div is named descriptionarea.

#container {
            width: 60%;
            height: auto;
            margin-left:auto;
            margin-right:auto;
        }

        .inner-container {
            min-height: 400px;
            display: inline-block;
            overflow-y: auto;
            border: 4px solid black;
            margin-left:auto;
            margin-right:auto;
        }

        #chats-container {
            height: 70%;
            width: 60%;
            padding-top:5px;
            padding-left:5px;
        }

        #users-container {
            height: 70%;
            width: 22%;
            padding-top:5px;
            padding-left:5px;
        }

        input[type=submit] {
        border: 1px solid #f44c0e;
        color: #fff;
        background: tomato;
        padding: 10px 20px;
        border-radius: 3px;
        }
        input[type=submit]:hover {
            background: #f44c0e;
        }

        textarea {
         width: 550px;
         height: 60px;
         font: normal 14px verdana;
         line-height: 30px;
         padding: 2px 10px;
         border: solid 1px #ddd;
        margin-left: auto;
        margin-right: auto;
        }


        .descriptionarea {
       min-width: 490px;
       position: relative;
       display: inline-block;
        margin-left: auto;
        margin-right: auto;
        }

    .descriptionarea textarea{
    width: 490px;
    height: 20px;
    height: 60px;
    font: normal 14px verdana;

    }

    .descriptionarea span.namefortxtarea{
        display: block;
        text-align: left;
        font-size:12px;
    }

    .descriptionarea span.buttonfortxtarea{
        display: block;
        text-align: right;
    }

This is how you can apply the two divs in HTML format:

First div:
<div id="container">
    <div id="chats-container" class="inner-container">
    </div>
   <div id="users-container" class="inner-container">

    </div>
</div>

Second div:

<div class="descriptionarea">
    <textarea id="new-chat-input" ></textarea>
    <span class="buttonfortxtarea">
    <button class= "btn btn-mini description_submit" id="new-chat-button">Submit</button></span>
    </div>

Even after applying the styles to both divs, they are still not aligned to the center and do not start at the same position.

Remember to include the following in your CSS:

margin-left:auto; margin-right:auto;

Answer №1

To achieve center alignment of your divs, you can remove the id attribute for div#container and instead place it within the .descriptionarea class. Then, wrap both divs with a parent div#container.

<div id="container">
<div class="descriptionarea">
    <div id="chats-container" class="inner-container"></div>
    <div id="users-container" class="inner-container"></div>
</div>
<div class="descriptionarea">
    <textarea id="new-chat-input" ></textarea>
    <span class="buttonfortxtarea">
             <button class="btn btn-mini description_submit" id="new-chat-button">Submit</button>
    </span>
</div>
</div>

Answer №2

To resolve this issue, consider utilizing Flexbox

Answer №3

Absolutely! Flexbox is fully compatible with all major web browsers. When naming your classes, remember to either use CamelCase notation or hyphens for better organization.

Answer №4

The issue at hand is a result of the use of display: inline-block. To resolve this, consider grouping your blocks within a single container and applying float: left to the first and second div elements, while removing the display: inline-block property.

<div class = "center">
    <div id="chats-container" class="inner-container">
</div>
   <div id="users-container" class="inner-container">

</div>
</div>
<div class="descriptionarea">
    <textarea id="new-chat-input" ></textarea>
    <span class="buttonfortxtarea">
    <button class= "btn btn-mini description_submit" id="new-chat-button">Submit</button></span>
    </div>
</div> 



.inner-container {
            min-height: 400px;
           float: left;
            overflow-y: auto;
            border: 4px solid black;
            margin-left:auto;
            margin-right:auto;
        }

.descriptionarea {
       min-width: 490px;
       position: relative;
       margin-left: auto;
        margin-right: auto;
        float: left;
        }

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

Error Alert: JQuery Pop Up Issue

Struggling with getting my JQuery Pop-Up Box to work properly. Any guidance for a newbie like me on how to fix it would be greatly appreciated. Here's the code I've been working on: <!-- POP UP HTML --> <div class="infocontainer"> ...

Tips for choosing a particular value in an HTML <script> query

Can someone please help me figure out how to select specific values in a form using a query? I'm trying to extract certain values like Hello and Lorem ipsum..., as well as Hi and Nullam fringilla... from the Content.join(''); section. I apo ...

What is the best way to apply a class to a container when a component in AngularJS receives focus or is clicked?

I am trying to apply a class to the container when either the input field is focused or when the dropdown component receives focus or is clicked. The class should be removed when the focus is lost. The requirement is for the input container to have the &a ...

Utilizing Bootstrap datepicker to set maximum and minimum dates based on another date input

I have been attempting to create a restriction on the date selection, where datepicker1 cannot exceed datepicker2, and vice versa. Initially, I tried implementing this feature using (Linked Pickers). Unfortunately, this did not achieve the desired date in ...

Arrangement of Bootstrap card components

Each card contains dynamic content fetched from the backend. <div *ngFor="let cardData of dataArray"> <div class="card-header"> <div [innerHtml]="cardData.headerContent"></div> </div> <d ...

Tips on enhancing the appearance of your numberbox with vibrant colors

Hello everyone! I am working with some devextreme code blocks and need help in changing the border color of a numberbox to red. Can anyone guide me on how to achieve this? import React, { useState } from 'react'; import { NumberBox } from 'd ...

sliding to the right gets jammed

I am encountering a peculiar issue with Multi-page slide functionality. Whenever I attempt to switch pages, the new page gets stuck on the left before sliding out into position. Feel free to test this problem using my JSFiddle This is the JQuery code that ...

Combining Text in HTML Using CSS to Create Layered Effects

Link to GitHub: http://github.com/user123/code I'm attempting to position "welcome" behind "home", but I've been struggling to achieve this. I've experimented with setting the position, but so far I haven't been successful. Here' ...

Issues with Jquery datatable causing column header to overlap the table header

I am facing an issue with my datatable as the table is not displaying correctly. Could someone advise me on how to troubleshoot and fix this problem? Below is the code snippet in question: $(tbName).dataTable({ "sScrollX": "100%", "sScro ...

Center the cropped image within a div container

Is there a way to set a maximum height for a div containing an image and hide the parts of the image that exceed this height, while keeping it centered vertically inside the div? For example, if the browser width is 1200px and the image aspect ratio is 4:3 ...

Obtain the href using a combination of xpath and id

I need to extract the href links for 9 search results from this specific website. The xpath and selectors for the first, second, and third items are as follows: '//*[@id="search-results"]/div[4]/div/ctl:cache/div[3]/div[1]/div/div[2]/div[2]/div[2]/p[ ...

A step-by-step guide on using Jquery to fade out a single button

My array of options is laid out in a row of buttons: <div class="console_row1"> <button class="button">TOFU</button> <button class="button">BEANS</button> <button class="button">RIC ...

Attempting to create a dynamic dropdown menu with animated effects triggered by a key press

I've been attempting to construct a menu that initially appears as a small icon in the corner. Upon pressing a key (currently set to click), the checkbox is activated, initiating the animation. Most of the menu and animation are functional at this po ...

What is the functionality behind a free hosting website?

Is anyone familiar with websites like Hostinghood, where users can create a subdomain and upload HTML, CSS, etc.? I'm curious about how they operate and how I can create a similar site. This is my first question here, so please respond instead of disl ...

Submitting data using various submit buttons in PHP

I am facing an issue with multiple submit buttons in my form, each containing hidden values that are dynamically generated. The problem arises when I can see the correct values in the HTML source code, but upon submitting one button, all other values get s ...

Difficulty encountered in closing div by clicking the background with the help of jquery

I am facing a challenge with properly closing a div container and restoring it to its original state when I click outside of it. Despite trying various solutions from stackoverflow and extensive internet research, I have been unable to find or come up with ...

Display Checkbox Selections in a Popup Message Box

I've run into a problem while working on an assignment. My goal is to show the checkbox values for toppings in the alert box popup upon submission. However, only the text box and radio values are displaying, while the checkbox returns as blank. Any ...

Is it possible to use bootstrap to hide specific components depending on the size of the media screen?

Struggling with styling responsiveness and hoping to find a Bootstrap class that can easily hide components on medium and small screens. I'm looking for a way to hide headings, paragraphs, buttons, and more without any extra hassle. ...

Effortlessly Concealing Numerous Elements with a Single Click in Pure JavaScript

I have successfully created an HTML accordion, but I am facing an issue. I want to implement a functionality where clicking on one accordion button will expand its content while hiding all other accordion contents. For example, if I click on accordion one ...

Refresh a DIV using two SQL queries in forms

I am encountering an issue with updating a single div element using the results from two different forms on an HTML page. I want either form1 or form2 to display results in the same div element, and it should be updated with one line of content fetched fro ...