Creating a Navigation Bar with HTML and CSS: Designing Image Alignment and Size

Take a look at the following code snippet in HTML and CSS:

html {
  background-color: green;
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
}

body {
  margin: 0;
}

nav#main-nav {
  background-color: white;
  height: 50px;
  display: flex;
}

#container-logo {
  width: 200px;
  height: 100%;
  flex-shrink: 0;
}
    
@media (max-device-width: 640px),
(max-width: 640px) {
  #container-logo {
    width: 60px;
  }
}

#container-logo a {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#container-logo img {
  content: url("http://www.dummymag.com//media/img/dummy-logo.png");
  max-width: 90%;
  max-height: 40px;
}
    
@media (max-device-width: 640px),
(max-width: 640px) {
  #container-logo img {
    content: url("http://coachmikelee.com/wp-content/uploads/2014/11/dummy-logo.png");
  }
}

#container-searchbar {
  height: 100%;
  width: 100%;
  background-color: orange;
}

#container-loginstate {
  width: 200px;
  height: 100%;
  flex-shrink: 0;
  background-color: blue;
}

@media (max-device-width: 640px),
(max-width: 640px) {
  #container-loginstate {
    width: auto;
    min-width: 50px;
  }
}
<!DOCTYPE html>
<html>

<head>
  <link type="text/css" rel="stylesheet" href="template.css">
</head>

<body>

  <nav id="main-nav">
    <div id="container-logo">
      <a href="template.php" title="RioAulas Home"><img></a>
    </div>

    <div id="container-searchbar"></div>

    <div id="container-loginstate"></div>
  </nav>
</body>

</html>

This setup represents a Responsive Top Horizontal Navigation Bar template with 3 distinct sections:

  • left-aligned logo area (white)
  • centered search bar section (orange)
  • right-sided login state part (blue)

While the code functions as intended, it appears that there are several complex CSS adjustments to properly handle the logo image (such as using anchor tag as flexbox with specific width and height).
Is there an alternative method for achieving this design?

Answer №1

I share your sentiment, it appears to be somewhat over-the-top. I implemented your solution using inline-block and floats, but encountered an issue with the width of the container-searchbar.

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

Enhance a disabled button by incorporating a ripple effect

I've got a button that toggles on and off depending on the required form fields. When it's enabled, clicking it activates a ripple effect. Now I'd like to have the ripple effect even when the button is disabled. I attempted something simila ...

When a table's row is clicked, it will expand to reveal a secondary table

I am facing an issue with rendering a table using v-for in Vue. I want to display a subtable or more information section below a specific row when it is clicked on. My attempts to add a new tr within the existing tr have resulted in errors or the new row ...

Object C UIWebView adapting to HTML

How can I get a responsive HTML page to display correctly in a UIWebView in Xcode? The page adjusts properly in other iPhone browsers, but not in the UIWebView. I would prefer not to use PhoneGap for this. Any suggestions for making the HTML responsive i ...

Tailored design - Personalize interlocking elements

I am currently working on a custom theme and I am trying to adjust the font size of Menu items. In order to achieve this, I have identified the following elements in the tree: ul (MuiMenu-list) MuiListItem-root MuiListItemText-root If I want to modify th ...

Safari is currently experiencing compatibility issues with iPad, resulting in a malfunction

I encountered a unique issue with my most recent website project. The website is built on WordPress, and the custom theme I created is presenting an unexpected problem: Despite functioning perfectly on all browsers and devices, the site fails to display p ...

Using a declared const in a separate React file

I've been searching for a solution, but haven't found anything that helps. I'm trying to retrieve data from an API and pass it to another page. The information is currently defined as "drink.strDrink", including the name and image. However ...

How to refresh the page when pausing the YouTube iframe API

I'm encountering an issue with my code where I am trying to refresh the page when exiting or pausing full screen mode. Exiting full screen is working as expected, however, pausing using the YouTube iframe API's "onstatechange" event does not seem ...

What is the process for detaching and attaching click animations using the on() method?

I am encountering an issue with a recursive loop that executes a simple animation. These animations are controlled by the page load and clicking on controls .carousel_item. Click here for live sample JSFiddles for demonstration purposes Challenge: The pr ...

Ways to decrease the height of the navigation bar in Bootstrap version 4.1

Struggling to figure out how to decrease the height of the navbar in bootstrap v4.1. I want it to be specifically 24px tall; Thanks, Richard. ...

List of radio buttons placed vertically

The documentation for Material Design Lite only shows examples of horizontal radio buttons, which may not be sufficient if you need to create a mobile-friendly version with longer label text. If you're looking for a solution, here's a link to a ...

Tips on using css filters on a fixed background image

I am currently working on a website that features a fixed background image with scrollable elements on top. I am facing an issue where applying a grayscale filter to the image also affects the text within the container, turning it gray as well. However, I ...

JQuery .hover triggering unexpectedly without actually hovering

My current code is causing $('#intro').fadeOut to activate even when I am not hovering on $('arena').hover. Are there any other issues in the code that would prevent it from working correctly? $(document).ready(function() { $(&apos ...

Material UI does not have built-in functionality for displaying colored text within a multiline textfield component in React

Attempting to utilize the material ui library in a react app has brought an issue to light. It appears that styling colored text does not work as expected for multiline textfields. Consider the following scenario: import React, { Component } from 'r ...

Position divs to be perfectly aligned and centered

I'm in the process of creating a webpage and facing challenges with aligning my divs properly, specifically the animated company logos. I want them to be positioned side by side rather than on top of each other, with space in between to fit the layou ...

What is the best way to transform the data received from this function into a DataTable that can be used with the Google Charts API?

Is there a way to easily convert the data returned by this function into a format that Google Charts API can read? public function ajax_get_dates(){ $data = ([['date1' => '04/08/2016'], ['date2' => '05/08/2016& ...

Analyzing user input in PHP against a mysqli database

I'm currently working on an online quiz application. I have everything in place with the form, and it's successfully sending emails to the administrator. However, there's a specific functionality that I want to implement: I need the script ...

`Is it possible to remove an empty frame using javascript?`

I have this script that generates a "layer" resembling a frame and I need to remove it. Here is the code for creating the layer: function disableLayer() { var layer = document.getElementsByTagName('div')[0]; d = document.createElement(& ...

Unable to retrieve element by ID from an external source

I've been attempting to conceal a div after submitting a form, but unfortunately, I have not been successful. I am curious about the process of creating a div when utilizing an external JavaScript file. Here is the code to consider: HTML: <!DOCTY ...

Adding an extra menu next to a select option in a React component

I recently came across a fantastic select library for React called react-select. However, I noticed that it does not support nested select options. Therefore, I am curious to learn how to add an additional menu next to a select option, similar to a bookm ...

What is the best way to arrange multiple elements on the second row within a div?

As I attempt to utilize CSS for positioning elements within a div, my challenge lies in the limitation of only being able to use classes/ids due to the fact that the div and its content are generated from a third-party plug-in (datatables). This restricts ...