Displaying article title above container with backdrop picture

Currently, I am working on a specific layout design using Bootstrap 5 where I am trying to render a tag outside its original container so that it takes up the full width of the page. My attempts with absolute positioning on the img tag have proven to be challenging as the footer is overlapping the tag unexpectedly.

Instead of just talking about it, here is a visual representation of what I am trying to achieve:

https://i.sstatic.net/mPjku.png

The optimal DOM structure I envision for this layout is as follows:

<article>
        <header>
            <div>
                <img />
                <h3 />
            </div>
            <div class="meta" />
        </header>
        <div class="content" />
        <footer />
    </article>
    
  • The img should occupy the full width horizontally
  • The img should maintain its y position without anything coming after it (such as a page footer)
  • The h3 tag should be displayed above the img

I would greatly appreciate any suggestions or ideas on how to make this work. I have been struggling with this small detail for over a week now :')


After some experimentation, I have come up with the following solution. I have divided the overall structure into three separate container boxes, but I find this approach a bit hacky and encountered issues when viewing it on mobile devices (e.g., the title extends below the box instead of staying at the bottom of the image).

<body class="position-relative">
        <div class="container-fluid container-lg">
            <header class="mt-5">
                <nav class="navbar navbar-expand-lg navbar-dark mt-5 mb-3"></nav>
            </header>
        </div>
        <main class="mt-5 mb-3" role="main">
            <section>
                <article>
                    <header>
                        <div class="go-article-banner d-flex position-relative">
                            <img style="object-fit: cover;position: absolute;height: 300px;left: 0;right: 0;" class="w-100" src="...">
                            <div class="container-fluid container-lg position-relative">
                                <div class="position-absolute bottom-0 start-0 right-0" style="background: rgba(0, 0, 0, 0.4);">
                                    <h3 class="display-3 p-4">The War of the Worthies: the spectre of Cardan’s aggression</h3>
                                </div>
                            </div>
                        </div>
                    </header>
                    <div class="container-fluid container-lg"></div>
                    <footer class="container-fluid container-lg"></footer>
                </article>
            </section>
        </main>
        ...
    </body>
    

If you want to see the current rendering, you can check it out here.

In my previous attempt, I ran into an issue where the marquee box was encroaching on the article banner. Furthermore, the title was extending beyond the image which was rendered using an anchor tag.

<body class="position-relative customize-support">
        <div class="container-fluid container-lg">
            <header class="mt-5">
                <nav class="navbar navbar-expand-lg navbar-dark mt-5 mb-3"></nav>
            </header>
            ...
        ...
    </body>
    

Answer №1

After much trial and error, I have finally found a proper way to achieve the desired result (or so I believe). This blog post https://css-tricks.com/full-width-containers-limited-width-parents/ served as my main reference point.

I opted for the last alternative mentioned in the article titled "No calc() needed," considering it to be the most versatile solution.

Let me begin by outlining the structure of the global DOM:

<div class="container-fluid container-lg">
    <header>
        <nav></nav>
    </header>
    <article>
        <header>
            <div class="go-article-header">
                <div class="go-article-banner" style="background-image: url('...');"></div>
                <div class="go-article-title-wrapper">
                    <h3 class="display-3 m-0">Article title</h3>
                </div>
            </div>
        </header>
        <div class="go-article-content">
            <p>...</p>
        </div>
        <footer></footer>
    </article>
    <footer></footer>
</div>
  • A distinct container covers the entire page
  • The container features a universal header and footer with navigation located within the header
  • An individual article is sandwiched between the header and footer, boasting its own header and footer
  • The header includes various wrappers for additional styling purposes (such as background design under the title). I made the choice to use a background image instead of an img tag to allow for the cover property. However, this decision was primarily driven by design considerations.

Now, onto the CSS styling :

.go-article-header {
    height: 300px;
    position: relative;
}

.go-article-banner {
    position: relative;
    left: 50%;
    right: 50%;
    width: 100vw;
    height: 100%;
    margin-left: -50vw;
    margin-right: -50vw;
    background-image: url("assets/img/eskari.png");
    background-color: #333;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
}

.go-article-title-wrapper {
    background-color: rgba(0, 0, 0, 0.6);
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 1rem;
}

The magic lies within the .go-article-banner class, aligning the element and positioning it outside the box before extending it across the full width of the screen using view width measurements.

As for the title, it can easily be adjusted horizontally and vertically within its parent container using the .go-article-title-wrapper class.

Here is the final outcome: https://i.sstatic.net/4Twe3.png

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

Shade of a row within a PHP table

I have a table that is populated with data from a database. Is there a way for me to dynamically change the color of a row based on certain conditions from a JSON file? For example, if the temperature exceeds a set minimum value, I want to highlight the ro ...

Bootstrap Slider Not Cycling Through Database Items

I have a notification list that is currently displaying all messages at once instead of one at a time through a carousel. How can I modify the code to loop through the list and display only one message at a time within the carousel? View <div id=" ...

Adjust the width of a textarea to expand evenly within its container when exceeding the specified width limit using CSS

I am currently attempting to adjust the size of this textarea proportionally. However, when I try to increase it beyond 100% width, it only expands to the right side. I have already experimented with using margin: auto and display: block but unfortunately, ...

Tips for focusing on a background image without being distracted by its child elements

I am trying to create a zoom effect on the background-image with an animation but I am encountering issues where all child elements are also animated and zoomed. When hovering over the element, I want only the background part to be animated and zoomed, and ...

Is there a way to smoothly incorporate a new animation into a page, while ensuring that existing elements adjust to make room for it?

I am looking to smoothly fade and move a hidden element (display:none) within the page, shifting other elements to animate into their new positions as necessary. When I reveal the hidden element by changing it to "display:initial," it should seamlessly a ...

Eliminate empty space in the table cell

I am working with a table that looks like this: <table> ... <td> <div class="btn-group btn-group-sm"> <button class="btn btn-info mini"><span class="glyphicon glyphicon-duplicate"></span></button&g ...

Getting an array of all visible text on a page using the same locator in Selenium

Within different sections of the webpage, I have utilized the following locator (excerpt from HTML): <table id="userPlaylistTable" cellspacing="0" cellpadding="0"> <p class="title"> ABC </p> <p class="title"> DEF </p> ...

Problem with spacing in Bootstrap horizontal layouts

My project is currently empty except for a code snippet I copied from Bootstrap's horizontal gutters page. Unfortunately, I am not seeing any horizontal gaps as expected. Why could this be? It seems that only vertical gutters are working... Click her ...

The primary division grows continuously in size

Currently setting up a basic layout for my homepage. Here's how I envision it: Having trouble defining the size of the main div. I want it to dynamically expand in both height and width, filling the remaining space not taken by menu and footer. HT ...

How can you override the selected classes for menu items in Material-UI using React.js?

Hey there! I'm facing an issue where I can't override the class that displays the correct styling when a menuItem is selected. Below is my code: <MenuItem component={Link} to="/Acceuil" className={{root:classes.menuItem ,selected:cla ...

Rotate background image upon each visit

I have a collection of around 50 background images that I want to display randomly each time a user visits my website. The idea is to provide a unique background image for every visit, without saving any information about which image was previously display ...

Removing Inline Styles Using jQuery Scroll Event

Whenever I reach the scroll position of 1400 and then scroll back to the top, my chat elements (chat1, chat2, chat3, chat4) receive an "empty" inline style but only for a duration of 1 second. After that, the fadeIn effect occurs again every time I scroll ...

Methods for animating .jpg images using CSS

I'm currently working on animating an image, specifically moving it from left to right and back. However, before I dive into implementing CSS animation keyframes, I noticed that I am having trouble getting the HTML element to follow the CSS styles I a ...

I'm facing an issue with my react-router Links not functioning properly when placed within a Bootstrap 5 dropdown navbar button. What could

I have encountered an issue while working on a React project. I copied the navbar code from Bootstrap and made some adjustments to work with React, such as changing "class" to "className". However, no matter what changes I make, the links in the navbar do ...

Looking to include some extra padding when an item is displayed - jQuery

So, I'm working on a jQuery code snippet that controls the visibility of a rectangle element: $("#rectangle").hide(); $("#toggle-rec").click(function () { $("#rectangle").toggle(2000); }); This code hides the rectangle initially and toggles it ...

Position a div element after another using the :after pseudo-element

My goal is simple to explain, but I have exhausted all my efforts trying to achieve it. I am hoping for the ★ symbol to appear immediately after the number 06 using jQuery. Any assistance would be greatly appreciated. The numbers are generated by a s ...

Best Practices for Utilizing Bootstrap 5 Tooltip and Popover Methods in AngularJS Web Applications

Our AngularJS 1.8.2 application requires an upgrade to Bootstrap 5 in order to properly support Tooltips, Popovers, and Modals. Previously, we had been utilizing ui-bootstrap for handling popups. However, migrating from ui-bootstrap to Bootstrap 5 has prov ...

Using Rails to assign a page-specific CSS class within a shared layout

Is there a way to apply unique CSS classes to specific tags within a common layout? In my application.html.erb layout file, the application.css.scss is loaded using <%= stylesheet_link_tag "application". . . %>, which then includes all CSS files in ...

What technique works best for changing the visibility of an image without causing other elements to shift position?

On my webpage, I have a table cell with an image that should only display when hovering over the cell. The problem is that when the image is shown, it shifts the other text to the left because its default state is "display:none". I'm searching for a s ...

jQuery menu fails to toggle the class name

The toggle functionality in the Menu is not working properly. Upon clicking the toggle button, I encountered the following JavaScript error: "message": "Uncaught TypeError: Cannot read property 'toggle' of undefined", "filename": "https://st ...