Stop flexbox child elements from shifting when the window size changes

Is there a way to prevent the "about" section (flexcontainer div) from being displaced by the header element?

I have a gap between the header element, consisting of a title and nav bar, and the "about" section. However, when resizing the window, the "about" section shifts even though there is still space between the elements.

HTML Code:

<body style="background-image: url(#); background-color: #a6afbe;">
    <header>
        <h1>Haruhi Suzumiya 3D School</h1>
        <nav>
            <ul class="nav-list">
                <li><a class="noselect" href="#">Home</a></li>
                <li><a class="noselect" href="#">About</a></li>
                <li><a class="noselect" href="#">Pictures</a></li>
                <li><a class="noselect" href="#">Contact</a></li>
                <li><a class="noselect"
                        href="#"</a></li>
            </ul>
        </nav>
    </header>

    <div class="flexcontainer" style="top: 30rem">
        <section>
            <h2>About</h2>
            <article>
                <p class="main">
                  texttexttext
                </p>
                <img src="#" alt="">
            </article>
        </section>
    </div>
</body>

CSS Style:

.flexcontainer {
  top: inherit;
  display: -webkit-flex;
  display: flex;
  position:relative; 
  -webkit-flex-flow: row wrap;
  flex-flow: row wrap;
  font-weight: bold;
  text-align: center;
  justify-content: center;
  
}

header {
  display: flex;
  flex-flow: row wrap;
  flex-direction: column;
  justify-content: center;
}

Here is an illustration showing how it looks when not being moved: Element not moved

And here is an illustration demonstrating how it appears when being resized and moved: Element being moved

Answer №1

The section designated as "about" is being shifted downwards from the top by a distance of 30rem due to the inclusion of inline styling with `style="top: 30rem"`. It is advisable to eliminate the inline style attribute and instead incorporate a media query within the primary CSS file targeting the "flexcontainer" class. For upcoming web projects, my recommendation is to prioritize mobile responsiveness during the design phase.

For instance:

@media screen and (max-width: 1000px){
   top: 30rem
}

@media screen and (max-width: 500px){
   top: 20rem
}

@media screen and (max-width: 300px){
   top: 15rem
}

Answer №2

I successfully resolved the issue by utilizing absolute positioning on the header.

Code:

<body style="background-image: url(#); background-color: #a6afbe;">
    <div class="flexcontainer">
        <header>
            <h1>Haruhi Suzumiya 3D School</h1>
            <nav>
                <ul class="nav-list">
                    <li><a class="noselect" href="#">Home</a></li>
                    <li><a class="noselect" href="#">About</a></li>
                    <li><a class="noselect" href="#">Pictures</a></li>
                    <li><a class="noselect" href="#">Contact</a></li>
                    <li><a class="noselect" href="#">Wiki</a></li>
                </ul>
            </nav>
        </header>

        <section  style="top: 45rem" >
            <h2>About</h2>
            <article>
                <p class="main">
                  texttexttext
                </p>
                <img src="#" alt="">
            </article>
        </section>
    </div>
</body>

CSS:

.flexcontainer {
  position: relative; 
  top: inherit;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  flex-flow: row wrap;
  justify-content: center;
  text-align: center;
  font-weight: bold;
}

header {
  display: flex;
  justify-content: center;
  flex-direction: column;
  position: absolute;
}

Illustration of no movement: no movement

Illustration of no movement upon resizing: image description here

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

The image will remain hidden until it is fully loaded and ready to be displayed

I am currently working on a script that involves displaying a loading icon until an image is fully loaded, at which point the loading icon should disappear and the image should be shown. Unfortunately, my current code is not working as intended. Here is a ...

Efficiently Displaying Multiple HTML Elements in React Native WebView

I am currently working on developing an Epub reader in React Native and facing a challenge. I need to find a way to efficiently transfer multiple html content files from the Epub container to the webview in order to achieve seamless pagination. ...

Comparing Embedded and Linked JS/CSS

In my experience, I understand the advantages of using linked CSS over embedded and inline styles for better maintainability and modularity. However, I have come across information suggesting that in certain mobile web development applications, it may be m ...

Adjust website aesthetics through JavaScript by modifying its CSS styling

Hello, in my PHP application I am looking to update the style using JavaScript. <div id="middle-cnt" class="ad-image-wrapper"><div class="ad-image" style="width: 1000px; height: 450px; top: 0px; left: 131px;"><img height="450" width="1000" ...

Overflow of Primary Text in Material UI List Item

I apologize if this question has already been asked, but I have searched and couldn't find the solution! I am facing an issue with a Material UI listview that has a fixed width within a sidebar. The titles of some options are not properly rendering a ...

Numerous slideshows using identical scripts

I am facing an issue with my code for multiple slideshows. Currently, it works fine for a single slideshow but when I have multiple slideshows and mouse over any of them, all the slideshows start running due to sharing the same class. However, if I try to ...

Unusual behavior of PHP echo when inserting into an HTML value attribute

Let's dive right into the problem. I have a database entry that is a string in the column name1 and it looks something like this: COMPANYNAME DIVADAY The key characteristic of this string is that it is mostly a combination of two or more words separ ...

Flex wrap is causing additional space within the layout

When using flex-wrap, if there are more textBoxes than the available width in the container, they shift to the next line. This is fine, but it leaves too much space between the button and the textBox. This spacing issue can make the layout look odd. I hav ...

The upper margin is ineffective

Apologies, I am new to CSS. Here is the HTML code I have: <div class="box-A" >Box A content goes here</div> <div class="box-B" >Box B content goes here</div> I attempted to apply the following CSS to it: .box-A{ background-c ...

Arrange multiple lines in a straight line

I am currently utilizing bootstrap 5 for my project. <div class="col-lg-3 col-md-6"> <h5 class="text-light mb-4">Stay in contact</h5> <p><i class="fa fa-map-marker-alt me-3"></i>619 ...

Extracting Data using Tags and Classes

Attempting to extract data from a website, I require information on sizes, prices, amenities, specials, and reservation options. I have tried using the code below but encountered numerous errors and could not copy the elements. Can someone assist me with ...

Calculate the width of the outer division based on the chosen inner division

Looking for a solution related to an HTML segment? <div class="outer" style="min-width: 150px;"> <div class="inner1"> <span>Lorem ipsum dolor sit amet</span> // Possibly long, truncate if needed <div class="inner2"/& ...

Ways to emphasize the current page in a masterpage menu

In my ASP.NET Web site, I have a menu inside a masterpage and I am looking to highlight the active page in both the masterpage menu and submenus. Here is the HTML structure: <ul id="nav" class="sf-menu"> <li class="current-menu-item">< ...

Creating a dynamic HTML5 video player with Fancybox and VideoJS

What is the best way to ensure that an HTML5 video remains responsive inside a fancybox that is already responsive? Here is the code that I currently have: <a class="preview" href="#inline123"> <img class="item" src="/thumbs/thumb.jpg" widt ...

What is the procedure for displaying the complete text for choices on an ionic select button?

On my page, I have included an Ionic select button. The problem is that the options display a long string, and I want them to show the full text without truncating with dot-dot-dot like this: This is how my code looks: <ion-item *ngIf="select == &apos ...

Swapping out background images for individual pages

How can I make sure that the background images stay fixed regardless of the amount of text on the page? I have two background images, one on the left and one on the right. The text article is overlaid on top of these images. However, each page has a differ ...

The image grows in size until it requires scrolling to view the entire thing

<!-- Bootstrap 4.1.x --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <! ...

How can I change it so that clicking on the YouTube icon directs me to www.google.com?

I have a YouTube icon on my website and I want it to redirect me to Google.com when clicked. However, it is not working as intended. I am trying to implement this functionality using JavaScript. HTML <div class="yt" > <i class=" ...

Resizing the container of a CSS-transformed image to match its dimensions. (Twitter Bootstrap)

Currently working on a new project using Twitter Bootstrap. The main focus is on a thumbnail gallery, similar to the examples provided by Bootstrap (found here: ). The issue I am facing involves applying css transformations, particularly rotations, to ima ...

PHP forms not showing up properly

I have encountered an issue where text fields created in HTML disappear when the file is converted to PHP. Why does this happen and how can I prevent it? Code: <!DOCTYPE html> <html> <head> <title>StatsCalc V1</title> <li ...