Is it possible for me to automatically add a line break following every image that is fetched from the Flickr API?

Hello there! I'm facing a little challenge with inserting a line break after each image that loads from the Flickr API. It's tricky to manipulate the style of data that hasn't loaded in the browser yet.

I've experimented with using the
tag within the "images" div in my HTML code. I've also tried tweaking the JavaScript when making the call to the Flickr API function.

<body>
  <div class="navbar">
     <input type="text" id="content">
     <button id="submit", type="submit" class="button">GO!</button>
  </div>
  <div class="container">


 <div id="images">

      <p>This is where the 25 pictures are loaded. 
      They load horizontally and move to the next 
      line when there is not enough room.
      I would like a line break <br> after each image.</p>

      </div>
  </div>
  <!--script-->
  <script>
     $(document).ready(function () {

       $("#submit").click(function (event) {

         var searchVal = $("#content").val();
         var flickrAPI = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=//KEY_GOES_HERE&nojsoncallback=1";
           $.getJSON( flickrAPI, {
           tags: searchVal,
           per_page: 25,
            //safeSearch
           safe_search: 1,
           format: "json"
         },  
           function( data ) {
               $('#images').empty();
           $.each( data.photos.photo, function( i, item ) {
             var url = 'https://farm' + item.farm + '.staticflickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg';

            $('#images').append('<img src="' + url + '"/>');
          });

        });
     });    
     });
  </script>
 </body>
</html>

When the user clicks "GO!", 25 pictures load into the "images" div. Each picture currently stacks horizontally before a line break
is applied.

Answer №1

If you're looking to have your images displayed as a block, the default display value for the <img> tag is inline-block. To change this, simply assign them a class when adding them.

$('#images').append('<img class="image" src="' + url + '"/>');


.image {
  display: block;
}

Alternatively, you can achieve the same effect by applying display: flex to your .images div.


.images {
  display: flex;
  flex-flow: column wrap;
}

Answer №2

To ensure proper display, it is recommended to use block-level elements or style the img tags as block elements. One simple solution is to enclose your img tags within a wrapping element, like so:

<div class="images">
      <img src="https://cataas.com/cat?width=100" alt="">
      <img src="https://cataas.com/cat?width=100" alt="">
      <img src="https://cataas.com/cat?width=100" alt="">
      <img src="https://cataas.com/cat?width=100" alt="">
      <img src="https://cataas.com/cat?width=100" alt="">
    </div>

    wrapped version below:
    <div class="images">
      <div><img src="https://cataas.com/cat?width=100" alt=""></div>
      <div><img src="https://cataas.com/cat?width=100" alt=""></div>
      <div><img src="https://cataas.com/cat?width=100" alt=""></div>
      <div><img src="https://cataas.com/cat?width=100" alt=""></div>
      <div><img src="https://cataas.com/cat?width=100" alt=""></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

Error encountered in Three.js while attempting to utilize THREE.JSONLoader.parse in place of THREE.JSONLoader.load

After successfully loading a JSON model using THREE.JSONLoader.load, I encountered an issue when trying to create a variable out of it and include data in the main script. This resulted in an exception being thrown by the THREE.JSONLoader.parse call. var ...

Sharing a variable with JavaScript within an OnClick event to update a progress bar in C#

When I click on a button, a JavaScript function is launched to create a progress bar. <asp:Button ID="btn_tel" runat="server" CausesValidation="False" OnClick="btn_telefono" Text="Check" CssClass="button" OnClientClick="move()" /></div> The f ...

AFNetworking failed to correctly deserialise a JSON-encoded dictionary within another dictionary

While working on my PHP backend, I have a situation where I am returning JSON data in the following format: $results_array=array(); $stmt->bind_result($IdUser,$username); while($stmt->fetch()) { $r ...

Leveraging Python Variables in HTML within a multiline Python string

Attempting to create HTML code using Python and run it from a browser. Check out the code below: import webbrowser f = open('image.html','w') message = """<html> <head></head> <body><img src="URL"></b ...

After calling the PHP API, the http.get method in Ionic 2 is returning a value of isTr

Even though a similar question has been posted before, I am still puzzled about why I am receiving: 'isTrusted': true every time I call a PHP API from Ionic 2. The code I am using is as follows: getProduct(id: string){ if(this._product){ con ...

Analyzing a tweet containing unique characters

I am currently working on extracting links from tweets, particularly hashtags and mentions. However, I have encountered an issue when the tweets contain special characters like " ...

Tips for generating a .csv document using data from an array?

I am currently utilizing a PHP class to validate email addresses in real-time. The PHP Script is functioning as expected: validating the emails and displaying the results on the same page by generating a <td> element for each validated email. Howeve ...

What is the best way to set up a condition that only runs if an element does not contain a certain class, such as "active"?

Click here for the picture I have a list of items: a, b, c, d, e, f. Whenever I click on item b, it becomes active and the other elements lose the "active" class. When an element has the "active" class, I want the background to change accordingly. If it ...

Transform JSON data into a vertical table format

I have a specific payload that I need to format into a horizontal column output with newlines between entries. Can anyone provide guidance on achieving this using jq or bash scripting? Here is the payload: StackId : arn:aws:cloudformation:us- ...

Exploring the Breakpoints of Bootstrap 4.x Grid System in Development

Understanding the grid system in Bootstrap 4.x can be beneficial, especially when trying to determine the specific grid breakpoint that aligns with the current viewport width. ...

Make sure the font size on your iPhone matches the font size on other web browsers

Once upon a time, I came across an insightful article detailing a method to size text using CSS that is compatible with multiple browsers. The approach outlined in the article includes: body { font-size:100%; line-height:1.125em; /* 16×1.125=18 * ...

What is the best way to display a page within a div when clicking in Yii?

I'm trying to use the jQuery function .load() to load a page into a specific div. Here's my code: <a href="" onclick="return false;" id="generalinfo"> <div class="row alert alert-danger"> <h4 class="text-center">Gen ...

Utilizing LitJson Library in Unity with C# for JSON Parsing

Currently, I am facing a challenge while trying to convert Java code to C# particularly in the area of JSON parsing. Below is an excerpt from the Java code: mJsonObject = new JSONObject(str); Iterator<String> keys=mJsonObject.keys(); ...

What could be causing this JavaScript code not to run?

Help needed! I'm a student struggling to diagnose the issue in my code. Whenever I click the buttons, nothing happens at all. I've tried debugging by isolating each function, but still can't seem to figure it out. I've searched through ...

Was not able to capture the reaction from the Angular file upload

I've been attempting to upload a single file using the POST Method and REST Calling to the server. Despite successfully uploading the module with the code below, I encounter an error afterwards. Is there anyone who can lend assistance in troubleshooti ...

I'm having trouble getting the gutter padding to show up in one specific content block on my Bootstrap webpage. How can I troubleshoot and resolve this

Currently, I am delving into the realm of Bootstrap 5 and have recently put together a simplistic webpage using the row/column structure suggested by Bootstrap. However, while my other columns seem to be cooperating and displaying the gutter padding as exp ...

<a href> click here to append a new query parameter to the existing ones

Is there a way to create a link that will add a query parameter to the current URL instead of replacing all existing ones? Here's what I'm looking for: If the current URL in the browser is example.com/index.html, clicking on it should lead to ...

What is the process for calculating and determining the exact area the div should be released?

I am currently developing a drag-and-drop application using only Javascript. I have successfully implemented the dragging functionality, allowing elements to be moved randomly within the page. However, I now face the challenge of creating a drop zone with ...

Crafting an interactive SVG element that maintains its clickability without interfering with mouseLeave events

Trying to Achieve: Changing color and displaying SVG when hovering over a row Clicking the SVG triggers a function, including external functions (not limited to ones defined inside script tags) Returning the row to its original form when mouse leaves Cha ...

what is the best way to center list items in material ui?

I have been attempting to align the list items (checkbox, text, and buttons) within a Material UI list in the center, but all my attempts have been unsuccessful. Is there anyone who knows how to resolve this issue? Your help would be greatly appreciated! h ...