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

Tips for creating sliding header content alongside the header

Is it possible for the content in the header to scroll up, displaying only the navigation list and hiding the logo when a user scrolls on the website? Currently, while the header background slides up, the content within the header remains in place. I feel ...

What could be causing PostgreSQL to remove HTML entities when using ts_headline()?

In the process of developing a prototype for a full-text search capability, I am working on returning the "headlines" of found documents in the search results. Here is an adapted example from the Postgres documentation: SELECT ts_headline('english&ap ...

Resolving Anchor Problems in PHP

I'm struggling with a line of code and need some help: echo "<td>"<a href="userdetails.php?".$row[username].">View Details</a></td>"; When I try to create the link on a page, I want it to be in the format userdetails.php?USER ...

What is the process for modifying the path and polyline in an SVG file?

I have a pair of SVG icons that I am working with. Recent Update: Icon One Icon Two One question that I have is whether it is possible to create both icons using the same HTML markup? For instance, can I use something like: <g> <circle ... ...

Unable to reset iframe style height in IE8 using JavaScript

I am encountering an issue with resetting the height of an iframe using JavaScript. Here is the code snippet I am working with: var urlpxExt = document.getElementById('urlPx'); urlpxExt.style.height = "200px"; While this approach works well in m ...

The animation of the splash screen in Angular is quite jarring and lacks fluidity

I am experiencing some issues with my angular splash screen animation. It works perfectly when there is no activity in the background, but when I simulate a real-life application scenario, the animation becomes stuttered, choppy, or sometimes does not anim ...

What steps should I take to effectively configure the header cache on my website?

After navigating through various links and attempting different solutions such as adding <meta HTTP-EQUIV="EXPIRES" CONTENT="Thu, 31 Dec 2015 12:00:00 GMT"> to my header without success, I am struggling to find a comprehensive guide on how and wher ...

Managing Positioning of PHP Print Statement

Recently, I developed a PHP script for my site that facilitates user registration and login specifically for membership purposes. If a user successfully logs in, the script redirects them to a landing page for members. However, if the login fails, the scri ...

Modify the c3 axis labels using CSS

I've been using the c3 package in R, which serves as a wrapper for the C3 javascript charting library created by Masayuki Tanaka. One issue I encountered is that my c3 chart was cutting off the last date on the x-axis. After inspecting it in Chrome I ...

The variable "tankperson" is not recognized

While attempting to run an AJAX request upon page load, I encountered a particular error even though I have ensured that all the necessary libraries are included. The variable tankperson is derived from $_GET['name'] An unhandled ReferenceErr ...

Understanding the Intrinsic Dimensions of Replaced Elements in CSS

I'm currently facing some challenges with the CSS Intrinsic & Extrinsic Sizing Module Level 3, specifically chapter 4.1 on Intrinsic Sizes. This section is proving to be quite difficult for me: When a block-level or inline-level replaced element h ...

Tips for building and implementing Angular URL Parameters for URLs in the form: "component/#/?id=..."

I am currently facing a situation where I have an application with an existing user base. I am looking to avoid disrupting their current links for a smoother transition. However, the previous links are in this format: (server)/viewer/#/?id=12. Please see t ...

Jquery plugin experiencing a malfunction

I am encountering an issue with my custom plugin as I am relatively new to this. My goal is to modify the properties of div elements on a webpage. Here is the JavaScript code I am using: (function($) { $.fn.changeDiv = function( options ) { var sett ...

Insert a fresh item into the existing unordered list

Every time I enter something in the prompt, it shows up as "undefined". What I actually want is for whatever I type into the prompt to be added as a new list item. For instance, if I type "Food" in the prompt, I expect to see "Food" appear on the second li ...

Guide on how to add multiple options to a select element in HTML using prototype.js

What is the best way to add multiple options to a select tag in HTML using prototype js? Appreciate your help. ...

Partial button clickability issue

There is a puzzling error where the button in the 'red area' is not clickable, and I can't seem to figure out why. Below are images illustrating the issue: https://i.stack.imgur.com/aPfcR.png https://i.stack.imgur.com/v5knZ.png Upon reac ...

Position the final list item in the column on the left side

Currently, I am facing an issue with creating spacing between li elements in a column layout. The problem arises when the last li in the column does not align to the far left edge of the column width. Take a look at the screenshot below for reference: Bel ...

Customize the background color of a popup modal except for one element

After much searching without finding a solution, I am reaching out here for help. Whenever I click on a Bootstrap modal, a popup appears with a background color and CSS applied. My issue is that I want to prevent the Bootstrap modal from applying the backg ...

Restrict the number of rows in a CSS grid

Hello there, I am in the process of creating an image gallery using CSS grid. Specifically, my goal is to initially show just one row of images and then allow users to click a "Show more" button to reveal additional images. You can see my progress here: ...

Transforming the MUI CircularProgress into a half circle shape

After utilizing CirculaProgress, I was able to achieve the following: Is there a simple method to transform it into a semicircle like shown here? ...