Positioning the left-right arrow buttons for a carousel I have a carousel (Owl Carousel) with vertically centered controls. Because of the structure, I have to absolutely position the previous and next arrow. Because the page is responsive, their position is dynamic. The size of the controls may also change.



I've written a function that runs on load and resize. It gets the height of the image and the height of the controls, subtracts the latter from the former, divides by two, and then uses that number as the controls' margin-top.

It works, but I'm questioning if I'm getting and using all the variables correctly. Does JavaScript read in order? Where it runs the first line, then the next, then the next... I'm strong in CSS but JS has always been a crutch.

Can I write this more efficiently?



function centerCarouselControls() {
  var carouselImage = $('.carousel-card &gt; img');
  var carouselControls = $('.owl-nav &gt; div');
  var carouselHeight = carouselImage.outerHeight();
  var controlHeight = carouselControls.outerHeight();
  var controlMargin = (carouselHeight - controlHeight) / 2;
  carouselControls.css('margin-top', controlMargin);
}

$('.carousel-card &gt; img').load(centerCarouselControls);
$(window).on('resize', centerCarouselControls + 'px');
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
Is it not possible to wrap both the 'arrow' and the image inside a relative div then do some absolute position and stuff?

jsfiddle so you can change the height to see it change without javascript: https://jsfiddle.net/3bm2hp5f/1/

Code:



html, body {
  height: 100%;
  width: 100%
}
&lt;div id="resizable" style="width: 30%; height: 50%; resize: both; position: relative; display:inline-block; border: 3px solid black"&gt;
  &lt;div style="position: absolute; top: 50%; transform: translateY(-50%);"&gt;
    &amp;lt;
  &lt;/div&gt;
  &lt;image style="width: 100%; height: 100%;"&gt;&lt;/image&gt;
&lt;/div&gt;
&lt;div style="display: inline-block"&gt;
 something else
&lt;/div&gt;