Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6835579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:14:58+00:00 2026-05-26T23:14:58+00:00

I have a div with width:100px and height:100px (say) Inside that, there is just

  • 0

I have a div with width:100px and height:100px (say)
Inside that, there is just an image, for which height is always fixed to 100px.

I want to make the image horizontally center.

Here there are 3 cases:

  1. image’s width is equal to div’s width, no issues
  2. image’s width is less than div’s width, I can use margin: auto here
  3. image’s width is more than div’s width

I want the center part of the image to be visible inside the div.

means, if image’s width is 120px and as div’s width is 100px and overflow:hidden
I want image’s 10th px to 110th px to be visible (so, the left: 10px and right: 10px of image are hidden under the div )

Is this possible through some CSS property?
(I dont know the width of image which is loading! so I want it to be dynamic.
Also want to avoid javascript side calculations to find the extra amount of width and giving margin-left: -ve value bla bla.. )

Also, I can’t give the image as background-image for the div!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:14:58+00:00Added an answer on May 26, 2026 at 11:14 pm

    See: http://jsfiddle.net/thirtydot/x62nV/ (and without overflow: hidden to easily see the centering)

    This will work in all browsers, with the possible exception of IE6.

    For .imageContainer > span, the margin-left is derived from the width, and the width is an arbitrary number which controls the maximal image width that will be supported. You could set width: 10000px; margin-left: -4950px; to support really wide images, if required.

    HTML:

    <div class="imageContainer">
        <span><img src="http://dummyimage.com/100x100/f0f/fff" /></span>
    </div>
    

    CSS:

    .imageContainer {
        border: 1px solid #444;
        overflow: hidden;
        width: 100px;
        height: 100px;
        margin: 15px;
        text-align: center;
    }
    .imageContainer > span {
        display: block;
        width: 1000px;
        margin-left: -450px; /* -(width-container width)/2 */
    }
    .imageContainer > span > img {
        display: inline-block;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a div with a width of 200px. There is text inside that
Let say I have an element like so <div id=myDiv style=height:10px; width:100px; overflow:scroll;> random
I have the following markup: <div style=overflow:auto;width:500px;height:100px;> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li>
I have a div element with fixed height and width and overflow:hidden, and I
So I have a div in my body that's a percentage width, and inside
I have a container div with a fixed width and height , with overflow:
I have a relatively -positioned div, which has overflow: auto set. Inside that, I
I have this html div tag defined: <div style=height:100px; width:100px class=total-title> first text </div>
I have the following HTML code: <script src=http://code.jquery.com/jquery-1.6.2.min.js></script> <div id=A style=width:100px; height: 100px; background:
I have this code: <div style=border:1px solid #afafaf;background-color:#efefef;width:100px;height:14px;> <div style=text-align:center;width:50px;height:14px;background-color:green;> 50% </div> </div> How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.