Align Divs Horizontally to Center

I'm trying to find a way to horizontally center multiple DIVs in a straight line. Here's how my code currently looks:

HTML:

    <div id="circle">
<div id="circle1"></div>
<div id="circle2"></div>
</div>

CSS:

    #circle {
    text-align: center;
}

#circle1, #circle2 {
    background: #D5DED9;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    margin: 0 auto;
}

Although they do center horizontally, there appears to be a gap between the circles and I'm unsure how to align them in a straight line.

I've tried searching online for solutions, but haven't come across anything that effectively addresses this issue.

Answer №1

To create a centered display, apply the property display:inline-block; to elements with IDs #circle1 and #circle2

Removing the margin: 0 auto; from both div elements is unnecessary because you already have text-align:center; set on your wrapper.

Check out this JSFiddle demonstration for more clarity!

Answer №2

Avoid using display: inline-block for centering elements like divs as it can be affected by whitespace in the HTML document, causing styling issues.

Check out this jsFiddle for a comparison. The inline-block divs have some unwanted whitespace between them that can only be removed by adjusting the markup. This can lead to difficult debugging situations, as I've experienced before.

Instead, try centering the container of the divs with margin: 0 auto. Then use float: left on the divs inside their parent to align them horizontally and apply a clearfix to the container.

Here's an example of how you can style it:

#wrapper {
    margin: 0 auto;
}
.clearfix {
    display: table;
    clear: both;
}
.circle {
    float: left;
}

And here's a sample HTML structure:

<div id="wrapper" class="clearfix">
    <div class="circle"></div>
    <div class="circle"></div>
</div>

Answer №3

If I understand correctly, you are looking to align two circles on the same line, centered within the wrapper circle div.

One way to achieve this is by using the float property to position one circle to the left and the other to the right. You can then adjust their proximity by modifying the width of the wrapper div in percentage relative to its parent, the body.

Here's a possible solution: http://jsfiddle.net/UFN5S/

It's worth noting that similar questions have been discussed on Stack Overflow before. Even if you've searched already, it's likely that someone else has encountered and resolved a similar issue on the platform.

For example:

How to center two divs floating next to one another

or

Aligning two divs side by side center to page using percentage width

I hope this information proves useful to you!

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

Styling with CSS: How to Adjust Word Wrap in a Container that Resizes Dynam

I'm struggling to ensure that long lines of text break and wrap correctly within a chat feature I am developing. The crucial elements in question are .chat__messages__item and .chat__messages__body (these are all contained within a parent element set ...

Display intricate header and preview in a printed datatable

Hey there, I've been using the Datatable plugin and it's really great. However, I've come across a problem with complex headers like this: <thead> <tr><td>some text</td></tr> <tr><td>some te ...

Tips for resolving issues with storing data in a text box that is constantly being added to

Can someone please assist me with this issue I'm facing? I am using isset to check if the index is defined, but it stores 0 instead of saving the value of the textbox. How should I tackle this problem? If I don't check if the index is defined and ...

React JS - Large image failing to display entirely

I'm currently facing challenges with displaying my image in full size. I am unsure what is causing this issue and would appreciate any guidance on correcting my approach. The specific image I am attempting to display at full-size can be found https:/ ...

Applying CSS borders with circular corners

Can this unique border style be achieved using only CSS? https://i.sstatic.net/wMtbG.png ...

Having issues with object-fit: cover not functioning properly in Safari browser

Encountering a browser support issue at the moment. Everything is functioning as expected on Firefox and Chrome, but Safari is causing problems with images. Currently, the image size is set using "object-fit: cover" and working properly on Chrome/Firefox. ...

SVGs do not display on iPhones

Having some issues with my code - specifically, I have an SVG animation that seems to work on all devices except iPhones. I've been struggling to find a solution. Here is the code snippet for the SVG: <div class="contenuto-svg"> <svg vi ...

Does HTML elements come with a pre-set background color of white or transparency by default?

Feeling a bit confused about a straightforward issue. Can someone clarify for me - What is the original background color of HTML elements? Is it white by default or transparent? ...

Transferring information from a component that includes slot content to the component within the slot content

This task may seem complex, but it's actually simpler than it sounds. I'm struggling with the correct terminology to enhance the title. I need to transfer data from a component that includes slot content to that slot content component. In partic ...

AngularJS: Users must click submit button twice for it to function properly

It's puzzling that I have to click the submit button on my form twice before it triggers the save function below. My HTML is written in Pug format. Below is a snippet of my HTML template: <form ng-submit="save()"> <div class="form-group" ...

Acquire HTML information using the Robot framework and xpath techniques

My first task is to visit the website www.google.de. I aim to extract the word Deutschland from this section of the HTML code: <div class="logo-subtext">Deutschland</div> Since the id in this div class is not available, I must utilize xpath. ...

"Utilizing Primeng's P-Table Component for Enhanced Interactivity

Greetings to all! I am currently working with p-table, p-headercheckbox, and p-tableCheckbox along with additional filters for each column. However, I am encountering an issue where the filters are not aligning properly with the headers. ...

The placement of my footer is off and not aligned with the bottom of the page

I am struggling to make my footer align with the bottom of the page rather than sticking to the bottom of the browser window and moving along with the scroll. How can I fix this issue? The goal is to have the footer remain out of sight at the bottom of th ...

Updating an HTML value based on the selected option using JavaScript

I'm working on a form that included a select element: <select class="form-control"> <option>10 pc</option><!--1 USD --> <option>20 pc</option><!--2 USD --> <option>50 pc</option><!--3 USD ...

Is it possible to load MySQL data into an HTML form?

My Inquiry: https://i.sstatic.net/QnRVk.png I'm currently developing a web-based product catalog that includes an edit feature. At present, I have a drop-down menu that displays all the products stored in my SQL database. Is there a way to automatic ...

Is there a way to prevent pop-up windows from appearing when I click the arrow?

Is there a way to display a pop-up window when the green arrow is clicked and then hide it when the arrow is clicked again? I tried using the code below but the pop-up window disappears suddenly. How can I fix this issue using JavaScript only, without jQue ...

What is the best way to align a dropdown menu and a textbox on the same line

I have recently been working on a search bar that includes a Dropdown for Location and a Textbox for Search. Prior to implementing bootstrap, these elements were displayed in a single line. However, after applying bootstrap, they are now stacked vertically ...

What causes the difference in CSS border-bottom-width rendering between IE and Chrome?

My ASP:MENU is styled with some CSS properties, such as setting the 'height' and 'line-height' of each element to 15px. This design appears fine in Internet Explorer but seems too cramped in Chrome. Additionally, a 'border-bottom-s ...

Incorporate a button within the input field

Looking to customize my search form with the search button on the right side, similar to this: https://i.sstatic.net/lwmig.png Below is the code for my current form setup: <form action="search.php" method="post" class="index-search-form"> ...

Is there a way to append the current path to a link that leads to another URL?

I am currently in the process of separating a website, with the English version being on the subdomain en. and the French version residing on the www. Before making this change, I have a drop-down menu that allows users to select their preferred language ...