The element is not displaying the correct width percentage value

When I try to style an element in HTML using Sass, setting the width using percentages doesn't seem to work.

I wonder if this issue is related to me using "content-max" to set the parent element's width and height?

Here is a simplified version of the code I'm working with. The goal is to limit the parent's size by the image size (which is functioning correctly), and have the text overlay occupy half the width and full height of the container element (hence the use of 50 and 100 percent).

<div class="container">
      <img src="image.png" alt="A square image">
      <div class="text-overlay">
                
      </div>
</div>

The separate compiled Sass looks like this:

.container {
        
     display: block;
     position: relative;
     width: max-content;
     height: max-content;
        
     img {
          display: block;
          position: absolute;
          z-index: 0;
     }
        
     .text-overlay {
        
           display: flex;
           position: absolute;
           width: 50%;
           height: 100%;
           z-index: 1;
     }
}

Both the HTML and SCSS codes are written in separate files and correctly compiled when needed.

Answer №1

It's important to note that Sass cannot be written directly within HTML markup and must be compiled before use.

In the code provided, nesting is used which is not valid CSS3 syntax. To address this issue, you can either write plain CSS as shown below or compile your Sass before applying it to your HTML.


Regarding the positioning question, the desired outcome can be achieved by explicitly setting the width and height for the .container element.


.container {
  position: relative;
  width: 200px;
  height: 200px;
}

.container img {
  position: absolute;
  z-index: 0;
}

.container .text-overlay {
  position: absolute;
  z-index: 1;
  width: 50%;
  height: 100%;
  background-color: green;
  color: red;
  opacity: 50%;
}
<div class="container">
  <img src="https://i.picsum.photos/id/793/200/200.jpg?hmac=3DeE830wjdSShKq_h_iFtV_jAxf43FO4xx-sivW0Q_Y">
  <div class="text-overlay">
    Overlay
  </div>
</div>

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

Using jQuery Datatables with Asp.Net

Having encountered an issue with displaying two jQuery datatables in separate menu tabs, I am seeking assistance to rectify the problem. The first datatable (id=gvSchedule) appends successfully while the second one (id=gvMySchedule) loses its formatting up ...

Looking to display or conceal several divs according to the option selected from a dropdown menu?

I've been searching for a solution to my simple dropdown issue, but haven't had any luck in the forums. This is the code for the dropdown: <select id ="category_faq"> <option value="1">item1</option> <option value= ...

Trouble with CSS navigation

Hello! In Dreamweaver, I have completed the design for my navigation bar. Here is how it looks: However, when I view it in Firefox, the navigation appears all jumbled up. It's quite frustrating... Below is the code that I'm using: #navbar { ...

What is the best way to place a child on top of a different parent in the stack?

There is a collection of figure tags, each with an expandable figcaption. Inside each figure, there is a clickable div that reveals the figcaption when clicked. However, the visible figcaption only overlaps the figure it belongs to and not others. Is there ...

What is the best way to assign group headings to my Codeigniter view results?

One of the challenges I'm facing in my view is organizing a list like this: <? foreach($services as $service): ?> <tr> <td><?= $service->name; ?></td> <td><?= $service->format; ?></td> < ...

I've been attempting to access the Marketo API, but unfortunately, I haven't had any luck

I've been attempting to make a Marketo API call, but I keep encountering this issue: net::ERR_NAME_NOT_RESOLVED Here is the code snippet I'm using for the function: var marketoAPIcallURL = 'https://182-EMG-811.mktorest.com/rest/v1/ ...

What is the process for including body text in a Bootstrap tooltip?

I am attempting to enhance a Bootstrap tooltip by adding body text, as it typically only includes a basic title. After reviewing the Bootstrap 5 documentation, it seems that the simplest approach is to utilize the template option: template Base HTML used ...

Reposition the jQuery tooltip arrow

I need help adjusting the position of the arrow to the left near the text box. Can anyone assist with this? Here's what I've tried so far: Check out the working example here: http://jsfiddle.net/b8fcg/ HTML: <input id="test" title="We ask ...

Activate CSS modules in ReactJS by incorporating a .module extension to the CSS file

Being new to ReactJS, I recently learned about enabling CSS modules and discovered the following: If you append .module to a class name, it will generate a base64 hash string for that class As an example, I created a function-based component called Lay ...

What causes certain images to have unspecified height and width measurements?

Currently, I am in the process of extracting image sizes from a website using a module called scraperjs. Most images have their height and width attributes defined, but some do not, resulting in the returned value being "undefined". How can I retrieve the ...

Unable to fetch the webpage element object

While attempting to extract data from the historical table on investing.com, I encountered an issue where I couldn't retrieve the column item and an error occurred. Traceback (most recent call last): File "<ipython-input-119-ba739f477693>", ...

What is the reason position:sticky does not align elements to right:0?

I attempted to align my element with the right:0 property, but it doesn't seem to have any effect. The element remains stuck to the left edge of the browser window. * { margin: 0; padding: 0; font-size: 50px; } .sticky { width: 50px; h ...

The div is incorrect and causing the label and input to move in the wrong direction

Whenever I try to adjust the position of the current-grade-name-input, the entire grade-point-input moves along with it inexplicably. /* final grade calculator: (wanted-grade - (current-grade * (1 - final%))) / final% Ex: have a 80 an ...

"jQuery Ajax function running smoothly on my local machine, but encountering issues when deployed on the server

I have explored extensively across the web in search of a solution to this issue. I possess a jquery ajax script that loads a csv file and creates html code by utilizing the data from the csv file. <script type="text/javascript"> $(document). ...

Personalize the File upload with HTML and Javascript

https://jsfiddle.net/yugxqopz/ I'm new to the world of UI and have a simple request: 1) I want to upload a document using an "attach file" option 2) Once the file is selected, I want to automatically trigger a specific JavaScript function as shown ...

Adaptable data table featuring varying sizes of columns and rows

I'm attempting to design a grid that resembles the following: https://i.sstatic.net/Sf7M9.png The challenge I'm facing is how to ensure that the column and row names adjust properly with the grid in the center. Currently, this is the code I hav ...

Is CSS General sibling selector not functioning properly when used with :focus?

How can I display text that turns into an input form when hovered over, and stays visible even if it's unhovered but the user is interacting with the input form? The issue is that when the input form is focused while unhovered, both the input form an ...

Troubleshooting a Vue.js formatting problem in Visual Studio 2019

Encountering an issue with VS2019 while attempting to format this code section. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="milestone.ascx.cs" Inherits="uc.dms.milestone" %> <section class="content-header"> <h1> ...

Enhance your video with Bootstrap 4 by overlaying text and buttons

I am in search of a solution that resembles the following design, but it needs to be styled using Bootstrap 4. Here is my current code snippet: <section class="banner embed-responsive-item"> <video class="hidden-sm-down" autoplay="" loop=""& ...

Steps to reposition an element with absolute positioning to the upper left corner of the screen

I am currently in the process of creating a hamburger menu without using JavaScript, which should drop down from the top to the bottom when clicked. The burger menu is situated at the top right corner of the screen, but every time I try to drop down the na ...