Tips for aligning an image in the center with a background attachment

I am facing an issue where only half of the image shows up when inserting the fixed effect. I need the entire image to be visible on the right side while adapting it for mobile devices.

* {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
}

.container {
   margin: 200px 0;
   background: #ccc;
   display: grid;
   grid-template-columns: repeat(2, 1fr);
}
.content:nth-child(2) {
   background: url(http://imgfz.com/i/sPTEXhi.png) no-repeat fixed #ccc;
   background-position: 100% 100%;
 background-size: contain;
   height: 500px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
</head>
<body>
<div class="container">
    <div class="content">
     <h2>Title Website<h2>
     <p>
     Lorem ipsum dolor sit amet consectetur 
     adipisicing elit. In,sequi.
     </p>
    </div>
    <div class="content"></div>
</div>
</body>
</html>

Answer №1

* {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
}

.container {
   margin: 200px 0;
   background: #ccc;
   display: grid;
   grid-template-columns: repeat(2, 1fr);
}
.content:nth-child(2) {
   background: url(http://imgfz.com/i/sPTEXhi.png) no-repeat fixed #ccc;
   background-position: center;
 background-size: contain;
   height: 500px;
}
<!DOCTYPE html>
<html lang="en>
<head>
<meta charset="UTF-8>
</head>
<body>
<div class="container>
    <div class="content>
     <h2>Unique Title<h2>
     <p>
     Unique text goes here.
     </p>
    </div>
    <div class="content></div>
</div>
</body>
</html>

Answer №2

Instead of using a fixed size, you can try adjusting the background-size property:

* {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
}

.container {
   margin: 200px 0;
   background: #ccc;
   display: grid;
   grid-template-columns: repeat(2, 1fr);
}
.content:nth-child(2) {
   background: url(http://imgfz.com/i/sPTEXhi.png) no-repeat #ccc;
   background-position: 100% 100%;
   background-size: 100% 100%;
   height: 500px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
</head>
<body>
<div class="container">
    <div class="content">
     <h2>Title Website<h2>
     <p>
     Lorem ipsum dolor sit amet consectetur 
     adipisicing elit. In,sequi.
     </p>
    </div>
    <div class="content"></div>
</div>
</body>
</html>

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

Text cannot be highlighted within DIV element

Check out this page and this page on my website. Text within the advert DIV tag cannot be selected, and some text in the Incinerator product page is unselectable in the topic-div above the iframe element containing a YouTube video. Here is the relevant HT ...

Displaying a PHP variable within an HTML tag that has been previously output within a PHP block

I'm struggling with printing a variable that was already echoed in a php tag within an html tag. Specifically, I want to print the variable $A in an h1 tag. Here is my code: <?php while($pr = mysql_fetch_assoc($qAccess)) { $ ...

Interactive SVG viewport dimension

Check out my "hamburger button" made in SVG, shown below. body { margin: 0; text-align: center; } svg#ham-btn { margin: 2rem; border: 1px solid black; fill: #383838; } <svg id="ham-btn" width="107.5px" height="80px" viewBox="0 0 80 80" pre ...

How come certain rectangles vanish when one rectangle completely fills the space?

Currently, I am encountering an issue with CSS paint worklet and I am trying to determine if it's a browser bug or an error on my end. In the worklet, I am drawing multiple rectangles. Strangely, when one rectangle covers the entire area, the others s ...

The "flickering" effect of the div is like a dance, moving gracefully between fade outs and

I am encountering a similar issue as described in this thread: same problem (fiddle: original fiddle). However, I am still learning JavaScript and struggling to apply the solution provided because my HTML code is slightly different. Can someone please assi ...

Boost website performance with Google Page Speed by optimizing CSS above the fold using this innovative Bootstrap Template Plugin

My Google PageSpeed Insights report suggests that I should place my CSS below the fold. However, Bootstrap is being used as my templating plugin. I am contemplating printing bootstrap.min.css inline on my homepage, but I hesitate because it may lead to i ...

Understanding the behavior of the enter key in Angular and Material forms

When creating forms in my MEAN application, I include the following code: <form novalidate [formGroup]="thesisForm" enctype="multipart/form-data" (keydown.enter)="$event.preventDefault()" (keydown.shift.enter)="$ev ...

Using jQuery, we can replace all `<span>` elements with `<img>` elements and then verify if the images have been successfully loaded

Similar Inquiry: jQuery check if image is loaded I find myself in a scenario where the following HTML structure is utilized: <div class="image"> <img class="" src="content-images/1.jpg"> <span class="slide" rel="content-images/ ...

Unable to show <LI> elements

I'm having trouble adding LI items to the #historial <UL> tag. Whenever the for loop is inside the function, the list displays with some elements duplicated. I believe the for loop should remain outside of the function. Could someone please he ...

Saving sortable portlet items to a database using JQuery AJAX

I've searched through numerous resources to find a solution to my problem, but none of them have been successful. Here is the code I am working with: http://plnkr.co/edit/pcfz33lzHz4wdehjGEuQ I am trying to utilize AJAX to save the order of two port ...

Analyzing the string's worth against the user's input

I need help figuring out how to save user input on a form (email and password) as variables when they click "Register", so that the data can be used later if they choose to click "Login" without using default information. I am working on this project for s ...

Issue with Sub-Menu closing unexpectedly upon hovering over Menu item

I created a webpage with navigation links that trigger separate menus to open upon hovering. Although I have successfully implemented this functionality, I encounter an issue when trying to click on a link within the sub-menu as it disappears. Below is a ...

What is the most efficient way to transmit an HTML document element from a client to a server in Node JS

I am attempting to capture a snapshot of my client-side document object and send it to the Node.js server. However, when I try to convert it into a string using: JSON.stringify(document.documentElement) I encounter an issue where it becomes an empty obje ...

What is the rationale behind browsers on OSX not supporting the styling of HTML option elements?

As an OSX user, I primarily use Chrome and recently discovered that styling the HTML option element is not applied in any browser (Chrome, Firefox, Safari) on OSX. // HTML <select> <option class="option">Value 1</option> < ...

Semantic UI (React): Transforming vertical menu into horizontal layout for mobile responsiveness

I have implemented a vertical menu using Semantic UI React. Here is the structure of my menu: <Grid> <Grid.Column mobile={16} tablet={5} computer={5}> <div className='ui secondary pointing menu vertical compact inherit&apos ...

How to Efficiently Organize OpenAI AI Responses Using TypeScript/JavaScript and CSS

I'm using a Next.js framework to connect to the OpenAI API, and I've integrated it seamlessly with an AI npm package. The functionality is incredible, but I've encountered an issue regarding line breaks in the responses. You can find the AI ...

Use jQuery to swap out every nth class name in the code

I am looking to update the 6th occurrence of a specific div class. This is my current code <div class="disp">...</div> <div class="disp">...</div> <div class="disp">...</div> <div class="disp">...</div> < ...

ajax request sends data to php file, which then processes the data and sends it back using $_POST

Currently, I am in the process of developing a budget management application using a combination of HTML, Javascript, and PHP. The main objective is to allow users to input data into a database through a form interface. To achieve this functionality, I hav ...

The box shadow feature does not function properly when the position is set to sticky

Applying position sticky to th and td in the example below seems to be causing issues with the box shadow not working. Can anyone provide insights on why this is happening? <div class="overflow-x-auto lg:overflow-visible"> <div> <t ...

glsify - encountering the error message 'This file type could require a specific loader to be handled'?

Currently, I'm attempting to implement a custom shader following the guidance provided at https://tympanus.net/codrops/2019/01/17/interactive-particles-with-three-js/. I have imported the .frag and .vert files (provided in the link) and replicated the ...