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

https://i.sstatic.net/8zHQG.jpg

After clicking on the third element

https://i.sstatic.net/urRbF.jpg

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

Iterating through JSON objects in JavaScript using 'Foreach'

After receiving a JSON Object from my PHP script, I am trying to extract specific data using JavaScript. { "Data":{ "Recipes":{ "Recipe_7":{ "ID":"7", "TITLE":"Wu ...

React textarea trigger function on blur event

https://codesandbox.io/s/react-textarea-callback-on-blur-yoh8n?file=/src/App.tsx When working with a textarea in React, I have two main objectives: To remove focus and reset certain states when the user presses "Escape" To trigger a callback function (sa ...

invoking a function from an attached HTML file using Angular

How can I invoke a function in Angular using inner HTML? I attempted the following: cellTemplateFunc(cellElement, cellInfo){ var subContainer = document.createElement('div'); subContainer.innerHTML = "<a href='javascript:v ...

What could be the reason behind my useEffect() not triggering when updating a value in a React context?

How come my useEffect hook doesn't trigger when I update a dependency that involves a React context? In my project, the context is used within a wizard to gather user data and then process it on the final step. I am able to update the "Person" fields ...

Guide on implementing jQuery Validation plugin with server-side validation at the form level

Does anyone have a suggestion for how to handle server-side validation errors that occur after passing the initial client-side validation on a form? $("#contact_form").validate({ submitHandler: function(form) { $.ajax({ type: 'POST', ...

What steps can I take to center the content of my webpage using react bootstrap?

Currently, I am working on creating a desktop webpage with react-bootstrap. This webpage consists of a logo, main heading, form, and some links that appear after the form. To ensure that all of this content is displayed in the center of the webpage, I am ...

The output of jQuery('body').text() varies depending on the browser being used

Here is the setup of my HTML code: <html> <head> <title>Test</title> <script type="text/javascript" src="jQuery.js"></script> <script type="text/javascript"> function initialize() { var ...

Laravel 4.2 email template not applying CSS inline style for text color

Working on setting up email functionality in Laravel 4.2 and I've got an email template ready to go. The data for the emails is stored in an array, but I'm running into an issue where if I pass a parameter like $variable, the styles are only bein ...

Looking for a specific text on $location update in AngularJS

Is there a way in AngularJS to search the DOM for text after a new view is loaded? The filter almost does this, but I'm not interested in returning a new array of elements. For instance; html <div ng-view=""> <ul> &l ...

Spin the item around the axis of the universe

My goal is to rotate an object around the world axis. I came across this question on Stack Overflow: How to rotate a object on axis world three.js? However, the suggested solution using the function below did not resolve the issue: var rotWorldMatrix; / ...

Place a small image within a list item to ensure it matches the height of the list element

I need help aligning images to the right within list elements. The images are squares with equal height and width, and I want them to fit snugly within the height of the list element on the right side. My HTML code is quite simple for now (see example bel ...

Tips for displaying a notification after a successful form submission using jQuery and AJAX

While attempting to submit a PHP form using jquery $.ajax();, I have encountered a peculiar issue. The form is successfully submitted, however, when I try to display an alert message - alert(SUCCESS); on success, it does not work as expected. Any ideas on ...

Is it Possible to Remove an Item from an Array in Vue without Explicitly Knowing the Array's

I'm currently working on a feature that involves removing an item from an array when it is clicked. The code I have so far looks like this: <span @click="deleteItem(index)" v-for="(item, index) in customTaxonomies.featured" v-html="item"></s ...

Performing an RxJS loop to retrieve the httpGet response, followed by executing httpPut and httpPost requests based

I am currently working on a UI form that allows users to update or add translation text. To achieve this, I need to create an rxjs statement that will perform the following tasks: Send an httpGet request to the database to retrieve translations in mult ...

What is the process for including a new item in a JavaScript dictionary?

I'm currently learning JavaScript and I've encountered a challenge. I have a dictionary that I'd like to update whenever a button is clicked and the user enters some data in a prompt. However, for some reason, I am unable to successfully upd ...

Incorporate a class into a link within the wp_list_pages function

I've been experimenting with creating a hover effect on list items using the wp_list_pages() function in my Wordpress theme. Despite my efforts, I've hit a roadblock in getting the hover effect to work properly. As a beginner in web development, ...

Ways to incorporate the setTimeOut function

<body> <script> $(window).scroll(function() { $('#csgo').each(function(){ var imagePos = $(this).offset().top; var topOfWindow = $(window).scroll ...

Position the Bootstrap button on the right side

this is the code I am working with: <p class="h1 p-3 mb-2 bg-danger text-white text-danger">Lagerbestand unter Mindestmenge! </p> <br /> <div class="border text-center"> <p class="h5 text-center">Durc ...

Display sqllite database information in an HTML view using Ionic 2

After executing a select query against the database, I am able to read the results from the logcat and view the data there. However, I am encountering an issue where the data is not rendering in the HTML view after binding. //data retrieve section db.exec ...

PartialView not functioning on External Network

I am attempting to load a partial view through a JQuery load function. This process involves calling a Load Action from Codes Controller. While this functions properly when the Web Application is accessed through an internal network, it fails to work when ...