Align the image and text vertically within the <ul> tag

I'm trying to center-align the items in the navbar.

https://i.stack.imgur.com/FmDYS.png

Although the image looks centered, I believe it's because its size is stretching the navbar. How can I achieve this without changing the size of the picture?

I attempted setting max-height, but it doesn't seem to make any difference.

Jsfiddle link

  .hoyre{
    background-color:#f19f00;
    
    }
    .hoyre:hover{
    background-color:#d98500;
    }
    header{
        background-color: white;
    }
    .logo{
        width: 57px;
        height: 34px;
        padding: 0;
    }
    
    ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #fff;
    text-transform: uppercase;
    width: 100%;
    color: black;
    }

   li {
        float: left;
    }

    li a {
        display: block;
        text-align: center;
        padding: 20px 20px;
        text-decoration: none;
         color: black;
    }
    li a:hover {
        color: #f19f00;
    }
    body{
        margin:0;
        font-family: 'Roboto', sans-serif;
        background-color: #333;
    }
    .container{
        max-width:1300px;
        margin-left:auto;
        margin-right:auto;
    }
    .active {
    position: relative;
    }
</style>
  <body>
    <header>
        <div class="container">
        <ul>
          <li><a href="#"><img src="http://i.imgur.com/QAEzQxp.png" class="logo"></a></li>
          <li><a href="#news" class="active">Home</a></li>
          <li><a href="#contact">Contact</a></li>
          <li><a href="#about">About</a></li>
          <li class="hoyre" style="float:right;"><a href="#about">Donate</a></li>
        </ul>
        </div>
    </header>
  </body>
  

Answer №1

A great way to style this layout is by using the flex property on the ul, allowing you to easily align the children vertically with align-items. Adding a margin-left: auto to the last link will shift it to the right, and you can apply the background color directly to the link itself to prevent it from affecting the parent's height.

.hoyre {
    margin-left: auto;
        background-color: #f19f00;
        min-height: 100%;
        align-self: stretch;
        display: flex;
        align-items: center;
      }

      .hoyre:hover {
        background-color: #d98500;
      }

      header {
        background-color: white;
      }

      .logo {
        width: 57px;
        height: 34px;
        padding: 0;
      }

      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #fff;
        text-transform: uppercase;
        width: 100%;
        color: black;
        display: flex;
        align-items: center;
      }

      li {}

      li a {
        display: block;
        text-align: center;
        padding: 20px 20px;
        text-decoration: none;
        color: black;
      }

      li a:hover {
        color: #f19f00;
      }

      body {
        margin: 0;
        font-family: 'Roboto', sans-serif;
        background-color: #333;
      }

      .container {
        max-width: 1300px;
        margin-left: auto;
        margin-right: auto;
      }

      .active {
        position: relative;
      }
      
    </style>
<body>
      <header>
        <div class="container">
          <ul>
            <li>
              <a href="#"><img src="http://i.imgur.com/QAEzQxp.png" class="logo"></a>
            </li>
            <li><a href="#news" class="active">Home</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#about">About</a></li>
            <li class="hoyre"><a href="#about">Donate</a></li>
          </ul>
        </div>
      </header>
    </body>

Answer №2

In order to utilize height:100% in your CSS, it is important to first define the height of the parent element. Afterwards, you can adjust the li element to act like a table and occupy 100% of the height. Here are the updated styles with comments indicating the changes:

li {
float: left;
height: 100%;  /*fill height */
display: table; /* behave as table*/
}

li a {
display: table-cell; /* behave as table-cell allowing for vertical alignment */
vertical-align: middle;
text-align: center;
padding: 20px 20px;
text-decoration: none;
color: black;
height: 100% ; /*fill height */
}

ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #fff;
text-transform: uppercase;
width: 100%;
color: black;
height: 80px; /*define height in order to use height:100% for child elements */
}

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

Show the div in the right position for both desktop and mobile screens, utilize an accordion or consider shifting the div

My webpage is designed using bootstrap 4 and showcases images of our team members along with accordion functionality to display bios upon clicking the image. On a desktop screen, everything functions smoothly with four images of team members displayed in ...

The dropdown menu with Bootstrap's <li> sub menu is not expanding as expected

<li class="dropdown"> <a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">Utilitas<span class="c ...

Interactive Dropdown-Buttons dynamically inserted

I have been utilizing a third-party library called Materializecss from this link: My issue arises when attempting to create a Dropdown feature upon clicking a button. It works perfectly fine when I manually place the button in the HTML and click on it, tr ...

Tips for identifying if the cursor is hovering over the :before or :after section of an element

One of the challenges I am facing involves CSS and defining drop areas for users to interact with, allowing them to either drop a section before or after existing sections. .section:before, .section:after { content: "[insert here]"; height: 64px; ...

Automatically populate fields when the selection changes

Currently, I am facing an issue with a mysql table named clients that I utilize to populate the options of a select in one of the rows. The goal is to automatically fill out 2 input fields with client information when a specific option is selected. To acc ...

The functionality of Jquery and JS lies in working on DOM elements as opposed to

Just to start off, I want to mention that my knowledge of JavaScript and specifically jQuery is quite limited. I've encountered an issue with some JavaScript and jQuery loading on my webpage. 1) I have written some code on JSFiddle http://jsfiddle.n ...

Adjust the message hues upon form submission with Ajax

Can anyone provide assistance? I am in need of a functional form for sending emails. <form enctype="multipart/form-data" role="form" id="form" method="post" action="handler.php"> <div class="form-row"> <div class="form-g ...

Guide to generating a dropdown menu and linking it with data received from a server

I am completely new to Angular and recently received a project involving it. My task is to create a nested dropdown list for Json data retrieved from a server using Rest Api calls. The data contains a Level attribute that indicates the hierarchy level of ...

Positioning the icon within the input field

I am facing a couple of challenges. I have chosen to utilize focus and blur for text values in input fields instead of placeholder text due to an issue in Chrome where the placeholder text is centered. My goal is to position the icon in the input field, p ...

Guide to designing a bar graph using HTML5 for a web app optimized for iPad

I am in the process of converting my iOS native app for iPad to a web app using HTML5. However, I am encountering an issue with creating a bar graph for the HTML5 app. Is there any documentation or built-in API available for this specific task? On iOS, we ...

Javascript's second element does not trigger a click event with similar behavior

I'm currently facing an issue with displaying and hiding notification elements based on user interaction. My goal is to have multiple popup elements appear when the page loads. Then, when a user clicks the ".alert-close" element within one of the popu ...

Implementing jQuery to showcase an element on the screen

I would like to express my gratitude to everyone who has helped me. Please forgive my poor English :) Can someone provide me with a jQuery script that can extract the name of the currently selected tab and display it in an h1 tag? Whenever the selected ta ...

Incorporate a caret into an element using CSS styling

issue I encountered an issue where <textarea> and Javascript had some quirks when trying to transfer the value into a <pre> tag. My aim was to implement a blinking caret in the <pre> as if I were pressing F7. The reason behind not using ...

Utilizing REACT to dynamically load multiple HTML elements

Recently, I began developing with React and wanted to load multiple new Input Fields and Labels in a Form using Hooks. However, when clicking the button, only one input field is created using the last value of my array. Upon checking the console, I notic ...

Arranging expandable divs horizontally

Looking for a way to align these blocks so they can expand while remaining in line, I'm struggling with maintaining their individual spaces. The layout I have in mind is as follows: In this setup, box 2 and 3 should automatically adjust to fill the a ...

What could be causing issues with angularjs ng-include not functioning properly in Chrome at times?

<html ng-app="myApp1"> <head> <title>NG-Include</title> <script src="angular.js"></script> <script src="filter.js"></script> </head> <body> <div ng-controller="myController"> ...

"Utilizing Bootstrap 4: Wide text causes columns to stack on top of

My website has a layout with three columns: col-auto on the left, col in the middle, and col-auto on the right. The col-auto columns display skyscraper ads (600x160) while the col column contains text. However, when the text inside the col column is wide, ...

Easiest method to change cursor to 'wait' and then return all elements to their original state

On my website, there are certain CSS classes that define a specific cursor style when hovered over. I am trying to implement a feature where the cursor changes to a "wait" image whenever an AJAX call is initiated on any part of the page, and then reverts b ...

identifying the element targeted on dynamically created links with the help of jquery

I have created dynamic links where clicking on a country link displays the cities of that particular country. I am trying to retrieve the event.target.id of the dynamically generated city link when it is clicked. $(".countryName").children().on('cl ...

Error displayed inline

I am facing an issue with a hidden textbox that I need to make visible based on a certain condition. Despite checking that the control is being triggered by the change event, I am unable to get it to display. I have experimented with different methods with ...