What is the best way to center an image within a DIV and have text aligned to the right without experiencing any overflow issues?

Looking for advice on how to create a layout with a centered image and text description to the right in a div container. Here are the specific requirements:

  1. The image should have a bottom margin of 35px.
  2. The image needs to resize dynamically based on screen size without cropping or using scrollbars, always fully visible on the screen.
  3. The image should be centered within the container, with the text positioned to the right.
  4. The text should be horizontally left-aligned, vertically center-aligned, and spaced 30px away from the image.

I've attempted using both tables and div elements but haven't found an elegant solution yet. Happy to share my code attempts upon request!

Answer №1

This particular challenge posed quite a dilemma. The specific tags utilized are not of utmost importance; rather, the focus lies on the quantity of elements and how they intertwine.

To achieve vertical alignment of text, Flexbox was employed, although its compatibility is somewhat restricted (Chrome, IE10, Opera, Safari, most mobile browsers). It's worth noting that Firefox faced an obstacle due to a bug inhibiting Flexbox functionality when dealing with absolutely positioned elements.

Code Snippet:

<div class="gallery">
  <article>
    <h1>My image title</h1>
    <img src="http://placekitten.com/640/480" alt="A really cute kitten" />
  </article>
</div>

CSS Styles:

.gallery {
  padding: 0 230px;
  text-align: center;
}

article {
  display: inline-block;
  max-width: 100%;
  position: relative;
}

img {
  max-width: 100%;
  vertical-align: text-bottom;
}

h1 {
  position: absolute;
  text-align: left;
  right: -230px;
  top: 0;
  bottom: 0;
  width: 200px;
  margin: 0;
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}

Alternatively, one could introduce an additional element and utilize the table/table-cell display properties for achieving vertical centering.

Markup Example:

<div class="gallery">
  <article>
    <h1><span>My image title</span></h1>
    <img src="http://placekitten.com/640/480" alt="A really cute kitten" />
  </article>
</div>

CSS Styling:

.gallery {
  padding: 0 230px;
  text-align: center;
}

article {
  display: inline-block;
  max-width: 100%;
  position: relative;
}

img {
  max-width: 100%;
  vertical-align: text-bottom;
}

h1 {
  position: absolute;
  text-align: left;
  right: -230px;
  top: 0;
  bottom: 0;
  width: 200px;
  margin: 0;
  display: table;
}

h1 span {
  display: table-cell;
  vertical-align: middle;
}

* Please note that slight discrepancies in padding/positioning may be observed in the demos, while the code provided herein remains accurate.

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

Is there a way to change the color of a single word without affecting the rest of the line?

https://i.sstatic.net/vFWgU.png I am encountering an issue where I only want the word "declined" to be color inverted, rather than the entire line. Can anyone provide assistance with this? .invert { background-color: white; filter: invert(100%); } ...

Obtain the input element's text content in C#

I am trying to extract the text beside an input field using C#. Here is my HTML structure: <p id="mpNames"> <input id="selectMp1" type="checkbox"> "Text next to the input element" This is the code I have implemented: var inputWeList = d ...

Mastering the art of integrating HTML5's localstorage feature with Jquery's select2 plugin

Is it possible to integrate HTML5's localstorage method with Jquery's select2 plugin? Currently, all entered data disappears when the browser/tab is closed, leading to potential confusion if you forget what was entered. Here is an example of my ...

Dropdown Selection for International Telephone Input

The International Telephone Input plugin is beneficial, but it only allows for input. Users are not able to modify after selecting countries. I am interested in using it as a dropdown rather than an input field, so I made the following change: <input i ...

What is the best way to horizontally align three animated progress bars alongside images?

Check out this jsfiddle that shows a vertical alignment of the progress bars. How can I modify it to align them horizontally and centered? https://jsfiddle.net/bLe0jLar/ .wrapper { width: 100px; height: 100px; } .wrapper > #bar, #bar2, #bar3 { ...

Tips for positioning a div between two vertically aligned input elements

I'm trying to figure out how to place a div between two inputs with the same height and vertically aligned. It seems like a simple task, but for some reason, the code below isn't working: input{ width: 35%; border: 1px solid #A7A7A7; ...

Maintaining the layout of two columns in Bootstrap 4, responsive design turns them into a single stacked

Currently using Bootstrap v4 with a two-column layout that splits the container in half. On larger screens, the items in each column display correctly. However, on smaller screens, Bootstrap shuffles the items from column 2 into column 1. I am looking to ...

Manipulate jQuery to set the table row containing empty table data cells to be lower than the row with

I am using HTML and jQuery to sort my table, including non-standard sorting with multi-tbody. The issue I am facing is that the prices (Цена) in the td are sorted ascending but not correctly. Why is this happening? Additionally, I need to move the tr&a ...

Guide on displaying a welcoming error notification within a Bootstrap modal form

How can I display an error message within a modal after clicking on the update button? In this example, I have included a required attribute on the input field. I would like to modify the error message based on recommendations from Form Validation Best Pr ...

Flexbox/Bootstrap column with the option to include a vertically centered span text suffix

Is there a way to display an optional text suffix within a vertically centered flexbox/bootstrap column, specifically only on small screens? The current code I have works using "display:contents" on the span-element, but this isn't universally support ...

Designing a nested box layout with bootstrap HTML/CSS styling

Can anyone provide guidance on how to create a HTML/CSS layout similar to the image below? I am specifically struggling with designing the silver "Add corkscrew" box, a white box beneath it, and then another silver box nested within. Any suggestions on ho ...

Utilize Python selenium for launching a YouTube video

I have been experimenting with using Python selenium to create a script that will open YouTube, search for a video, and then click on one of the results. I have successfully completed the first two steps, but I am now stuck on how to actually open or click ...

What is the best way to smoothly hide and reveal a flex child element?

I am looking to create a flex container where the child element can smoothly animate to shrink and grow, while the remaining child elements expand to fill in the space. The method I have found involves setting the max-width to 0px and the borders' wi ...

What are some ways to utilize JavaScript for expanding an HTML form?

I am in the process of developing a basic web application that allows users to create messages with attached files. multiple html file inputs using this javascript link: http://img38.imageshack.us/img38/4474/attachments.gif The functionality to "attach a ...

Obtaining Beverage Overall with Loop

I am currently using a function to calculate the total of each drink, with a total of 5 drinks: var totalEstimate = 0; var locoCost = 0; var blackCost = 0; function calcLoco() { totalEstimate -= locoCost; locoCost = docume ...

Take out a text field from a cell within a table

I am looking for a way to dynamically remove a textarea when the 'x' button next to it is clicked. Each 'x' button has an id that corresponds to the id of the associated textarea. For example, if I click the 'x' button under t ...

Ensure that all input fields are validated simultaneously using the required attribute

I have been designing a form with various fields including text, number, and textarea. My goal is: I want to create a validation system for the form that checks all input fields with the required attribute in one single condition. For example, if the inp ...

Tips for incorporating a simple jQuery table

Is there a way to incorporate a simple table with 2 columns and 10 rows on a specific section of a webpage? The table will consist of 10 dynamic entries that are refreshed manually from the back-end DB. The second column of each entry will contain a bit.ly ...

Tips for iterating through a collection of images

I am interested in creating a loop to iterate over the images folder and display them on the screen, similar to the code snippet below: <section id="photos"> <a target="_blank" href="images/1.jpg"> <img src="images/1.jpg ...

Having trouble getting the equirectangular panorama example from Three.js to work on your device even though WebGL is supported?

After examining the WebGL equirectangular panorama example, it performs well on both desktop and mobile devices (such as the Samsung S4 Android 4.4.2) using the latest version of mobile Chrome. However, when tested on the Samsung tablet SM-T230 also runni ...