Image can be centered, but unable to center div in IE7

When trying to center an element both vertically and horizontally, everything seems to be working correctly except for one issue I'm facing in IE7. I am able to center an img vertically but not a div. What style is being applied by IE to the image that is not being applied to the div?

HTML

<!-- Image - working correctly -->
<div class="container">
    <img class="inner" src="http://placekitten.com/200/200?image=2" />
</div>
<br/>
<!-- Div - not working (aligned to top) -->
<div class="container">
    <div class="inner">123</div>
</div>

CSS:

.container {
  height: 300px;

    background: #EEE;
    text-align: center;
    line-height:  300px;
}

.inner { vertical-align: middle; width: 100px;
 height: 100px; background:red; display: inline-block; line-height: 1.3; }​

Fiddle:

http://jsfiddle.net/kpdxu/7/

Also:

  • I am uncertain about the size of the DIV
  • I can utilize JavaScript, however, I cannot determine the size of the DIV as it contains dynamic content

Thank you!

Answer â„–1

utilize this css:

.container {
   height: 300px;
   background: #EEE;
   text-align: center;
   line-height:  300px;
   position:relative; //<--this will contain the absolute positioned elements
}

.inner { 
   vertical-align: middle; 
   width: auto;
   height: auto; 
   background:red; 
   display: block; // <--display block works for ie 7
}

using jquery:

$.fn.center = function () {
   this.css("position","absolute");
   this.css("top", ( $('.container').height() - this.height() ) / 2 + "px");
   this.css("left", ( $('.container').width() - this.width() ) / 2 + "px");
   return this;
}

then apply it in this way

$('.inner').each(function(){
   $(this).center();
});

however, the parent must have position relative.

view the fiddle: http://jsfiddle.net/kpdxu/14/

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

Waiting for data to be passed from a parent component within a method

I have a situation where I need to make an API call in my layout and send the received data as an object to my component. The problem arises because the object is empty when the method is called inside the mounted() function. Therefore, I want to execute ...

The br tag in HTML cannot be utilized in conjunction with JavaScript

I am currently working on a project involving HTML and JavaScript. I have a "textarea" where data is inserted into the database upon pressing the "Enter key." However, I am encountering two issues: Currently unable to save data like "Lorem Ipsum" (the ...

Expanded MUI collapsible table squeezed into a single cell

I'm experimenting with using the MUI table along with Collapse to expand and collapse rows. However, I've noticed that when utilizing collapse, the expanded rows get squished into one cell. How can I adjust the alignment of the cells within the p ...

What is the reason behind genNewColor function's malfunction?

I'm having trouble with this code that is supposed to generate a random color. The CSS formatting works perfectly, but I suspect there might be an issue with the function itself. When I try to run the code, it doesn't produce any error messages â ...

An Easier Way to Incorporate an Image Insert Button into CKEditor

Having trouble displaying the Insert Image button in CKEditor 4.1.1? Here's my current configuration setup in config.js: CKEDITOR.editorConfig = function( config ) { // Customizing default configuration here. // For more details, check: http: ...

Did you manage to discover a foolproof method for the `filesystem:` URL protocol?

The article on hacks.mozilla.com discussing the FileSystem API highlights an interesting capability not previously mentioned. The specification introduces a new filesystem: URL scheme, enabling the loading of file contents stored using the FileSystem API. ...

interrupt the node script using async behavior

I encountered an issue while running the npm install command to install a list of modules on Node, specifically related to async. TypeError: undefined is not a function What could be causing this problem? var fs = require( "fs" ), path = require( ...

React's input onChange event does not trigger for the second time. It only works the first time

Recently, I developed a React JS application to import images from external sources and process them. To handle the user's onChange event, I utilized the onChange attribute to execute my handleChange function. However, I encountered an issue where it ...

Tips for keeping the hover effect of a sibling element in Material-UI

Card Component import image from "../../Assets/pic.jpg"; import React, { useState } from "react"; import { makeStyles } from "@material-ui/core/styles"; import Card from "@material-ui/core/Card"; import CardActionAre ...

Tips on slowing down the Jquery UIBlock Plugin

Currently, I am implementing a plugin found at http://jquery.malsup.com/block/#overview. However, I am interested in configuring the blockUI feature to only be displayed if an AJAX request takes longer than 1 second. Otherwise, I would prefer for nothing ...

What is the best way to enlarge an image while keeping it contained within a card using Bootstrap?

Is it possible to create a visually appealing card similar to the image provided using Bootstrap 5.3.3 and HTML on a website? https://i.sstatic.net/OH4U0.png ...

What is the best way to enlarge icons and space them out evenly along a single line?

Would anyone be able to assist me in increasing the size of and separating these three icons that are close to each other using CSS3 in a single line? I am utilizing Bootstrap5 and Google Icons. <link href="https://fonts.googleapis.com/icon?family= ...

Can you explain the process of sending an AJAX request and managing it on a web server coded in C?

Could someone please provide an example of an AJAX request that retrieves a global variable stored on a webserver written in C? I am unfamiliar with JQuery and AJAX, so I would appreciate any guidance on how to accomplish this task. Thank you in advance ...

Issue with scrollTop not functioning when using onclick on an <a> tag within a div

After various attempts and research online, I am still experiencing erratic scrolling behavior with the scrollTop function. My goal is to scroll within a specific div when clicking on links. The content of my div: <div id="test" style="height:400px; o ...

Tips for effectively organizing a collapsible list

Here is a list that I have: <ul> <li><span class="Collapsable">item 1</span> <ul> <li><span class="Collapsable">item 1.1</span></li> </ul> </ul> I am looking to create ...

Preventing duplication of code execution in recycled PHP elements

Currently, I have implemented a generic toolbar that is used on multiple pages of my web application by using PHP include. This toolbar contains log in/log out functionality and checks the log in status upon loading to update its UI accordingly. Initially ...

Clear radio selection after displaying warning message

Looking for a solution involving radio buttons - one and two. Requirement: Upon selecting radio button two, a warning message should be displayed and the selection should reset to one. Any suggestions on code implementation? ...

EmeraldSocks Tweenmax motion design

I need help with Tweenmax animation. I'm attempting to animate an id selector, but nothing is happening. Both the selector and content are unresponsive. Can someone assist me? Here is the code: <!DOCTYPE html> <html> <head> ...

"Mastering the art of underlining individual components in React with the power of

Hey there, I'm having some issues working with the CSS for my component. I've created a joke component that has joke and punchline props. The issue is that when it appears in the compiler, my joke component (which I call twice) only shows up as t ...

Behavior of Bootstrap Modal When Closing

I'm encountering an issue with a button labeled Sign Up that is supposed to load content in the form of a Modal from another file. Initially, when I click the button for the first time, everything works smoothly without any errors or warnings. However ...