Expanding the width of a child element causes it to exceed the boundaries of its parent

When I increase the width of a child element, it sometimes goes outside the boundaries of its parent element. The child element tends to only expand towards the right side, so I tried using the float property on the child element. Is there a CSS trick that can help me restrict the width of the child element from going beyond that of its parent?

Here is the code snippet:

HTML :

<div id="tdScreen" style="width: 500px; height: 300px;position:relative;border:3px solid black" class="ui-selectable ui-droppable"><textarea id="input1" rows="1" style="float: left; resize: horizontal; position: absolute; overflow: hidden; cursor: pointer; width: 100px; top: 100px; left: 300px;"></textarea></div>

JQUERY :

$('#incresewidth').on({
   keyup: function (e) {
        if (e.keyCode == 13) {                
           $("#input1").css("width",$('#incresewidth').val());
        }
    }

Here's the link to the jsfiddle.

Answer №1

Make a simple check in your code to ensure that the value is not greater than the size of the element.

var width = $('#incresewidth').val() > 190 ? 190 : $('#incresewidth').val();

https://jsfiddle.net/Panomosh/kwbr35uw/1/

It's not perfect since the maximum width value is hardcoded, but it gives you an idea of how to implement this dynamically.

Answer №2

Looking for a CSS workaround?

Absolutely, in cases where the element's container size and position are fixed or follow specific conditions as outlined in your example...
you can make use of the max-width property.

/*
  Assuming:
    container width: 500px
    container border: 3px
    textarea positioned at left: 300px

  then:
    500 - 3 - 300 = 197
*/

#input1 {
    max-width: 197px
}

Check out the updated demonstration here: https://jsfiddle.net/kwbr35uw/2/

Now, if properties such as dimensions, positions, or other factors are subject to change dynamically, opting for a javascript/jQuery solution might be more feasible..
similar to the suggestion provided by @joshStevenson (with dynamic measurements, of course)

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

Refresh the main view in MVC from a partial view

I am currently working on an application following the MVC architecture. Within my project, I have a Parent View that incorporates a Partial View. The Partial View contains a submit function that triggers a Controller Method. In case of any errors, I nee ...

What is the best method for centering text input within an Angular Material toolbar?

Can anyone help me with aligning input elements with buttons on an Angular Material toolbar? Check out the code pen: http://codepen.io/curtwagner1984/pen/amgdXj Here is the HTML code: <md-toolbar class="md-hue-1" layout-align="start center" style="mi ...

Looking to add a dynamic divider between two columns that can be adjusted in width by moving the mouse left and right?

If you're looking for an example of two columns adjusting their width based on mouse movement, check out this page from W3Schools. I'm trying to implement this feature in my React app, but I'm unsure of how to proceed. Below is the JSX code ...

How can parameters be passed to a named function from a controller in AngularJS?

In my controller, I am working on breaking up my code into named functions to make it more readable. However, I am facing an issue where the scope and injected dependency are null in the parameterized named functions. How can I access these inside the name ...

Incorporate Thymeleaf's HTML using the powerful JavaScript framework, jQuery

I am facing an issue where the Thymeleaf rendered HTML code is not being displayed by the Browser when I try to insert it using JavaScript. $('<span [[$ th:text#{some.property.key}]] id="counter" class="UI-modern"> </ ...

transferring iterative information via ajax data flow

My form includes hidden input fields that are manually declared in the AJAX data without a loop. How can I efficiently loop through them in the AJAX data? Below is my form script: <form method="POST" name="myform"> <?php for($i=1;$i<=5;$i+ ...

What is the best way to display input data (with names and values) in a textarea field

I'm currently working on a project that requires the value of a textarea to be updated whenever one of the input values in the same form is changed. Here is the HTML code: <form id="form" action="" method=""> <textarea readonly class="overv ...

Achieving Center Alignment for Material-UI's <Table> within a <div> using ReactJS

Currently, I am working with a Material-UI's <Table> embedded within a <div>. My goal is to center the <Table> while maintaining a fixed width. I want the table to remain centered in the browser, but if the browser window is minimize ...

Is there a way to ajax just specific HTML table rows instead of transmitting all the form inputs?

For weeks, I've been struggling to use AJAX POST with a JSP script to update my HTML table rows without success. Can anyone provide guidance on this? Below is what I have attempted so far. window.addEventListener("DOMContentLoaded", function () { ...

Stack div in a vertical orientation

I am dealing with a div tag that has a set height of 300px. Inside this parent container, there are multiple child divs and the content within them is dynamic so I cannot predict how many child elements there will be. How can I make sure that the parent co ...

Ways to iterate through an array within a datatable

I have a javascript function that renders a table like this: $('#serviceTable').DataTable({ responsive: true, aaData: services, bJQueryUI: true, aoColumns: [ { mData: 'service_name&a ...

Retrieve the appended element's object

I am currently facing an issue with accessing elements that are automatically added by a library in my code. In my HTML, I have the following line: <div class="bonds" id="original" style="display:block;"></div> The library appends some elemen ...

Place a picture and words within a submit button

I need assistance with aligning text and a small airplane image on a submit button. Currently, the text is overlapping the image and I am unsure how to fix this issue. How can I adjust the position of the text to display to the right of the button? Is ther ...

Issue with Bootstrap 4 Navbar collapsing and not expanding again

Help needed! My navigation bar collapses when the window is resized, but clicking on the hamburger icon does not open it back up. I have included my code below. Can someone please provide guidance on how to expand the collapsed navbar? <html lang=&quo ...

Is the JSON object missing from the DailyMotion API response?

Having some issues accessing the DailyMotion API. If you try running this code snippet in your JavaScript console: $.getJSON('https://api.dailymotion.com/video/xirgl9?fields=id', function(data) { console.log(data); }); Any ideas why it's ...

Angular animations do not seem to be functioning properly with ng-show when changing the class using ng-class

I need assistance in creating a slider for a list of objects using ng-show and animations. The current setup works smoothly when the objects slide in one direction. However, I am facing issues when trying to implement the functionality for users to slide ...

"Enhancing the spacing between background images through CSS: A guide to using the repeat property

Looking to design a background with a repeating image using CSS. background: url(../images/bg.PNG) repeat; However, facing an issue where the images are too close together. Any suggestions on how to add padding around each image? ...

Organize elements with precision using html and css properties

I am struggling to get the buttons on the card to align properly. I have set a minimum height to ensure they are consistent in size regardless of content, but the alignment of the buttons is off. Take a look at the photo attached for reference - I want the ...

Disabling all images within a <td> element by selecting them

Here is a sample of my HTML code: <table class="disabled"> <tr> <td> <input blah blah> </td> <td> <img id="reallyLongASP.NetID" etcetc/> </td> </tr> < ...

I'm stumped by this code; I can't figure out what's going wrong

$(document).ready(function() { $.ajax({ type: 'GET', url: 'data.json', dataType: 'json', success: jsonParser }); }); $(".btn_val1").click(function() { ...