Ways to align a flex child in the middle depending on the width of the

I'm currently working on centering the navigation menu horizontally by aligning it with the width of the header tags using tailwind CSS. However, I've run into an issue where it only centers based on the navigation tags' width.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <script src="https://cdn.tailwindcss.com"></script>
  </head>
  <body>
    <header class="flex justify-between items-center h-16 bg-gray-400 px-2">
      <div class="flex space-x-4 items-center">
        <img src="https://picsum.photos/500/100" alt="" class="h-12 w-auto" />
        <span>Company Name</span>
      </div>
      <nav
        class="flex space-x-4 border border-blue-400 flex-grow justify-center"
      >
        <a href="#">Home</a>
        <a href="#">Search</a>
      </nav>
      <div>
        Profile
      </div>
    </header>
  </body>
</html>

Answer №1

Is this the desired effect? I made changes by using relative for <header> and absolute w-full for <nav>.

<header class="flex justify-between items-center h-16 bg-gray-400 px-2 relative">
  <div class="flex space-x-4 items-center">
    <img src="https://picsum.photos/500/100" alt="" class="h-12 w-auto" />
    <span>Company Name</span>
  </div>
  <nav class="flex space-x-4 border border-blue-400 flex-grow justify-center absolute w-full">
    <a href="#">Home</a>
    <a href="#">Search</a>
  </nav>
  <div>
    Profile
  </div>
</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

The Table and Div elements do not have identical widths when set to 100%

I am facing an issue with a Div and Table placed inside a container. Although they are meant to completely fill the container, occasionally there is a one pixel difference which can be quite annoying. I have prepared a test case and attached a screenshot h ...

Alternatives to ::ng-deep

Currently, I am attempting to customize the appearance of an element that has been placed by the router outlet in Angular. It is essential for me to ensure that this generated element has a width of 100%. After reviewing various responses, it seems like u ...

Issues with the output of files uploaded on GitHub

Why is it that every time I upload my project file to GitHub, the output size increases significantly compared to when viewing it on the VS Code Go-Live server? This discrepancy seems to occur consistently across all of my project files. Can anyone expla ...

Uploading Multiple Files with Base64 Encoding in PHP

I am looking for a way to store images in a database and retrieve them using base64 encoding/decoding. Can anyone guide me on how to achieve this? Here is the HTML code: <form method="POST" action="add_script.php" enctype="multipart/form-data"> ...

`The side panel staying fixed at the top of the webpage and not the edge of the monitor.`

I recently started my journey in web development and am currently working on a portfolio website using django and bootstrap 5.3.3. I am facing an issue with my sidebar not sticking to the top of the user's screen, but rather just the page itself. I wa ...

Images in jQuery rotate depending on the movement of the cursor

My oven has knobs that rotate when you hover over them, but they only turn clockwise. Is there a way to track which direction the cursor enters the image from - left or right - and rotate it accordingly? For example, clockwise if from the left, countercl ...

The loader image does not appear on mobile screens during an AJAX call, but it functions correctly on desktop screens

While I have successfully implemented a loader (.gif) using a div tag in an AJAX call for desktop screens, the same code is not functioning properly on mobile devices. Instead of displaying the loading animation, it simply shows a white screen. <div ...

Aligning SVG shapes within each other

I recently encountered a scenario where I needed to position SVG shapes in the center of each other with varying scales. For instance, placing a rectangle or triangle within the center of a circle. While I found some solutions that worked for shapes like ...

Implementing jQuery functionality on elements that are generated dynamically

I'm facing a challenge when working with dynamic elements in jQuery and I really could use some help. Let me provide an example from my current project: main.js $(function () { renderPlaceList(places); setupVoting(); } The two functions are ...

Is it possible to run both the frontend and backend on the same port when using vanilla JavaScript for the frontend and Node.js for the backend?

Is it possible to run frontend and backend on the same port in Rest APIs if using vanilla JS for the frontend and Node.js for the backend? I've come across information on how to do this with React, but nothing specific to vanilla JS. Any insights on t ...

Why doesn't setting the SVG viewBox to "0 0 100 100" scale my path to 100%?

My current dilemma involves an SVG path that is only displaying at 50% of its intended size. How can I adjust it to show at 100%? Below is the code snippet in question: <div className="svgPathContainer"> <svg width="100%" he ...

Filtering Completed: Table Returned

Recently, I worked on a fun project where I organized JSON wine data into a table and created an object that defines various wines along with their attributes like color, taste, and body. The main objective: When clicking the red button, I want a function ...

Utilize Node.js and Cheerio to extract data from an HTML table

Having trouble converting an HTML table to JSON. HTML table structure: <div id="content"> <h1>content-information</h1> <table class="testinformation"> <thead> <tr> ...

Achieve vertical alignment and responsiveness of elements in primefaces using css techniques

In order to vertically align 2 elements inside an outputpanel, I initially used a margin-top of 3em. However, this method proved to be non-responsive, as shown in the following images: https://i.sstatic.net/yMRXc.png In mobile view: https://i.sstatic.net ...

What is causing the <iframe> to malfunction when I use it?

I am encountering a problem with a basic snippet of HTML code: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8> <title>iframe Example</title> </head> <body> <div> & ...

The behavior of React Native margin is applied internally within the object, rather than externally

Visit the link and then open the file login_screen/components/Form.js. Within this file, you will notice that the textInput is styled as follows: textInput: { flex:1, height: 50, marginBottom: 20 } If you observe closely, you'll see ...

Using Material-UI with React to solve the issue of DatePicker being hidden under modal

The MUI Parent Component includes a Page, followed by a modal also from MUI. Inside the modal, I am rendering the DatePicker component. // @mui import { DatePicker } from '@mui/x-date-pickers'; This is how it appears on the screen. <Contr ...

Ways to adjust flex division to occupy the entire height of its container

https://i.sstatic.net/4YInI.png https://i.sstatic.net/hu8mG.png https://i.sstatic.net/Q51ha.png I am looking to activate an onClick event on the body where discussions and icons are located. While triggering onClick on the body itself poses no issue, I a ...

"I can't figure out why my footer keeps showing up at the top of the page unexpectedly

Currently, my footer is showing up at the top of the page but I really want it to be at the bottom. I'm new to HTML so apologies if this is a simple fix. Here is my file with internal CSS: .middle { position: fixed; top: 50%; left: 50%; tr ...

Encountering an "unexpected token" error while utilizing the JSOP API in a ReactJS application

I am facing an issue while fetching data from a JSON API, as it is throwing an "Unexpected token" error. Below is the code that I have tried so far. Any help in resolving this problem would be greatly appreciated. Below is the code snippet: var Demo = Re ...