Aligning CSS elements in a horizontal manner

Could someone please help me with centering my buttons within the red area regardless of their width?

If you would like to view the Demo, click here: http://jsfiddle.net/v3X2k/

Below is the code I am currently using:

<div style="width:660px;height:40px;display;block;background:red">

    <div id="wrapper3" style="margin:0 auto;width:160px">

        <button>Test</button>
        <button>Test 2</button>

    </div>

</div>

<hr />

<div style="width:660px;height:40px;display;block;background:red">

    <div id="wrapper3" style="margin:0 auto;width:160px">

        <button>Test Test Test Test Test Test</button>
        <button>Test 2</button>

    </div>

</div>

In the second example, the second button drops below instead of staying inline with the first one. My goal is to keep them in line just like in the first example.

I am willing to remove the width:160px statement if needed, but I really need these buttons centered.

Any suggestions on how to achieve this?

Thank you in advance :)

Answer №1

No need for a wrapper div with fixed width and margin auto, simply apply text-align: center; on the container div

Check out the Demo here

Answer №2

text-align: center;

Since buttons are inline elements, the text-align property will successfully center them.

Example

Answer №3

There are differing opinions on the use of <center></center> in web design - some view it as bad practice while others believe it can be used with caution.

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

Adjust the positioning of several divs using CSS floats

Can the pink div (link3/link4) start at the same top position as the div (link1/link2)? Or do I still need a wrapper div for the left button bar, TreeDiv, and another one for the right button bar + DataGridDiv? CSS html { background-color:Yellow; ...

extract the value from the chosen option in a web element

I'm struggling to retrieve the selected option value in my web component so that users can easily access it. It seems like the issue lies within the shadow root. __loadOptions() { const SELECT = this.shadowRoot.querySelector('select'); ...

Position the button to the right using the Bootstrap float-right class

Currently, I am utilizing reactstrap within my React component. My primary intention is to position a button to the right side of the container but unfortunately, the following code doesn't achieve the desired result: <td class="col-md-4 clearfix" ...

What is the best way to divide a string into an array containing both linked and non-linked elements?

I'm struggling to find the right solution to my problem. I need to create a view that is enclosed in a clickable div. The content will consist of plain text mixed with clickable URLs - the issue arises when clicking on a link also triggers the method ...

Tips for preventing an element from scrolling horizontally along with the page body

This is the structure of my HTML: <div class='header-wrapper'> <div class='title'>Title</div> </div> <div class='some-wide-content'> </div> Let's imagine a scenario where the br ...

Click to solve the equation

I want to build a basic HTML page with three input variables (A,B,C). After the user inputs these values, they will click "calculate", and JavaScript will execute the following equations: ((A + B)/2) * 3 = X and (C * 2) = Y The results will be displaye ...

Highcharts - Troubleshooting the chart reflow feature

Take a look at the fiddle I created. I encountered an issue with the chart width when toggling the sidebar. After seeking help on Stack Overflow from this post, I was able to solve it. Now, I'm facing another bug where adding transitions while togg ...

External elements - My physical being is in a state of chaos

Hello there, I hope everything is well with you. I have been working on creating a form using MaterializeCss to calculate an invoice for a craftsman. The user is required to input a number or select an item. Everything seems to be going smoothly so far. ...

The menu seems to be off-center

I'm struggling to center my menu/navigation bar horizontally. I can't seem to figure out what mistake I've made or what I've forgotten. Additionally, there is right padding after my last list item. How can I get rid of it? HTML <di ...

The email generated by Laravel does not conform to HTML formatting standards

After creating an email using markdown in Laravel, I noticed that the email content received was not displaying correctly. The HTML tags were not being rendered properly. You can view my email here. Any help on this issue would be greatly appreciated. I h ...

Attempting to trigger CSS transitions using JavaScript will not be successful

I'm facing an issue where CSS transitions do not work as expected when triggered by JavaScript. let isSearchBarOpen = false; function toggleSearchBar() { if (isSearchBarOpen) { searchBar.style.display = "none"; } else { searchBar.sty ...

How can I retrieve values from HTML5 data attributes using Selenium in Python?

How can I extract the value "165796011" from data-gbfilter-checkbox? I attempted to do so using the following code: element= driver.find_element_by_css_selector('#widgetFilters > div:nth-child(1) > div.a-row.a-expander-container.a-expander-inl ...

Please complete this mandatory field. Issue detected with ImageField in Django

I encountered an error stating 'This field is required' when trying to upload an image. I am puzzled as to why this error is occurring, as my model is quite simple. Any assistance in identifying the issue would be greatly appreciated. cl ...

The popover seems to be failing to appear

I am struggling to get a popover to appear when clicking on an appended href element dynamically. Here are the code snippets I have tried: <div id="myPopoverContent"> ...stuff... </div> <div id="div3" style= "width:200px;height:200px;" ...

Is there a potential white-space issue with jQuery CSS in Internet Explorer versions 7 and 8

Recently, we encountered an unusual issue with jQuery and CSS while working on compatibility with IE7 and IE8. Our project involves animations and height measurements, and during the animations, we wanted to prevent text from wrapping. To achieve this, we ...

Tips for retrieving user input from an HTML form and using it to make an Axios POST request

I am facing an issue with sending a post request to my API using axios. A user enters an email and password in a html form, but when the request is sent, the body of the request appears to be empty based on the chrome dev tools. I am struggling to extract ...

Guide on showcasing an alert notification when the data is already existing within an array through javascript

Need help with displaying an alert message in JavaScript for duplicate values in an array? var names = []; var nameInput = document.getElementById("txt1"); var messageBox = document.getElementById("display"); function insert() { names. ...

Vanishing Tooltip following an implementation of the backdrop-filter

I'm having an issue with the blur effect on my background image. Although it works well, my tooltips also end up being blurred behind the divs: https://i.stack.imgur.com/BMdh4.png Is there a way to ensure that my tooltips stay on top of the subseque ...

What is the best way to insert a page break only when necessary?

My code includes multiple dynamic tables within a div element: <div> <table> <tr></tr> <tr></tr> ... </table> <table> <tr></tr> <tr>< ...

When the form is submitted, any blank inputs and their corresponding hidden fields will be disabled

I created a form that has multiple input fields, and users have the option to enter values or leave them blank. Each input field is accompanied by a hidden input field which contains a specific id unique to the corresponding visible input field. To disable ...