Achieving the perfect vertical alignment of navigation buttons alongside a responsive image

Is there a way to ensure that the button always vertically aligns in the middle of the image responsively? I've managed to make the image responsive using .img-responsive, but I'm having trouble keeping the arrow centered on the image at all times. It seems like the issue is related to not being able to match the height of the arrow's div with the height of the image. Any suggestions on how to achieve this?

Check out my jsFiddle for reference.

PS: If you have any suggestions for a better title, feel free to change it! ^^

Answer №1

Utilizing CSS exclusively, you can achieve the desired layout using a combination of display table and table-cell properties. Although I had never experimented with this technique prior to this project, a bit of research led me to a solution that gave me a solid starting point.

The key is to create a container element with the display table property. Within this container, all other elements should have the table-cell property to ensure they stack correctly beside each other, mimicking the behavior of table cells.

Setting the height of your table-cells to 100% allows them to adjust according to the wrapper's height. This enables you to utilize the vertical align property for precise centering of navigation buttons.

To ensure responsiveness, apply the max-width 100% property to images. Avoid using Bootstrap's image responsive class as it may include unnecessary CSS properties that disrupt the layout.

I made adjustments to the HTML structure to ensure proper alignment of each element in the intended sequence.

VIEW WORKING EXAMPLE JSFIDDLE

HTML:

<div class="row">
    <div class="col-xs-12">
        <div class="image-container">
            <div class="prev-btn nav-btn"> < </div>
            <div class="inner-container">
                <img src="http://farm9.staticflickr.com/8072/8346734966_f9cd7d0941_z.jpg" class="center-block">
            </div>
            <div class="next-btn nav-btn"> > </div>
        </div>
    </div>
</div>

CSS:

.image-container{
    display:table;
    height: 100%;
    text-align:center;
}

.inner-container{
    display:table-cell;
    width: 100%;
    height: 100%;
    padding: 0 15px;
    vertical-align: middle;
    text-align: center;
}

.inner-container img{
    width: 100%;
    height: auto;
    max-width: 100%;
}

.nav-btn{
    font-size:50px;
    font-weight:700;
    font-family: monospace;
    color: #000;
    cursor:pointer;
}

.nav-btn:hover{
    color: #B6B6B6;
}

.prev-btn{
    position: relative;
    height: 100%;
    display: table-cell;
    vertical-align: middle;
}

.next-btn{
    position: relative;
    height: 100%;
    display: table-cell;
    vertical-align: middle;
}

Answer №2

If you're looking for a Javascript/Jquery solution, here's a simple trick to position your navigation buttons according to the height of an image when the browser is resized. By finding the center of the image and adjusting the position of the buttons accordingly, you can ensure they are always aligned correctly.

$(function(){
    //Call On Resize event on load
    screenResize();
    //Bind On Resize event to window
    window.onresize = screenResize;
});

function screenResize() {
    //Adjust Nav buttons position according to image height
    $('.nav_btn').css({
        'top': (($('.center-block').height() / 2)-($('.nav_btn').height() / 2))
    });    
}

To improve the appearance of your buttons, consider changing the line-height with this CSS:

.nav_btn p{
    line-height: 1.25;
}

You can also use Media-Queries to adjust the font-size and line-height of the buttons as needed. And as suggested by user Valentin, using images for the navigation buttons could simplify the design process and eliminate the need for media queries.

For a live example, check out this JSFIDDLE

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to manage the size of Material-UI vertical tabs?

After receiving assistance from others on the thread about Creating a Material-UI tab with image tabs instead of text labels, I was able to successfully implement images for my Material-UI tabs. However, I am facing an issue where I am unable to control th ...

What is the best way to include a line break in a text sourced from a React state?

Here is a functional React code snippet: const about={ text1: 'Lorem, ipsum dolor sit amet consectetur adipisicing elit. Facere, qui?', text2: 'eligendi voluptates nesciunt quam dolor reiciendis dolorum voluptas? Labore, a pariatur?&apos ...

Struggling to align Social Media Share Buttons in inline format for a website

I'm having trouble aligning these social media share buttons with my inline list. I tried using vertical-align: top; on the <li> element, but it didn't work in Chrome. You can view the page here: Here is the full HTML/CSS code: <!DOCT ...

The solution to enabling multiple inputs when multiple buttons are chosen

Below is a link to my jsfiddle application: http://jsfiddle.net/ybZvv/5/ Upon opening the jsfiddle, you will notice a top control panel with "Answer" buttons. Additionally, there are letter buttons, as well as "True" and "False" buttons. The functionali ...

Load a distinct JSP file from the sidebar in JSP

I have an index.jsp file containing a sidebar: <div id="sidebar"> <li ><a href="create">Create</a></li> <li ><a href="view">View</a></li> <li ><a href="Delete">Delete</a></li> & ...

A Firefox Browser View of a Next.js and Tailwind Website magnified for closer inspection

After creating a website using Next.js and Tailwind CSS, I noticed that the site is appearing zoomed in when viewed in Firefox. However, it looks fine in all other browsers. When I adjust the screen to 80%, the website displays correctly in Firefox. What ...

Is there a way to include all images from a local/server directory into an array and then utilize that array variable in a different file?

I'm currently using Netbeans version 8.0.1 with PHP version 5.3 Here is a PHP file example: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/199 ...

Having trouble accessing the value of a node in JavaScript, as it keeps returning as "null"

Experimenting with converting HTML elements into lists. The objective is to generate a list of all values in a table upon clicking the "page next" buttons on it. Afterward, an alert should display the value of the first item in the list, which corresponds ...

Is there a way to turn off Emmet's CSS Abbreviations feature in Sublime Text 2?

One thing I really enjoy is how Emmet can generate HTML using 'CSS-like' strings. However, I prefer not to use their CSS Abbreviations. For example, when I write the following line of CSS: li a| I expect to get a tab when I press 'TAB&apos ...

Switch the hue when altered

My document contains various input elements such as text, radio buttons, and checkboxes. I want each of these elements to change color when a change is made. This is the method I am currently using: $("document").on('click', 'change', ...

Rails Polymer is experiencing a glitch where the CSS and Polymer elements are not displaying correctly until a page refresh is

Whenever I first load a page, my polymer elements (like paper-input) fail to load their CSS properly. Here is an example of how the page looks before and after refreshing: https://i.sstatic.net/vsIpE.pnghttps://i.sstatic.net/YAFjP.png Below is the code sn ...

Centered Navigation Bar Aligned with Header

I'm having trouble aligning my header vertically with the text in my nav-bar using Bootstrap 4.6. The nav-bar items are right-aligned, so the text isn't centered in the middle of the page like shown in picture 3. .jumbotron { background: #3 ...

Is it possible that images appear normal locally, but become stretched once deployed? (CSS/HTML only)

I designed a straightforward webpage using only HTML and CSS. Everything looks great when I view it on my local machine. However, once I deployed the site on Droppages, a static-page hosting service integrated with Dropbox, all my images became stretched ...

Is it possible to have images automatically resize to fit the content div without becoming distorted?

I have created a webpage that I really enjoy, but now I need to ensure it supports smaller resolutions like 1020X768. However, for those with larger monitors, I want to take advantage of the extra space by setting a minimum width that can grow with the scr ...

Locate a button element and dynamically assign an identifier using jQuery or JavaScript

I am currently working on a webpage that contains two forms, both enclosed within a <DL> element. <dl> <form action="" method="POST" style="display:inline-block;"> <dl><dt></dt><dd><input class="submit" typ ...

App script: Extracting HTML form data from a web application

I have been trying to work on a script that involves taking input from an HTML form and processing that data in order to populate a Google Sheet. Unfortunately, I haven't had much success with it so far. Here is the code I have been working on: Index ...

What is the best way to dynamically set an ID in Angular using the pound symbol (#)?

I am currently developing a dynamic Angular 6 application that utilizes dynamic components. My approach involves using @ViewChild('<id>', { read: ViewContainerRef }) <id>; to reference the divs where I intend to populate with dynamic ...

Setting up pagination for displaying data from a database on the client-side: a step-by-step guide

Greetings to all my fellow programmers on Stack Overflow! I am currently in the process of developing an e-commerce application using JSP. The products for the application are stored in a server-side database (Mysql) within a table named product. The prod ...

What is the best way to send a variable to a URL when a link is clicked?

Recently, I started learning PHP and ran into a challenge while working on a school project. The specific issue that I'm facing is how to pass variables to a URL when a link is clicked. Essentially, my problem involves a dropdown menu displaying diffe ...

"Enhabling tablesorter pagination to ensure that buttons always stay in sync with

I am experiencing an issue with the pagination buttons staying at the bottom of my page, even when there are only 2 entries left on the last page. Check out my table here: Is there a way to make the pagination buttons dynamically move to the top based on ...