Is there a way to verify if the overflow of an element with a width of 100% has occurred?

Here is the structure I am working with:

<div class="card">
    <div class="container">
        <slot />
    </div>
</div>

The Card element has a fixed width of 33rem. The container within it spans 100% of the width. I need to determine when the content inside the slot overflows, so that I can add a class to the container element with a word-break property.

I attempted to achieve this using the following code snippet:

 if(el.clientWidth < el.scrollWidth || el.clientHeight < el.scrollHeight) {
      console.log('overflows')
}

However, this approach does not work because the el(='container') is set to a width of 100%. How should I proceed in order to accomplish this task?

PS: The styles applied to my container element are as follows:

  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;

Answer №1

It appears that the code example works when examining the snippet in which I implemented your provided code. The presence of white-space: nowrap; results in the log being displayed, while its removal eliminates it. If this demonstration does not exactly align with what you intended, please share a functional snippet to gain a clearer insight into your issue.

const el = document.getElementById('cont');

if (el.clientWidth < el.scrollWidth || el.clientHeight < el.scrollHeight) {
    console.log('overflows')
}
#cont {
  width: 100%;
  outline: 1px solid red;
  white-space: nowrap;
}
<div class="card">
    <div id="cont" class="container">
        Lorem ipsum...Lorem ipsum... Lorem ipsum... Lorem ipsum... Lorem ipsum...  Lorem ipsum... 
    </div>
</div>

Answer №2

My approach to handling document bodies has been successful, but when it comes to containers, Chromium's bugs can sometimes cause these values to return as zero!

It seems like the issue may be related to positioning, display, and other properties.

if((e.scrollHeight>e.offsetHeight)||(e.scrollWidth>=e.offsetWidth)){
    
}

By the way, instead of instructing people on the container dimensions, it's better to insert them directly into the code!

You should provide a complete working example with Lorem ipsum text included for reference.

Answer №3

To address the issue, I solved it by including a width: 0 property to the flex-item component which was initially set with flex:1.

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

Concealing the nearest div that has a particular class

I'm currently developing an application that simulates desktop-style application windows with the ability to open, minimize, and close. While I have successfully implemented the functionality to expand and collapse these windows, I am facing difficult ...

Issue with div refresh switching from $.post to $.get using jQuery and duplicate div

I created this code to trigger the display of another div (#comments-popup) when hovering over #story-comments-text. This div then makes an $.get request for its HTML code. The first part is functioning correctly. However, I encounter an issue when makin ...

Resolving Issues in HTML and the Dynamic Duo of PHP and MySQL

Hey there, I've been a reader for a while now but this is my first time posting. I'm really into PHP and I've been working on a page lately. Right now, I've got the DB connection set up nicely and my SELECT statement is doing exactly wh ...

Remove items from an array using other items from another array as the index

Let's consider the following scenario: let arr1 = [0,2] // This array is always sorted Just to clarify, these elements in the "arr1" array represent indexes that need to be removed from another array. We also have another array: let arrOvj = [1,4,6, ...

How to manage the state when multiple forms are generated by clicking a button

I am currently working on a feature that allows users to add multiple addresses, with a limit of 3 additional addresses in addition to the default one that cannot be removed. For this specific case, each address consists of a street name and city. My cha ...

MongoDB's findOne is returning null when it shouldn't

Why is my template returning null when it shouldn't be? Here is my findOne function: await this.findOne({name}, async (template) => { console.log(template); if (template) return cb(new Error('Template already exists')); I am ce ...

Extracting Table(Array) Index into a Component in Angular

For my data on the HTML page, I created a 2-dimensional array. <tbody> <tr *ngFor="let i of myarray2"> <td *ngFor="let j of i"> {{j}} </td> </tr> </tbody> This is how it appears: https://i.sstatic.ne ...

The button's status changes to disabled until I click outside the input field in Angular

I am currently facing an issue with a form (heat index calculator) that requires 2 inputs - a dropdown and a button. The button is disabled when there are no inputs or if the inputs are invalid. Everything works correctly, except for the fact that even whe ...

Create automated scripts with Selenium using the programming language JavaScript

Is it possible to create selenium webdriver scripts using only javascript? If so, what are the benefits of choosing javascript over languages like java or C#? In what situations would javascript be the preferred option? Appreciate your insights. ...

Position the image to the right side of the Bootstrap Card

When working within a Bootstrap Card, I am trying to position the image to the right of the title and text, but it currently appears below them. How can I make the image align all the way to the right of the screen, next to the title and text? HTML: <d ...

The comparison between local variables and data can result in a significant drop in performance

My current project involves VueJS and Cesium, but I'm facing a performance issue with a significant drop in frame rate. While I have identified the problem area, I am unsure of why this is happening and how to resolve it. export default { name: ...

Creating uniform image heights using Flexbox

My webpage features a library of books, with each book displayed in rows within a flexbox container. Each book is represented as a separate block consisting of a heading (the book's name) and an image of the book cover directly below it. I'm curr ...

What is the functionality of background attachment fixed?

I am curious about how background images behave when the background-attachment property is set to fixed. Here are my questions: If I set a background image for a div with dimensions of 500px by 500px and add a 1px solid red border, then apply backgro ...

Why am I unable to view the image in the material-ui-next "Cards" example?

I came across this example on the material-ui-next website, but strangely I am unable to view the lizard image when I try to run it on my computer. Why is the image not showing? There are no errors in my console, and the "simple card" example on the websi ...

Interacting with a form input by triggering the onChange event

I am encountering a problem where I need to be able to select a radio button both onChange via keydown and mouse click. However, I am struggling with accessing both event parameters of the on keydown and on mouse click within the same function. As a result ...

Is there a method by which I can access information from a linked document in Firebase?

I am relatively new to firebase and I am attempting to retrieve data from a referenced document in another collection to display. Link 1 Link 2 This is how I add a student and the parent's ID: const newStudent = { name: req.body.name, grade: ...

I encountered an issue with 'JSEncrypt is not defined' when running ng test in Angular 4

function encryptData(data) { var encryption = new JSEncrypt(); } I implemented this function in my security.js file and imported it as follows: 'declare var JSEncrypt: any;'. However, when I run the ng test command, I encounter the following ...

What are the benefits of using a combination of design patterns in JavaScript?

Currently, I am working on a personal project for learning purposes, which is a simple To-Do List. I am implementing the modular pattern (specifically, the revealing module pattern). The image below showcases my general idea of how I intend to build it. V ...

Getting the Mongoose Model using Express parameters is a simple process

Is it possible to dynamically fetch a mongoose model based on the req.params.model? Check out this example of a Schema const mongoose = require("mongoose"); const Schema = mongoose.Schema; const SmartSchema = new Schema({ SmartPriority: { type: Stri ...

Building a RESTful API in Node.js using Express and MySQL

https://i.sstatic.net/ZXYoC.png Hello everyone, I recently created a REST API using Node.js Express and MySQL. Everything was working perfectly fine on my local machine. I decided to host it on Cpanel and now I am facing an issue when trying to fetch data ...