aligning a floating image vertically

One issue I am facing in my UI is that I have buttons with images floating next to text. While the setup works well, I am struggling to vertically align the image with the text.

I've experimented with various solutions including using vertical-align: center;, attempting absolute positioning, and even trying a transform technique that didn't work out as planned.

Since the content for these buttons is user-generated and dynamic, adjusting line height isn't a viable option in this case.

Any assistance on resolving this matter would be greatly appreciated!

The HTML structure of my code resembles the following:

<div class='btn-primary col-md-4'>
    <img  src='https://developers.google.com/web/fundamentals/imgs/placeholder--small.png'/ >
        <span class='imgInfo'>This is info. (Repeat) </span>
        <p class='clearfix'></p>
</div>

The CSS style applied looks something like this:

img{
    vertical-align: middle;
    float:left;
    width:50%;
}

.imgInfo{
    margin-left: 0px;
    overflow:auto;
    display:block;
    float:right;
    width:50%;
    padding-left:1em
}

To interact with and test the code further, feel free to utilize this jsFiddle link: http://jsfiddle.net/52VtD/7356/

Answer №1

Check out the Demo

Styling with CSS

img{
    float:left;
    width:50%;;
    position: absolute;
    top:0;
    bottom:0;
    margin: auto;
}

(source: Learn more from a Trusted Source)

Explanation of Absolute Centering Technique

After thorough research and analysis, here is how the Absolute Centering method operates:

  1. When used in the normal content flow, margin: auto; sets the margins to '0' for top and bottom.
    Referenced from W3.org Documentation: If ‘margin-top’ or ‘margin-bottom’ are ‘auto,’ their actual value becomes 0.
  2. The use of position: absolute; removes the block element from the conventional content flow, occupying no space as stated in Developer.mozilla.org Reference.
  3. By defining
    top: 0; left: 0; bottom: 0; right: 0;
    , the browser establishes a new bounding box for the block within its offset parent (body or position: relative; container).
  4. To prevent the block from occupying all available space, setting a specific width or height prompts the browser to calculate margin: auto based on the updated bounding box dimensions.
  5. Since the block is absolutely positioned and not part of the normal flow, equal values are assigned to margin-top and margin-bottom for vertical centering, following the guidelines outlined by W3.org Specifications.

The technique of Absolute Centering relies on the use of margin: auto; according to standards and should function effectively across compliant browsers.

In summary: Elements positioned absolutely do not affect the normal flow, allowing margin: auto; to center vertically within the specified bounds (

top: 0; left: 0; bottom: 0; right: 0;
).

Answer №2

My recommendation would be to implement the following:

.img {
  margin-top:40%
}

This approach is more effective than relying on vertical-align.

Answer №3

You may want to try this approach -

img{
    position: absolute;
    width: 50%;
    top: 0;
    bottom: 0;
    left: 5px;
    margin: auto;
}

Setting top: 0; and bottom: 0; simultaneously will ensure they don't overlap.
Additionally, using margin: auto; will center align the img properly.

See it in action on this FIDDLE

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

Unable to fetch css file in Node.js

I am currently in the process of learning Node.js, but I have encountered a small issue with retrieving data from a css file. Interestingly, when I directly add the data to the index file's code, it seems to work perfectly. Let me share the relevant ...

Formatting won't assist in aligning Facebook button

I am currently working on a page that includes a div with a Facebook recommend button. In order to style the Facebook code, I enclosed it with this formatting: <div style="align:center"> <div id="fb-root"></div> <script src=" ...

Creating an endless ticker animation in AngularJS that dynamically adjusts to element dimensions

This is my initial foray into AngularJS, where I am creating a ticker display comprised of boxes. Check out the CodePen here The Code: index.jade: doctype html html(ng-app="ticker") head script(src="../bower_components/angular/angular.js" ...

Choose all checkboxes across the entire webpage

Given the code below: <input type="checkbox" name="categories[9507]"> Is there a way to write a JavaScript command that can automatically select all checkboxes with similar naming structures on the entire page? The only difference in the names is t ...

Ways to prevent the input value from rising on a number type input when the up button is pressed

I'm curious about whether it's possible to prevent the input value from increasing when I press the up button on a type number input. Any ideas? ...

Tips for bringing in pictures from outside directories in react

I am new to React and trying to import an image from a location outside of the project's root folder. I understand that I can store images in the public folder and easily import them, but I specifically want to import them from directories outside of ...

Utilize JavaScript or JQuery to create a dynamic pop-up window that appears seamlessly on the

Looking to create a unique feature on an HTML page? Want a clickable link that opens a "pop-up" displaying a specific image, right within the page rather than in a new tab or window? Ideally, this pop-up should be movable around the page like a separate ...

What is the best way to check for a matching array value in my menu list and apply a corresponding class tag to it?

I need a way to dynamically add a class to tags that match specific array values. My menu list consists of 150 items, and I want to add a class to the tag whose text matches an element in the array. <ul class="nav main" id="tabs"&g ...

Styling with ngStyle and changing the background image

Having an issue with a component that has an @Input string value linking to an image. In my HTML file, I originally had: <div class="parallax" [ngStyle]="{'background-image': 'url({{parallaxImage}})'}"></div> This was not ...

Master the art of keeping track in just a single line

I need to ensure these tabs stay in a single line and remain responsive for mobile devices. Below is the code snippet: <div class="navbar"> <div class="navbar-inner"> <ul class="nav nav-tabs"> <li class="active"& ...

Is It Possible to Create Flash Content Without Using a SWF File?

Is there a way to embed Flash directly in HTML, rather than linking to an external SWF file? I am looking to send an HTML form via email for the recipient to fill out by opening it in a browser. The final step would involve copying the result to their clip ...

What is the process for displaying a JSON element within an HTML component in a Bootstrap modal?

My application is developed using CodeIgniter, and now I am looking to manipulate data using AJAX in jQuery. How can I display JSON elements within an HTML element in a Bootstrap modal? Here is the HTML and jQuery code: <td class="center"> < ...

Type Fury: A Multiplayer Gaming Hub for Speed Typists and Puzzle Solvers

Curious about creating a website that allows users to log in and participate in competitions against each other. Any tips on where to start or recommendations for existing frameworks would be greatly appreciated. Appreciate your help in advance! ...

The functionality of jQuery's .hide method is not effective in this specific scenario

HTML section <div class="navigation-bar"></div> Jquery & JavaScript section function hideUserDiv(){ $('.ask-user').hide(); } var ask = '<div id="ask-user" style="block;position:absolute;height:auto;bottom:0;top ...

I am experiencing some unwanted movement of divs when I hide one element and show another. Is there a way to prevent this from happening

My webpage features a dynamic div that transforms into another div upon clicking a button. Although both divs share similar properties, clicking the button causes some elements to shift unexpectedly. Strangely enough, when the height of the replacing div i ...

The tooltip feature for icon buttons within Material UI list items is not functioning properly as anticipated

Just starting out with Material UI and React, I've encountered a strange UI problem that I can't quite figure out. Hopefully someone here can help me identify what I did wrong. My Approach: I have a List in my code where each list item has butto ...

Adjust the translateX value and element size based on either the parent element's size or the window's dimensions

Is this question specific enough to relate to others' problems? My issue involves two elements, a child and a parent, with the child element rotating around the parent using CSS animations. <div class="planet"> <div class="moon"></di ...

best practices for choosing items from a dropdown menu using Angular

I have a dropdown list displaying existing tags/chips created by users in the past. However, I'm having an issue where when I select a tag from the dropdown list, it doesn't show up in my input field (similar to the Chart tag currently). I am abl ...

Maximizing the potential of Angular forms through native FormData

Currently, I am revisiting an older project that still uses traditional methods for form submission. The HTML includes a form element with defined method and action. My goal is to submit the form in the old-fashioned way using the action attribute to post ...

Modify/Adjust/Move the image uploaded by the user in VueJS before transferring it to an aws s3 bucket

Can you resize images in vuejs using vanilla js? I've managed to upload an image and send it to my s3 bucket using vue, but I'm struggling to figure out how to transform an image within vuejs and display it. Document.querySelector doesn't se ...