Adjusting the position of an element when the width of its parent div shifts

Struggling with the challenge of absolute and relative positioning in CSS. My GUI allows users to view folders and select how many columns they appear in, as shown in the images provided. Beneath each folder is an input field for renaming and a clear button.

The issue arises when the width of the columns changes, causing the absolute positioning of the clear button to misalign. See examples here: Picture 1 - correctly positioned; Picture 2 - misaligned button.

<div ng-class="colStyle" ng-repeat="folder in getFolders(currentFolder)">                               
    <input type="image" ng-src="{{folder.img}}" ng-click="enter(folder)"/>                              
    <div ng-show="showFont && editing" class="testing">
        <input type="text" ng-model="folder.name"/>                                     
        <img class="clearName" src="img/clear.png" ng-click='clearName($index, folder)'/>                               
    </div>                              
</div>

My HTML code uses "colStyle" from responsivegridsystem.com to define the number of columns displayed. Relevant CSS classes are "testing" and "clearName":

.testing{
    position: relative;
    margin: 0px;
    padding: 0px;
}
.clearName{
    position: absolute;
    left : 80%;
    top: 5px;
}

The problem lies in setting the absolute position of the clear button based on column changes. How can I ensure it remains consistent regardless of the number of columns chosen?

Answer №1

Why not start styling from the right of the input field?

Imagine you want to position the clear button 5px from the right side, you can achieve this with the following CSS:

.clearName{
 position: absolute;
 right: 5px;
 top: 5px;
}

This method assumes that the input box spans the full width of the outer div.

The use of percentages in this case is dynamic because it adjusts based on the width of the outer div (100px => 80% => 20px and 200px => 80% => 40px).

If you don't want to make the input field span the width of the outer div (.testing), you can reverse the approach by making the outer div (.testing) match the width of the input field.

To do this, you can apply the following CSS:

.testing {
  display: table;
  margin: auto;
}

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 retrieve an element from a JSON scope based on the value of another JSON scope in AngularJS?

I am dealing with two simple JSON scope variables. My goal is to display a specific object's value from the first JSON based on another object's value in the second JSON, For example: {{json1.json2.id}} The issue I'm facing is that Angula ...

AngularJS Modal Button

After adding a button and writing the necessary code for implementing a modal in AngularJS, I encountered an issue where the modal was not displaying as expected. Below is the HTML code snippet that I used: <body> <div ng-controller="Mod ...

I'm having trouble getting my window resize event listener to function properly. I'm using a combination of Three.js and Vuetify.js

My goal is to incorporate a three.js renderer into a div element using the vuetify.js framework. I want my code to dynamically adjust the dimensions of the div element whenever the window is resized. I have excluded unnecessary code blocks from this excer ...

The KeyboardAvoidingView disrupts the structure of the flexbox layout

Check out this code snippet: return ( <KeyboardAvoidingView style={{ flex: 1 }} behavior="padding" enabled> <View style={style.flex1}> <View style={style.imageContainer}> <Image style={style.image} ...

Offspring element overrides styling of its parent

#popBox{ width:100%; height:100%; position:fixed; left:0; top:0; border-collapse:collapse; background:black; opacity:0.8; display:none; } <table id="popBox"> <tr> <td></td> <td></td> ...

Keep table header stationary while accommodating varying column widths

I have come across various solutions for freezing a table header row, such as creating a separate table containing only the header and hiding the header of the main table. However, these solutions are limited to cases where column widths are predetermine ...

Avoid repeating media queries in MUI JSS by combining them efficiently

Is there a more efficient way to apply the same set of CSS styles to multiple media query rules without repetition? Currently, the only workaround seems to be: [theme.breakpoints.up('xl')]: { color: 'red', backgroundColor: 'gre ...

Guide to positioning an image absolutely within a div

I am struggling to position a small close image within a div without success. The goal is for the image to always appear on the left side of the div, regardless of the content. The content consists of a text label with a maximum length of 8 characters. Iss ...

Retrieving Basic HTML Source Code in UITextView Without CSS Styling Using Objective-C

I am looking to make changes to HTML text within a UITextView. The original text was created using an HTML editor in the web version of the app and needs to be edited with a default font on iOS. Here is the simple HTML code for the text that needs editing ...

What is causing the material design dialog body to appear without any height on an iPad?

Currently, my web application utilizes angularjs along with material-design. For adding and editing certain records from the database, I rely on md-dialog. It works perfectly on desktop browsers except for IE (which is not surprising). Check out how it sh ...

Is there a way to programmatically click on an href link in Firefox? The method that is typically used in Internet Explorer

http://jsfiddle.net/HVGre/1/ - check out this test link. I have a link in my HTML code that needs to be clickable dynamically. While it functions well with the .click() method in Internet Explorer, it fails in Firefox. Unfortunately, changing the link to ...

What is the best way to smoothly transition in an image that is dynamically set using Angular?

I am using Angular to set a background image for my page, but the current loading behavior is not visually appealing as the image loads from top to bottom. I would like to implement a fade-in effect or load the image from a blurry view to enhance the user ...

Using a variable to store the value of the id attribute in HTML code

How can I dynamically add an ID attribute to an HTML element using a variable declared in JavaScript? Using jQuery var table_row = $('table').find('tr#' + pid); var name = table_row.find('td:nth-child(1)').html(); table_ ...

Guide on displaying Adsense ads specifically for mobile devices

Can anyone provide me with a proper method to show/hide my AdSense ads on both mobile and desktop? I am currently using a method that results in 2 console errors. Here is the current approach: We are utilizing two classes, "mobileShow" and "mobileno," t ...

Enhance your HTML rendering with Vue.js directives

Check out this cool code example I created. It's a simple tabs system built using Vue.js. Every tab pulls its content from an array like this: var tabs = [ { title: "Pictures", content: "Pictures content" }, { title: "Music", c ...

Angular.js: The $setDirty() method on a form does not automatically affect its child form elements

Currently, I am working on a form validation project using Angular.js. A specific challenge that I am facing is setting the dirty state on a child element of a form in an isolated scope within a directive. Does anyone know how to achieve this and set the i ...

Adjusting the transparency of one element and rotating another element that are the same size

This issue is driving me nuts: I'm attempting to rotate the border of a circle element and simultaneously adjust the opacity of the circle overlay when a user hovers over it. However, in my current setup, only the opacity changes: <div id="main-c ...

AngularJS uiBreadcrumbs failing to display correct breadcrumb trail

For my current project, I have implemented angular breadcrumbs using the library available on GitHub. The necessary javascript files have been included through a bower install and save process. Here is the HTML code snippet: <div ng-include=" ...

Using a CSS button to activate a JavaScript function: A step-by-step guide

My current project involves writing a script to change the color of text when a specific button is clicked. The idea is that clicking the "Change color1" button triggers the text color change using the following code snippet: <button onclick="myFunction ...

The method of stamping masonry is not encircling

I recently discovered a new method in Masonry 3 called "stamp" that allows you to fix an element in place. However, I am finding that it is not working as expected. Check out this example from David DeSandro: http://codepen.io/desandro/pen/wKpjs Initial ...