Transitioning a static div within a CSS grid to fill the entire screen

I have created several blocks within a CSS grid layout structured as shown:

$('.box').click(function(){
    $(this).toggleClass("active");
});
.grid-container {
  display: grid;
  grid-template-columns: repeat(14, 1fr);
  grid-template-rows: repeat(10, 1fr);
  gap: 20px;
  height: calc(100vh - 40px);
  width: calc(100vw - 40px);
  margin: 20px;
}

.box{
  width:100%;
  height:100%;
  transition: all 1s;
  opacity: 1;
  z-index:1;
  font-size: 10px;
  line-height: 1;
  color: white;
  background-color:#ebebeb;
  
}

.box.active{
  width:100vw;
  height:100vh;
  z-index:50;
}

.item-1 {
  grid-column: 1 / span 2;
  grid-row: 1 / span 2;
  background: linear-gradient(to right, #ffb347, #ffcc33);
}

.item-2 {
  grid-column: 13 / span 2;
  grid-row: 1 / span 2;
  background: linear-gradient(to right, #ffb347, #ffcc33);
  opacity: 0.8;
}

.item-3 {
  grid-column: 3 / span 2;
  grid-row: 8 / span 2;
  background: linear-gradient(to top, #d38312, #a83279);
  z-index: 1;
  opacity: 0.8;
}

.item-4 {
  grid-column: 1 / span 2;
  grid-row: 5 / span 2;
  background: linear-gradient(to left, #b3ffab, #12fff7);
}

.item-5 {
  grid-column: 13 / span 2;
  grid-row: 9 / span 2;
  background: linear-gradient(to top, #485563, #29323c);
}

.item-6 {
  grid-column: 4 / span 2;
  grid-row: 2 / span 2;
  background: linear-gradient(to right, #fe8c00, #f83600);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="grid-container">
<div class="box item-1">1</div>
<div class="box item-2">2</div>
<div class="box item-3">3</div>
<div class="box item-4">4</div>
<div class="box item-5">5</div>
<div class="box item-6">6</div>
</div>

When a user clicks on one of the colored boxes, I would like it to expand and move across the screen to cover 100% of the height and width.

Currently, clicking on each box makes it expand to 100vh and 100vw, but due to its static positioning within the CSS grid, I am unable to move it to cover the entire screen.

If I apply position:absolute to .box.active, the transition is not smooth and appears unattractive. If I use position:absolute on .box, then the .box elements are no longer positioned within the grid...

Is there an elegant solution that allows the boxes to remain in the grid and expand/move to fill the entire screen when clicked? Any suggestions are welcome.

Answer №1

Here's a clever workaround (and I eliminated that troublesome jQuery :P) so why not give it a try ...

function enlarge() {
  if (this.classList.contains('active')) {
    this.classList.remove('active')
  } else {
    this.classList.add('active');
  }
}

const elements = document.querySelectorAll('.box');
const elementsArray = [...elements];

elementsArray.forEach(element => {
  element.addEventListener('click', enlarge);
});
.grid-container {
   display: grid;
   grid-template: repeat(10, [row] 1fr) / repeat(14, [col] 1fr);
   grid-gap: 20px;
   height: calc(100vh - 40px);
   width: calc(100vw - 40px);
   margin:20px;
   overflow: hidden;
}
 .box {
   height: 100%;
   width: 100%;
   opacity: 1;
   z-index:1;
   font-size: 10px;
   line-height: 1;
   color: white;
   transition: 1s;
   background-color:#ebebeb;
}
 .box.active {
   transform: scale3d(20, 20, 1);
   transform-origin: center;
   z-index:50;
}
 .item-1 {
   grid-column: col 1 / span 2;
   grid-row: row 1 / span 2;
   background: linear-gradient(to right, #ffb347, #ffcc33);
}
 .item-2 {
   grid-column: col 13 / span 2;
   grid-row: row 1 / span 2;
   background: linear-gradient(to right, #ffb347, #ffcc33);
   opacity: 0.8;
}
 .item-3 {
   grid-column: col 3 / span 2;
   grid-row: row 8 / span 2;
   background: linear-gradient(to top, #d38312, #a83279);
   z-index: 1;
   opacity: 0.8;
}
 .item-4 {
   grid-column: col 1 / span 2;
   grid-row: row 5 / span 2;
   background: linear-gradient(to left, #b3ffab, #12fff7);
}
 .item-5 {
   grid-column: col 13 / span 2;
   grid-row: row 9 / span 2;
   background: linear-gradient(to top, #485563, #29323c);
}
 .item-6 {
   grid-column: col 4 / span 2;
   grid-row: row 2 / span 2;
   background: linear-gradient(to right, #fe8c00, #f83600);
}
<div class="grid-container">
  <div class="box item-1">1</div>
  <div class="box item-2">2</div>
  <div class="box item-3">3</div>
  <div class="box item-4">4</div>
  <div class="box item-5">5</div>
  <div class="box item-6">6</div>
</div>

Answer №2

Give this a shot:

.container.active{
  width:100%;
  height:100%;
  position: fixed;
  top: 0px;
  left: 0px;
  z-index:50;
}

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

Setting the flex direction to column and enabling wrapping will allow the flex items to overflow

A basic design I'm aiming for with minimal use of html tags Utilizing only <img>, <h1>, and <p> tags without any additional elements Implementing flexbox with column layout and wrap feature The primary column displays a single ima ...

The attempt to remove the ajax-loaded page while preserving the original div is proving to be

I have a function that is triggered when a link is clicked. This function uses ajax to load a new page over a div. <pre><code> function displayContent(id) { loadPage_signup = "register.php"; loadPage_info = "userinfo.php"; $.aj ...

Align a bootstrap navigation item to the right without a dropdown menu

I'm having trouble positioning the Login/Register link to the right of my navbar. I've checked other solutions on this platform, but they either involve dropdown menus or don't work for me. Here's my Navbar code: <link href="http ...

Menu options

I am currently working on developing a mouseover navigation website. Initially, my design included main buttons for "Our Team", Locations, and Patient Resources. Here is the basic structure I had before attempting to switch to a mouseover approach... &l ...

Positioning of buttons within a div

I am currently designing an inspiration post style: https://i.stack.imgur.com/I5J0H.png However, I am encountering some challenges while creating it: The buttons are not rounded when the window size is changed Social icons are not being displayed I am ...

Hide the scrollbar in the div depending on the specified condition

I'm having trouble with this particular process and haven't been successful so far... I currently have a customized scrollbar within a div element, and what I am attempting to do is remove the scrollbar when the height of the div is less than 19 ...

`The toggleClass function will only respond to the first click in Internet Explorer and Firefox browsers

This is my first time asking for help as I usually find the answers myself, but this issue has me stumped. Here's what I have: HTML <!-- This div opens/shows the navigation. I need to change the div to a button tag --> <div class="menu"> ...

"Learn the trick to concealing a modal and unveiling a different one with the power of jquery

Whenever I try to open a modal, then click on a div within the modal in order to close it and open another one, I encounter an issue. The problem is that upon closing the first modal and attempting to display the second one, only the background of the seco ...

element with singular border

I overlaid one div on top of another to create a border effect. Check out this example: However, I am facing an issue: when I hover the mouse over the image, only the div with the border gets focus (for instance, for right-clicking and saving the image), ...

What is the best way to unselect the "all" selector if one of the inputs is no longer selected?

I am facing an issue with a search filter functionality. When all filters are selected and then deselected individually or together, the "all" button remains selected. I need help in ensuring that when any filter is deselected, the "all" button also gets d ...

CSS Rules with Lower Specificity Will Take Precedence Over Rules with Higher Specific

Currently, I am enrolled in an online web development course and have been working on creating webpages to grasp the concepts of HTML and CSS. However, I encountered an issue where the font-family rules in my p and h3 elements were overriding the font-fami ...

Creating a website layout with two evenly sized columns, one of which can be scrolled through

I am facing a challenge with my HTML table that has one row and two columns. I want the two columns to have equal height, but achieving this has proven difficult using floats or flexbox. Therefore, I turned to using a HTML-table. The content in the left c ...

What is the best way to display a red cross on a button?

Could you please help me add a red cross (font awesome icon) to the button? I attempted to implement the code on JSFiddle. However, I still need: UPDATE Initially, I found Kiranvj's answer suitable for its simplicity and applicability to all r ...

What are the steps to design a fluid responsive grid with three boxes?

I really need help creating a responsive grid using CSS and HTML. The image below should give you an idea of what I'm trying to achieve. Thank you in advance for any assistance! Here is the image as a reference: https://i.sstatic.net/FO9q5.png ...

HTML code with embedded messages

I am interested in creating a straightforward message forum system. My goal is to organize messages in a nested structure (answering questions). For my website, I want it to be in Hebrew (dir="rtl"). I am considering generating <ol> elements dynam ...

What causes the blurriness in an image during a motion tween?

I had a collection of over 20 images that I wanted to scroll from right to left in Flash MX 2004. During the preview in the timeline window, the images appeared to move smoothly. However, once exported to swf format, the movement resulted in blurred images ...

Retrieving pictures by their unique identification number

Utilizing a flexslider, I have set up an image display feature as shown in the JSFiddle link below. The slider has been optimized to dynamically extract images from a specific directory, where images are named with numbers like 1023, 2045, 304654, and so o ...

Adjust div height to match the dynamic height of an image using jQuery

I'm facing an issue with my image setup. It has a width set to 100% and a min-width of 1024px, but I can't seem to get the 'shadow' div to stay on top of it as the window size changes. The height of the shadow div also needs to match th ...

Utilizing Jquery for Targeting a Specific div Element and Implementing CSS Styling

In my collection of styles, I have some interesting ones like: .line{} And also: .line:focus{} Each style has its own individual charm. I am looking to use jQuery to focus on a div with the class .line and change its style to .line:focus. However, eve ...

Implementing a tracking mechanism

My goal is to incorporate a counter into this feature. When a number is clicked, the message should read "You've selected 1 out of 5," indicating that the user needs to choose 5 numbers. If the same number is clicked again, it should deselect and reve ...