CSS: Struggling to properly position two flex items

I'm having some trouble with the alignment in my flex container. The title is perfectly centered, but the breadcrumb content is not aligning correctly.

Specifically, I want the breadcrumbs to be right-aligned and positioned 25px from the top within the container (like the example shown).

Can anyone spot where I might be going wrong?

#site-wrapper {
  background: #ececec;
}
.site-wrapper-inner {
  overflow: hidden;
  position: relative;
  background: #fff;
}
.site-wrapper-inner.contained {
  max-width: 1500px;
}
.container {
  margin: 0 auto;
  width: 100%;
}
header .container, 
main .container {
  position: relative;
  padding: 0 30px;
  max-width: 1118px;
}
header.hero {
  position: relative;
  clear: both;
  overflow: hidden;
  height: 340px;
  margin-bottom: 50px;
  display:flex;
  align-items:center;
}
.container.hero-center {
  text-align: center;
  max-width: 560px;
}
.breadcrumbs {
  color: rgba(255,255,255,0.7);
  font-size: 0.8em;
  align-self: flex-start;
  text-align: right;
  position: absolute;
  top: 25px;
  right: 0;
}
.breadcrumb-bg {
  padding: 0.2em 1em;
  background: rgba(19,71,91,0.7);
  display: inline-block;
  border-radius: 0.25em;
}
<div id="site-wrapper">
  <div class="site-wrapper-inner container contained">
    <main id="main">
      <section>
        <header class="hero" style="background:#ccc;">
          <div class="breadcrumbs container"><div class="breadcrumb-bg">Breadcrumb</div></div>
          <div class="hero-center container">
            <h1>Center Aligned Content</h1>
          </div>
        </header>
      </section>
    </main>
  </div>
</div>

<h1>Without Breadcrumb</h1>

<div id="site-wrapper">
  <div class="site-wrapper-inner container contained">
    <main id="main">
      <section>
        <header class="hero" style="background:#ccc;">
          <div class="hero-center container">
            <h1>Center Aligned Content</h1>
          </div>
        </header>
      </section>
    </main>
  </div>
</div>

Answer №1

The exact position you are utilizing is being overridden by the declaration of position: relative within this specific rule -

header .container, main .container
.

To address this issue and achieve proper alignment, you can selectively style it as follows:

header .breadcrumbs.container {
  position: absolute;
  right: 0;
  left: 0;
  width: auto;
}

View the demonstration below:

#site-wrapper {
  background: #ececec;
}
.site-wrapper-inner {
  overflow: hidden;
  position: relative;
  background: #fff;
}
.site-wrapper-inner.contained {
  max-width: 1500px;
}
.container {
  margin: 0 auto;
  width: 100%;
}
header .container, 
main .container {
  position: relative;
  padding: 0 30px;
  max-width: 1118px;
}
/*ADDED THIS*/
header .breadcrumbs.container {
  position: absolute;
  right: 0;
  left: 0;
  width: auto;
}
/**/
header.hero {
  position: relative;
  clear: both;
  overflow: hidden;
  height: 340px;
  margin-bottom: 50px;
  display:flex;
  align-items:center;
}
.container.hero-center {
  text-align: center;
  max-width: 560px;
}
.breadcrumbs {
  color: rgba(255,255,255,0.7);
  font-size: 0.8em;
  align-self: flex-start;
  text-align: right;
  position: absolute;
  top: 25px;
  right: 0;
}
.breadcrumb-bg {
  padding: 0.2em 1em;
  background: rgba(19,71,91,0.7);
  display: inline-block;
  border-radius: 0.25em;
}
<div id="site-wrapper">
  <div class="site-wrapper-inner container contained">
    <main id="main">
      <section>
        <header class="hero" style="background:#ccc;">
          <div class="breadcrumbs container"><div class="breadcrumb-bg">Breadcrumb</div></div>
          <div class="hero-center container">
            <h1>Center Aligned Content</h1>
          </div>
        </header>
      </section>
    </main>
  </div>
</div>

<h1>Without Breadcrumb</h1>

<div id="site-wrapper">
  <div class="site-wrapper-inner container contained">
    <main id="main">
      <section>
        <header class="hero" style="background:#ccc;">
          <div class="hero-center container">
            <h1>Center Aligned Content</h1>
          </div>
        </header>
      </section>
    </main>
  </div>
</div>

Answer №2

The issue lies in the conflicting styles affecting your absolute positioning. Try using the !important declaration on your breadcumb class to prioritize its styling.

Upon further inspection, it appears that the container class within your breadcrumb is causing the conflict. You have the option to either apply !important to the position attribute of your breadcrumb class or remove the container class altogether from the breadcrumb element.

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

Error Handling in PHP Form with HTML and Image Upload

Let me give you some context first. Recently, I've delved into the world of php and have been at it for a few days now. My code might be a bit messy, but I've done my best to markup as much as possible. I have a form where three things are expec ...

Increment and decrement the like count on fa-heart multiple times

Is there a way to increment the count of a fa-heart value on click and decrement it on the second click? The issue I'm facing is that I have multiple fa-heart elements on the same page, making it challenging to increment or decrement the clicked fa-h ...

Is it possible to utilize CSS to generate a full-width slider while maintaining the aspect ratio of the content?

I am currently utilizing the unslider plugin to design a full-width slider header on a webpage (). Although it functions well at the dimensions I set it up for (1920x450), issues arise when viewed on different screen resolutions. See below: The background ...

incorporating video content onto a webpage

Let me provide more details for better understanding. Currently, the website is not operational yet. Once it's live, I'll be hosting it on a platform like YouTube. The issue I encountered was when trying to embed a video using the code below: & ...

Entering numerous numerical values across a variety of input fields

My website currently has a form with 6 input fields where visitors need to enter a 6 digit code. To make it easier for them, I want to allow users to simply paste the code we provide into the first input field and have the remaining digits automatically po ...

Whenever I select a link on a navigation bar, it transports me to the desired section of the page. However, I often find that the navbar ends up

Recently, I came across some website templates where clicking on a link in the navbar smoothly scrolls to the corresponding section with perfect alignment. The content at the top of the page aligns perfectly with the top of each division. Upon attempting ...

Learn how to efficiently redirect users without losing any valuable data after they sign up using localStorage

I am currently facing an issue with my sign up form. Whenever a user creates an account, I use localStorage to save the form values. However, if the user is redirected to another page after hitting the submit button, only the last user's data is saved ...

Ways to navigate a div within an iframe that has been loaded

As I load a page(A) inside an iframe, the HTML structure of the embedded content is as follows: <html><body> <div id="div1"></div> <div id="div2"><button>Hello</button></div> </body></html> The ...

Incorporate the stylish PayPal icon from Font Awesome into the PayPal code

I'm experimenting with adding the font awesome paypal icon <i class="fa fa-paypal" ></i> before the PAY WITH PAYPAL text on my button. You can see a similar setup on this page with the shopping cart icon <i class="fa fa-shopping-cart"& ...

How to use jQuery to update the href attribute of a parent link

Can someone help me figure out why I am having trouble changing the 'href' of the parent link to a header logo? Thank you in advance! Here is the HTML code snippet: <a href="https://URL.IWANTO.CHANGE"> <img id="partner_logo" src="/ ...

CSS: The Drop Down menu suddenly vanishes every time I attempt to select an item from the visible list

I'm facing an issue with my dropdown menu that I can't seem to resolve. Whenever I hover over the menu, it disappears before I can even click on any items. <ul id="menu"> <li><a href="#title">Home</a></li> ...

I'm still trying to figure out the property position. Why won't my page scroll? (My first time using HTML)

I'm having an issue with my webpage where it doesn't scroll even when the content exceeds the page length. I've tried adjusting the position settings for the body, html, and div elements but haven't seen any changes. Here is the HTML ...

I'm noticing that when I refresh the page, my inputs seem to disappear from the page, yet I can see that they are saved in the console's local

If anyone can help me with this issue I'm facing, I would greatly appreciate it. My aim is to retain values in the text box along with local storage even after refreshing the page. Here is an example of the code I am working with: Link to my web appl ...

Algorithm that continuously increases a given date by 3 months until it surpasses or matches the specified creation date

I am currently working with QuickBase, a platform that involves HTML. My goal is to develop a code that can take a [fixed date] (always in the past) and increment it by 3 months until the MM/YYYY exceeds or equals the MM/YYYY of the [creation date]. Once t ...

No feedback received from JSON

Hi, I'm having trouble receiving JSON response using JavaScript. My goal is to display the JSON data as a treeview. index.html: <!DOCTYPE html> <html> <head> <title>JSON VIEW</title> <link href="https:// ...

Using Jquery and AJAX to insert a PHP file into a DIV container

I am facing a challenge where I need to dynamically load a PHP file into a DIV element upon clicking a button, all without the page having to reload. If you refer to the 'Jsfiddle' document linked below, you will find further details and explana ...

Is it possible to combine numerous -webkit-transition animations in my css code without using keyframes?

I'm experimenting with chaining multiple -webkit-transition animations in my CSS without using keyframes. I understand it's possible to achieve this by calling a keyframe animation, but I prefer to simply overwrite the properties. However, for s ...

Having trouble getting a dropdown menu to function properly with jQuery

Within my project, I have implemented a menu that transforms into a dropdown menu for smaller screens. Below is the HTML code for this functionality: <select> <option value="index.php" id="home">Home</option> <option value="about ...

How can we avoid repeated evaluations by using memoization in a function?

Currently, I am working on a jQuery function designed to switch HTML elements based on specific viewports. The concept is quite simple: <div data-swap-for="#element" data-swap-on="phone"></div> This code snippet will insert the element with t ...

Discover the method for storing multiple values in local storage using a dictionary with JavaScript

I have a scenario in my code where I need to update a value without storing a new one. Let's say I need to input values in the following format: { firstname:'kuldeep', lastname:- 'patel' } After entering the values, they g ...