Effortless CSS alignment (centering text and a tall image within a div)

I've been struggling to achieve the following task: I want to have a <div> element that takes up the entire width of its parent div. Within this, I aim to place A. some text and B. an image.

A. The text (which can be loose or wrapped in a <p>, <h2>, <span>, or <div> element) should be on the left.

B. An image defined with an <img> tag where both height and width are known.

Additional requirements:

  1. There should be 12px of space between the text and the <img> element.

  2. Crucially, both the text from A. and the image from B. must be centered as a group.

  3. The text from A. needs to be vertically centered within its container.

How can I accomplish this arrangement? Despite my attempts, I am unable to position the image next to the text or get the text A. properly centered vertically.

Does anyone have a solution for this straightforward issue?


Thank you all for providing your answers; it seems CSS can make simple tasks incredibly challenging. In any case:

  div#content_whatsnew, div#content_bestsellers { clear: both; height: 108px; font-size: xx-large; text-transform: uppercase; margin-left: 380px; }
  div#content_whatsnew p, div#content_bestsellers p { float: left; height: 108px; line-height: 108px; padding: 8px 12px 0px 0px; color: black; }
  div#content_whatsnew img, div#content_bestsellers img { float: left; height: 108px; }

Answer №1

Does this match your desired outcome?

Answer №3

Give this a try:

<div class="main-container">
 <div class="inner-container">
  <div class="text-content">This text is just perfect, simply delightful.</div>
  <img src="image.jpg" alt="" class="my-pic" />
 </div>
</div>

.main-container {
 width:auto;
 text-align:center;
}

.inner-container {
 width:YYYpx; /* insert desired width here */
 margin:0 auto;
 overflow:auto;
}

.text-content {
 width:YYYpx; /* insert desired width here */
 float:left;
 margin-right:12px;
}

.my-pic {
 width:YYYpx; /* insert desired width here */
 height:YYYpx; /* insert desired height here */
 float:left;
}

Consider implementing the vertical centering tip mentioned in the guidance from @biziclop above.

Answer №4

One common approach is to use 'vertical-align:middle;' but it doesn't always produce the desired outcome.

After doing some investigation, I came across this code snippet on a helpful resource found here:

I hope you find this solution beneficial!

<style type="text/css">
    #myoutercontainer { position:relative }
    #myinnercontainer { position:absolute; top:50%; height:10em; margin-top:-5em }
</style>

<div id="myoutercontainer">
    <div id="myinnercontainer">
        <p>Wow! I am now vertically centered!</p>
        <p>Such a neat trick, isn't it?!</p>
    </div>
</div>

Answer №5

To ensure a div is centered, it must have a defined width. If the div extends the width of its parent div, the only way to center content inside is by giving it a fixed width. A possible solution would be to enclose text in a left-floated div with a fixed width, do the same for images, and then place both within a holder div with a fixed width that is centered using margin:auto;

For reference, see this example:

Additionally, I vertically aligned the text by placing it in a table. Tables are the most reliable method for achieving vertical alignment across different browsers.

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

JavaScript code that triggers when a checkbox is not selected

I'm attempting to dynamically add an input field when a checkbox is clicked, with the intention of having the checkbox already checked by default. However, I am encountering an issue where the checkbox remains unchecked even after the input field is d ...

Display a list with collapsed columns on an accordion format for a more compact view on smaller screens

My Bootstrap table setup looks like this: <table class="table table-striped"> <thead> <tr> <th>Date</th> <th>Name</th> <th>LastName</th> <th>Color</th> <th>Car</th> ...

The Selenium Webdriver's isSelected() method in Chrome browser does not return true for radio buttons

I have been attempting to confirm whether my radio button is selected or not, but I keep receiving a false value even after selecting the radio button. Here is the HTML code: <div class="controls tss-radio-text-format"> <label class= ...

Guide on updating BgImage dynamically through JavaScript

I've been exploring a way to dynamically update the background image of an HTML document using JavaScript. While I am familiar with hardcoding the links, I'm interested in loading it from a variable URL that I specify. Here is what currently work ...

Importing a 3D Model Using Three.js

I've been trying to import a 3D model by following tutorials. I managed to successfully import using A-Frame Tags, but encountering issues with Three.js. The code snippets are from a tutorial on YouTube that I referred to: https://www.youtube.com/watc ...

Identify line counts and hyphenated lines in a text box with the help of regular expressions

I have been struggling to find a solution to the problem of excluding lines starting with double hyphens when using regular expressions. I need to count single lines separately and double hyphenated lines separately, and display them outside the text area. ...

Aligning text to the right in Bootstrap 5 input fields

I'm trying to align the values of my inputs to the right. Any suggestions on how to achieve this? Here's a visual representation of what I'm looking for. This is the snippet from my code: <td style="text-align:center; vertical-alig ...

Is there a way to keep left-aligned text centered even when it wraps around?

I would like to center a block of left-aligned text within a div, ensuring that the visual width of the text block is centered rather than the block itself. In situations where the div is narrow and the text is too long to fit on one line, causing it to o ...

Detecting User Interaction with Email Link

On my webpage, there is a link that when clicked, opens the default Email client. I also want to track in my database if the user has clicked on this link or not. Since this interaction occurs at the client-side, I am wondering if there is a way to check ...

Utilizing React State to dynamically alter the look of a circle element upon clicking a button component

I'm having trouble adjusting the style of a circle component when it's selected by a button in another component. Both components need to change appearance upon selection, but I am facing issues with state management and feeling a bit overwhelmed ...

Locate the <img> tag within an HTML string using the CID and substitute it with text

Currently, I am developing a project in asp.net using C# in Visual Studio 2015. I have obtained an HTML string that contains embedded images like: string sHTML = "<table> <tr> <td><img src='cid:ii_15b82060d11f1e16' ...

Keyframes for CSS3 animations

I've spent the past hour trying to figure out this issue. I rely on Notepad++ as my code editor, and I've been attempting to create a css3 animation without success. The problem lies in Notepad++ not recognizing the "@" symbol in "@-webkit-keyfra ...

Upgrade to iOS 8 and experience the new Full Screen Mode feature. When you tap on a text field for the first time, the onscreen keyboard will flash briefly

Recently, I encountered a strange issue with my web app (AngularJS HTML5) after upgrading my iPad to iOS8. The onscreen keyboard started acting strangely - when I tapped a text field once, the keyboard would very briefly flash and then disappear. It was so ...

Adjust the background color of Ant Design button

I have seen similar topics on this issue, but I am struggling to find a solution that works for my specific situation. Despite trying the available information, I haven't been successful so far. My current challenge is changing the background color o ...

The div element loses its label and span elements once the timer event has completed firing

I am currently developing a web application that includes an "infobar" spanning the entire width of the screen in the DEFAULT.aspx page. The purpose of this infobar is to always stay on top and display updates, statuses, and other relevant information to t ...

Creating a Jquery-powered dungeon generator

I am attempting to replicate the functionality described in the link using jQuery, but I am encountering difficulties. 1) I have successfully created 10 different rectangles and positioned them in the center of the canvas in a mixed-up manner - no issues ...

In Django templates, when you break a line in the code it results in a space being

Looking for a solution in a simple template {% with var1="super" var2="man" %} <p> {{ var1 }} {{ var2 }} </p> {% endwith %} The code currently outputs super man, but I need it to be superman. Using {% spaceless %} d ...

The webpage is unable to display the image from an external source due to security restrictions

Trying to display an external jpg image on my webpage: "" If I paste the link into my Chrome browser, the poster loads. However, if I use it as an img src and clear my cache beforehand, it doesn't load: <!DOCTYPE HTML"> <html> ...

What's the reason for my badge being an oval shape?

I am attempting to design a badge that hovers above my cart icon and shows the total number of items currently in the cart. However, I have encountered an issue where the badge appears as an oval shape rather than a perfect circle. Can anyone assist with ...

Arrange the selection options by price using jQuery

Sorting options by price is my goal, but despite trying various codes, I have not been successful! This is the original structure: <select id="product-list"> <option>product 1- [2$]</option> <option>product 2- [4$]< ...