-3

I have a div 1024 x 768 px. I want show center a 1920 x 1080 px image right in the middle of the div and continue to be visible with scrolling div.

How can it be done?

Here is the Test code.

var el = document.querySelector('div.mainDiv');

var newWidth = Math.ceil($("#baseImg").width()/2);
var newHeight = Math.ceil($("#baseImg").height()/2);

el.scrollLeft = newWidth;
el.scrollTop = newHeight;
<!-- begin snippet: js hide: false console: true babel: false -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mainDiv">
  <img id="baseImg" src="https://dummyimage.com/1920x1080/000/fff">
</div>
.mainDiv {
  margin: 0 auto;
  width: 1042px;
  height: 768px;
  overflow: scroll;
}

.mainDiv img {
  width: 1920px;
  height: 1080px;
}
<div class="mainDiv">
  <img src="https://dummyimage.com/1920x1080/000/fff">
</div>

---EDIT---

I solve the problem with some code of javascript

        var el = document.querySelector(element);

        var newWidth = Math.ceil($(elementId).width()/2);
        var newHeight = Math.ceil($(elementId).height()/2);

        el.scrollLeft = newWidth;
        el.scrollTop = newHeight;
MajidRamzi
  • 438
  • 2
  • 4
  • 12
  • You should provide a live demo and what you done so far. – Pedram Oct 28 '17 at 07:01
  • Something like this --> https://jsfiddle.net/y02bL8m0/ ? – UncaughtTypeError Oct 28 '17 at 07:12
  • 1
    Possible duplicate of [How to scale image to fit the container?](https://stackoverflow.com/questions/30595321/how-to-scale-image-to-fit-the-container) – Alexander Oct 28 '17 at 07:13
  • no this question isn't my question. i want show big picture on smaller div and scroll div to show all part of picture. @Alexander – MajidRamzi Oct 28 '17 at 07:34
  • In this case, your question possible duplicate of [Aligning image to center inside a smaller div](https://stackoverflow.com/q/8149747/7914637) – Alexander Oct 28 '17 at 07:52
  • believe me , this is't my question. @Alexander – MajidRamzi Oct 28 '17 at 07:58
  • You will have to do this with javascript, as far as I am aware, there is *no way* to achieve this with css only. See the following examples which demonstrate **jQuery** *and* **vanilla javascript** solutions, adjust values accordingly to suit your requirements on a case-per-case basis where necessary. **CodePen:** https://codepen.io/anon/pen/pdoeWp | **JSFiddle:** https://jsfiddle.net/vr6dkf24/1/ – UncaughtTypeError Oct 28 '17 at 08:24
  • I solve my problem with element.scrollLeft and element.scrollTop javascript code. thanks all. – MajidRamzi Oct 28 '17 at 08:29

4 Answers4

0

May this be of help.

.container {
  position: relative;
  width: 200px;
  height: 200px;
  overflow: scroll;
}

.my-image {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div class="container">
  <img class="my-image" src="http://www.placehold.it/400x400" alt="">
</div>
Adriano
  • 3,788
  • 5
  • 32
  • 53
0

All you would need to do is tell the image that it can not be larger than the container div

<div id="div-one">
  <img src="http://lorempixel.com/400/400" alt="">
</div>

#div-one {max-width: 200px;}
#div-one img {max-width: 100%}

alternatively as a background image you could do it like this

<div id="div-one">
  <p>some text</p>
</div>

#div-one {
  max-width: 200px;
  height: 200px;
  background: url("http://lorempixel.com/400/400");
  background-position: center;
  background-size: cover;
}
0

Simple solution

div{display:flex;align-items:center;}
img{
margin: auto;
 object-fit: cover;
 display: block;
 overflow:hidden}
<div style="width:1024px;height:768px;border:1px solid blue">

<img src="https://dummyimage.com/1920x1080/000/fff">
</div>
Znaneswar
  • 3,329
  • 2
  • 16
  • 24
0

Your Css Should look like this the container class(.container) has the fixed width and height with an overflow on x and on y. To Center add a class(.centerDivWrapper) .

.centerDivWrapper{
width:1024px;
height:768px;
margin-left:auto;
margin-right:auto;
}
.container{
width:1024px;
height:768px;
float:left;
position:absolute;
overflow-x:auto;
overflow-y:auto;
}
.imgclass{
height:auto;
float:left; 
}

Your HTML should be something like this. to center the div add a wrapper(centerDivWrapper).

<div class="centerDivWrapper">
<div class="container">
<img class="imgclass" alt="YourAltSrc" src="YourImageSource">
</div></div>
Jonny
  • 1,319
  • 1
  • 14
  • 26