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

Opposite of CSS Media Query in a precise manner

Can you provide the opposite of this specific CSS media query? @media only screen and (device-width:768px) To clarify, what we are looking for is everything except iPad or not iPad. Just an FYI, I have attempted the code below without success: @media n ...

How to use jQuery to adjust the opacity of a CSS background

I am facing a challenge with changing the transparency of a <div> element's background color using jQuery. An external API provides me with a hexadecimal value for the color: #abcdef I make use of jQuery (version 1.9) to apply this color using ...

What is the best way to create a cornered button using CSS?

Is there a way to create a button using only CSS? https://i.stack.imgur.com/7mjVV.png This is the code I've come up with so far: .customButton { width: 100px; height: 100px; background: #6d1a3e; position: relative; transform: rotate(-90 ...

Wordpress articles refuse to appear side by side

I've been at it for hours now, trying to solve this issue with no luck. I have a Wordpress loop on my homepage that displays post thumbnails vertically, but I want them to appear in sets of three images before starting a new line. Here's the cod ...

Discovering the position of elements in relation to their (grand^N)parent

Among my possessions are elements A and B. B has the ability to exist as a direct child of A, or there may be anywhere from 0 to N elements separating them. It is imperative that I determine how B is situated in relation to A, specifically noting the dist ...

Triggering a click event on various instances of a similar element type using the onclick function

Hey there, I'm a newcomer to Javascript. I've been practicing my Javascript skills and trying to replicate something similar to what was achieved in this example: Change Button color onClick My goal is to have this functionality work for multip ...

Unexpected disappearance of form control in reactive form when using a filter pipe

Here is a reactive form with an array of checkboxes used as a filter. An error occurs on page render. Cannot find control with path: 'accountsArray -> 555' The filter works well, but the error appears when removing any character from the fi ...

Tips for preventing the impact of angular mat-panel position changes on matdialog

In my Angular project, I utilized Matmenu and MatDialogModule. I needed to change the position of mat-menu, so I achieved this by adding the following snippet to my .scss file. ::ng-deep .cdk-overlay-pane { transform: translate(78%, 10%); } However, ...

inactive toggle being released

I am facing an issue with my two slider menus, where only the right menu holds the active value when clicked. The left menu slides out only when the toggle button is held down, but I need it to slide out when the toggle is simply clicked and not held down. ...

Update the link by simply clicking on a div tag

I am currently working on a PHP page and my goal is to add the extension ?id=variable_value to its URL when I click on a specific div. However, whenever I try to do this, it shows me an error message stating that the URL with the extension is undefined. B ...

How can I avoid anti-aliasing in browsers when enlarging images?

Back in April 2012, Google's Chart Image API became deprecated but it was possible to generate QR codes using it. Although I know that I can adjust the size of the image produced by the API, I prefer to simply use CSS along with the width and height ...

Increase the spacing between elements in a row using Bootstrap's margin utility

I am working with Bootstrap and have a row class containing three different elements. I want to create a margin of 20px between each element in the row container while keeping them all on the same line. Currently, when I add a margin class with a parameter ...

How can I improve the smoothness of my CSS transition when resizing background images?

I'm currently working on creating an intro movie for a game and I thought it would be interesting to use CSS animations. However, I've noticed that some parts of the animation appear bumpy and inconsistent. It seems that depending on the transiti ...

Ajax is updating the information stored in the Data variable

Recently, I reached out to tech support for help with an issue related to Ajax not executing properly due to Access-Control-Allow-Origin problems. Fortunately, the technician was able to resolve the issue by adding a file named .htaccess with the code Head ...

What is the best way to create a text input that is both secure and non-editable?

While I am aware of the <input type="text" readonly /> possibility, it is not entirely secure. For example, if there is an input field that must remain uneditable by users, making it "readonly" can still be bypassed by inspecting the code and remov ...

MVC Template with a Defective BootStrap

Struggling with creating an MVC master template along with sub-pages using Bootstrap. Sadly, I seem to have messed it up and can't quite figure out where the problem lies. You can check out my test website at Do you see how the different sections a ...

Learn how to dynamically add comments to HTML elements in AngularJS

Here is the meta tag that I have: <meta title='raja' identifier> I would like to know how to manipulate the DOM so it looks like this: <!-- reference <meta title='raja'> --> Could you recommend a way to manipulat ...

Ensure that the columns are consistently displayed on a single row

On larger screens, everything displays correctly. However, when viewing on a phone, the issue arises. I want these two columns to always stay on one row, but currently they stack on mobile devices. How can I ensure that they resize and remain on the same ...

Utilize jQuery and HTML simplistically to display or conceal divs throughout a webpage

After developing some basic jQuery code using if-statements to toggle the visibility of Divs based on a select list in HTML, I am seeking ways to enhance this code: 1) How can I achieve the same functionality with fewer lines of code? 2) Rather than manu ...

Troubleshooting problem with text overlaying images upon hovering in CSS

Several months ago, I successfully implemented a cool effect on a Shopify website that I had designed. However, recently the client decided to switch to a new theme without my knowledge, and now one of my CSS tricks is no longer working. If you look at t ...