Achieve image alignment to the left, center, and right without relying on float properties

Can three images be positioned in a row to the left, middle, and right using only text-align instead of float?

Answer №1

If you're looking to achieve a specific alignment using CSS, one approach is to utilize the text-align: justify property. Here's how you can implement it:

Assume you have the following HTML structure:

<div class="wrapper">
    <img src="http://placekitten.com/120/100">
    <img src="http://placekitten.com/120/100">
    <img src="http://placekitten.com/120/100">
    <span class="filler"></span>
</div>

Apply the following CSS rules to achieve the desired layout:

.wrapper {
    border: 1px solid blue;
    text-align: justify;
    line-height: 0;
    height: 100px; /* Adjust as needed */
}
.wrapper img {
    vertical-align: top;
}
.filler {
    display: inline-block;
    width: 100%;
    font-size: 0px;
    vertical-align: top;
}

The .filler element with display: inline-block and a width of 100% ensures that other inline elements (like images) are evenly distributed within the parent container.

In some cases, there may be unwanted white space beneath the images. To address this, setting the font size to 0px and vertical-align to top can resolve the issue.

For a live demonstration, check out this JSFiddle example.

Note:
While you can use a pseudo-element to add the filler, the provided solution without it works well if you don't mind additional markup.

Answer №2

To achieve the desired layout, you can create three divs with equal width and place each image inside its own div. Then, you can align them as needed within those divs.

   <div id="container">
   <div id="left"><img src="image1.png" /></div>
   <div id="center"><img src="image2.png" /></div>
   <div id="right"><img src="image3.png" /></div>

   #left,#right,#center {width:300px;}
   #left{text-align:left;float:left;}
   #center {text-align:center;float:left;}
   #right {text-align:right;float:left;}

Remember to float the divs and use text-align for the images to ensure proper alignment.

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

Eliminate any unnecessary gaps that may exist between the cards within the deck

Utilizing Jinja2 with Flask to render a list on an HTML page, I aim to produce card decks containing data from the list using a Jinja2 loop. This is my current HTML code: <div class="container-fluid"> <div class="row"> <div clas ...

Is there a way to continually repeat a hover effect?

Currently, I am focusing on creating a timeline and have managed to get the basic function working in jQuery. You can view my progress on jsFiddle here: http://jsfiddle.net/Jason1975/6nwkd2c8/38/ The issue I am facing is that I cannot get the hover effect ...

I'm seeking answers on selenium and the proper use of CSS selectors, as well as troubleshooting the error message 'selenium.common.exceptions.ElementClickInterceptedException'

After some research, I think I have a grasp on CSS selectors and their appearance. However, when using selenium, how can I locate a CSS selector on a website and then click on it? The correct syntax eludes me. I encountered this error as well: selenium.co ...

Tips on displaying hyperlinks within a text area in an Angular application

In my Angular application, I am facing an issue with displaying hyperlinks within a text area box for dynamic content. The hyperlinks are not recognized and therefore cannot be clicked. Can someone please advise on how to properly display hyperlinks with ...

Distinct styling for the start and subsequent lines of an anchor element using ::before pseudo-element

I'm currently working on a project that requires adding a decoration in front of some anchor links (A). Right now, I am using ::before to achieve this. However, there is an issue where some of the links will line-break and the second line and subseque ...

PHP code for sending a file alongside displaying text on the browser using the echo command using X-SendFile

I am currently utilizing the X-SendFile Apache Module to facilitate the download of large files from our server. The downloads are functioning as expected; however, I am faced with an issue regarding outputting text to the browser when a download is initia ...

Issue with Jquery autocomplete not populating data in additional fields

Issue: I have implemented a dropdown list for selecting suppliers, and based on the selected supplier, users can search for items using JQuery Autocomplete. However, when an item is selected from the search results, the text boxes for 'description&apo ...

How to update data in FullCalendar using Bootstrap modal

While using a FullCalendar within a modal, I encountered an issue where the information for a user would not refresh when opening another modal. Despite trying various solutions from Stackoverflow, none seemed to work. The only time it did function properl ...

What is the best way to hide the input field when there are multiple parent classes present?

I am currently implementing dynamic fields with jQuery and everything is functioning correctly. The problem arises when I attempt to remove these fields. After searching on Google and browsing past questions on StackOverflow, I noticed that everyone seems ...

JavaScript validation malfunctioning

Whenever I click on the submit button, nothing happens. An error is displayed as follows: Form.js:102 Uncaught TypeError: Cannot set property 'onsubmit' of null. I am having trouble understanding why this error occurs... // JavaScript Document ...

Issues with organizing a Portfolio Gallery

Currently, I am in the process of creating a portfolio page and I am utilizing placeholder images until I have actual projects to showcase. I am encountering difficulties trying to structure one large image with two to four smaller images underneath it, al ...

Bourbon encountering difficulty with font-face mixin in Rails version 3.2.2

After watching Ryan Bates' screencast on Bourbon, my application.css.scss file looks like this: @import "bourbon"; @import "main"; @import "events"; ..etc. In my main.css.scss stylesheet, I am able to use the @include transition() mixin without any ...

Adjust the height attribute of a DIV element within a webpage that contains a "footer"

I'm hoping to achieve a scrollable middle section without the bottom scrollbar, and I prefer a CSS solution over using JavaScript for this. On my website, I have a 150px high div filled with icons and images that needs to be fixed while allowing the r ...

Detect with JavaScript to determine if the angular-sanitize.min.js file has been successfully fetched from the content delivery

Is there a way to determine if angular.min.js has been loaded from a CDN or locally? if(!window.angular){ //download it from another source } Assuming that the first file, angular.modified.min.js, is always loaded locally, how can we verify if the second ...

"Embracing the power of dynamic CSS customization within Umbraco

I recently inherited an Umbraco system without much prior knowledge of the platform, and I am currently investigating the site's performance. The branding and styling of the site is currently managed through a "Brand" document type, allowing the site ...

How can I use JavaScript to find a keyword on a webpage that is not located within an <a> tag or its href attribute?

I'm on a mission to locate a specific keyword within a webpage. Sounds simple, right? Well, here's the tricky part - I need to disregard any instances of this keyword that are nested within an <a> tag. For example: <p>Here is some s ...

Retrieve the HTML code from a webpage on a different domain

Looking to extract HTML from another domain. Attempted solution: $.get("http://website1.com", function(data) { alert("Data Loaded: " + data); }); (not working) For example, as the owner of two websites: website1.com website2.com Now, the goal is to ret ...

CSS Drop-Down Menu Fails to Function Properly in Firefox and Internet Explorer

Can you help me with this issue I'm having with my website design? In Google Chrome, Opera, and Safari, everything looks great. But in Firefox and the latest version of IE, it's a different story. Here is the HTML code: <div id="navbar"> ...

"Create dynamic tables with AngularJS using ng-repeat for column-specific rendering

I have a model called Item with the following structure: {data: String, schedule: DateTime, category: String} I want to create a report that displays the data in a table format like this: <table> <tr> <th>Time Range</th&g ...

How can I align the tags properly when inserting a tab box within a section tag?

How can I successfully insert a tab box within my section tag? I found this tab box tutorial on the website here The download link for the source code is available here: source code Below is the HTML code snippet: <!DOCTYPE html> <html> & ...