Adjust text placement with Image resizing using CSS and HTML

I've spent countless hours on this task, scouring through over 20 articles, and I am still at a loss on how to proceed. My expertise lies in designing backgrounds, where I carefully position text in specific locations. However, when I view the design on a monitor with a larger resolution, everything falls out of place. The background adjusts perfectly, but the text does not cooperate.

Below are images that illustrate my dilemma:

Desired text placement at any resolution (1440 x 900 monitor):

Actual appearance on a 1080p Monitor:

 <body>
  <div id="blah">
<p id="pr">This is a paragraph!</p>
</div>
  </body>
</html>


body {background-image:url(back.jpg); background-size:cover;}
#blah{font-size:large; left:300px; top:200px; position:absolute;}

edit: I attempted the suggested solutions involving divs and adjusting text positioning with absolute and relative values, yet the issue persists - the text continues to shift.

#contain{
position:relative;
width:7000px;
margin:0 auto;}


#blah{font-size:large; left:100px; top:200px; position:absolute;}    

I do not seek a fixed solution as I plan on adding more content, necessitating vertical scrolling without the text displacement.

Answer №1

Ensure that your #blah div is placed within a relative div to avoid any positioning issues, especially when using absolute positioning in relation to the body. Consider nesting it inside another div or utilize fixed positioning instead.

#containerDiv {position:relative;}
#blah {position: absolute; top:200px; left:300px;}

<div id="containerDiv "><div id="blah"></div></div>

Alternatively:

#blah {position: fixed; top:200px; left:300px;}

If you want the div to stay centered and unaffected by window resizing, try something like this:

#containerDiv {position:relative; width:700px; margin:0 auto;}
#blah {position: absolute; top:200px; left:300px;}

Keep in mind that background-size:cover; may not be supported in certain versions of Internet Explorer.

For examples, see:

http://jsfiddle.net/mYcXX/1/ (absolute) vs http://jsfiddle.net/mYcXX/2/ (fixed)

Answer №2

This appears to be a fixed design structure. If that is the case, why not simply trim the central portion of the background and enclose it within a div element with the following style:

{
  width: 960px; // adjust as needed based on the central image's width
  margin-left: auto;
  margin-right: auto;
}

Then align the paragraph content within that specific container (the div).

Answer №3

To tackle this issue, I suggest separating the background image style from your container to potentially solve the problem at hand. Give this code snippet a try...

body{
  background: url(black.png) top center no-repeat;
}

#container{
  width: 960px;
  margin: 0 auto;
}

It's advisable to prioritize the natural flow of the DOM wherever possible. Avoid relying on position: absolute unless it is absolutely necessary. Even then, it may just be a temporary workaround.

Answer №4

After some trial and error, I managed to solve the problem on my own with a little help from yisela's guidance. Taking inspiration from websites like yahoo.com, I noticed that having a centered layout with a white background made everything look clean regardless of screen resolution. With this in mind, I made sure my image was gray and adjusted the background color accordingly for a seamless blend.

When it came to container styling, I placed the image within its own container and centered it, then set the paragraph relative to the container to maintain its position.

html{  background-color:gray }

body{ }

#contain{
width:1280px;
height:2000px;
margin-left:auto;
margin-right:auto;
background-size:cover;
background: url(back6.png);
}

#blah{font-size:large; left:120px; top:230px; position:relative;}

<div id="blah">

<p id="pr">This is a paragraph!</p>

</div>

</div>
  </body>
  </html>

With this experience under my belt, I'm looking forward to returning to c# with newfound appreciation for CSS.

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

When displaying multiple images using HTML5 Canvas in a for loop, only the final set of images is drawn

I'm facing an issue with drawing images using an image loader inside a for loop. The images are only being drawn on the last iteration. The console is not displaying "draw -> 0" or "draw -> 1", it only shows "draw -> 2" Any assistance would ...

Changing the color of a tooltip for a specific element using Bootstrap 4

Is there a way to customize the tooltip background-color for icons with the classes "alert-danger" to red and "alert-success" to green using the following CSS commands: .alert-danger + .tooltip > .tooltip-inner { background-color: red !important; } ...

Struggling to hide empty columns in an HTML table with jQuery - it's just not cooperating!

<html> <head> <script src="jquery-1.4.4.js"></script> <script> $('table').each(function(a, tbl) { var currentTableRows = $(tbl).attr('rows').length - 1; $(tbl).fin ...

Can the <container> block be positioned beneath the <header> block?

header { background: blue; color: white; width: 100%; } .container { background: green; text-align: center; width: 100%; height: 100px; transform: skew(0, -10deg); color: white; position: relative; top: 55px; } .container .home{ p ...

The font color in Bootstrap is set to be lighter than the background color

Bootstrap has made great improvements in displaying navbar item colours that blend well with the background colour of the navbar. Can we apply a similar technique to body text as well? For example, if the container background is purple, can we have the t ...

What is the most efficient method for applying multiple classNames to elements in Next.js?

Could you provide me with a list of all methods, both with and without packages? Also, I'm interested in knowing why one method is considered better than the others. ...

implementing smooth scrolling using jquery following object insertion

I have a Ruby on Rails application with Bootstrap styling. Recently, I updated an object and reinserted it into the page. Now, I want to scroll to the top of a specific div with a smooth animation since I'm ordering based on the 'updated_at' ...

Steps to generate an excel document from an HTML table and store it in the project folder

I have created an HTML table with loaded data and now I am looking to convert it into an Excel file and save it in the project directory. Below is the HTML Table structure: <table class='tblNemoList' border='1' cellpadding='3 ...

What is the reason behind the varying dimensions of images on Chrome compared to Firefox?

I've been working on enhancing my web development skills and decided to create a personal portfolio. During the development process, I focused mainly on Firefox as my browser of choice. One section of the portfolio includes showcasing my skills using ...

Change the HTML table's toggle element to either 'on' or 'off' based on the specified value using jQuery

I am looking to modify the appearance of an HTML/CSS/jQuery toggle element based on the value of a cell within a table, either displaying "YES" or "NO". The required functionality includes: If the text in the table cell is "yes", the toggle element sh ...

The slick jQuery plugin seems to be malfunctioning

I recently downloaded the jQuery files in order to implement some image sliders on my website, but unfortunately, they are not functioning properly. Is there anyone who can help me identify any issues with the code? Thank you in advance. <body> ...

Prevent side-to-side scrolling using the translate property

With Bootstrap 4, I successfully implemented a functionality where clicking a button hides/shows the sidebar on desktop, allowing the main content area to occupy the remaining space: HTML: <a class="sidebar-toggle" href="#"><i class="fa fa-bars" ...

Design images with rounded corners using CSS

I'm looking to design an image avatar similar to the contact avatars on Skype. Can someone assist me with this? I have tried using border radius-boundary, but I haven't had much luck. ...

Angular element fails to display properly

I'm currently working on developing a website using Angular and creating a header component. To generate the necessary files, I used the command ng g c commons/header which creates the HTML, SCSS, TS, and .spec.ts files. I then made modifications to t ...

Different text in AMP

Is there a way to make a line of text quickly fade in, stay visible for 5 seconds, fade out quickly, then have a different line of text quickly fade in, stay for about 5 seconds, fade out quickly, and repeat this process infinitely in AMP? I tried using ...

Is it true that using <meta charset="utf-8"> specifies the character encoding of the document

According to what I've learned, an HTML document must include the <meta charset="utf-8"> element within the head section in order to be considered standard-compliant. What is the logic behind indicating the encoding of a file within the file it ...

A step-by-step guide on showcasing three post images in separate divs at the bottom of a webpage with Reactjs and Css

In the array below, there are three post photos. When I click on each post button, I should see a corresponding post photo div at the bottom for each post. Issue: I am facing a problem where only one post photo div is being displayed, which keeps replaci ...

What is the best way to create flexible Bootstrap 3 columns with varying heights?

I'm struggling to remove space and create a flex column .col-md-5 with varying heights. Despite trying various solutions found on Google, I still can't get it to function properly. The current outcome is not as expected. https://i.sstatic.net/fc ...

If the data attribute of the div ID is equal to 1 in jQuery, then

Using a jQuery plugin, I have created some "3-D stack divs". Each div has its own unique id, and the active div is marked with data-position="1", while inactive divs are marked with data-position="2" or data-position="3". Now, how can I create an if-else ...

What is the best way to access the onclick calling object?

Is there a way to have a handler on the calling object of the onclick event? <a href="123.com" onclick="click123(event);">link</a> <script> function click123(event) { //I need access to <a> in order to man ...