Best practices for ensuring text remains in the correct position on all screen sizes when overlaying a large image

In the introductory page, I have a large image that spans more than 4000px in width to accommodate different resolutions. The image is set with the following CSS:

#source-image {
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

On top of this image, I added text using the following style properties:

.description {
    position:absolute;
    top:510px;  
    left:23px;
    width:340px
}

Everything looks fine on my 15.6-inch laptop with a resolution of 1366x768.

However, when my roommate viewed it on his high-resolution monitor, the text was not correctly positioned. I understand why this happened.

My question is how can I ensure that the description text maintains the proper position across all resolutions dynamically? Thank you!

Answer №1

To ensure proper positioning, it is recommended to set the distance from the bottom instead of the top or use percentages for more flexibility.

UPDATE: An experiment has been transformed into a practical example: The description's placement is determined in relation to the image container's bottom and left (with the image fully filling its container).

In the first scenario, fixed pixel distances are used for the left and bottom of the image container.

In the second case, these distances are specified as percentages, adjusting accordingly when resizing the browser window.

The key CSS rules employed are

figcaption {
    bottom: 5px;
    left: 23px;
        /* additional rules here */
}

for fixed distances in pixels, and

figcaption.perc {
    left: 10%;
    bottom: 17%;
}

for percentage-based distances.

It should be noted that position: absolute and setting the top and left properties for the image are unnecessary. However, position:relative must be applied to the parent element of the description box.


To achieve full horizontal screen coverage by the image, you need to apply margin:0; and padding:0; on the body element, and width: 100%; along with margin: 0; on the figure element. The example has been adjusted to reflect these modifications

If aiming for complete horizontal and vertical screen coverage by the image, consider using the image as a background for the body element without an img tag, while setting the heights of both the html and body elements to 100% - see example

Cautions to bear in mind include potential distortion of the image upon resizing the browser window and the need for content below the image requiring the outer element to have position: absolute and top: 100% set. Both aspects can be observed in the linked example. Content beneath the image can be removed if not needed.

Answer №2

When styling the elements, be sure to apply position:relative; to the wrapping div containing the image and position:absolute; to the text div.

Answer №3

Need to establish the percentage.

Take a look at the illustration - description box aligned in horizontal center,

Initially, ensure that the position is relative within the wrapper div

.description {
    position:absolute;
    top:510px;  
    left:50%;
    width:340px;
    margin:0 0 0 -170px
}

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

Issue with Bootstrap cards overlapping while searching

On my Bootstrap 4 page, I've incorporated cards that can be searched, displaying only those with matching titles. However, I am facing two main issues. Firstly, when two matching cards are on the same row, they overlap. Secondly, if a matching card is ...

How come the information isn't being transferred between components?

Is my method correct if I want to pass the this.prompt() function from TitleBar to Portfolio? This is what my index.html file looks like: var TitleBar = React.createClass({ render: function() { return( <div className="jumbotron"> ...

What is the process for establishing a reference to a property of an object in JavaScript?

Imagine you have an object structured like this: obj = {a:{aa:1}, b:2}; You decide to create a convenient variable (referred to as a pointer) named x that points to obj.a.aa with the following code: x = obj.a.aa; Next, your goal is to update the value ...

axios in node does not support relative URLs

When running my node server, the code below works just fine axios.get('http://localhost:8080/myPath') // works Unfortunately, using relative paths does not work axios.get('/myPath') // doesn't work This is the error I encounter ...

There is no 'depto_modules.length' property in this row. How should I go about fixing this issue?

I have a table set up to display data from an associated table. The functionality is working fine, but I keep seeing a warning message when I apply certain filters: The warning states that the property depto_modules.length does not exist in the row. It ad ...

Avoid directing to the identical component within a React application

When I try to redirect my component to the same component with different parameters, it is not working properly. <BrowserRouter basename={process.env.REACT_APP_DEFAULT_PATH ?? ''}> <Switch> ... < ...

Steps for resetting the HTML5 meter element

I have implemented the meter element to rate between 0 and 5, but I want to display a specific image for each value instead of the default meter style: <meter min="0" max="5" value="5"> <img width="80" height="20" alt="5 out of 5 stars" src=" ...

including a "continue" option to the jplayer interface

Does anyone know how to add a next button on jplayer? I've tried using the click function, but it doesn't seem to be working. The player is set up to use ajax to fetch songs and information about them. When one song ends, another is retrieved th ...

Tips on extracting images from a blob:http localhost URL using jquery, angularjs, or webgl

Can anyone guide me on how to retrieve an image from a localhost URL by using jQuery, AngularJS, or WebGL? I have the URL of the uploaded image file in the format: blob:http%3A//localhost%3A9000/87424398-8ee0-4db1-a653-bc18838d6b24. My goal is to display t ...

Tips for downloading a file using a Django function triggered by JavaScript instead of redirecting to the URL

Managing a page with multiple buttons that trigger various functions, such as sending an SMS (via API), emailing a file, or downloading a PDF. The button actions are powered by Ajax requests through JavaScript instead of using forms. I initially used Java ...

Encountering a problem with integrating Vue js and making a jquery

Hello there! I'm currently working on fetching data through ajax and utilizing a vue wrapper component. Here's the code snippet I'm using: <html> <head> <title>title</title> <meta charset="UT ...

Transform an array containing strings into an array containing objects within a JSON property

I have received the following JSON data from an endpoint: { "response": { "lines": [ "[{'line':'3007621h7s2','type':'national'},{'line':'3007663f7s9','type':&apo ...

Save this page for later by using JavaScript to create a

Does anyone have a solution as to why window.location.href does not save the current webpage URL as a bookmark? Thank you ...

Transitioning from Bootstrap 4 to Bootstrap 5 raises concerns about container compatibility

Interested in transitioning from Bootstrap 4 to Bootstrap 5. Noticed that the container max-width appears different on the default setting. Any insight on why this is and how it can be adjusted to resemble Bootstrap 4? I'm a beginner with Bootstrap, s ...

AngularJS: Utilize the ngStyle directive to add styling to a specific child

I'm trying to style an embedded svg file within a span using ng-style based on its index. Can anyone help me figure out how to do this? <li ng-repeat="menuItem in menuItems" class="menuitem" ng-class="{'smenuitem': ($index === menuselect ...

Challenges Faced when Connecting JavaScript to HTML

This is a snippet of my HTML where I link the .js file <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="CSS/Style.css"> <title> Learning JavaScript </title& ...

Display or conceal content based on checkbox status

i am trying to create a system where content is hidden by default and only displayed when a checkbox is checked. The goal is to show the content when the checkbox is checked and hide it when unchecked, so that other content can be shown as well upon checki ...

What steps should I take to ensure that the <select> tag is compatible with Microsoft Edge?

Currently, I am working on editing jsp files that utilize struts1. <html:select property="someProperty" style="width:110;height:110" styleClass="someClass"> However, when viewing it in Microsoft Edge, I noticed that the drop-down menu already ...

Rotate text vertically (90 degrees) on IE and Firefox browsers

On one of my pages, I have an asp GridView and I want to display the text vertically to improve its printing quality. I am currently using CSS to achieve this effect: .rotate { -webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg); width: 25px; ...

JavaScript is able to access the HTML content of the previously opened tab when saving the window

Seeking advice from the knowledgeable community at Stack Overflow! I have a project that I'm unsure how to start, and I could use some fresh ideas. My goal is to access the HTML source code of a previously opened tab or one that is still loading on m ...