Using CSS in combination with AngularJS, you can create a dynamic growth effect for a div element that initially

I am currently using AngularJS to dynamically add divs to a section. My goal is to have each div start with a static width and then grow dynamically as I continue to add more divs. How can I achieve this?

The following code is not producing the desired result:

<body ng-app="plunker" ng-controller="MainCtrl">
  <section>
    <div ng-repeat="div in divs track by $index">
      <div class="square"></div>
    </div>

  </section>
  <button ng-click="add()">add</button>

</body>

section {
  border: 1px solid red;
  overflow: hidden;
  padding: 10px 0;
  width: 400px;
}
.square {
  width: 100px;
  height: 100px;
  background: #000;
  margin-right: 10px;
  float: left;
  margin-top: 1em;
}

Link to Plunker:

http://plnkr.co/edit/SqGXBh9zXK2P3LCIVOPG?p=preview

Answer №1

Follow these steps to achieve the desired layout:

  min-width: 450px; //set minimum width for all blocks within the section
  display: inline-block; //allow width to adjust based on content

For the square element:

display: inline-block; //use inline-block instead of float to prevent height from collapsing

Check out this example on Plunker for reference.

//Note: The 'square' class has been moved to the parent node of 'ng-repeat', but this is optional. Simply adding 'display: inline-block;' will achieve the same structure.

Answer №2

To achieve this effect, you can also apply the float:left style to the section:

section {
  border: 1px solid red;
  padding: 10px 0;
  float:left;
}

Ensure that the squares are direct children of the section, which will cause them to fill up the entire section:

<section>
   <div class="square" ng-repeat="div in divs track by $index"></div>
</section>

Check out this Plnkr example

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

Creating a compact array from a larger array in JavaScript

I am currently using the jquery.bracket library and I am looking to split a large array into pairs like ["'Team 1', 'Team 2'"],["'Team 3', 'Team 4'"] from var all= ["'Team 1', 'Team 2'","'T ...

Determine whether the user has authorized the website with long-term access to obtain location information in the browser

Everything is running smoothly as I call navigator.geolocation.getCurrentPosition in my web app. Users are guided to a screen that explains the benefits of sharing their location, and once they press a button, the request is initiated without any issues. ...

Unable to alter the background color of the text box

I am currently struggling with my code where I am trying to overlay a text box on an image. Even after setting the background color to white, it still shows up as transparent. I have successfully done this in the past, but for some reason, it is not work ...

Having trouble putting Protractor to "rest"

Having trouble getting Protractor to rest. I have the following line in my spec file: browser.driver.sleep(5000); I've experimented with different solutions, but none of them seem to be effective.. Your assistance is greatly appreciated. ...

The error message from Object.create() indicates that the argument provided is not recognized as

Description Within my project, I am handling a JSON file, parsing it into an object, and attempting to transform it into an instance of the ProjectFile class using Object.create(). Code let tmpFileContent = fs.readFileSync(tmpPath, {encoding: 'utf- ...

What is the best way to place <a> links next to each other in the header?

Looking to align the links horizontally in the header using CSS, can someone assist me with this? <body> <nav> <div class="wrapper"> <a href="index.php"><img src="spaceship.png" alt="blogs logo& ...

Stubbornly Persistent Server Error (Django and Ajax)

I've been using Ajax to send my data over to views.py, but despite terminating all the urls and configuring the form action for my url, I'm still encountering an internal server error. I'm struggling to pinpoint the issue, so any assistance ...

Having trouble with closing the window on Mozilla and Chrome? Keep getting a scripting error message?

To close this window, click <a onclick="self.close();" href="#">here</a>. This is the code I am using. When I submit in Chrome and Mozilla, an error occurs. The error message is: "Script must not be allowed to close a window that was not o ...

Creating a webpage that loads directly to a specific section of content

After searching online, I couldn't find the solution I was looking for. My goal is to have the visible part of the page load halfway down the actual page. This way, when users visit the site, they can immediately scroll up to see more content. I hope ...

Despite receiving the response, the AJAX GET request is still encountering an error

I'm having an issue with a GET request to fetch JSON from an API for my website. Despite displaying the response correctly in the network tab of the Console and showing a 200 status (images attached), it keeps resulting in an error. I've verified ...

React dynamic table

I've been experimenting with creating a dynamic table in React that allows users to add and delete rows. I need the data entered by the user to be saved, possibly using in-state management so that I can work with it later. Essentially, I'm looki ...

Pairing AngularJS Material with the Ionic framework

Looking to develop an app with Ionic AngularJS material components? Or perhaps using the Material Framework Design Lite for your design? ...

perform an action if any division element is void of content

Currently, I have a statement that checks if any of the Divs with the ID #Drop are empty. If one is found to be empty, an alert is shown. However, there seems to be an issue where the statement stops working when any div contains content. What I am trying ...

Having trouble targeting a div with jQuery

Is it possible to target a specific div within an unordered list and list items? I'm having trouble with it. Here is the HTML code: <ul class="grid"> <div id='categoria' cat='web'></div> <li id=' ...

The function get() in p5.js will provide you with an array of pixels that is empty

I am currently working on a project that is inspired by this particular video. Within p5.js, I have been utilizing the get() function. My goal is to divide a large tileset into smaller images and store them in an array. However, I have encountered an issue ...

What is the best method for retrieving text from elements designated by class?

I am currently using BeautifulSoup to scrape data from a website, but I am facing an issue with extracting only the text I need. The specific part of the website data that I want to grab is: <div content-43 class="item-name">This is the te ...

How can I display all categories in a Radar chart using amcharts 5

I'm currently using React with amcharts to generate a Radar chart. The data I have is structured as an array of objects like this: { category: 'Something', value: 5 }. There are a total of 12 items in the data set. However, when plotting t ...

Unable to find a solution for creating a new element within the angular UI modal

After struggling to incorporate an Angular UI modal and set default values prior to opening the create person screen, I generated a new person object as follows: var person = {}; person.name = 'default'; Within this function modalCtrl. ...

Is there a way to automatically clear the text field value after submitting?

Greetings everyone, I'm looking for guidance on removing the text field content once I click submit. I have a button labeled "senden" and my goal is to clear the text fields and uncheck the checkbox after clicking this button. I've attempted se ...

Is the new Twitter API truly incapable of utilizing ajax, or is there a workaround to make it function?

Currently, I am working on resolving an issue with an older connection string to Twitter's API. This connection string is designed to simply extract the number of followers in order to display it. The website in question is: www.democracywatch.ca To ...