How can I move a child element off-center within a parent div in CSS without affecting the position of the other child

Below is an example of code that demonstrates what I'm trying to accomplish:

<nav className="flex flex-row justify-between items-center w-full h-[100px]">
    <div className="flex flex-row items-center sm:pl-0 ml-auto">
        <div className="mr-10">
            Child 1
        </div>
        <p className="text-center">Child 2</p>
    </div>

    <div className="ml-auto">
        <p>Child 3</p>
    </div>
</nav>

My goal is to have Child 3 aligned to the right and Child 2 aligned in the center. When Child 1 is removed, Child 2 aligns perfectly in the center. However, when Child 1 is added and slightly shifted left of center, it causes Child 2 to move to the right, throwing off the alignment.

Do I need to resort to using absolute positioning for this layout or is there a better solution?

Appreciate any guidance!

Answer №1

Indeed, if you wish to maintain the positions of other elements unaffected, a potential CSS-only solution could involve utilizing absolute positioning within your existing HTML structure:

<script src="https://cdn.tailwindcss.com/3.4.1"></script>

<nav class="flex flex-row justify-between items-center w-full h-[100px]">
  <div class="flex flex-row items-center sm:pl-0 ml-auto relative">
    <div class="mr-10 absolute right-0 top-0 w-max">
        Child 1
    </div>
    <p class="text-center">Child 2</p>
  </div>

  <div class="ml-auto">
    <p>Child 3</p>
  </div>
</nav>

An alternative suggestion could be to explore a grid layout as another CSS-only approach, while still maintaining your current HTML structure:

<script src="https://cdn.tailwindcss.com/3.4.1"></script>

<nav class="grid grid-cols-[1fr_auto_1fr] items-center w-full h-[100px]">
  <div class="grid col-span-2 grid-cols-subgrid">
    <div class="mr-10 -ml-10 col-start-2 row-span-full">
        Child 1
    </div>
    <p class="text-center col-start-2 row-span-full">Child 2</p>
  </div>

  <div class="justify-self-end col-start-3">
    <p>Child 3</p>
  </div>
</nav>

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

What is the best way to center text vertically within an image?

How can I vertically align a blog entry title on an entry thumbnail image? The image size changes with the window size and the title varies in length for each entry. After finding a website that successfully achieved this effect, I tried replicating it us ...

When utilizing ThreeJS video texture, it is necessary to ensure that CORS headers are included, regardless of whether the <video> tag typically requires it or

I am facing an issue with my localhost web server where a remote video from Firestore Storage is being played on a page using a <video> tag. The video plays correctly on the web page. However, I am now trying to use the same video as a texture in a T ...

Next.js: React component has an invalid ARIA attribute `ariaHidden`. Maybe you meant to use `aria-hidden` instead?

My component definition looks like this: <HiChevronDown aria-hidden="true" className= "ml-2 h-5 w-5 ..." /> However, the console warnings are telling me that I am camelCasing it. Am I missing something obvious here? Console ...

Struggling with transferring form input data to a different file using JavaScript, Node.js, React.js, and Next.js

I've been struggling with writing form input to a separate file in JavaScript. I created a demo repo to showcase the issue I'm facing. Check it out here: https://github.com/projectmikey/projectmikey-cant-write-to-api-dir-stackoverflow Locally, t ...

a versatile tool or JavaScript module for dissecting different HTML documents

Looking to develop a versatile wrapper or JS plug-in that can parse HTML content and locate tables within the files, allowing users to generate visual graphs like pie charts and bar graphs. The challenge lies in the fact that the HTML structure is not fixe ...

Can we expect every bullet point to be consistently indented at precisely 1.8em?

Recently, I created a dropdown menu using only CSS. The challenge was to have a horizontal bar of items that can each expand into a vertical menu. However, I wanted the expanded menus to display bulleted lists instead of tertiary menus. By nesting three ul ...

Using jQuery to create a seamless transition in font size as you scroll

Currently, I have a jQuery animation function in place to adjust the font size of the .header-wrap text when the document is scrolled beyond 50px. While this method does work, I am not completely satisfied with it as the transition is not very smooth. Idea ...

What could be causing my slider to malfunction?

No errors are being returned by the console. Currently utilizing Unslider. The setup includes: <div class="slider"> <ul> <li><img src="img/one.jpg" alt=""></li> <li><img src="img/one.jpg" ...

Display two images consecutively within a single img tag

My goal is to display two images using a single <img /> tag. The first small image will be loaded and shown using the src attribute, while the second large image will be contained within the data-src attribute. Only one image will be displayed at a t ...

Steps to Import an Image from a Subdirectory:

Currently, I am working on C# and HTML. While working on a view page, I encountered an issue when trying to load images from subfolders. When attempting to load an image from the immediate folder, it opens correctly. For example: <img data-u="image" s ...

Cut off text inside an HTML anchor element using an ellipsis, while ensuring the image remains at the end - all without the need for JavaScript

How can I truncate link text on a header with an ellipsis but keep the image at the end as a glyphicon? And all of this without using JS. Here's my code: <!DOCTYPE html> <html> <body> <ul> <li> <a href="# ...

Angular components are not receiving the Tailwind CSS attributes applied to :root classes

In my Angular 12 project, I have integrated Tailwind CSS. The structure of the package.json is as below: { "name": "some-angular-app", "version": "0.0.0", "scripts": { "ng": "ng&quo ...

Ways to conceal the header and footer on specific React pages

I currently have my header and footer wrapping the content on all pages of my website. However, I am looking to hide the header and footer on specific pages like the customer and admin dashboard. Can anyone suggest the best approach to achieve this? cons ...

Custom Email Template for Inviting Msgraph Users

I'm currently exploring the possibility of creating an email template for the MS Graph API. I am inviting users to join my Azure platform, but the default email they receive is not very visually appealing. public async sendUserInvite(body: {email: < ...

Error encountered: NextJs Docker container unable to run due to permissions issue (sh: next: Permission denied

I have encountered an issue while attempting to run a docker compose file on my ubuntu server that contains a nextjs 13 application. When I execute docker-compose up -d, the following error arises: => ERROR [builder 4/4] RUN npm run build ...

Angular's ng-model is unable to access the value of an object array

When selecting the days, users should be able to input check-in and check-out time ranges dynamically. However, there seems to be an issue with retrieving the values and data format. The ng model is unable to capture the check-in and check-out values. The ...

Customize Swiper js: How to adjust the Gap Size Between Slides using rem, em, or %?

Is there a way to adjust the spacing between slides in Swiper js using relative units such as 2rem? The entire page is designed with relative units. All measurements are set in rem, which adjusts based on the viewport size for optimal adaptability. For i ...

Customizable form with dynamic content and interactive button within a set location

One distinct challenge not addressed in the similar inquiries below relates to a FORM component with both a variable segment and a fixed footer for submit buttons. The objective is to present: A header (a table set to 100% width including a logo and tex ...

utilizing symbols from a vector graphic icon库

There are countless icon images to be found on the internet. Recently, I came across this particular set and now I'm stumped on how to actually utilize them. Despite my efforts to find tutorials online, I have come up short in finding any helpful reso ...

Enhance the spacing between the UL-LI navigation menu and the currently selected item

I'm struggling with adjusting the spacing between items in my navigation menu, specifically the li elements that are currently too close together. I've attempted to increase the margin-left within the .main_menu li CSS rule, but it doesn't s ...