Div with a background image that adjusts based on screen size

Currently, I am working on creating a parallax effect within a div through adjusting the background image based on scroll movements. My goal is to ensure this effect remains responsive across different screen sizes. I want the image to scale appropriately with 100% width and an automatic height adjustment. However, as the image is set as a background, the div itself appears empty necessitating me to assign it a fixed height. This limitation restricts the responsiveness of the design since the height is predetermined. Is there a way to maintain flexibility in changing the height according to the background image's requirements?

To better understand my query, please refer to the following fiddle: http://jsfiddle.net/d2620kgk/

HTML:

<div id='bg-image'></div>

CSS:

#bg-image{
    background-image: url('../images/portfolio/stoneybrook-full4.jpg');
    background-size:100% auto;
    width:100%;
    height:500px;
    background-repeat:no-repeat;
}

Answer №1

#background-img{
background-image: url('https://example.com/image.jpg');
    background-size:cover;
    position: fixed;
    width:100%;
    height:100vh;
    top: 0;
    left: 0;
    background-repeat:no-repeat;
}

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

Having trouble aligning two divs horizontally on the same line in Bootstrap 4

I am facing an issue with my HTML code where I have two div elements that are supposed to be horizontally aligned, but the second div is appearing underneath the first one. To fix this, I tried using bootstrap classes for alignment. <div class="c ...

Positioning a sticky footer in a dual-column design

This updated post is a revised version of the one found at this link. I aim to provide a clearer explanation of my issue compared to the previous post. The problem revolves around the placement of the footer in two different scenarios. Scenario 1: https: ...

What is the best way to showcase the sub-menu options within this dropdown menu?

function rotateIconMenu(key) { ... } function displaySubMenuItems(k) { ... } .menu-part{ margin-top: 120px; } .menu-parent{ color: #a6afbb; font-size: 13px; ... } <ul class="menu-part"> ...

Is there a way in JQuery to apply the CSS of one element to another selected element?

I'm curious if there is a JQuery function available that can be used to transfer the exact CSS styles from one element to another. I want all the CSS properties to be applied without any additional inheritance. For example: <div id="d1"> &l ...

Understanding the float feature in CSS

Why is there an empty space to the left of the image in my code section? #features{ background-color: #f9f6f7; } .featuresimg{ width: 40%; float: left; } <section id="features"> <img class="featuresimg" src="https://via.placeholder.com/ ...

CSS: Caption text below image remains on same line

I am struggling with positioning a div that contains an image and a caption. My goal is to make the caption break into a new line when it reaches 95% of the width of the image. Despite my efforts, I can't seem to achieve this. The text always pushes ...

The z-index of two elements seems to be behaving differently than anticipated

I am encountering an issue with the code provided below, which is a simplified version of a larger problem. Can you explain why, if: .div_A {z-index: 10;} < .div_C {z-index: 20;} the button inside div_C appears behind div_A? I need to understand the ...

The toggle button on Bootstrap v3.3.6 Navbar is visible but does not reveal the menu

My navigation bar is functioning properly, except for when I try to collapse it. It shows everything, including the button, but doesn't display the menu items. I am using Bootstrap 3.3.6 and I'm not sure why this issue is occurring. <nav cla ...

What is the best way to create an iframe positioned above a navigation bar using CSS?

When I click the button, an iframe pops up on my website. However, the navbar overlaps it unless I scroll down. You can view my practice site at: This is the issue I am facing: https://i.sstatic.net/TKmw7.png Oddly enough, everything looks fine when I ...

Arrange the text below the button containers in a straight line

This is the current appearance of my page: Current This is what I'm aiming for: Expected Moreover, I want to ensure that when the screen is minimized and the width is reduced, the text remains neat and in the correct position. It should adapt and ...

Manipulating Objects with CSS Transform in Global/Local Coordinates

If we take a closer look at our setup: <div id="outside"> <div id="inside">Foo</div> </div> and apply a rotation to the outer element - let's say turning it 45 degrees clockwise: <div id="outside" style="transform: ro ...

Bootstrap 4 Alpha Grid does not support aligning two tables side by side

When working with Bootstrap 4, I am aware that the .col-xs classes have been removed. Instead of using .col, I have tested it on Bootstrap 3.5 which is currently being utilized. <div class="col-xs-4"> Table content for left ...

How to vertically align multiple lines of text using CSS

Struggling with vertical alignment of text in a tabbed layout on my website. Some tabs have single-line text while others span two lines, making it challenging to position the text consistently in the middle of each tab without aligning by the first line. ...

Eliminate the option to display/hide password icon on Safari

Is there a method to eliminate the show/hide icon in Safari that pops up when entering symbols into an input field with type "password"? I was able to achieve this in IE/Edge by incorporating the following CSS rule: input[type=password]::-ms-reveal, input ...

Icon-enhanced Bootstrap dropdown selection

I have a unique select dropdown using Bootstrap where I want to display icons like the example below: https://i.sstatic.net/dZmTS.png <!DOCTYPE html> <html> <head> <script src="/scripts/snippet-javascript-console.min.js?v=1"& ...

What is causing images to stretch unnecessarily in Flexbox on IE11?

I am encountering a problem with flexbox on IE11, and despite being aware of the numerous known issues, I am unable to find a solution... <div class="latest-posts"> <article class="post-grid"> <img src="http://lorempixel.com/ima ...

Jquery fails to adjust div width on smaller screens despite successful resizing on larger screens

I have a script that automatically adjusts the width of child divs based on the number of children. On smaller screens, I want them to be 100% wide. I've attempted inserting the script within the existing code for regular screens as well as at the end ...

various designs for multi-location infrastructure in angular js

Our angular app is designed with a multi-site architecture, where the base URL of each domain varies. Each site requires a unique header and footer to be incorporated, along with custom CSS and links specific to that site. Is there a way to dynamically in ...

Flex bug in safari causing issues with inline form display

I have created a simple inline form using flexbox. However, I encountered an issue in Safari where the button loses its width. Here is a screenshot for reference: https://i.stack.imgur.com/3s5AR.jpg Interestingly, the form looks fine in all other browsers ...

Creating a Fully Responsive Card Design in Bootstrap 4 - Ensuring Your Card Occupies the Entire Width of the

I am trying to achieve a layout similar to the example shown here: https://i.sstatic.net/iMFx6.png The design I have in mind involves creating a card that spans the full width of the container. Inside this card, I would like to display a video on the lef ...