Adjust the size of text overlaying an image using CSS, and modify it further

I am working on updating a sensor value every few seconds and displaying the type of sensor using an image. Here is an example of my code structure:

<div class="sensor">
    <img src="images/ssensor.png" 
         class="img-fluid img-max">
    <div class="sensortext">
        sensorvalue
    </div>
</div>

using the following CSS:

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

.sensortext {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-30%, -80%);
}

While this setup works well, I encounter issues when resizing the window - the images change size but the text remains static. How can I ensure that they scale proportionally?

Answer №1

If you're looking for a solution, consider using the `vmin` unit as recommended by Rene in the feedback. Another approach could be to incorporate it with a `clamp` function, which allows you to define minimum, optimal, and maximum values. Experimenting with these parameters can help you determine the best fit for your needs!

Additionally, when it comes to simple overlays, opting for a grid layout instead of absolute positioning might be more beneficial. This eliminates the need to deal with stacking contexts.

.container {
  display: grid;
  place-items: center;
}

.container * {
  grid-area: 1 / 1;
}

.container-text {
  color: white;
  font-size: clamp(3rem, 10vmin, 6rem);
}
<div class="container">
  <img src="https://loremflickr.com/640/360" alt="placeholder">
  <div class="container-text">
    Overlay text
  </div>
</div>

Answer №2

Here is a suggestion for CSS implementation:

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

.sensortext {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.img-max {
max-width: 100%;
height: auto;
display: block;
margin: 0 auto;
transform: translate(-50%, -50%) scale(1);
}

An issue arises when the text remains in a fixed position while the image is resized. To address this, you can also apply the CSS "transform" property to the image element. By using the values of "translate" for X and Y axis displacement and "scale" for resizing the image, the text will dynamically center itself in relation to the image.

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

Struggling to effectively use XPath to target LI elements that contain specific text

Below is the HTML code for the list item in question: <li class="disabled-result" data-option-array-index="1" style="">4" (0)</li> Here is my attempt at using JavaScript to hide this list item, but it's not working as expected: var xpat ...

Insert a picture within the text input field

I'm facing a specific scenario where I need to add text followed by an image, then more text followed by another image and so on. Please see the input text with values in the image below. Can someone guide me on how to accomplish this with jQuery and ...

Trouble outputting ng-repeat with JSON data

I am having issues with my chatController.js and chat.html chatController.js $scope.getMessage = function() { Chat.getMessage("C0RRZEDK4") .success(function(response) { console.log("getMessage: " + JSON.stringify(response)); $sco ...

Center content within a div using the middle alignment

Here's a question that goes beyond your typical "vertical align center" query. What I want to accomplish is to take a div with an unspecified height and position it at a specific percentage down the page, let's say 33%. I have managed to get this ...

Will my HTML5 canvas effects be compatible with iPhone and Android devices?

Experience the magic of HTML5 canvas effects on my website With just a click, you can add clouds to the sky and even track the frames per second. This feature runs seamlessly on supported browsers like Chrome, Firefox, Safari, IE9, but I am unsure about ...

The innerHTML of the p tag remains unaffected when using document.getElementsById("")

HTML {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'lessons/style.css' %}" /> <script> function openNav() { document.getElementById("mySidenav").style.width = "20%"; document.getElementById("main" ...

Bottom section of a multi-level website with horizontal dividers

I was struggling with aligning divs next to each other, but I managed to find a solution. The only issue is that the text now aligns to the right instead of going below as it should. Typically this wouldn't be an issue since all content is within the ...

Database entries displayed using a CSS grid

Struggling to organize elements in a grid layout using CSS from my database. Despite all efforts, the items keep aligning to the left instead of forming a proper grid https://i.sstatic.net/5SfJZ.jpg I've attempted various methods to grid the items, b ...

Removing the nested UL element while preserving the child elements

Looking to simplify a nested list structure by removing the <ul> tags and moving its child <li> elements to the parent <ul> element. Here is an example : <ul> <li>Item 1</li> <ul> <li>Item 2& ...

Utilizing JavaScript to dynamically resize an element within a slide as soon as the slider transitions to the following slide

RESOLVED! ISSUE FIXED! While working on my personal website using WordPress and the Smart Slider 3 plugin, I encountered a problem with the positioning and size of an element in the second slide. Despite finding a tutorial explaining how to manually trigg ...

Breaking HTML text at the end of an Android TextView line

When setting the HTML text in my text view, I use the following code: mTextView.setText(Html.fromHtml("THIS USEFUL INTERESTING HTML TEXT IS DISPLAYED IN ANDROID TEXT VIEW")); The Text appears like this: https://i.sstatic.net/sUmSN.png However, I noticed ...

Expanding an HTML table with a click: A step-by-step guide

I have successfully generated an HTML table using JavaScript. However, I now need to display additional data in a row by expanding it on click. Table functionality: The table is populated with brands and their respective items. When a brand is clicked, ...

What's the best way to ensure a paragraph element larger than its container is centered using bootstrap?

Need help with centering a paragraph element (email) that is larger than its container: https://i.sstatic.net/f3E55.png Here's the code I have so far: <div class="col-lg-2 col-lg-offset-1 text-center"> <a href="mailto:{{ site.email }}" ...

What is the best method for choosing the next item with jQuery?

I am facing an issue while trying to apply some design on the next element. The error message that I am encountering is: Error: Syntax error, unrecognized expression: [object Object] > label Below are my selections for browsing by category: BROWSE BY ...

Getting tags with text containing specific substring by utilizing CSS selectors

Here is the HTML snippet I am working with: <tr bgcolor="#DEDEDE"> <td> <b>OK:Have idea</b> </td> <td> <b>KO:Write code</b> </td> <td> <b>KO:Ru ...

Exploring the integration of Polymer elements within a .Jade document

Recently, I began delving into the world of Polymer and web development. I noticed that the Polymer elements load perfectly when I view the HTML file in my browser. However, when I convert the HTML file to Jade format, the resulting Jade file does not disp ...

Do we really need to include the viewport tag in order to make a website responsive to various devices?

Hey there, I'm looking for some help with customizing my website for different devices. I've been experimenting with media queries and successfully adapted my phone (Motorola Moto G) to both landscape and portrait orientations without using the ...

Tips for dynamically incorporating input forms within AngularJS

I am trying to dynamically change the form inputs using ng-bind-html. However, I am only able to display the label and not the text box on the DOM. The content inside ctrl.content will depend on the values received from the server. Important Note: The ...

The feature to swap out the thumb of a range slider with an image is currently not

I'm attempting to design a unique range slider using CSS. <style> .slide-container { width: 300px; } .slider { -webkit-appearance: none; width: 100%; height: 5px; border-radius: 5px; background: #FFFFFF; outline: none; opacity: ...

Is it possible to use Selenium in Ruby to locate an element based on two properties simultaneously?

Let's start by looking at an element structured like this: "<"id="select_a_boundary" class="dataset_select2">Homes name<> In selenium with Ruby, when we want to find an element based on a property, we typically use methods like: @driver ...