What is the best way to make text adapt to different screen sizes when placed on an image

I'm encountering some difficulty in finding the right approach to pose this question, but my current task involves loading text on top of an image. This seems quite challenging, but I managed to achieve it by following a tutorial found here. However, the tutorial is outdated and does not provide guidance on dynamically adjusting both font size and span size for mobile devices while keeping the text correctly positioned over the image.

When resizing the window, the text and box do not scale properly and end up overflowing outside of the image boundaries.

Despite trying percentage sizing and other techniques, I have been unsuccessful in resolving the issue. Below is the CSS code I am currently using to overlay text on the image with a background:

What is considered the best practice for placing text on top of an image and ensuring its responsiveness? For desktop browsers, I am employing the following code:

.herotext span {
  position:     absolute;
  top:          80%;
  font-family:  'museo_slab500';
  font-size:    150%;
  color:        #fff;
  padding-left: 20px;
  width:        40%;
  line-height:  35px;
  background:   rgb(0, 0, 0);
  background:   rgba(0, 0, 0, 0.7);
}

If anyone has insights on how to address this issue effectively in contemporary web design, I would greatly appreciate your advice. The referenced article dates back to 2009, suggesting that there may be better methods for overlaying text on images nowadays.

Answer №1

Check out the recommended modifications:

  • To maintain a consistent margin between the span and the bottom of the image, position the span using bottom instead of top.
  • Implement a percentage-based line-height to ensure it adjusts proportionally to the font-size.
  • Incorporate some padding to the right side of the span to prevent text from directly touching the edge.

CSS Update:

.herotext span {
    position:    absolute;
    bottom:      20px;
    font-family: 'museo_slab500';
    font-size:   150%;
    color:       #fff;
    padding:     0 20px;
    width:       40%;
    line-height: 150%;
    background:  rgb(0, 0, 0);
    background:  rgba(0, 0, 0, 0.7);
}

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

What is the best way to build a personalized discussion platform: employing tables, <ul> <li>, <dl> <dt>, or div elements?

Looking to create flexible columns and rows with borders on the left and right of each column. If one row in a column stretches more, the other columns in that row should also stretch accordingly. Came across liquid 2 column layouts and 3 column layouts, b ...

Is it possible to adjust the font size of text files that are embedded?

I have been struggling to change the font size of an embedded text file, but the style doesn't seem to be working. Despite trying various approaches, nothing seems to make a difference. <style> pre { font-family: "Courier New", monos ...

"What is the best way to prevent a button from being enabled in Angular if a list or array is not

I am incorporating data from my service in the component: ngOnInit() { this._sharedService. getReceiptItem().takeUntil(this.ngUnsubscribe). subscribe(products => this.receiptItems = products); } Is there a way to deactivate a button if there ...

`The challenge of a single web page not displaying correctly`

I was working on a website for a local computer building company, and I decided to make it a single-page applet. The HTML is mostly done, and I don't have much trouble with CSS. However, I encountered an issue with hiding and displaying different sect ...

Change to a dark theme using React hooks in typescript

Hello, I am new to React and English is not my first language, so please excuse any mistakes. I have been trying to enable a dark mode feature on my website. Most examples I have found involve toggling between dark and light modes where you need to specify ...

Error message: Issue encountered while rendering Angular Bootstrap accordion template

Despite my attempts to find a solution before reaching out for help, I am struggling to get the accordion feature to work in my Angular and Bootstrap application utilizing ui.angular.js. The error message in the developer tools indicates an issue with the ...

Angular is unable to iterate through a collection of ElementRef instances

My list of ElementRef contains all my inputs, but adding listeners to them seems to indicate that textInputs is empty even though it's not. @ViewChildren('text_input') textInputs!: QueryList<ElementRef>; ngAfterViewInit(): void { ...

The Opera browser displays a horizontal orientation for vertical menus

Having some trouble with my vertical menu rendering correctly in different browsers. It's appearing horizontal in Opera, but looks good in Firefox and Chrome. I'm pretty sure it's just a small tweak needed in the code, but I can't quite ...

Retrieve the value associated with the data attribute of the chosen dropdown option

I need to extract the custom data attribute from the chosen option of a dropdown menu and insert it into a text box. This will be the second data attribute I'm working with. I plan to implement code that will run whenever the user changes the option ( ...

Can footer.html be omitted from the base.html file when extending it for Django?

I'm using base.html as my template which includes {% include 'footer.html' %} in every page. However, there are certain pages where I don't want the footer to appear. Is there a way to exclude the footer on specific pages using some sor ...

How to Use WebDriverJS to Target Multiple Elements by the Same ID

For my latest project, I am tasked with creating a test using Selenium, WebDriverJS, and Jasmine. The test is for a function that shows the content of a specific div only when its associated button is clicked. The challenge is to hide all other elements wi ...

What is the best way to create an adaptive <pre> tag for ASCII art without using media queries?

Currently, my username is displayed as an ASCII art banner/header and I am looking to enhance its responsiveness. Instead of relying solely on media queries, I am exploring the possibility of making it scale within a CSS grid container. This approach would ...

Unable to adjust the width of a table column within a horizontally scrollable container

I am struggling to resize the columns of a large table with 20 headers. Despite trying to place the table inside a div with overflow:auto, I still cannot achieve horizontal scrolling or make the div expand when resizing the columns. A sample code snippet ...

Empty vertical space on the right side in iPad view

I am currently working on the development of this website and facing an issue. https://i.sstatic.net/Y7B7Q.png When switching to iPad view in Chrome's developer mode, I noticed a blank column on the extreme right side that spans the entire page. I a ...

Customized link routing / Deep linking

I need some help. I am managing a standard website and I want to redirect custom URLs to specific pages when visitors access the site. For instance: - index.html should point to custom_index.cfm?custom_id=1&page_id=1&inc=index - about.html shou ...

What is the method for removing the border style property in an unordered list?

Currently working on designing a dropdown list using CSS. Below is the HTML code I am using: I have applied a border-right property to each li a element with border-right:1px dashed silver. However, I am trying to remove this property for the last li a ...

Elements powered by jQuery failing to load upon dynamic webpage(s) loading via ajax

Dynamic loading of jQuery elements, such as Ibuttons, seems to be causing issues when implemented with an ajax dynamic content loader. The entirety of my website is rendered through Ajax just like this: <html> <header> {jQuery} </header> ...

Modify jQuery to update the background image of a data attribute when hovering over it

Something seems to be off with this topic. I am attempting to hover over a link and change the background image of a div element. The goal is to always display a different picture based on what is set in the data-rhomboid-img attribute. <div id="img- ...

What is the best way to add style to a class or id with a dynamic index value?

This is my current style: #detailTable4 tbody tr td:last-child { background-color: #7E0303; } I want to apply this style to all elements with #detailTable (regardless of the index at the end). Can I achieve this using just CSS? ...

Load JSON information into a multi-select HTML dropdown box

For the multi-select box, users are required to select multiple items at once. The looping structure in the AJAX call will differ compared to a dropdown list. I have JSON data that contains names, addresses, and more. Additionally, I have a multi-select bo ...