Center Vertically Using CSS Flexbox

Hey there, I'm currently facing an issue with aligning my website's logo vertically in the middle with my navigation link list. I have attempted to use "vertical-align: middle" on my div columns but it doesn't seem to be working. Right now, both my logo and navigation links appear a bit out of place and floating around. Any suggestions or advice on how to fix this would be greatly appreciated.

HTML:

   <div class="sixteen columns clearfix">
    <div class="five columns">
      <a href="{{ shop.url }}" title="{{ shop.name }}">
      {% if settings.use_logo %}
        <img src="{{ 'logo.png' | asset_url }}" alt="{{ shop.name }}" />
      {% else %}
        {{ shop.name }}
      {% endif %}
    </a>
      </div>
    <div class="eleven columns">
    <div id="mobile_nav"></div>
    <div id="nav">
      <ul id="menu">
        {% for link in linklists[settings.main_linklist].links %}
          {% if linklists[link.handle] == empty %}
            <li><a href="{{ link.url }}" title="{{ link.title }}" {% if link.active %}class="active"{% endif %}>{{ link.title }}</a></li>
          {% else %}
            <li><a href="{{ link.url }}" title="{{ link.title }}" {% if link.active %}class="active"{% endif %} {% for link in linklists[link.handle].links %}{% if link.active %}class="active"{% endif %}{% endfor %}>{{ link.title }} 
              <span class="arrow">&or;</span></a> 
              <ul>
                {% for link in linklists[link.handle].links %}
                  {% if linklists[link.handle] == empty %}
                    <li><a href="{{ link.url }}" title="{{ link.title }}">{{ link.title }}</a></li>
                  {% else %}
                  <li><a href="{{ link.url }}" title="{{ link.title }}">{{ link.title }} <span class="arrow"> &gt;</span></a> 
                    <ul>
                      {% for link in linklists[link.handle].links %}                        
                        <li><a href="{{ link.url }}" title="{{ link.title }}">{{ link.title }}</a></li>
                      {% endfor %}
                    </ul>
                  </li>
                  {% endif %}
                {% endfor %}
              </ul>
            </li>
          {% endif %}
        {% endfor %}
      </ul>
    </div>
     </div>
  </div>

Column CSS:

  .column, .columns { 
float: left; 
display: inline; 
margin-left: 10px; 
margin-right: 10px;
vertical-align: middle;
}

This is the current appearance:

Looking to center it like this:

Answer №1

  1. To center an item vertically within a parent container, define the parent container with either position: relative or position: absolute.
  2. Set a fixed height for the child container.
  3. Apply position: absolute and top: 50% to the child container to align its top to the middle of the parent.
  4. Use margin-top: -yy where yy is half the height of the child container to adjust the position by moving it up.

()

  • Either set .columns to position: relative or position: absolute
  • Assign a specific height value to .column
  • Use absolute positioning for .column with position: absolute, and specify top: 50%
  • Give .column a negative top margin equivalent to half of its height value (margin-top: -yy where yy represents half of .column's height)

Answer №2

If you've already utilized the style vertical-align: middle within your class, simply incorporating the property display: table-cell should resolve your issue.

Please keep in mind: There are multiple methods for vertically aligning content or div elements, such as utilizing line-height (for only the first line), padding-top, table-cell, and applying absolute positioning.

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

JSON.stringify does not transform arrays into strings

It seems like I may have approached this task incorrectly, perhaps due to mishandling recursion. I'm unsure of where the mistake lies. You can view the example here. Below is the JavaScript code snippet - function propertyTest(currentObject, key) { ...

Emulating an Ajax request

I am struggling with incorporating a button that has three states - initial, loading, and success/failure - into my code. Specifically, I want the button to display a "tick" symbol and a success message after a timed-out ajax call. The HTML code below sho ...

Ways to cease a setInterval after a specified duration or a specific number of iterations

I have successfully implemented a jQuery code for creating a loop of "changing words" by referring to the solution provided in this Stack Overflow answer: jQuery: Find word and change every few seconds My question is, how can I stop this loop after a cert ...

Tips on creating a unique d3js tree design

I am a beginner when it comes to d3js and javascript in general. My goal is to create an interactive IP administration overview using d3js by modeling json data. I know that the key tool for this job is likely d3.layout.tree, which will provide me with the ...

Ensure that the children elements can be resized without exceeding the boundaries

My goal is to ensure that the resizable elements stay within their parent element. What I tried: I have set the minHeight : property and while resizing, I am using Math.max to limit the height to 30px. Expected Result: All resizable child elements sh ...

Tips on incorporating a repeating gradient instead of a linear gradient

I have the following code for a linear-gradient. Click here to view the code Is there a way to modify it to use a repeating gradient instead? The goal is to maintain the same image but with a repeating gradient effect. background-image: repeating-linear ...

Is it acceptable to employ async: false when requesting small amounts of data?

I am curious about the best practices for retrieving small data from the server. One example is using an ajax (or sjax) call to check for new notifications for a user. function checkNewNotifs() { $.ajax({ url: '/Home/CheckNewN ...

Return empty parameters for nested routes efficiently

Here is an example of my situation: localhost:8000/api/news?category=tech At the moment, I have a router set up for the /api section and another one specifically for /api/news. However, when I attempt to display req.params in the /api/news router, it doe ...

Move your cursor over one container to see the impact on another

My goal is to create a hover effect on a box that triggers another div with easing effects. Below, you'll see that the .images{} class has a 0s infinite scroll initially, but when the .box:hover > .images{} selector is activated, it changes to a 10 ...

The POST request made using Postman shows an empty Node.js/Express req.body

Here is the current code snippet I am working with: var express = require('express'); var router = express.Router(); var db = require('../helpers/db'); var data = { "1": 127, "2": 236, "3": 348 } router.get('/', ...

Issue with initial width calculation of Slick slider

Having issues with implementing the Slick slider. The calculated width of each slide seems to be incorrect due to padding on the right side of the image. <div class="responsive-slick"> <img src="Gallery/large/01.jpeg"> <img src="Gal ...

Cypress Error: Unable to locate module: Error - Unable to find 'cypress/types/lodash'

I am currently conducting my inaugural test in Cypress directly from the Visual Studio IDE. Everything was running smoothly until I suddenly encountered the error below, without making any changes or updates whatsoever. My Cypress (version 7.0.0) was inst ...

What is the reason for needing a page reload in Javascript or JQuery?

I'm curious why Javascript or jQuery require a page reload before applying certain effects. CSS updates in real-time, as demonstrated by the following example: This changes dynamically without needing to refresh the page @media all and (max-width:7 ...

Is there a way to logout when the select event occurs using the key?

I am trying to figure out how to log out the key value when the select event is triggered. Specifically, I want to access the key={localState.id} within the <select></select> element in my HTML. This key value is crucial for a conditional stat ...

jQuery UI dynamically adjusting content layout based on browser size changes

Having an issue with my custom accordion script where resizing the browser causes formatting problems in one of the sections. I want the content to remain intact and to utilize a scrollbar if necessary to view the content. The problem is visible in section ...

Received a 404 error when executing a Fetch POST request to /send endpoint, even though there was no error reported

Whenever I attempt to upload data to the school database upon clicking a submit button on a form, I encounter a challenge. The server.js file has been configured and confirmed functional with Postman. But when attempting to POST the data on button click, a ...

Transfer the information from dropped files in a div to a separate page

document.getElementById('drag_drop').ondrop = function(event) { event.preventDefault(); var form_data = new FormData(); var drop_files = event.dataTransfer.files; for(var count = 0; count < drop_files.length; count++) ...

The search function in Typeahead is not activating the API request while typing

Having some trouble implementing a typeahead search feature in my nodejs application with mysql. I can't seem to figure out what's wrong with my code. When I manually access http://localhost:5000/search?key=s, I'm able to see the results an ...

Email header image alignment issue in Thunderbird

I recently created an HTML email using a template and it looks great on most platforms, but not in Thunderbird. The header image is not aligned properly above the content as shown in the screenshot. Does anyone know how to troubleshoot this issue or have a ...

how to attach a function to a dynamically generated element

Currently, I am in the process of developing a placeholder-enabling feature. var t=document.createElement("input"); "placeholder" in t||$("input").each(function(){ if("submit"!==$(this).attr("type")){ var n=$(this),l=n.attr("placeholder"); ...