How to position text to the left, center, or right with CSS, HTML, using span, text align, and inline-block styling

I have 4 boxes. In one of the boxes, I have placed 3 smaller boxes and I want to align them to the left, center, and right while still keeping them on the same line. I've attempted using inline-block, but it doesn't seem to be working properly. Can you please review my code below and see where I might have gone wrong?

 

#boxContainer {
    border: 1px solid black;
    width: 500px;
    height: 500px;
}
#leftBox {
    text-align:left;
    border: 1px solid black;
    display: inline-block;
    width: 100%;
}
#centerBox {
    text-align:center;
    border: 1px solid black;
    display: inline-block;
    width: 100%;
}
#rightBox {
    text-align:right;
    border: 1px solid black;
    display: inline-block;
    width: 100%;
}
span {
    border: 1px solid red;
}
<div id='boxContainer'>
  <div id='leftBox'><span>Left</span></div>
  <div id='centerBox'><span>Center</span></div>
  <div id='rightBox'><span>Right</span></div>
</div>

Answer №1

If you want all the elements to be on the same line, you can achieve that by changing the order of the divs and floating the left and right elements accordingly. No need for the spans as suggested in the previous question.

#outer {
  border: 1px solid black;
  width: 500px;
  height: 500px;
  text-align: center;
}
#innerLeft {
  float: left;
  border: 1px solid black;
}
#innerMiddle {
  border: 1px solid black;
  display: inline-block;
}
#innerRight {
  float: right;
  border: 1px solid black;
}
<div id='outer'>
  <div id='innerLeft'>Left</div>
  <div id='innerRight'>Right</div>

  <div id='innerMiddle'>Middle</div>
</div>

Answer №3

The reason for this behavior is that you have set the width of all the divs to 100% in your CSS, which enables them to occupy the entire width available within their respective parent containers. Therefore, even though they are displayed as inline blocks, they still expand to fill the entire width of their parent elements.

Answer №4

If you're wondering why the elements are displaying one per line, it's because their widths are set to 100%. To achieve the desired layout, change the width to 33.33%.

For any border-related issues, consider using box-sizing: border-box. More information can be found here: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

#outer {
    border: 1px solid black;
    width: 500px;
    height: 500px;
    box-sizing: border-box;//add this
}
#innerLeft {
    text-align:left;
}

#innerRight {
    text-align: right;
}

#innerMiddle {
    text-align: center;
}

#innerRight, #innerMiddle, #innerLeft {
    border: 1px solid black;
    display: inline-block;
    width: 33.33%;//change this
    float: left;//add this (reliable way of getting rid of inline-block gap)
    box-sizing: border-box;//add this
}
span {
    border: 1px solid red;
}

Check out the Fiddle for a live example: https://jsfiddle.net/0wuzgcba/

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

Tackling the sticky header problem with in-browser anchoring and CSS dynamic height restrictions

I am experimenting with a unique combination of a Pure CSS in-page anchoring solution using scroll-behavior: smooth and scroll-margin-top settings, along with a sticky header for in-page navigation. By utilizing IntersectionObserver, I can identify when t ...

Tips on customizing the appearance of mat-card-title within a mat-card

Is there a way to truncate the title of a mat card when it overflows? I tried using the following CSS: overflow:hidden text-overflow:ellipsis white-space: nowrap However, the style is being overridden by the default mat-card style. I attempted to use mat ...

Delivering HTML Email

I want to send an HTML email with images using Google's SMTP servers. Here is the HTML code with inline CSS that I have generated: <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type& ...

The webpage is drifting sideways to the right without any content being displayed

Recently, I launched a new website and encountered an issue with a static page I designed. While it may not be a critical problem, it is certainly bothering me! If you want to check out the website in question, visit: In essence, I have used CSS to hide ...

Using single page anchor tags in Next.js to toggle content visibility

The Issue Currently working on a project using Next.js and facing a challenge: needing to hide or replace content based on the selected category without reloading the page or navigating to another route. Furthermore, ensuring that when the page is reloade ...

Customize the font size in Bootstrap 5 using REM units

When it comes to setting font sizes for easier calculations in REM values, I currently use this approach: html { font-size: 62.5%; } body { font-size: 1.6rem; } /* 16px */ However, Bootstrap 5 uses the root value (which is typically set at 62.5%) to calc ...

What could be causing the top navigation bar's background color to not display properly?

Currently working on a website and trying to implement a top navigation bar. However, encountering an issue where the background-color is not displaying properly and appearing transparent. CSS: body { margin: 0; font-family: "Century Gothic", Cent ...

Design a background image that is optimized for both high-resolution retina displays and standard non-ret

Scenario I am working on a web page where I want the background image to be fixed, covering the entire screen or window (excluding tablets and smartphones). The background image has been created using ImageShack. Everything is running smoothly so far. T ...

Floating Action Button is not properly attached to its parent container

When developing my React Js app, I decided to utilize the impressive libraries of Material UI v4. One particular component I customized is a Floating Action Button (FAB). The FAB component, illustrated as the red box in the image below, needs to remain p ...

Tips for automatically scrolling to the bottom of a div when new data is added

I'm working on a project with a table nested inside a div, and it has some unique styling: #data { overflow-x:hidden; overflow-y:visible; height:500px; } Currently, as the table fills up with data, a vertical scroll bar appears. But I wa ...

Utilizing the after pseudo element for an active selector in CSS

I'm struggling to make an active icon selector work with the after pseudo element. Here is the code I have attempted: <html> <head> <style type="text/css"> .btn{ width:25%; } .name{ background: #fff; color: #000; text-alig ...

Steps to Hide Pop-Up Cookie Module in HTML with Bootstrap 5

Could someone please assist me in figuring out how to close the popup module in bootstrap when the user accepts or cancels? I can't seem to remember how to do it. <div class="cookie-consent"> <span>This site uses cookies to enhan ...

Ensuring the width of a div matches the adjacent containers

I need assistance in creating a form that adjusts its width based on the form fields. The form contains both explanatory text and input fields, each enclosed in a div with inline-block set. The outer div (also using inline-block) should not expand beyond ...

Enhancing React Performance: Storing Component Identity in Redux State

Can I safely pass this to a Redux action creator from a component defined using React.createClass? In my code, I have created the following reducer: const unsavedChangesProtectionReducer = handleActions({ [ENABLE_UNSAVED_CHANGES_PROTECTION]: (unsaved ...

Style the modal body to display as a block on both desktop and mobile devices using CSS

My goal is to ensure that the modal-body is vertically aligned for optimal visibility on both mobile phones and PCs. I am utilizing bootstrap for this purpose. <div class="modal-body" style="display: inline-block;"> <div> ...

Is there a way for me to retrieve the price using the xpath?

I have this code snippet currently: CurrentPrice=driver.find_element_by_xpath('/html/body/div[2]/div[4]/div[2]/header/div/div[3]/div[1]/div/div/div/div[1]/div[1]').get_attribute("title") This is the price data I am attempting to extrac ...

Combining an if-statement with an HTML button within a single function in JavaScript

I'm currently developing an HTML table that includes fields that must be filled, but I want to allow one of the fields to be optional. The structure of my code is as follows: name.html ... <table> <tr> <td>Number 1:</td& ...

Misaligned Div Content

Check out my jsfiddle here The text inside the <div> is shifting upwards Could someone explain why this is occurring? And suggest a solution? I would truly appreciate any assistance. Thank you! ...

Trying out basic form validation - Adjusting border color for an empty input

Currently, I am experimenting with a basic login form using PHP and testing it out on XAMPP in Chrome. Below is the code for the login form: <form action="login.php" method="POST"> <label>User: </label> <i ...

Images with full-width will scroll horizontally

Is there a way to fix the horizontal scroll issue? <!DOCTYPE html> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https:// ...