Adjust the size of an image to fit a flexbox container using CSS

Check out this codepen where I have created a flexbox navbar as shown below:

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  background-color: #f9fafb;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
  line-height: 1.5;
}

nav {
  background-color: #fff;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
  box-shadow: 0px 0px 10px 10px #ddd;
}

#items {
  display: flex;
  flex: 1 0 auto;
  justify-content: space-evenly;
  list-style-type: none;
  padding: 32px;
}

#items>li {
  display: flex;
  justify-content: center;
  align-items: center;
}
<nav>
  <ul id='items'>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
  </ul>
</nav>

Note that the height of this navigation bar is set to 88px.
When an image is added as a child element within the nav, it increases the overall height of the parent container (the navbar) by 4px (new height: 92px).

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  background-color: #f9fafb;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
  line-height: 1.5;
}

nav {
  background-color: #fff;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  width: 100%;
  display: flex;
  justify-content: space-between;
  box-shadow: 0px 0px 10px 10px #ddd;
}

#logo {
  display: flex;
  flex: none;
  justify-content: center;
}

#logo>img {
  height: 100%;
  width: auto;
  object-fit: contain;
}

#items {
  display: flex;
  flex: 1 0 auto;
  justify-content: space-evenly;
  list-style-type: none;
  padding: 32px;
}

#items>li {
  display: flex;
  justify-content: center;
  align-items: center;
}
<nav>
  <div id="logo">
    <img src="https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg" alt="Example logo" />
  </div>
  <ul id='items'>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
  </ul>
</nav>

Is there a way to make sure the image (in this case, the Google logo) fits 100% of the available height while
a) preserving its aspect ratio
b) without increasing the height of the navbar to compensate?

Thank you!

Answer №1

Make these adjustments:

#logo > img {
height: 97%;
width: auto;
object-fit: contain;
margin:4px;

I'm not sure if this will impact the height of the navigation bar as I'm working on my phone. If it does, you can simply reduce the nav height to fit your desired 92px.

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

Combining the power of Javascript with Python's Flask framework

Currently, I am managing an HTML web server with CSS. My goal is to incorporate a digital clock onto my website, and I believe the script should be stored in a .js file for JavaScript purposes. However, when I attempt to include or run the script from my ...

Creating personalized dots in the owl carousel

I have successfully added custom previous and next buttons to my carousel using Owl Carousel, but I am having trouble displaying the navigation dots. Can anyone help me identify where I might have made a mistake? // owl.carousel.css .owl-controls { ...

Form not submitting when selecting option

<form action="action.php" method="post" id="map-form"> <select name="selectfrom[]" id="select-from1" size="8"> <option value="Email">Field Email</option> <option value="ID">Field ID</option> & ...

When I utilize position absolute, the elements start overlapping each other

I am currently working on displaying sidebar content within tabs, and I need to adjust the height dynamically based on the content. However, I encountered an issue where the elements above and below the tabs are overlapping. To address this problem, I remo ...

What is the best way to integrate multiple HTML static pages within an ASP.NET MVC project while utilizing the Layout page?

I am currently working on an MVC project that requires me to include 40 static pages. I would like these pages to utilize the Layout page. What would be the most efficient method to achieve this? I have searched for answers to this question before ...

What could be causing the bootstrap 4 col-md-3 block to shrink when using position fixed?

When working with Bootstrap 4, I encountered an issue where changing the block position from relative to fixed using a script caused the block to decrease in size. The script I used includes a .sticky class: .sticky { position: fixed !important; top: ...

Guide on generating a child element in a React application using server response

I have developed a react application with multiple nested routes. One of the nested routes utilizes a backend API that returns complete HTML content. My goal is to display this HTML content with the same styling in my UI without directly manipulating the ...

Proper formatting for setting text indentation in CSS

Issue: I am struggling with indenting text properly on responsive devices. While the desktop version looks fine, the text goes out of control on mobile views. Here is how it currently appears: https://i.sstatic.net/shW2P.png Below is an example after ap ...

Asynchronously load an AngularJS controller from AJAX without altering the route

I am looking to dynamically load an Angular controller after making an AJAX call that generates a new view in HTML. Here is the setup I currently have: Example of a View: HTML Snippet From AJAX <!-- CVS Pharmacy Extracare - Add View --> <d ...

Internet Explorer 8 is causing alignment problems with sidebars on certain websites within a Wordpress multisite setup

I am managing a Wordpress multisite setup with the following websites: The main portal: which connects to The issue arises on all pages of gprgebouw.nl and gpradvies.nl but not on the other domains. Attached is a screenshot of the homepage of gprgebouw. ...

The item starts blinking as it is being moved

Hey everyone, I'm facing an issue that's been bothering me. Whenever I try to drag an element, it flickers a lot and it's really annoying. I've been struggling to figure out what's causing this problem. Below is the code snippet: ...

Tips for adjusting the item count in mat-autocomplete

For quite some time now, I've been attempting to adjust the number of items in a mat-autocomplete without any luck. My project involves using material.angular.io Below is a snippet of my code import { Component, OnInit } from '@angular/core&a ...

The parent div's height is not compatible with the CSS grid

I created a CSS grid layout featuring a header, side menu, and scrollable content. My goal is to test this layout in a container div where I've specified the width and height. The layout adjusts well according to the container's width, but seem ...

Users are unable to access the most recent version of the CSS file

Running a website through Hostinger has presented a unique challenge - when I make changes to the CSS file, it updates for some users but not all. It seems that the changes are being cached for certain individuals. I attempted to rectify this issue by add ...

Combining Angular variables within HTML tags: A Guide to AngularJS

As a newcomer to AngularJS, I am truly impressed by its capabilities. One thing I am eager to learn is how to include an AngularJS binding within an HTML attribute, while also adding another string to it. I often use unique names like "thisform" and "thisd ...

JavaScript: Download a .PNG file from a URL

Once the user selects Accept from the confirm box that appears, an already established .png file will be downloaded. <script> if(confirm('Press accept to download the .png')){ //code to download the .png } </script> ...

Adjust the CSS of a different class when the :focus state is triggered

Imagine you have the following code snippet: HTML <div class="container"> <input class="myAwesomeInputBox"> </div> CSS .input [type=text]:focus > .//ANY CLASS SOMEWHERE ON THE WEBSITE{ //Some sweet CSS. } This code does not ...

What is the best way to distinguish between a button click and a li click within the same li element

In my angular and css GUI, each line is represented as an li inside a ul with a span. The functionality includes a pop-up opening when clicking on the li background and another action occurring when pressing the button (as intended). The issue arises when ...

The full height of the image cannot be captured by html2canvas

It baffles me that html2canvas is failing to capture the full height of the div. html2canvas($container, { height: $container.height(), onrendered: function(canvas) { var data = canvas.toDataURL('image/png'); ...

running a for loop for multiple onclick events

I have a situation where I need to create an onclick event that performs multiple tasks. There is a button labeled Add Item in the image below. When clicked, it should add the HTML code shown below: var addItemHTML = '<tr class="hold-item&quo ...