Brad
Brad

Reputation: 59

How to float elements over an image on a responsive layout?

Here's the code:

The HTML:

<html>
    <head>
        <title>Sample Title</title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
        <meta name="viewport" content="width=device-width">
    </head>
    <body>
        <div id="container">
            <img id="image" src="http://blog.clove.co.uk/wp-content/uploads/2013/01/Motorola_Razr_HD_Cam_Sample%20(7).jpg">
            <img id="arrow_left" src="http://iconizer.net/files/WPZOOM_Developer_Icon_Set/orig/arrow-left.png">
            <img id="arrow_right" src="http://iconizer.net/files/WPZOOM_Developer_Icon_Set/orig/arrow-right.png">
        </div>
    </body>
</html>

The CSS:

#container {
    position: fixed;
    margin: auto;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 90%;
    height: 90%;
    background-color: silver;
    text-align: center;
}

#image {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    max-width: 100%;
    max-height: 100%;
}

#arrow_left {
    width: 50px;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 0;
    z-index: 1000;
}

#arrow_right {
    width: 50px;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 0;
    z-index: 1000;
}

What I'm trying to do is float arrow images to the left and right side of the image, halfway up the image. I don't want the arrows to be outside of the image.

You'll notice if you resize this layout to landscape, there will be a gap between the image and the outside border of the container on the left and right. The arrows float outside of the image. I need to stop this from happening.

--I updated the code, again. X)

Upvotes: 0

Views: 995

Answers (1)

Omer
Omer

Reputation: 1796

That can be achieved through adding a little jQuery if its acceptable :)

Here's the code:

HTML

<div id="container">
  <img id="image" src="http://blog.clove.co.uk/wp-content/uploads/2013/01/Motorola_Razr_HD_Cam_Sample%20(7).jpg">
  <div class="controls">
    <img id="arrow_left" src="http://iconizer.net/files/WPZOOM_Developer_Icon_Set/orig/arrow-left.png">
    <img id="arrow_right" src="http://iconizer.net/files/WPZOOM_Developer_Icon_Set/orig/arrow-right.png">
  </div>
</div>

CSS

#container {
  position: fixed;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 90%;
  height: 90%;
  background-color: silver;
  text-align: center;
}

#image {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  max-width: 100%;
  max-height: 100%;
}

.controls {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-top: -30px
}

#arrow_left {
  width: 50px;
  float: left;
}

#arrow_right {
  width: 50px;
  float: right;
}

JQUERY

var img_w = $('#image').width();
$('.controls').css({
  'width': img_w,
  'margin-left': '-' + (img_w / 2) + 'px'
});
$(window).resize(function() {
  var img_w = $('#image').width();
  $('.controls').css({
    'width': img_w,
    'margin-left': '-' + (img_w / 2) + 'px'
  });
});

JS Fiddle Link: https://jsfiddle.net/omerblink/d6q9uoe3/

Let me know if you need more help in comments...

Upvotes: 1

Related Questions