How to resize and center an image within a div element as they both scale proportionally

Can someone help me figure out how to center a resized image within its container, which should also be centered? I've been struggling with this for 7 hours and can't seem to get it right. Any assistance would be greatly appreciated :-)

Here is the html code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<link rel="stylesheet" href="styles.css" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function imageresize() {
var conHeight = ($(window).height())* 0.56; 
    $(".resize").css({'height' : conHeight + 'px'}); // set large-image container to 56% of window height
}
$(document).ready(function() {
  imageresize();
});

$(window).resize(function() {
  imageresize();
});
</script>
</head>
<body>
<div id="navigation-area">
    <div id="navigation">
    </div>
</div>

<div id="set-overflow">
    <div class="resize" id="large-image">
        <img src="images/bg-home.jpg" class="resize">
    </div>
</div>
</body>
</html>

This is the css code:

/*------------------------------*/
* {
    margin: 0;
    padding: 0;
}
#navigation {
    width:990px;
    border:1px solid green;
    margin:23px auto;
    height:42px;
}
#navigation-area {
    width:100%;
    height:135px;
    background:url(images/bg-navigation.png) top center no-repeat;
    overflow:hidden;
    position:absolute;
    top:-3px;
    left:50%;
    margin-left:-50%;
    z-index:50;
}
#large-image {
    width:1970px;
    position:absolute;
    margin-left:-985px;
    left:50%;top:0;
    z-index:40;
    display:block;
}
#set-overflow {
    overflow:hidden;
    width:100%;
    height:100%;
    border:1px solid red;
    position:absolute;
    z-index:20;
}
.resize {
    margin-left:auto;
    margin-right:auto;
}

I included overflow:hidden on the containing div for future use as it will be part of a single-page scrolling site where the large image is only needed in the first div. The image needs to occupy 56% of the viewport or window.

If you need more clarification, please let me know!

Thanks in advance

Answer №1

Is this the style you had in mind?

http://jsfiddle.net/9ZgWg/26/

If it's not quite right, just let me know and we can make adjustments together.

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

Ensuring a DIV remains fixed in place for a specific number of scrolls

I'm looking to create a 'Page section' that remains in place while scrolling for a specific distance and then smoothly transitions to the next section. I've attempted to implement this within the child theme without success... Any sugge ...

Turning a jQuery object and its contents into a string

Currently, I am selecting multiple jQuery objects to store in an array and then display them as HTML later on. I am attempting to transform the jQuery object into a text string so it can be displayed as HTML in the future. My current approach is as follo ...

blending javascript and php variables within ajax requests

I am working on a simple jQuery ajax feature, where I need to combine form data retrieved by JS with some PHP variables and send them all through ajax GET method. Here's what I have: var longform = $("input:text").serialize(); $.ajax({ url: & ...

Utilize the power of EasyAutocomplete in jQuery to easily filter JSON

I have a functioning code, but I need help filtering JSON data to display labels of corresponding IDs greater than or equal to 3. In this scenario, only "India" and "France" should appear in the suggestion list. It's important to note that I am usin ...

Developing User-Friendly Websites with Responsive Design

While I was working on my project, I realized that using only pixel values in tables and sidebars was a big mistake. This caused issues because if someone had a different screen resolution, my website would look distorted and unappealing. Could you please ...

`how to extract the href attribute value from the hyperlink that triggered a modal in jQuery UI`

Recently, I began diving into Jquery and javascript coding. As a beginner, it feels odd to refer to myself as a noob due to my age. Here's the scenario: I have a hyperlink that triggers a dialogue box and sets a cookie. The dialog asks the user, "Are ...

Identifying Even Selections in jQuery Even with the First Option

I am currently working with a select set-up that looks like this: <select> <option value="1">one</option> <option value="2">two</option> </select> My goal is to be able to detect when a choice is made, regardless of wh ...

What is the best way to ensure that two div elements within a list element fully utilize the available width on a webpage?

I am looking to create a 'settings' div that adjusts its width based on the available space on a page. This could range from 1000px to 600px, and I want the div to always be next to the 'title' div without wrapping onto the next line. F ...

What is the process of implementing a particular FormControl from a FormArray in my HTML file?

My FormArray initialization code is as follows: this.contents.forEach(content=> { this.formArray.push( new FormControl(content.text, Validators.required)); }); Now, I am trying to associate a specific FormControl with my textarea by using i ...

Continuously simulate mousewheel events

I'm trying to make my mousewheel event trigger every time I scroll up, but it's only firing once. Can you assist me with this issue? Please review the code snippet below. $('#foo').bind('mousewheel', function(e){ if(e.o ...

Navigational dropdown menu in Bootstrap

I'm a bit rusty with Bootstrap and I'm struggling to make my NavBar dropdown toggle. I've gone through all the documentation and forums available but can't seem to figure it out. Any ideas on what I might be missing? <nav class=" ...

"Enhance readability by adjusting the font size on mobile devices and iPhones for an

I'm currently working on a website that needs to maintain a consistent look across all types of devices, including laptops, PCs, iPads, iPhones, and other smartphones. To achieve this, I've implemented fittext.js, a pure JavaScript solution that ...

Tips for setting an image as the background on various mobile screen sizes

Is there a way to set an image as a background that will work effectively with multiple different mobile screen resolutions without distorting or cropping the image? For example, if I want the image to display correctly on both iPhone4 and iPhone6 without ...

Bootstrap's landing page problem

I am currently working on creating a landing page using bootstrap and have encountered an issue with the text alignment in my section containing icons and tags. In order to make the text appear clean and concise, I want the text to be displayed in either o ...

Change the selection so that only the initial one appears in gray

Is there a way to make the text inside a select field gray out when the first option is selected? Can this be accomplished with just CSS or does it require JS? I have attempted altering the style of the first option, but it only affects the text color in ...

The Box element surpasses the height of the Grid component

Homepage Component <Box component="fieldset" sx={{borderRadius:2, height:"100%"}}> Application Structure Component <Grid container> <Grid item xs={4} > <Root/> </Grid> ...

Unraveling the onsubmit FormObject in an HTML form within the Google Sheets Sidebar: Tips and tricks

In the sidebar, I am setting up a form with 27 inputs to create new sheets based on various criteria. The form includes a text entry field and multiple groups of radio buttons and checkboxes. However, I am currently facing an issue when trying to create a ...

Issues with Stylesheet Rendering in Firefox

Take a look at this Kendo UI Dojo example: When rendered in Firefox, the "right" should also be placed in the header, but it's not. It works correctly in IE11 and Chrome. What am I doing wrong? ...

Getting text between Span tags using Selenium in Python

Struggling to extract the name "Margaret Osbon" from the following HTML using Python and Selenium. Despite trying different techniques, the printed output always comes out blank. <div class="author-info hidden-md"> By (autho ...

Is it possible for jQuery UI Dialog to function similar to an iFrame?

Is there a way to set up my dialog box with a form so that when the form is submitted, only the dialog box changes instead of the browser location? ...