Achieving an element placement in a navigation bar with flexbox and grid layout | HTML and CSS exclusive techniques

Need help achieving the desired result using only flexbox and grid layout.

The goal is to place a search bar vertically centered and to the right of the navbar, while keeping the existing items on the left intact. Can you assist?

/* Reset */

* {
  margin: 0;
  padding: 0;
  border: 0;
  box-sizing: border-box;
  list-style: none;
  text-decoration: none;
}

/* General styles */

html {
  font-size: 100%;
  line-height: 1.5;
}

body {
  max-width: 1850px;
  font-family: "BenchNine", sans-serif;
}

/* Header */

.top-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  align-items: center;
}

.slogan {
  text-align: right;
  font-family: "Chela One", cursive;
  font-size: 4em;
  color: #527bea;
}

/* Navbar */

nav {
  background-color: #ae2123;
}

ul {
  display: flex;
  justify-content: left;
}

a {
  display: block;
  padding: 15px 25px;
  text-align: center;
  color: #fff;
}

a:hover {
  color: #999;
}

input {
  padding: 5px 20px;
  border-radius: 20px;
}
<header>
    <div class="top-container">
      <div class="logo-box">
        <img class="logo" src="/img/logo.png" alt="logo" />
      </div>
      <div class="slogan-box">
        <h1 class="slogan">La passion des films</h1>
      </div>
    </div>
    <nav>
      <ul>
        <li>
          <a href="#">Programmes</a>
        </li>
        <li>
          <a href="#">Actualités</a>
        </li>
        <li>
          <a href="#">Jeune Public</a>
        </li>
        <li>
          <a href="#">Tarifs</a>
        </li>
        <li>
          <a href="#">Accés</a>
        </li>
        <div class="form-container">
          <form class="form" action="#">
            <input type="text" placeholder="Rechercher" />
          </form>
        </div>
      </ul>
    </nav>
  </header>

Answer №1

It appears that your HTML is not properly structured. A div should not be a direct child of a ul. Consider using another li element instead.

To center the content vertically within the ul, you can add align-items: center;. Additionally, you can use margin-left: auto; to position it on the right side.

/* Reset */

* {
  margin: 0;
  padding: 0;
  border: 0;
  box-sizing: border-box;
  list-style: none;
  text-decoration: none;
}


/* General styles */

html {
  font-size: 100%;
  line-height: 1.5;
}

body {
  max-width: 1850px;
  font-family: "BenchNine", sans-serif;
}


/* Header */

.top-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  align-items: center;
}

.slogan {
  text-align: right;
  font-family: "Chela One", cursive;
  font-size: 4em;
  color: #527bea;
}


/* Navbar */

nav {
  background-color: #ae2123;
}

ul {
  display: flex;
  justify-content: left;
  align-items: center;
}

.form-container {
  margin-left: auto;
  margin-right: 1em;
}

a {
  display: block;
  padding: 15px 25px;
  text-align: center;
  color: #fff;
}

a:hover {
  color: #999;
}

input {
  padding: 5px 20px;
  border-radius: 20px;
}
<header>
  <div class="top-container">
    <div class="logo-box">
      <img class="logo" src="/img/logo.png" alt="logo" />
    </div>
    <div class="slogan-box">
      <h1 class="slogan">La passion des films</h1>
    </div>
  </div>
  <nav>
    <ul>
      <li>
        <a href="#">Programmes</a>
      </li>
      <li>
        <a href="#">Actualités</a>
      </li>
      <li>
        <a href="#">Jeune Public</a>
      </li>
      <li>
        <a href="#">Tarifs</a>
      </li>
      <li>
        <a href="#">Accés</a>
      </li>
      <li class="form-container">
        <form class="form" action="#">
          <input type="text" placeholder="Rechercher" />
        </form>
      </li>
    </ul>
  </nav>
</header>

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

After refreshing, the base href in the environment is characterized as undefined

Within my angularjs application, I have configured the env.js file as shown below: (function (window) { window.__env = window.__env || {}; // API url window.__env.apiUrl = 'http://aaaaaaa.com/'; // Base url window.__env.baseUrl = '/angula ...

The Bootstrap Tooltip seems to be glued in place

Utilizing jQuery, I am dynamically generating a div that includes add and close buttons. Bootstrap tooltips are applied to these buttons for additional functionality. However, a problem arises where the tooltip for the first add button remains visible even ...

Navigating with Angular Material and md-nav-bar for efficient routing

Currently, I am delving into Angular and have opted to utilize the Angular Material library for my initial application. After tweaking some basic code borrowed from this source, which I adjusted to suit my requirements, I encountered difficulties with rout ...

Transforming Button style in jQuery Mobile version 1.3.0

There are reports of a new function in jQuery Mobile 1.3.0 that allows for dynamically changing button themes using JavaScript: http://jquerymobile.com/blog/2013/02/20/jquery-mobile-1-3-0-released/ However, upon attempting to run the provided code snippe ...

Java: Issue with JLabel Text Alignment

At the bottom of my panel, I have a JLabel that provides text instructions to the user. When some of the text exceeded the screen width, I used tags to fix this issue. However, after implementing the code shown below, the text is no longer centered an ...

Is there a way to conceal the loading screen until the website has completely loaded?

I've been working on my personal portfolio/website and encountered a bug that I can't seem to fix on my own. The issue is with the logo (aa) and text under it showing even after the content page has fully loaded, taking around 3 seconds to hide. ...

Tips for reducing text in a card design

As a newcomer to codeigniter 4, I recently created an event card with a lengthy description. However, when I attempted to display the event data from the database on the card, it ended up becoming elongated due to the length of the description: https://i. ...

When I hover over my images, they begin to flash before my eyes

My layout is set up so that when I hover over the printer image, it should switch to another image. However, instead of smoothly transitioning, the image starts to flash. This issue occurs in all browsers, indicating that I have made a mistake somewhere. ...

deactivating image mapping on mobile media screens

I'm looking to disable my image map on mobile screens using media queries. I've attempted to include some javascript in the head of my html file, but I encountered an error: <script src="http://code.jquery.com/jquery-1.11.3.min.js"></s ...

Strategies to avoid red squiggle lines in a contenteditable div that has lost focus

While the div is focused, spell checking is enabled which works well. However, once the focus is removed and there are spelling mistakes, the red squiggle lines for spell checking remain visible. After following the advice provided in this query: spellch ...

Placement of Buttons Inside a Division Tag

Is there a better way to align button 3 & 4 in a vertical column next to button 1 & 2 without using tables in CSS? See the working example below: https://jsfiddle.net/Jaron787/0te3cs66/1/ Code: HTML <div id="lse" class="display"> <di ...

Bootstrap card elements are failing to display properly, deviating from the expected

My Bootstrap cards are displaying strangely. Instead of having a border, white padding, and a white background like the examples I've seen, mine look like plain text without any styling. Here is an image for reference: https://i.stack.imgur.com/9MsP ...

How can external webpages be effectively integrated under a banner for a cohesive user experience?

Google Images serves as a prime example. Upon clicking on an image, a persistent frame appears at the top of the page, prompting users to easily navigate back to Google. Is there a specific term for this method and what would be the most effective approach ...

Guide on incorporating the WHERE clause into an insert statement in PHP version 5.1.6

What is the correct way to incorporate a where clause into an insert statement in PHP 5.1.6? In my code, I have declared a global variable $module and I am trying to use a where clause to refine my search results. Specifically, I want to include where mod ...

Unable to get CSS to function properly on website

I am having trouble getting my text to format properly on my web page. I have defined rules in my style sheet to center an object on the screen, but it's not working as expected. Here is the code for my web page: body { text-align: center; } h1 { ...

Guide on redirecting to a different URL when the query string contains a particular keyword

Currently, I am facing an issue with the code below that is used for redirection based on URL string matching. The error message "Warning: Use of undefined constant" is being thrown and the code fails to work if the string is case sensitive. Can anyone a ...

The reverse of the alt text is displayed

Utilizing a right-to-left language with dir='rtl' on the body element. This causes the entire page to flow correctly from right to left. However, the alt text within images is showing up in reverse for some reason. Check out this jsFiddle link ...

Extracting props data from a component in React.js - a step-by-step guide

I am currently developing a react.js application with react-select. I have created a dropdown menu and when an item is clicked, I need to pass that item to a function that is connected to the redux store. How can I retrieve data from a component used in re ...

Create an HTML table using data from a Python dictionary

Can anyone help me convert the dictionary below into an HTML table? {'Retry': ['30', '12', '12'], 'Station MAC': ['aabbccddeea', 'ffgghhiijj', 'kkllmmnnoo'], 'Download&ap ...

Tips for centering the second content within a div block

<div style="height: 100%; background: red"> <div style="height: 100px; background: green"> </div> <div style="background: blue;"> <h1>Content</h1> </div> </di ...