What could be causing the Grid Child, which has a fixed width and a max-width of 90%, to overflow the

Question:

Could you please explain why the .header element overflows its grid track when the viewport width reaches 1500px?

Code::

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

body {
  background: #6e28d9;
  color: white;
}

.container {
  display: grid;
  grid-template-areas: 'header';
  grid-template-columns: 1fr;
  background-color: rgba(255, 255, 255, 0.2);
}

.header {
  display: flex;
  justify-content: space-between;
  grid-area: header;
  width: 1500px;
  max-width: 90%;
  margin: 0 auto;
}

What I wanted:

I intended for the .header element to have a fixed width of 1500px, but switch to 90% of its grid track when the available room is insufficient.

What I tried:

Initially, I experimented with setting width: min(1500px, 90%) and removing max-width in the .header element. However, my understanding remains limited, as the behavior seems tied to how the grid track calculates its width based on content size. The significance of 1fr still eludes me.

Thoughts:

While many praise the advantages of Grid, I seem to encounter challenges whenever I venture beyond the familiar territory of Flexbox.

Answer №1

It seems there is some confusion regarding Max-width, width, and percentage in CSS. In CSS, when you set an element's width to 10%, it means it will be 10% of the parent div's width. The pixel unit represents a static value in CSS. Moreover, the minimum width of the parent is equal to the child div's width. If you have defined the header's width as 1500px in your project, then the minimal width of the parent element is also 1500px. This results in at least 1350px for 90% of the header. To verify this, you can inspect the width of the header using Chrome DevTools and see that even when resizing the viewport below 1500px, the minimal width remains at 1350px due to the header being set at 1500px. To correct this issue, consider swapping the values of width and max-width, which should resolve the problem. Please note that if you resize the viewport under 1500px, the text may appear smaller than usual because the pixel size decreases proportionally.

Answer №2

Opting for 90vw over 90% appears to be effective in this case

*{
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

body {
  background: #6e28d9;
  color: white;
}

.container{
  display: grid;
  grid-template-areas: 'header';
  grid-template-columns: 1fr;
  background-color: rgba(255, 255, 255, 0.2);
}

.header{
  display: flex;
  justify-content: space-between;
  grid-area: header;
  width: 1500px;
  max-width: 90vw;
  margin: 0 auto;

  border: 1px solid red; /*Added just to visualize the exact width*/
}
<div class="container">
  <div class="header">
    <div class="header-start">header start</div>
    <div class="header-end">header end</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

How to use jQuery to target every anchor element within an unordered list

I am trying to manipulate an unordered list structure by changing the class of the anchor tags when a specific link is clicked. For example, when the link in List Item 1 is clicked, I want to change the class of all links underneath List Item 1 to "yes". ...

Is it possible for me to interact with different elements upon hovering?

html, <div id="first"> <a>Click here</a> </div> <div id=second"> second div content </div> css, #first a:hover{ color:blue; /*change color on hover */ #second{ background:blue; } } In ...

Ways to incorporate a while loop into a randomizing function

I've been diving into JavaScript and managed to create a website that displays random gifs when clicked from an array. Now, my goal is to implement a while loop to prevent duplicate images from being shown consecutively. However, I seem to be facing ...

What is the process for adding a naked domain (without www) on GoDaddy that is deployed through Heroku?

I have successfully deployed a domain through Heroku, and it is functioning properly with the www prefix. However, when attempting to access the domain without the www, it fails to render correctly. I have tried adding both versions of the domain (with www ...

Adjust the background color dynamically as you scroll, given that I am using a linear-gradient

Is it possible to change the color background using JavaScript while scrolling, especially if I have a linear gradient like this: background: linear-gradient(115deg, #42c344 0%,#42c344 40%,#c4efc4 40%,#ffffff 40%,#ffffff 100%); background-attachment: fi ...

Guide on programmatically integrating images into the user interface in ASP.NET from the code behind, while ensuring that all styles have been pre-defined in the .as

I have created a static aspx page with the following code: <div class="container"> <div class="contain" id="subdiv" runat="server"> <input type="checkbox" id="cb1" runat="server" /> <label for="cb1"> <img sr ...

Retrieve the paragraph located directly under the specified heading from any location

I'm having trouble figuring out how to specifically target a paragraph after the heading. Here is an example of my HTML file script: <!doctype html> <html> <head><title>SAM.com</title> </head> <body><h1&g ...

Creating interactive buttons with CSS and jQuery

As a newcomer to coding, I'm trying my hand at creating two buttons that transition from green to blue with a single click, and eventually incorporating them into a live website. The functionality I'm aiming for includes: Both buttons startin ...

Clicking on another function - JavaScript

I'm looking for a way to capture the onclick function of a div and trigger it when the backKey is pressed. Any help would be greatly appreciated! document.addEventListener("keydown", keyDownTextField, false); function keyDownTextField(e) { var k ...

jQuery smooth scrolling problem causing a one-second page "blinking" issue

Currently, I have implemented a smooth scrolling effect on my website using the following jQuery code: jQuery('html,body').animate({scrollTop:scrollTarget}, 1000, "swing"); Although the effect works well in general, I have noticed that when mul ...

Issue with mouseout gap in Microsoft Edge when using the <select> element

A noticeable gap appears in Microsoft Edge between the <select> menu and its options when expanding the menu, as shown here: https://i.stack.imgur.com/SprJ2.png This particular issue can cause problems with the mouseout event due to inconsistent be ...

Managing and Streaming Music in a Rails Application

Currently, I am storing mp3s using paperclip and they only play back if I utilize Amazon S3. However, I am hesitant to use Amazon S3 because I am unsure how to secure the files. Now, I am reconsidering my approach as I need to secure the mp3s and provide ...

Perform Jquery trigger on mobile by clicking, and on desktop by hovering

Despite encountering this question numerous times, I am still unable to find a solution. I have a dropdown menu and I want it to open on hover for desktop and on click for mobile devices. I attempted using the provided code, however, it only works after r ...

Using createStyles in TypeScript to align content with justifyContent

Within my toolbar, I have two icons positioned on the left end. At the moment, I am applying this specific styling approach: const useStyles = makeStyles((theme: Theme) => createStyles({ root: { display: 'flex', }, appBar: ...

Tips for adjusting image size within CSS Grid to ensure grid fits the width of the page

I'm new to web development and currently learning HTML and CSS. My goal is to create a portfolio page on my website using a 3-column CSS Grid that fits the page width and is centered. I've managed to display the images with margins, but they are ...

Looking to retrieve a specific portion of HTML data returned from an AJAX request

Looking to extract specific HTML content from the response received from an ajax call made to the controller. This task will involve utilizing an ID reference. I am encountering difficulty in employing the find function directly on the data as it is curre ...

Detecting When an Element is About to Be Deleted by Pressing Backspace in a Contenteditable HTML Element

As I develop a text editor using <div contenteditable>, one of my goals is to implement a confirmation message for users before they delete an image element within the editor. My idea is that when a user presses the backspace key while attempting to ...

The layout of an HTML and CSS page does not adjust according to the size of the

Having trouble scaling down my HTML and CSS code properly using only %'s and vw's. The entree div and drink div are dynamically populated, making formatting even more challenging. <!DOCTYPE HTML> <html> <link rel="stylesheet" h ...

Utilizing CSS to Create Fixed Position Elements

How can I create a CSS Style with a fixed position property for a div? When I place this particular div inside another div containing text, the fixed-positioned div overlaps the text, causing it to be hidden. I want to align the text and image divs next ...

Elements displaying outside of block 'a' tag

I have a set of <a> elements that are styled with display:block. These contain various nested <divs>, for example: <a href="#" class="sliderframe"> <div class="container-caption"> <div class="slider ...