The JQuery CSS function is unable to alter the height of the element

I am working with a grid that contains various elements, each with the class item. When I click on one of these elements, I toggle the class expand to resize the element.

<body>
    <div class="grid">
        <a href="#">
            <div class="item" style="background-image: url('https://i.giphy.com/l0NwI1oBNxYfoRRny.gif'); height: 270px; background-size: cover; background-repeat: no-repeat; position: absolute;">
            </div>
        </a>   
        <a href="#">
            <div class="item" style="background-image: url('https://i.giphy.com/pYI1hSqUdcBiw.gif'); height: 202px; background-size: cover; background-repeat: no-repeat; position: absolute;">
            </div>
        </a>   
        <a href="#">
            <div class="item" style="background-image: url('https://i.giphy.com/l3vR58nYgtIUKua1G.gif'); height: 149px; background-size: cover; background-repeat: no-repeat; position: absolute;">
            </div>
        </a>   
        <a href="#">
            <div class="item" style="background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif'); height: 202px; background-size: cover; background-repeat: no-repeat; position: absolute;">
            </div>
        </a>   
           ...
    </div>
</body>

CSS

.item {
    color: white;
    display: table;
    font-size: 1.4em;
    text-align: center;
    margin: 5px;
    width: 270px;
}

.expand {
    transition: width 0.5s, height 0.5s, left 0.5s, top 0.5s;
    -webkit-transition: width 0.5s, height 0.5s, left 0.5s, top 0.5s;
    height: 100% ;
    width: 100%;
    left: 0 !important;
    top: 0 ;
    z-index: 99;
    text-indent: -9999px;
}

JS

$('.item').click(function() {
    $(this).toggleClass('expand');
});

Initially, the width is resized to 100% of the parent size (grid), but the height remains unchanged.

This is the grid view before clicking on any item element

After clicking on the third element

The problem arises when trying to change both height and width using the following JS method:


$('.item').click(function() {
    var height = $(this).height();
    var width = $(this).width();
    var act = $(this).hasClass("expand");
    if(act) {
        $('.expand').css('height', Math.floor($('.grid').width() * height / width) + 'px !important');
        $('.expand').css('width', '50% !important');
    }
});

However, despite this attempt, the height and width do not change. It seems that the $('.expand').css() method is not applying the new values for some unknown reason.

Answer №1

Since you have specified the height as an inline style for .item, it will take precedence over styles from an external stylesheet. To resolve this, you can either move the height to .item or utilize !important with height in .expand

$(".item").click(function() {
  $(this).toggleClass("expand");
});
.item {
  color: white;
  display: table;
  font-size: 1.4em;
  text-align: center;
  margin: 5px;
  width: 270px;
}

.expand {
  transition: width 0.5s, height 0.5s, left 0.5s, top 0.5s;
  -webkit-transition: width 0.5s, height 0.5s, left 0.5s, top 0.5s;
  height: 100% !important;
  width: 100%;
  left: 0 !important;
  top: 0;
  z-index: 99;
  text-indent: -9999px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<body>
  <div class="grid">
    <a href="#">
      <div class="item" style="height: 202px; background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif'); background-size: cover; background-repeat: no-repeat; position: absolute;">
      </div>
    </a>
    <a href="#">
      <div class="item" style="height: 202px; background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif');  background-size: cover; background-repeat: no-repeat; position: absolute;">
      </div>
    </a>
    <a href="#">
      <div class="item" style="height: 202px; background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif'); background-size: cover; background-repeat: no-repeat; position: absolute;">
      </div>
    </a>
    <a href="#">
      <div class="item" style="height: 202px; background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif');  background-size: cover; background-repeat: no-repeat; position: absolute;">
      </div>
    </a>
    ...
  </div>
</body>

Answer №2

To enhance the appearance, eliminate the inline style width: 150px; from div.box and introduce width: 150px to the css class .box

The inline style carries more importance compared to your stylesheet. This is why the font-size: 20px in .header does not take effect.

Answer №3

To start, I recommend using CSS to assign the pointer instead of relying on the <a> tag as it could potentially cause issues...

Another suggestion is to have a hidden div with Height:100% Width:100% that loads the image and then displays it over everything else.

<body>
    <div id="showDiv"></div>
    <div class="grid">
       <div class="item" style="background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif'); height: 202px; background-size: cover; background-repeat: no-repeat; position: absolute;"></div>
       <div class="item" style="background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif'); height: 202px; background-size: cover; background-repeat: no-repeat; position: absolute;"></div>
       <div class="item" style="background-image: url('https://i.giphy.com/3o7abxtmPxanzaESGY.gif'); height: 202px; background-size: cover; background-repeat: no-repeat; position: absolute;"></div>
           ...
    </div>
</body>

Next

 $('.item').click(function () {
     var img = $(this).css("background-image");
     $('#showDiv').css({'background-image':img});
     $('#showDiv').show();
   }
}
$("#showDiv").click(function(){
     $(this).hide();
     $('#showDiv').css({'background-image':""});
});

Don't forget to style the showDiv

 #showDiv{
     display:none;
     height: 100%;
     width: 100%;
     z-index: 999;
 }

This approach will help avoid any unwanted disruptions to your page...

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

Resolve the 'undefined offset error' in Drupal Views Nivo Slider

After updating to the latest version of Drupal, I encountered an error while trying to use the "Views Nivo Slider" module. I keep seeing this error message: Notice: Undefined offset: 0 in template_preprocess_views_nivo_slider_view_nivo_sliderfields() (lin ...

hidden behind the frame is a drop-down menu

I am currently working on a website that uses frames. The topFrame.php contains a menu with drop-down submenus, while the main frame displays the website content. However, I am encountering an issue where the submenu in the topFrame is getting hidden beh ...

"Enhance Your Website with jQuery Checkbox Multi-Select and Row Selection

Take a look at the code and screen below. It's a configuration screen. When the Fieldname checkbox is clicked, all checkboxes will be selected. If a check is removed, all checkboxes will be deselected. Clicking the Field1 checkbox will select child ...

What steps should I follow to transform SRM into L*a*b* values using the E-308 algorithm?

I'm grappling with the application of ASTM E-308 to SRM measurements in beer. The project I'm working on necessitates a reliable conversion from SRM to RGB (or sRGB) through the Lab* route. It's clear that each site I visit for beer recipe c ...

How to locate a particular element containing specific text using xpath

I have a unique set of spans to work with: <span> <span>foobar</span> <span>textexampleone</span> </span> Currently, I am attempting to utilize xpath in order to locate the span containing "textexampleone" with ...

Easily transforming data between JSON and CSV formats in JavaScript using user-defined headers

Can someone provide guidance on creating import/export functionality for CSV format without using external libraries? It would be a huge help! I have a CSV string labeled as "A": "First Name,Last Name,Phone Number\r\nJohn,Doe,0102030405&bso ...

utilizing async/await without the need for babel-polyfill

Here is the code I am working with: @action async login(payload){ try { this.loginLoading = true const data = await request('/admin/login', { method: 'post', data: payload }) this.logined = ...

Obtain JSON information from a Javascript response using Puppeteer:

While developing a test with Puppeteer and Node, I encountered the need to extract an access token from a response after logging in. Here is the current code snippet: //Click Login Button const loginButton = await page.$(".click-button.dark.ng-star-i ...

Validating Forms using JQuery

I am currently working on a jQuery AJAX code to validate specific fields in an HTML form before submission. If any errors occur during validation, I want the error message to be displayed in a div with the ID "response." However, the code doesn't seem ...

The power of Rails unleashed through Ajax Remote Javascript

Trying to locate Employees who are engaged in multiple Job roles, I have set up three select boxes that dynamically adjust their options. The problem arises when my CoffeeScript file only triggers once. After the initial selection and rendering of the part ...

nextJS does not recognize the term 'Window'

I'm encountering the error "window is not defined" in my NextJS project. The variable isMobile determines whether the window size is less than 767.98 to handle the hamburger menu functionality. This code worked in ReactJS but seems to be causing issue ...

Load data from a file into a dropdown menu using node.js

Exploring the realm of front end development on my own has been quite a challenge. I am currently struggling with the concept of populating a drop down box with data from a file. While utilizing node and JavaScript, I have decided to stick to these techn ...

Tips for using the foreach loop to print out multiple arrays

I am working with two arrays in PHP: post_titles and posts. I need to figure out how to print them sequentially using a foreach loop. Printing one array at a time is not an issue, as shown below: <?php foreach ($titles as $row) { ?> <?php ec ...

Cordova: Opening App through a Website Link Click

Embarking on my app development journey, I recently dived into creating an Android app with Cordova. However, I found myself stuck at a particular issue: In the sign-up process, users receive an email containing an activation link to click and confirm the ...

How to arrange elements at the same level in Node.js using sequelize-hierarchy?

Is there a way to change the order of items on the same level in sequelize-hierarchy for creating a menu? I've noticed that currently, the items are ordered by their sequence of insertion. But what if I want to manually set the order? Here is an exam ...

Access the modal by simply clicking on the provided link

I have implemented a code snippet below to display data from MySQL in a modal popup. Everything is functioning correctly, but I am interested in triggering the modal by clicking a link instead of a button. Is it feasible to achieve this considering I have ...

Tips for properly aligning numbered lists in text documents

My issue involves designing an order list paragraph where I also need a paragraph in the middle of the ordered list numbers. The first paragraph is displaying correctly, but the second and third paragraphs are only appearing as single lines, so I adjuste ...

Uncovering unseen glitches while coding in Vue.js

After changing the page, I noticed that the errors from the form are still visible. Is there a way to make them disappear when navigating away and only show up if I return to the page? Here are the errors I am encountering: <template> <b-form @ ...

Error: Unable to execute function abc, it is not defined as a function in this context

Having trouble with a fronted or JavaScript issue where it can't find the defined function in the file. I'm working on integrating Apple Pay and need to call the back-end API based on a specific event. Here is my code. ACC.payDirect = { _autoload ...

An inclusive CSS-compatible calendar control for ASP.NET

I am currently using the Calendar control provided by .NET but I have encountered issues with how it renders HTML in certain situations. Unfortunately, this is hard-coded and cannot be changed. It seems that the Calendar control has not been updated in .NE ...