Centering an Image with CSS

Looking for assistance in centering my code vertically and horizontally, while also dealing with images of various sizes.

CSS:

body {
   margin: 0px;
}

.wrapper {
   text-align:center;
   width: 100%;
   height: 100%;
   position: relative;
   z-index: 21
}

.wrapper img {
   display: table-cell;
   vertical-align: middle; 
}

HTML:

<div class="wrapper">
  <img src="http://static.desktopnexus.com/thumbnails/196734-bigthumbnail.jpg" />
</div>

Answer №1

To align an image, simply adjust the margin.

.wrapper img {
    margin: auto;
}

This technique works for various elements, but ensure that the parent element has a defined width or height.

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

What are some ways to ensure a JQM-created button is clickable in Firefox?

I am working on making a div clickable in a jQuery Mobile powered website. The current code I have functions properly in Chrome and Safari, however, it does not work in Firefox - both on desktop and Android FF. <button data-icon="eye"> <a href ...

The expression `Object.prototype.toString.call(currentFruit) === "[object Date]"` checks if the object referenced by `current

Hi, I'm just starting to learn JavaScript and I have a question about an if condition that I came across in my code. Can someone please explain what this specific if condition does? Object.prototype.toString.call(currentFruit) === "[object Date]& ...

Position text above an image using CSS

.content { float: left; width: 33.33%; font-weight: bold; text-align: center; margin-top: 5px; margin-bottom: 2px; overflow-wrap: break-word; } .content_div { position: absolute; width: 100%; left: 0; top: 100%; font-size: 16px } ...

Delay the loading of CSS files until after the page has fully loaded in order to implement animations with

My angular 4 project has animation added in a CSS file, with classes applied to elements for animation on page load. However, it is currently not working properly on the first page load. The app takes time to fully load and the CSS animations apply intermi ...

Two CSS Issues - One specific to mobile devices, the other unique to Chrome browser

Having trouble with the top banner not stretching all the way on iPhone. Any solutions? Here's a screenshot for reference: Below is the CSS applied to the div: #banner { background-color: #F7F7F7; background-size: cover; box-shadow: 0 0 ...

The font size on my website fluctuates up and down every time I refresh the page or navigate to a different one

I am currently in the process of updating my research lab's website using a Jekyll framework with Bootstrap. However, I have encountered an issue where the font size grows slightly whenever I navigate to a new page or refresh the current one, before r ...

Attach image to the edge with a responsive layout using Bootstrap

I'm currently working on a project that involves the following elements: https://i.sstatic.net/rAZ0x.png I have images for the left and right sections, as well as an image of a monkey. My goal is to create a layout where the left image occupies col- ...

Leveraging webpack alongside url-loader for embedding images within React applications

When styling with an inline style and passing in the URL of an image file, I typically do it as shown below: let url = `url(${this.state.logo})` var cardStyle = { backgroundImage: url, backgroundSize: '200px 50px' ...

Customize image position based on div width using jQuery and CSS

I am dealing with dynamic input sourced from a JSON feed that populates a div with different names of varying lengths. Within this setup, there is an image positioned absolutely, and I need to adjust the CSS left value of the image dynamically based on th ...

Previewing multiple images with multiple file inputs

I am trying to find a way to preview multiple images uploaded from different inputs. When the button is pressed, the content from a textarea containing <img src="$img" id="img1"> is written inside a div called seeimg. The IDs for these images range f ...

Bootswatch alternative for Bootstrap CSS without utilizing Google Fonts

Currently, I am in the process of developing a webpage and utilizing Bootswatch for styling. However, there are times when I need to work offline and host locally. The issue I am facing is that Bootswatch cannot be used offline due to the fact that it util ...

Using the HTML <span> and svg elements together does not allow them to be inline with each other

In my current scenario, I have a fieldset that is inline with another one on the page. The specific fieldset in question contains an SVG circle and some text. My objective is to have both elements inline with each other, while allowing the fieldset to adj ...

Guide to implementing CSS3 transitions with prefixes using JavaScript

Is there a way to apply CSS styles using JavaScript when I don't have access to the CSS file? #fade div { -webkit-transition: opacity 1s; -moz-transition: opacity 1s; -o-transition: opacity 1s; -ms-transition: opacity 1s; transition: ...

What is the best way to remove the default outline in the <img> tag?

<img class="image_cover" src = "" /> .image_cover { width:25px; height:25px; border-style:none; border: 0; box-shadow:none; } Showing an example in a live demo on jsfiddle Encountering an issue in Chrome Version 33.0.1750.152, ...

Display 'Div 1' and hide 'Div 2' when clicked, then reveal 'Div 2' and hide 'Div 1' when a different button is clicked

I need help figuring out how to make two separate buttons work. One button should display 'Div 1' and hide 'Div 2', while the other button should show 'Div 2' and hide 'Div 1' when clicked. My knowledge of jquery an ...

Change the right border style for the second and third ToggleButtons in the ToggleButtonGroup

I've been working on this for a few hours now and I can't seem to get it right. Currently, I'm using Mui v5 and trying to style the ToggleButtons to look like regular MUI buttons. So far, I was able to achieve this transformation: https:/ ...

The alignment of Material-UI Button and ButtonGroup is not consistent

I'm puzzled as to why the Button and ButtonGroup elements are not aligned on the baseline in the code snippet provided. Is there a specific property that can be adjusted on the ButtonGroup element to achieve alignment? <!DOCTYPE html> <htm ...

What is the best way to change an ASP.NET MVC Navigation Bar from vertical to horizontal?

On my MVC5 page, I am facing an issue where the navigation bar options are not displayed side-by-side but are collapsed vertically. Here is a snippet of my bootstrap.css file: .navbar-nav { display: -webkit-box; display: -ms-flexbox; display ...

The focus is on the last row that was selected

I've noticed that when I check and select a row in ui-grid, only the last selected row is focused with a background color. Should all rows be automatically painted by default? Thank you. ...

Tips on effectively centering a wide div

After much experimentation, this is what I came up with: (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en ...