Centered image inside a div with an unknown height and width

I am trying to center an image using a CSS Trick, even when the image size is random and not fixed. I don't want to make the image responsive, but rather bring it to the center without changing its actual pixel dimensions.

Here is my code:

HTML:

<div class="slider"><img class="center" src="images/mySlide.jpg" /></div>

CSS:

.slider{
       width: 100%;
       position: relative;
}
.center {
       width: 300px;
       height: 300px;
       position: absolute;
       left: 50%;
       top: 50%; 
       margin-left: -150px;
       margin-top: -150px;
}

Answer №1

To perfectly align an image, follow these steps:

http://jsfiddle.net/NicoO/y5vh3/2/

.banner {
    width: 100%;
    position: relative;
    text-align: center;
    height: 400px;
}
.banner:before
{
    height: inherit;
    display: inline-block;
    vertical-align: middle;
    content:"";
}

.align-center {
    display: inline-block;
    margin: auto;
    vertical-align: middle;
}

Implement the HTML like this:

<div class="banner">
    <img class="align-center" src="http://www.placehold.it/100x40" />
</div>

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

In a bootstrap column containing an inner element with a wide width that is taking up more space than intended

I am attempting to create two columns of equal size, containing nested elements with large dimensions and adding overflow: scroll;. However, the column width is defaulting to 100%. Interestingly, when I apply a small width to the column (), the issue is re ...

Automatically navigate to a different page using JavaScript after 5 seconds without interrupting the execution of other code

Is there a way to redirect to a specific URL after 5 seconds without halting the execution of other code on the page? I want all the other code to run first before triggering the redirection. Wrapping the entire page in a setTimeout block is not an option. ...

What is the best way to create an HTML form on-the-fly from a JSON object?

Could someone please assist me in understanding how to dynamically generate an HTML form based on a JSON object using JavaScript? ...

Tips for animating input width as the size value changes during an ajax query transformation

This is my JavaScript code: function updatePriceDisplay() { $.ajax({ url:"query.php?currency=<?=$currencycode;?>" }).done(function(data) { $("value").attr("value", data).attr("size", data.length - 2); }); } updatePriceDi ...

Unusual hue in the backdrop of a daisyui modal

https://i.stack.imgur.com/fLQdY.png I have tried various methods, but I am unable to get rid of the strange color in the background of the Modal. Just for reference, I am using Tailwind CSS with Daisy UI on Next.JS. <> <button className='btn ...

Is there a time limit for processing express requests?

My API runs a child process upon request and returns its final output. The processing can take anywhere from 5 to 30 minutes, which is acceptable. However, Express drops the connection after some time and only logs POST /api/v1/check - - ms - -. This means ...

What is the best way to eliminate the border color on a drop-down menu's bottom border?

I need help removing the border color from a dropdown with the style border-bottom: 1px solid rgba(0, 0, 0, 0.42); After debugging, I discovered that it is originating from the class MuiInput-underline-2593. However, the CSS class MuiInput-underline-2593 ...

The looping mechanism in Angular is not being updated because of how the $mdDialog from Material Design is being implemented

After reviewing my previous post on Angular loop is not updating, I made a slight modification to the code by incorporating a dialog box for user interaction. The only change in my app.js file was the addition of a $mdDialog box directive. Therefore, the ...

Step by step guide on creating a CSS triangle shape in front of a span element

Can you help me troubleshoot why my css triangle isn't displaying before my span? I've tried everything but it just won't show up. .triangle:before { width: 0; height: 0; border-top: 3px solid transparent; border-right: 6px solid ...

Librsvg encountering issue with CSS not applying to descendant selectors

While attempting to utilize librsvg to render charts produced in SVG format for a larger project, I noticed that the rendered result differs significantly from how the SVG file appears when opened in a web browser. After analyzing the SVG file piece by pi ...

What could be causing the malfunction of this Ajax conditional dialog?

Can anyone help me troubleshoot this code? I've been trying to fix it for a while now, making multiple changes, but still can't find the solution. If you have any ideas please let me know, I haven't seen any errors in the console. The fir ...

Looking to create a unique custom popup in your React JavaScript project with the power of HTML5

I attempted to access the following links: http://codepen.io/anon/pen/zxLdgz Unfortunately, they didn't work in: I inserted this code in a jsx file <a href="#openModal">Open Modal</a> <div id="openModal" className="modalDialog"> ...

What is the proper way to reference automatically generated class names within nested style rules when using makeStyles in Material-UI?

When using makeStyles, how can I reference generated classnames when creating nested rules? For instance, if I have a "container" class with nested "item" elements and I want to use the generated "item" class in the style definition. The approach that wor ...

Ways to enhance the data for my YouTube video uploads version 3

Currently facing an issue where Google has restricted my daily queries to 10,000. I am in search of a solution to adjust my chunksize either up or down. Uncertain about the exact amount to increase or decrease to limit the number of queries per upload, her ...

The issue with jspdf is that it is failing to generate PDF documents of

I'm currently developing a resume builder app using ReactJS. One of the functionalities I'm working on is enabling users to download their resumes as PDFs. However, I've encountered an issue with the generated PDFs when using jsPDF. The down ...

What is the best way to alter an HTML element using Ruby with Sinatra and Bootstrap?

Below is the content of my index.erb file: <a class="btn" href='javascript:TestFunction()' role="button">TestFunction</a> <script language="javascript"> function TestFunction() { $.post("test_function", { action ...

Exploring new options to deduct time from Kendo UI timepickers without relying on Javascript

My current project involves an Asp.net MVC 4 application that includes a Time entry screen. This screen utilizes various Kendo controls and Razor Html helpers to enhance the user experience, such as: @(Html.Kendo().TimePickerFor(m => m.StartTime).Name( ...

Having trouble with a broken link on your HTML email button?

I'm attempting to add a button to an HTML email that redirects to a link within an href tag. Admittedly, my knowledge of html code is minimal. Here's what I've attempted: <p> <input style="width: 200px; padding: 15px; box-shadow: ...

Error: JSONP Label Validation Failed

My JSON URL is: The above URL returns the following JSON: { token: "2cd3e37b-5d61-4070-96d5-3dfce0d0acd9%a00a5e34-b017-4899-8171-299781c48c72" } Edit: Changed it to {"token": "2cd3e37b-5d61-4070-96d5-3dfce0d0acd9%a00a5e34-b017-4899-8171-299781c48c72"} ...

jQuery function designed for elements inside modal dialog not triggering

After implementing a jQuery UI modal dialog, everything seems to be working correctly. However, I have noticed a problem with some elements within the dialog. For example: <div id="test_div"> Test Click me </div> Although the elements app ...