What's the best way to align several buttons in the center of the screen horizontally?

I'm struggling to properly align divs with images inside another div. My goal is to insert buttons into my HTML and use jQuery to center them automatically, but I can't seem to get it right.

You can view the fiddle I've created here: http://jsfiddle.net/uucp/m3UmA/

Originally, I planned to calculate the container's size (named "tabs") and divide it by the number of buttons (the count of "tab" classes on the page), then set each tab div's width to that value.

I must be overlooking something basic: default padding or margins perhaps? Any guidance would be greatly appreciated.

Here is the HTML:

<div id="tabs" class="tabs">
    <div id="tab_chat" class="tab">
        <img class="tab_button" id="tab_chat_img" src="http://hanford.org/users/uucp/jsfiddle/daychat.png">
    </div>
    <div id="tab_users" class="tab">
        <img src="http://hanford.org/users/uucp/jsfiddle/dayusers.png">
    </div>
    <div id="tab_images" class="tab">
        <img src="http://hanford.org/users/uucp/jsfiddle/dayimages.png">
    </div>
    <div id="tab_night" class="tab">
        <img src="http://hanford.org/users/uucp/jsfiddle/daynight.png">
    </div>
    <div id="tab_refresh" class="tab">
        <img src="http://hanford.org/users/uucp/jsfiddle/dayrefresh.png">
    </div>
    <div id="tab_settings" class="tab">
        <img src="http://hanford.org/users/uucp/jsfiddle/daysettings.png">
    </div>
</div>
<p>
    <div id="console"></div>
</p>

The CSS rules are as follows:

.tabs {
    height: 34px;
    padding-top: 1px;
    padding-bottom: 0px;
    background-color: #000;
}
.tab {
    text-align: center;
    border: 0;
    display: inline-block;
    color: #fff;
}
div.tab_button {
    border: 0;
    width: 32px;
    height: 32px;
}

The JavaScript code used:

var tabCount = $(".tab").size();
var offset = 24;
var tabWidth = Math.floor(($("#tabs").width() / tabCount));
$('.tab').each(

function (i, tab) {
    $(tab).css("width", tabWidth + "px");
    $("#console").append("tabCount=" + tabCount + " tabWidth=" + tabWidth + " tabs.width=" + $("#tabs").width() + "<br/>");
});

Thank you for any help you can provide.

Answer №1

You can achieve this by using CSS and setting the text-align: center property.

Here is an example:

.tabs {
height: 34px;
padding-top: 1px;
padding-bottom: 0px;
background-color: #000;
text-align: center;
}

Take a look at this JSFiddle

Answer №2

For a responsive design, you can utilize the following solution: DEMO

To implement this, simply add the class justified-adjustment to the container div.

The necessary CSS rules are as follows:

.justified-adjustment { text-align: justify; }

.justified-adjustment > * {
   display: inline-block;
   vertical-align: middle;
}

.justified-adjustment::after {
   content: "";
   width: 100%;
   display: inline-block;
}

This is how the markup should look like:

<div class="tabs-container justified-adjustment">
    <img src="#" alt="Image 1" />
    <img src="#" alt="Image 2" />
    <img src="#" alt="Image 3" />
</div>

When you resize the window, you'll notice that the layout adjusts accordingly, making it a responsive solution.

Answer №3

If you're looking to achieve this layout, consider using a fixed table structure approach. You can create a table with one row and multiple columns while applying the CSS property table-layout: fixed;. This will automatically calculate equal column widths for you. Additionally, make sure the table has a width of 100% and center-align the content by using text-align: center.

It's as simple as that. Feel free to check out the demo here: Demo Link

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

Using anchor tags to send HTML code through email

I currently have an anchor tag on my website that looks like this: <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e0d11131b11101b3e0d11131b09161b0c1b501d1113">[email protected]</a>?subject=Wel ...

Having issues with the onchange attribute in the rails text_field_tag not functioning as

Hey there! I am working on an ajax submitted form that includes only a text_field_tag. My goal is to have the form submit as soon as the user begins typing their search string. Thankfully, bandwidth and lag are not concerns since the application will be ho ...

Is Angular capable of displaying a date within a specific timeframe using ng-show?

So I have a button that, when clicked, displays a list of data. I am adding a unique font awesome icon in there if the JSON key value exists. However, here lies the issue. The particular key happens to be a date. I need my ng-show function to check whether ...

How can data tables be generated using AJAX, JSON, HTML, and JS?

HTML: <table> <tr> <th>Student Name</th> <th>Student Grades</th> </tr> <tr> <td> <select name="dropdown" id= ...

particular shade for button border and background

I'm looking to create a button using Material UI that has a specific design. Right now, I've only been able to achieve this: https://i.stack.imgur.com/xvv7s.png Here is the code I am currently using. If anyone can help me identify what I'm ...

Employing the html.validationmessagefor to display a client-side error notification

My text fields have server-side validation messages. The field 'title' is a required field on the server side with a '[Required]' data attribute in the class, but it only seems to be checked on a postback or form submit. I am saving the ...

What steps can I take to stop my html table from expanding beyond its intended size

There are instances where the length of a data piece in one of my table cells causes it to expand and distort the overall layout of the table. Is there a way to avoid this from happening? ...

Having trouble initiating an AJAX call in Django and returning values to my form

Seeking assistance to run an AJAX call for a quick calculation in my django view and display the result within a span tag on my HTML page. New to Javascript, struggling with triggering my AJAX call. Below is my HTML and JS code: <input type="text" nam ...

Using a custom font in a Django project without relying on the Google Fonts API

I've hit a roadblock in my programming journey and I'm seeking help. As a newcomer to both programming and Django, I've been following a helpful tutorial up until part 4. Everything was going well until I stumbled upon a new font online cal ...

Saving multiple selected boxes based on a process using jQuery, PHP, and Ajax

I am working with a form that includes multiple select boxes. <form id="form"> <?php for($i = 1; $i<=9; $i++) { ?> <input type="hidden" name="modelID" value="1"/> <div> Process <?php ec ...

Leveraging JQuery to retrieve the string value from an onclick() event

Curious if there's a more efficient approach to tackle this issue, decided to seek input from the SO community... There's a third-party web page over which I have no control in terms of how it's displayed, but they do allow me to integrate ...

When jQuery fails to detach() due to the presence of an input tag

I have a situation where I am trying to rearrange elements within a table. Everything works fine, until I introduce a tag, which triggers this error message:</p> <pre><code>Error: this.visualElement is undefined Source File: http://192. ...

Displaying properties of objects in javascript

Just starting with JavaScript and struggling to solve this problem. I attempted using map and filter but couldn't quite implement the if condition correctly. How can I find the records in the given array that have a gender of 0 and color is red? let s ...

Utilize jQuery and JSP (or PHP) to showcase detailed information about a specific item when a user clicks on it within an HTML page

For my upcoming college project, I am tasked with developing a movie library. The main page of the library will feature various movie posters. Upon clicking on a poster, users will be directed to a dedicated page displaying detailed information about the ...

Steps for positioning a z-indexed division

In my main wrapper, which has a fixed width of 960px, there is a login button located at the top right corner. When this button is pressed, a div layer opens directly below and in front of the main wrapper using z-index: 1. The issue I am facing is that I ...

Position the Figcaption over the Image

Running a website, I am facing an issue with adding copyright information to the top-left corner of an image. The img element is set to float:left; and the figcaption appears after it. However, there are some complications: 1) Using the position:absolute ...

Ways to establish a default option in a dropdown menu using JavaScript

I am currently attempting to link a JSON object to a dropdown in the following manner: JSON data "securityQuestions": [ "First Pet's Name", "City I was born in", "Mother's Maiden Name", "Favorite teacher's Name" ] This i ...

Is there a way to specifically select the input field that the user has typed into using a selector?

Is it possible to use javascript/jquery to specifically target the input field that a user has just typed in? ...

Splitting a CSS hierarchical list into horizontally aligned levels of hierarchy

I am trying to horizontally lay out an ordered list with multiple top level nodes while preserving the vertical layout of child nodes. For example, consider a basic list structure like this: <ol> <li>Top 1 <li>Sub 1</li ...

What is the best way to input two distinct variables into the Firebase database?

I have encountered an issue while trying to upload two variables into my Firebase database. Only the first variable, newRoute, is being successfully uploaded while new User isn't getting uploaded. The setup of the Fire configuration for the entire web ...