2

I've been trying to work this problem out, but even though there are so many solutions to it even here on stack I couldn't find one that works according to my needs.

I want to center an img inside its wrapper-div that has "overflow: hidden" so that the image could have any width possible. Problem is that the wrapper-div is aligned with another div on the same height which contains text.

I've tried solutions with absolute positioning but as the neither the width nor the height are set in a specific amount of pixels this wouldn't work and the image would no longer be displayed.

Trying to use flexbox also ended up in a mess and everything else I've tried doesn't work because of something else.

As this part containing the image and the text are part of a whole page they're dependant on quite a few other things. So I wouldn't have to post the whole code here I've prepared a codepen.

<div id="event_wrapper">
    <div id ="event_imagewrapper">
        <img id="event_picture" src="http://www.freedigitalphotos.net/images/img/homepage/87357.jpg"/>
    </div>
    <div id="event_textbox">
        <h2>Event-title</h2>
        <div id="event_text">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</div>
        <div>Continue...</div>
        <div id="event_date">01 APR</div>
    </div>
</div>

Everything inside the #event_wrapper can be manipulated but the conditions are as follows:

  1. The image-wrapper may only get into the middle of the event_wrapper (covering only half of the width of it)
  2. As the image will be loaded by a backend, it's impossible to use the CSS-property "background-image"
  3. Images may vary in size
  4. If the page is resized the text should be readable until it's very end, the picture may have more overflow (which would get hidden...)
  5. Everything should stay as dynamic as possible, so setting specific heights and widths will not be possible
  6. The solution should be browser-compatible (old versions of IE may be ignored... And yes I know, variables don't work in IE, this will only be used as long I'm not finished with the design! :-))

The following pictures will show what I mean:

Wrong img-positioining: wrong!

Correct img-positioning: correct!

Anyone who can help me resolve this problem would be greatly appreciated.

Did I make a mistake somewhere along the way? Is it even possible to do this with CSS-only?

EDIT

The following Stack-Questions I have considered and they might help you identify my request and issue:

Center a large image of unknown size inside a smaller div with overflow hidden

Aligning image to center inside a smaller div

Center an image in a div too small for it?

center oversized image in div

center a big image in smaller div

You will notice that these solutions either use absolute positioning on the img, need an explicit height or width or use flexbox which in my case due to unknown reasons didn't work.

EDIT 2

As stated in my answer below I found the reason for flexbox not working in my previous attempt with the solution that was given in Center a large image of unknown size inside a smaller div with overflow hidden : The solution there used "flex:none" on the image which didn't do anything with my picture and especially did not center it in my imgs parent-div.

Community
  • 1
  • 1
Kathara
  • 1,226
  • 1
  • 12
  • 36
  • can you please post a screenshot of how you want it to look? It's a little hard to clearly understand where the problem really is. Thanks for the detailed question. – Gurtej Singh Apr 11 '17 at 07:23
  • I want the image to be horizontally centered inside its wrapper-div. At the moment when you resize the window width, you'll see that the image is cut at the right side but not on the left, which leaves the right part of the image that is not focused. If the image would horizontally be centered, the other duck would be visible. This is what I intend doing so I'll try to create a picutre showing it. – Kathara Apr 11 '17 at 07:25
  • Can you see if this is what you are looking for : http://codepen.io/anon/pen/mmbPQE. Please let me know then I would try and explain it in an answer. Thanks. – Gurtej Singh Apr 11 '17 at 07:41
  • Sadly not. I've forgot to mention that using background-image will not be working... :( – Kathara Apr 11 '17 at 07:43
  • 1
    `flex` does work. Break down your problem into smaller areas and then test. See here - https://jsfiddle.net/abhitalks/h6xb6g7k/ – Abhitalks Apr 11 '17 at 11:51
  • @Abhitalks funny, when I tried this last time everything got mixed up. Now it seems to work. Maybe I had a mistake somewhere else before. Thank you very much on pointing this out. :) Would you please set this as answer so I can accept it? – Kathara Apr 11 '17 at 11:55
  • @Kathara: Don't worry about accepting and answer. I am marking this question as a dupe pointer to the first of the links that you had in your question. Just accept the dupe so that future visitors know that the solution works. – Abhitalks Apr 11 '17 at 11:59
  • Possible duplicate of [Center a large image of unknown size inside a smaller div with overflow hidden](http://stackoverflow.com/questions/19673895/center-a-large-image-of-unknown-size-inside-a-smaller-div-with-overflow-hidden) – Abhitalks Apr 11 '17 at 11:59
  • @Abhitalks I think this one was the problem. Unlike the answer you gave me for the img they put flex: none, which didn't work at all. The first answer to that question didn't work either so I'd rather have my question marked as some other duplicate. I don't mind accepting the duplicate then. – Kathara Apr 11 '17 at 12:04
  • Ahhh.. got it. You may add an answer of your own describing the problem you faced with the other solution and how it was solved. You may then accept your answer. Happy learning. – Abhitalks Apr 11 '17 at 12:06
  • @Abhitalks I will do that. Thanks a lot though for your help. :) – Kathara Apr 11 '17 at 12:07

2 Answers2

1

Thanks to Abhitalks I found the problem I had with the flexbox. I've adapted the codepen so that it works with flexbox now.

For everyone who's seeking a solution to this, this is what your markup should be like:

HTML

<div class="container">
    <img src=""/>
</div>

CSS

.container {
    display: flex;
    justify-content: center;
    width: 50%; /* may have any percentage */
    height: 100%; /* this is needed so that there will be no border at the bottom of the picture if window is resized */
    overflow: hidden;
    float: left; /* this can be left out if there is no textbox on the right side */
}

img {
    min-width: 100%;
    min-height: 100%;
}

Happy coding!

Kathara
  • 1,226
  • 1
  • 12
  • 36
0

see if this helps fiddle

#event_imagewrapper {
    display: inline-block;
    position: relative;
    width: 49%;
    height: 100%;
    overflow: hidden;
    float: left;
    background: url(http://www.freedigitalphotos.net/images/img/homepage/87357.jpg);
    background-position: cover;
    background-repeat: no-repeat;
}

#event_picture {
    display: inline-block;
    width: auto;
    height: 100%;
    position: relative;
    visibility: hidden;
}

I've used the image as background of the imagewrapper then hide the image not display: none so it would take the same width and height as the previous setting.

GvM
  • 1,685
  • 11
  • 13
  • I've forgot to mention that I can't use background-image on this as the image will be loaded from a backend... I've got to add this to the conditions... – Kathara Apr 11 '17 at 07:40
  • what type of backend are you using? – GvM Apr 11 '17 at 07:44
  • Typo 3 and according to my boss it's only possible to load images into an img-tag.... Though I'm not sure if it would be possible but I won't be able to argue with him about it ^^ – Kathara Apr 11 '17 at 07:45
  • @Kathara Will you be able to add some javascript or jquery? – Lal Apr 11 '17 at 07:45
  • @Lal It would be possible, yes and I have to admit that I'm not very experienced with it – Kathara Apr 11 '17 at 07:48
  • you should ask if your boss can load the images as inline style for example: `
    `
    – GvM Apr 11 '17 at 07:51
  • @Kathara please check if the issue is solved if you add a style as follows: `#event_picture{ width:100%; }` – Lal Apr 11 '17 at 07:52
  • @GvM I believe this would be possible but I'm quite sure he won't allow it otherwise I wouldn't have had such problems with another issue I had before.... – Kathara Apr 11 '17 at 07:52
  • @Lal sadly not because if you resize the window there will be appearing a a white margin underneath the picture. That's why I put the width on auto in the first place... :( – Kathara Apr 11 '17 at 07:54
  • try `max-width: 100%;` on the image if that would do – GvM Apr 11 '17 at 07:56
  • @GvM This at least does not create borders but the image is squeezed together when resizing... – Kathara Apr 11 '17 at 08:01
  • if you're using same size images you can use translateX like this https://jsfiddle.net/6aysjc5L/1/ – GvM Apr 11 '17 at 08:02
  • @GvM this almost works but the images may vary in size which causes another problem... – Kathara Apr 11 '17 at 08:14
  • @Lal do you know a solution with jquery or javascript? – Kathara Apr 11 '17 at 08:23