How do I make an image fill the entire space of a div using JQuery and CSS?

html:

<body>
    <div class="custom-div"><input type="button" id="x" name="button" value="Search"   onclick="showUser()" class="button"/></div>
    <input type="image" name="button" value="Search" onclick="showUser()" class="button"/>
    <input type="submit" name="button" value="Search" onclick="showUser()" class="button"/>
</body>

css:

#x
        {
            width: 100%;
            height: 100%;
            -webkit-border-radius: 19px;
            -moz-border-radius: 19px;
            border-radius: 19px;
            background-color: #424242;
            -webkit-box-shadow: 0 2px 2px rgba(0,0,0,.75), inset 0 1px 2px rgba(255,255,255,.75);
            -moz-box-shadow: 0 2px 2px rgba(0,0,0,.75), inset 0 1px 2px rgba(255,255,255,.75);
            box-shadow: 0 2px 2px rgba(0,0,0,.75), inset 0 1px 2px rgba(255,255,255,.75);
            background-image: -webkit-linear-gradient(bottom, #454b54, #2b323b 50%, #363e46 51%, #6d7278);
            background-image: -moz-linear-gradient(bottom, #454b54, #2b323b 50%, #363e46 51%, #6d7278);
            background-image: -o-linear-gradient(bottom, #454b54, #2b323b 50%, #363e46 51%, #6d7278);
            background-image: -ms-linear-gradient(bottom, #454b54, #2b323b 50%, #363e46 51%, #6d7278);
            background-image: linear-gradient(to top, #454b54, #2b323b 50%, #363e46 51%, #6d7278);


        }

        .custom-div {
            width: 500px;
            height: 500px;
        }

JQuery:

$(function(){
        var xxx ="img/on.png";
            $("#x").click(function(){
                $('#x').css({'background':' url('+xxx+') no-repeat','cursor':'pointer','border':' none'});
                alert("click");
                console.log("click");
            });
    });

i have input="button", and i will change my css of input="button" to add one image, but image not get full size of my div.

How i can set image full size of my div after i set event click (change image)

Answer №2

Check out Demo 1 See Demo 2 in action

background-size: cover;

Alternatively, you can use:

background-size: 100% 100%;

$(function(){
        var backgroundImage = "http://www.atecs.biz/img/nav_gradient.png";
            $("#x").click(function(){
                $('#x').css({'background':' url('+backgroundImage+') no-repeat','cursor':'pointer','border':' none','background-size':'cover'});
            });
    });

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

Utilizing AJAX for file and data uploading in combination with Spring MVC and JSP

Struggling to create code for file and data uploading... Finding it difficult...!!!!!!!! Clicking "btn-upload" button does not trigger any response... No error message in eclipse... Seeking help, please assist...! This is JSP. <script> $("#btn- ...

Struggling to click on a link while viewing the site on your mobile device?

Recently dove into the world of implementing mobile responsive design for a website I've been working on. While conducting some testing, I observed that the main tabs which direct users to different sections of the site, normally easy to click on desk ...

What is the best way in jQuery to show each element of an array one at a time with a space in an input field?

I have a large array filled with strings that I want to display one by one in a text field, automatically concatenated with a space bar. Below is my code: <script> x = 0; function abc() { for (i = 0; i < words[x].length; i++) { ...

Determine if a draggable item is currently positioned over another element

In my body, there are several divs located: <div id="one"></div> <div id="two"></div> <div id="three"></div> All of these divs are draggable using jqueryUI: var $divs = $('#one, #two, #three'); $divs.draggab ...

Update the CSS for InputLabel

I have a drop-down list that I want to customize. The issue is illustrated below: https://i.sstatic.net/hzVtl.png I'm looking to center the text "choose format" within the field and adjust the font size. return ( <FormControl sx={{ m: 1 ...

The sticky menu feature in jQuery does not assign classes to elements after the page is refreshed; it will only work when scrolling is activated manually

Currently, I am customizing a WordPress theme and have incorporated this jQuery code snippet to ensure that the menu sticks to the top of the page when the user scrolls down: /* ----------------------------------------- Fixed navigation on scroll -------- ...

IE8 experiencing issues with jQuery live click event functionality

I have this code snippet that is working perfectly fine in all browsers except for IE8. The #cal_popup_table element is dynamically added to the page. $("#cal_popup_table tbody tr td a").live('click', function() { $('.da ...

CSS page header not appearing on all pages when printing from TryitEditor in Safari on iOS

Experimenting in W3's TryitEditor, I am currently using the following CSS: p.hr { position: running(header) } @media print { .pagebreak { page-break-before: always; } @page { @top-center { content: element(header); } } } Ther ...

Iterate through each element in the array and manipulate the corresponding data

I am trying to figure out a way to assign a unique id to each item in an array and send it to the "script.js" file for individual data processing. <script href="script.js"></sciprt> script.js: $(function(){ $('#box' ...

What is the complete list of features that ColorTranslator.FromHtml() supports and what are the reasons behind its departure from traditional CSS color interpretation guidelines

I'm looking to enhance an API by providing users with the ability to specify the color of something. I want it to accept various representations of colors, such as hex codes and CSS colors. Initially, I thought that ColorTranslator.FromHtml() would fu ...

Craft a stunning transparent border effect using CSS

I am trying to achieve a unique border effect for an element using CSS, where the transparency is maintained against the background: This is the desired outcome: https://i.stack.imgur.com/6Z1MU.png However, the border I am currently able to create looks ...

Guide on how to use a tooltip for a switch component in Material-UI with React

I am attempting to incorporate a tooltip around an interactive MUI switch button that changes dynamically with user input. Here is the code snippet I have implemented so far: import * as React from 'react'; import { styled } from '@mui/mater ...

Identifying the precise image dimensions required by the browser

When using the picture tag with srcset, I can specify different image sources based on viewport widths. However, what I really need is to define image sources based on the actual width of the space the image occupies after the page has been rendered by th ...

Using jQuery's .grep() method on an array will only return the final digit

Could someone help me understand the behavior of jQuery's .grep() method? I'm creating a jQuery object array based on the names of these elements: <div class="small1 other">S1</div> <div class="small2">S2</div> <div c ...

Disabling directional focus navigation in CSS: A comprehensive guide

Is there a way to disable directional focus navigation properties, such as 'nav-up', 'nav-right', 'nav-down', 'nav-left', for elements on an HTML page? button { position:absolute } button#b1 { top:0; left:50%; ...

When attempting to execute a script that includes document.write, it will not function as expected

Our web program is utilizing ajax and jquery, specifically Netsuite. I've been attempting to adjust elements on a page using document.ready and window.load methods in order to load an external script onto the page. Regardless of whether I load this ex ...

Styling an unordered list with CSS in HTML is a powerful

I am working with an unordered list where each list element contains two spans: span A and span B. My goal is to style these elements so that they are displayed horizontally on the screen, with span A above span B in each set. Example: spanAItem1 sp ...

Expand and collapse dynamically while scrolling

// Closing Button for Main Navigation $('button#collapse-button').click(function () { $('nav#main-nav').toggleClass('closed'); }); $(window).on('scroll', function () { if ($(wind ...

Having issues transferring the variable from JavaScript to PHP?

When attempting to pass a variable via AJAX request in JavaScript, I am encountering an issue where the variable is not being received in my PHP file. I'm unsure of where the problem lies. Can anyone help? JavaScript code var build = { m_count : ...

Is there a way to stylize the initial words of a brief text block by blending small caps with italics, all while avoiding the use of a span tag?

I have a series of images with numbered captions like "Fig. 1", "Fig. 2", "Fig. 3", followed by a brief description on the same line. Is there a way to programmatically style these strings (the “Fig. #” only) using CSS or Javascript to make them italic ...