Tips for keeping an absolute element aligned within a flex-parent that is constantly changing

I'm facing an issue with banners overlapping images inside a flex-box. When the flex direction is set to row, everything aligns perfectly based on CSS. However, switching to column orientation causes them to stack at the top and cover one another.

<html>
    <div class="locations">
        <div class="location">
            <img src="resources/images/norbert-toth-I1oL89qxefc-unsplash.jpg" />
            <div class="location-container>
                <h2>Herring Bone Library</h2>
                <p>141 Address St. <br>Detroit, MI <br>49449</p>
            </div>
        </div>
        <div class="location ">
            <img src="resources/images/kristen-colada-adams-wpCJlKxCNRA-unsplash.jpg" />
            <div class="location-container">
                <h2>Sunrise Botanical <br>(Downstairs)</h2>
                <p>2397 Hatchet Rd. <br>Suite 11 <br>Detroit, MI <br>49447</p>
            </div>
        </div>
        <div class="location">
            <img src="resources/images/daria-volkova-_IhXaHmTJr8-unsplash.jpg" />
            <div class="location-container">
                <h2>Black Cat Coffee</h2>
                <p>686 My Mind <br>Detroit, MI <br> 49448</p>
            </div>
        </div>
    </div>
</html>

The location-container banners don't adjust with the flex-box even with additional CSS styling. They overlap only within the main parent container.

Here's how they are styled:

.locations {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 3rem 4rem;
    gap: 2rem;
    position: relative;
}

.location-container {
    position: absolute;
    background-color: #fffaf7;
    margin-left: 1rem;
    padding: 1rem;
    top: 1rem;
}

.location-container h2 {
    font-family: "Roboto Condensed", sans-serif;
    margin-bottom: 1rem;
} 

.location-container p {
    font-family: "Cabin", sans-serif;
}

.location img {
    width: 100%;
    max-height: 46rem;
}

@media only screen and (max-width: 1200px) {
    .locations {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        margin: 3rem 4rem;
        gap: 2rem;
        position: relative;
    }

    .location-container {
        position: absolute;
        background-color: #fffaf7;
        margin-left: 1rem;
        padding: 1rem;
        top: 1rem;
    }
}

As the webpage width hits 1200px, the flex-box switches to a column layout while maintaining the positions of text containers.

Any suggestions on keeping the banners aligned properly within flexing parent containers?

Answer №1

When your media reaches 1200px, it's important to make a change in the CSS code. Instead of using position:absolute, switch the location-container class to relative. Additionally, ensure each location-container has a set min-width for consistency.

.locations {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 3rem 4rem;
    gap: 2rem;
    position: relative;
}

.location-container {
    position: absolute;
    background-color: #fffaf7;
    margin-left: 1rem;
    padding: 1rem;
    top: 1rem;
}

.location-container h2 {
    font-family: "Roboto Condensed", sans-serif;
    margin-bottom: 1rem;
} 

.location-container p {
    font-family: "Cabin", sans-serif;
}

.location img {
    width: 100%;
    max-height: 46rem;
}

@media only screen and (max-width: 1200px) {
    .locations {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        margin: 3rem 4rem;
        gap: 2rem;
        position: relative;
    }

    .location-container {
        position: relative;
        background-color: #fffaf7;
        margin-left: 1rem;
        padding: 1rem;
        top: 1rem;
        min-width:300px;
    }
}
<div class="locations">
    <div class="location">
        <div class="location-container">
            <h2>Herring Bone Library</h2>
            <p>141 Address St. <br>Detroit, MI <br>49449</p>
        </div>
    </div>
    <div class="location">
        <div class="location-container">
            <h2>Sunrise Botanical <br>(Downstairs)</h2>
            <p>2397 Hatchet Rd. <br>Suite 11 <br>Detroit, MI <br>49447</p>
        </div>
    </div>
    <div class="location">
        <div class="location-container">
            <h2>Black Cat Coffee</h2>
            <p>686 My Mind <br>Detroit, MI <br> 49448</p>
        </div>
    </div>
</div>

Answer №2

It appears that there is a minor issue in the original code - the first

<div class="location-container>
is missing a closing " in the class attribute, which could potentially cause some problems (already fixed in the snippet).

You have set the location-container to be absolutely positioned relative to .locations, but it should actually be relative to .location without the plural "s".

Here's what you need to do:

  • Remove position: relative from .locations
  • Add .location { position: relative } to your CSS file

Once you make these changes, everything should work correctly.

By the way, I didn't address any potential overflow issues with .location, but I did add some random images to demonstrate the effect.

UPDATE: Upon further inspection of your code, I noticed that all properties for

.locations { flex-direction: column }
are different. You only need to define properties once and override them as needed inside media queries.

A general rule to follow:

  • Define all properties outside media queries that remain consistent across devices.
  • Inside media queries, only change or add properties that need adjusting. In this case, only adjust flex-direction.

I have made the necessary adjustments to the snippet...

.locations {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 3rem 4rem;
    gap: 2rem;

/*    position: relative; /* REMOVE */
}

/* ADD */
.location {
    position: relative;
}

.location-container {
    position: absolute;
    background-color: #fffaf7;
    margin-left: 1rem;
    padding: 1rem;
    top: 1rem;
}

.location-container h2 {
    font-family: "Roboto Condensed", sans-serif;
    margin-bottom: 1rem;
}

.location-container p {
    font-family: "Cabin", sans-serif;
}

.location img {
    width: 100%;
    max-height: 46rem;
}

@media only screen and (max-width: 1200px) {
    .locations {
        flex-direction: column;
   }
}
<div class="locations">

    <div class="location">
        <img src="https://picsum.photos/1200/700?random=1">
        <div class="location-container">
            <h2>Herring Bone Library</h2>
            <p>141 Address St. <br>Detroit, MI <br>49449</p>
        </div>
   </div>

    <div class="location">
        <img src="https://picsum.photos/1200/700?random=2">
        <div class="location-container">
            <h2>Sunrise Botanical <br>(Downstairs)</h2>
            <p>2397 Hatchet Rd. <br>Suite 11 <br>Detroit, MI <br>49447</p>
        </div>
    </div>

    <div class="location">
        <img src="https://picsum.photos/1200/700?random=3">
        <div class="location-container">
            <h2>Black Cat Coffee</h2>
            <p>686 My Mind <br>Detroit, MI <br> 49448</p>
        </div>
    </div>
</div>

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

Adjusting the image width and height dynamically based on the parent container dimensions using CSS and HTML

I'm trying to utilize CSS to fill my window with an image while maintaining quality. Is there a way to maximize the width/height of the image until all the white space is filled without compromising its quality? <div class='rel-img-cont' ...

Card flipping functionality is not functional on Internet Explorer browsers (versions 11, 10, and below)

After coming across a tutorial on CSS3 transform by David Desandro, I tried implementing the code but unfortunately, it does not work in Internet Explorer... I noticed that clicking "flip" only results in Card 1 being displayed while card 2 remains hidden ...

Adjust the column width dynamically in an Ionic framework

Need help with changing the size of a Column element dynamically. <ion-col [size]="this.size" [size-xl]="this.size_xl" [size-md]="this.size_md" [size-sm]="this.size_sm" [size-xs]="this.size_xs"> Faci ...

Tips for retrieving the file name from the <input type="file" tag within a JSP page

I am looking to retrieve the file path from an HTML input type="file, which is selected by the user in the file dialog. <script> function OpenFileDialog(form) { var a = document.getElementById("inputfile").click(); SampleF ...

Tips for arranging two jumbotrons with proper spacing in Bootstrap without stacking them vertically while using the mr-1 class

My issue involves two jumbotrons, each with a col-md-6 class, positioned nicely next to each other. Currently, they are contained within a modal, but for the sake of example, I have created a fiddle without the modal to showcase the same behavior. If I app ...

I recently encountered a minor CSS issue while working on a simple program. I created a div element with two span elements nested underneath it, and I wanted to utilize flexbox to style them

Currently, I am utilizing mapping in React and attempting to display the data within a CSS flex box. However, I am encountering some issues with it not functioning correctly. Please review the following code snippet: Here is the React code. I have applied ...

When you click on links and buttons, a focus outline appears

I am currently troubleshooting an issue within an existing application that relies on the use of jQuery. The problem arises when I click on any link or button on the page, causing the element to display a focus outline (such as a blue glow in browsers like ...

Can you explain the connection between CSS and WebKit?

Lately, the tag "webkit" has caught my eye on various questions. These inquiries typically pertain to web-related topics like CSS, jQuery, website layouts, and cross-browser compatibility problems... But what exactly is "webkit" and how does it interact w ...

Problem: NgFor is failing to render the data

Hey there! I'm working on a project that involves Spring Boot, SQL, and Angular. I have set up one-to-many relationships, but I'm struggling to display the second object in my component's HTML. Here's my current setup: @Component({ se ...

Display or conceal a field based on the content of another field using jQuery

Is there a way to hide or show a field on my website based on the value in the shopping cart? I've created a function for this, but I'm struggling with the condition. Can you help me figure out how to write it correctly? <script> $(docume ...

jQuery template does not respond to input text when a click event is enabled on an iPhone device

Below is a jQuery template I have: <div class="parent-class"> <div class="sub-class"> <div clas="sub-input-class"> <input type="text" /> </div> </div> </div> Recently, I ...

Stop the removal of the CSS content property

When a user enters and re-enters their password, I have a form that checks the strength of the password and displays text accordingly. However, I noticed that if a user makes a mistake and uses the backspace to re-enter the password, the text from data-tex ...

The CSS for the page is failing to load due to a basic HTML and JavaScript file

I'm embarking on a journey to learn HTML, CSS, and JavaScript by creating a basic webpage. Despite linking my CSS file in the HTML document, I am facing an issue where none of the styles are being applied. I am currently running this project locally ...

Centering an Unordered List within a Div Container with a Colored Background Box

I'm facing a challenge at the moment. I've been trying to center the unordered list box, but all my attempts have been unsuccessful. It seems to be in the middle, but not perfectly centered. I suspect there's an issue with the margins. I nee ...

Struggling to achieve success in redirecting with passport for Facebook

For a "todolist" web application that utilizes passport-facebook for third party authentication, the following code is implemented: passport.use(new FacebookStrategy({ clientID: '566950043453498', clientSecret: '555022a61da40afc8ead59 ...

Angular 1.5 - Component for fetching HTML content with dynamic data

Help needed with using Angular Component method. Developing a main html file with its main controller containing a JSON list of client data: clients: [{ "name": "John Jackson", "age": "21", "hair": "brown", }, { "name": "Janet Doe", ...

Experiencing inconsistencies with CSS on certain devices?

When faced with a research issue, I usually turn to Google for answers. However, this time, I was unsure of where to begin. As part of my efforts to enhance my frontend development skills, I undertook The Odin Project and worked on a calculator project. U ...

What is the best way to apply box shadow to a particular region using CSS?

I am looking to add a box shadow using CSS, but I only want it in a certain area of the box. https://i.sstatic.net/dcFZu.png In the screenshot above, you can see that I have a blue header on my website. I would like to add a shadow to this header, specifi ...

Cursor customized image vanishes upon clicking anywhere on the page on a MAC in various web browsers

I am currently trying to set a custom image as the cursor using jQuery in this manner $(document).ready(function(){ $("body").css('cursor','url(http://affordable-glass.com/test/penguinSm.png),auto'); }); While this method works, ...

Customize your header and footer for printing to display on each page

Does anyone know how to use the window.print function to print a div content with custom header and footer that will show on every page? I have been able to add a header and footer, but they only appear at the top and bottom of the print document. How can ...