Ways to modify the alignment of specific inline-block elements inside a DIV

Is there a way to align two inline-block elements to the left and one to the right without changing their display property?

I am looking for a CSS solution, but would also consider using jQuery if necessary.

In simple terms, I want the first two buttons to be aligned on the left side and the third button on the right side.

Check out this example on jsBin

<table>
    <thead>
      <tr>
        <th>
          <div style="background-color: lightblue; width: 600px;">
            <span style="display: inline-block; height:20px; background-color: red; margin: 5px 3px;float: left;">
              Button
            </span>

            <span style="display: inline-block; height:20px; background-color: red; margin: 5px 3px; float:right;">
              Button right
            </span>
        </div>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr><td>where did the background of the header cell go? the background is still there if the elements do not float.</td></tr>
    </tbody>
  </table>

Answer №1

To align your span element to the right, simply apply the property float:right.

See it in action here

In the other scenario you mentioned with the table, the issue arose because you applied the style to the wrong tag. Make sure to set the background-color and width properties to the correct th tag instead of the div tag for the desired outcome.

View another demonstration here

Answer №2

The use of <span> elements as inline-blocks is redundant, as they are already naturally inline-block elements. Instead of enclosing everything in a <div>, consider styling the <th> element directly and using the :last-child selector for customization. Removing the float:left property may require additional CSS for vertical alignment of buttons.

CSS
    th { background-color:lightblue; width:600px; }
    th span { background-color:red; float:left; height:20px; margin:5px 3px; }
    th span:last-child { float:right }

HTML
    <th>
        <span>Button</span>
        <span>Button</span>
        <span>Button right</span>
    </th> 

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

Display the AJAX reply within a designated div based on the selected value

I currently have two dropdown menus where the options in the second menu depend on what is selected in the first one. The behavior I want is for the second menu to update as soon as the user makes a selection in the first one, without needing to submit the ...

Footer that sticks in a vuejs application

I am facing a challenge in implementing a sticky footer in my vuejs application. Vuejs and similar frameworks require the presence of a root element in the template. This requirement is causing complications when trying to use Flex to create a sticky foo ...

How can I display a jQuery QTip on a React JS cell?

Our team has utilized both React JS and Angular JS in the development of a table component. I am currently facing a requirement to display qtip when hovering over a specific button inside any cell of the table. I have implemented an angular directive with ...

Using jQuery to generate columns depending on the quantity of occurrences of an element inside a specific Div

I've encountered a challenge while using a plugin that restricts access to the HTML, but allows for adding CSS and Javascript. I am aiming to achieve the following: Determine if a specific class within a div is active If it's 'active' ...

grasping the intricacies of certain JQuery syntax

I could really use some assistance understanding this jQuery code. I've tried searching online without much luck, and I'm not even sure what exactly to search for! (It seems like the lines in question may relate to a jQuery extension). JSP < ...

The outcomes of my JavaScript code are not aligning with my expectations

I've been experimenting with printing objects from an API using JSON and AJAX, and I noticed that the console.log works perfectly to display the output. However, I'm having a bit of trouble with my generateCreatureDiv function as it doesn't ...

Utilize jQuery to apply inline styles to dynamically created div elements

I am attempting to apply inline styles to a div using jQuery. The current state of the div, as seen in Firebug, is shown below: https://i.sstatic.net/rqhIc.jpg My goal is to retain the existing CSS while adding margin-left:0 and margin-right:0 to the cur ...

Firefox is having trouble fetching #header from style.css, but other web browsers seem to be working fine

Visit my website at http://dayforfailure.com My website appears fine on Chrome and Safari, but on Firefox the header and other elements are not displaying correctly. It seems like there may be a cross-browser compatibility issue. I am new to dealing with ...

Enclosing two smaller boxes within a larger box

I'm attempting to replicate the layout shown in the desired.png file (specifically with an outer box surrounding two inner boxes). Currently, my code doesn't display the expected outer box around the two inner boxes. How can I modify it to achie ...

The event binding for input with the specific class `.SomeClass` in the document is functional in Chrome, but encounters issues in Internet Explorer

I am facing an issue where I have a span with the contenteditable attribute and a function to detect input within it. //TextArea <span class="SomeClass" contenteditable></span> //Function $(document).on("input", ".SomeClass", function () { ...

Utilizing AJAX requests in PHP's MVC framework

I am currently working on a game site using a combination of php and javascript. All my classes and data are stored in php and then encoded into json format. The view calls a javascript file which, through an ajax request, retrieves the php data encoded in ...

Japanese Character File Naming Convention

When dealing with certain Japanese characters, the content disposition header appears as follows: Content-Disposition: attachment; filename=CSV_____1-___.csv; filename*=UTF-8''CSV%E3%82%A8%E3%83%93%E3%83%87%E3%83%B3%E3%82%B91-%E3%82%B3%E3%83%94%E ...

What are some ways to customize the appearance of VueJS components?

I am new to Vue, so I apologize if this question seems silly or if I have overlooked something. I believed that you could target a custom element like this: my-element { styles go here} However, when I created an element, it appears that I can only targe ...

Ways to modify selectedValue to enable a case-insensitive setting

I am looking to update the value of a knockout observable that serves as the value for a select with case-insensitivity. For example, passing justin should set the selectedValue to Justin. Issue: View Fiddle Here Solution: Working Example HTML Code < ...

What causes text inside a fieldset to sometimes appear bigger when viewed on mobile devices?

Have you ever noticed a strange quirk in Chrome that you couldn't quite explain? I recently stumbled upon a peculiar "feature" while using fieldset with responsive design. It seemed that the text within the fieldset appeared disproportionately larger ...

Tips on dividing a string into an array in jQuery using randomization

Here's the scenario: let text = 'apple, banana, cherry'; let wordsArray = text.split(', '); Currently, the above code splits the string into an array of words. But I'm wondering if there's a method to shuffle the words ...

Odd behavior of dropdown list on Internet Explorer 8

Thank you for taking the time to review this. I have a dynamic dropdown list that changes its content based on a specific value (for example, "51") inputted into the JavaScript code on each page. Different pages display different lists depending on this va ...

What are the steps for adding a PDF attachment to an email using nodemailer?

I want to send email with attachments dynamically using Nodemailer and jQuery in the front end. <script> $(document).ready(function () { var from, to, subject, text; $("#send_email").click(function () { to = $("#to").val(); s ...

Jquery deactivates the CSS hover effect

Having a dilemma with linked photos on my website. I have set their opacity to 0.45 by default, but want them to change to full opacity (1) when hovered over or clicked. I've implemented a JQuery function that resets the rest of the photos back to 0.4 ...

What is the best way to retrieve the identity or specifics of the item that was right-clicked within the context menu

My AngularJS populated table includes a link button. When I right-click on this button, I've created a specific context menu using jQuery that pops up. The issue arises when I try to retrieve the ID of the item I clicked on in the context menu (such a ...