Adjusting the size of background images using media queries to scale down accordingly with the size of the browser window while

Currently, I am working on a website project where I am using a background image with Bootstrap 4. However, I am facing an issue where the image does not center properly as it resizes at different breakpoints. I am contemplating whether to use resized images from Photoshop or resize the images using CSS in order to maintain the centering of the image on the screen. Below is a snippet of the media query code that I have used:

.landpage-img {
    background: url(../img/bg-neon-01-2000x1333.jpg) no-repeat center center fixed;
}
Thanks for your help in advance!

Answer №1

If you want to center an image within a parent element, you can create a parent query like this:

.parent {
width: 100%;
position:relative;
}

Then add the following code for the image:

.landpage-img {
position:absolute;
left: 50%;
transform: translateX(-50%);
}

This will position your image in the middle of the parent selector.

Answer №2

You have the choice of utilizing either background-size: cover; or background-size: contain;.

For example:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<style>
.landpage-img {
    background: url(https://picsum.photos/2000/1333) no-repeat center center fixed;
    background-size: cover;
}
</style>

<div class="container-fluid landpage-img" style="height: 100vh">
    <div class="row">
        <div class="col">
            Some content goes here
        </div>
    </div>
</div>

background-size: contain; retains the image proportions, while background-size: cover; ensures complete coverage even if slight cropping is required. Therefore, choosing background-size: cover; is usually recommended.

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

Placing three div elements within a container, one of which has a height set to auto

I need help arranging 3 divs side by side using float:left. The height for two of the div's (child1 and child3) is fixed, but there is no set height for child2 - I would like child2 to have the same height as the container div. <div id="container ...

Enhancing the visual appeal of Struts 2 with property tag styling

Utilizing the Struts2 property tag to fetch the user's name, my code looks like this. <div class="MyStyle"> <p> Hi! <s:property value="#session.firstName"/></p> </div> However, I am facing an issue wh ...

Modifying the appearance of the search box

I'm looking to customize the appearance of my search box. On Stack Overflow, you'll notice the search box is a perfect rectangle. I want mine to have rounded edges, like an ellipse, rather than being rectangular. How can I achieve this look? Than ...

Utilizing jQuery and CSS to make an entire div clickable, and activate an 'a' tag hover state upon hovering

Looking to create a clickable link for the .wrapper div that directs users to the location of a.icon. Want the .wrapper div to activate the a.icon:hover state when hovered over, not just when hovering over the icon itself. Any assistance would be highly a ...

Puzzling blank area found beneath the form component

There seems to be some extra space at the bottom of a form when it's placed inside a div. How can we remove that unwanted space? In examples from stack overflow, the extra space is not visible in the code snippets provided. However, it appears elsewh ...

Creating a choppy movement when switching between content with jQuery

I am experiencing a choppy effect when trying to toggle the content visibility with a button click. Can someone advise if there are any issues in how I have structured the markup? Also, is there a way to achieve a smoother toggle effect? View Code $(do ...

Is it possible to utilize an AJAX request to access a custom JSON file stored locally in order to dynamically generate Bootstrap

I want to create a page featuring Bootstrap 4 cards with an image, title, brief text, and a "read more" button (basically a blog page with a list of posts). The catch is that this all needs to be done without a server, completely locally. Since this is a ...

Centered Bootstrap navbar logo with left and right aligned navigation items

I am attempting to create a customized navbar using bootstrap. I have been struggling to achieve a design where the logo is perfectly centered with the rest of the elements aligned to the left and right. The current setup looks messy and I would greatly a ...

Achieving a 100% width input with Flexbox: Tips and tricks for success

Check out this flexbox form I created using bootstrap 4: https://jsfiddle.net/kkt0k2bs/1/ I have implemented a media query to adjust the form elements for smaller screens. For larger displays, I want the form items to be displayed inline. However, on sm ...

Issues with CodeIgniter paths causing disruption to CSS background-image declaration

I've been working on a website with backend for two separate customers, each with their own unique URLs. I encountered an issue with Javascript links (ajax calls using url:) but managed to resolve it by using a global variable: var SiteURL='< ...

What is the solution for positioning an input or select element at the end of a tr element?

Within a table, I have a form where each <td> tag contains either a <select> or an <input>. However, due to the varying lengths of the labels within the <td> tags, the <input> or <select> fields are not aligned in the sa ...

Importing CSS with Node Sass using npm

Instead of inlining css within sass, I attempted to utilize the npm package Node Sass CSS importer. Unfortunately, I am struggling to get it to function properly. I typically use npm as my build tool: "build:css": "node-sass some_path/style.scss some_pa ...

Change the body selector in CssResource to apply custom styles

I have implemented a CssResource within a ClientBundle following the guidelines provided in the documentation at this link. To access the styles from my UiBinder xml, I have used ui:with as explained in the documentation available at this guide. While th ...

Switch up the webpage's font using a dropdown selection box

I'm interested in adding a drop-down box to my webpage that allows users to change the font of the site when they click on it. I attempted the code below, but it didn't seem to work. Any suggestions? Regards, Sam CSS .font1 p { font-family: " ...

Arrange two internal divs on top of one another with the option to maintain the float

While searching for a solution to my issue, I noticed that most responses involved removing the float property. However, since I am new to CSS, I would like to explore using float and see if it's the best approach. Here is the fiddle I've been w ...

Exploring the world of MVC4: Enhancing user experience with client-side

Solution: The answer provided by @www.innovacall.com is correct, I initially misunderstood it but now it works perfectly. Thank you for the help. Initial issue: I have been struggling with finding a solution to my problem. In my project, there is a mod ...

Implementing CSS and HTML in Email Communication

I'm noticing a difference between how this appears in a browser versus an email. The text on the image looks perfect when viewed on Mozilla/Chrome, but when I use the same code in an email, the text ends up displaced below the image (right beneath it) ...

The font background color appears to be malfunctioning

body { margin: 0; background-color: white; background: linear-gradient(to right, #000000, #ffffff, #ffffff, #000000); line-height:25px; } h2{ color: black; text-align: center; display: block; font-family: 'Montserrat', san ...

Utilizing cookies to track the read status of articles - markers for reference

Currently, I am in the process of developing a website and am interested in implementing a feature that allows users to track which articles they have read. I am considering adding a small circle next to each article heading to signify whether it has been ...

Converting R.id.image to a string in Android programming

I am currently working on a project where I need to display multiple image files by programmatically manipulating the filename of one of the images. For example, I may have the following image filenames: filename.jpg, filename_top.jpg, filename_middle.jpg ...