Guide to Embedding an Image in a List in HTML

How do I resize an image according to the size specified in CSS, and ensure that it fits within a list of items where each item consists of both an image and a name? Currently, my code only displays the image in its original size.

 for(var i =0; i< obj.length; i++){
            item = document.createElement("li"); 
              let img = document.createElement("img");
              img.src = obj[i].imagePath;
              item.appendChild(img);
              theList.appendChild(item);
              }
.bird-gallery {
    display: block;
    float: left;
    width: 300px;
    height: 600px;
    margin: 1em;
    overflow: hidden
}

.bird-gallery .bird-list {
    width: 280px;
    height: 100%;
    list-style-type: none;
    margin: 25px 0;
    padding: 0;
    overflow-y: auto
}

.bird-gallery .bird-image {
    display: block;
    width: 50px;
    height: 50px;
    margin: 0;
    float: left;
    border-radius: 50%;
    border-style: solid;
    border-width: 2px;
    border-color: #FFF
}

.bird-gallery .bird-name {
    display: block;
    font-size: 24px;
    color: #333
}
<div class="bird-gallery">
            <input type="text" class="bird-search" />
            <ul class="bird-list">
                
            </ul>
        </div>

Answer №1

Apply a max-width:100% style to all img elements.

var obj = [{
  imagePath: "https://cdn.pixabay.com/photo/2015/03/29/01/54/tree-696839_960_720.jpg"
}, {
  imagePath: "http://www.flemings.com.au/wp-content/uploads/2015/07/Hero-Acer-Autumn-Blaze-Domain-Chandon-1500x700.jpg"
}]
var theList = document.getElementsByClassName("bird-list")[0]
for (var i = 0; i < obj.length; i++) {
  item = document.createElement("li");
  let img = document.createElement("img");
  img.src = obj[i].imagePath;
  item.appendChild(img);
  theList.appendChild(item);
}
.bird-gallery {
  display: block;
  float: left;
  width: 300px;
  height: 600px;
  margin: 1em;
  overflow: hidden
}

.bird-gallery .bird-list {
  width: 280px;
  height: 100%;
  list-style-type: none;
  margin: 25px 0;
  padding: 0;
  overflow-y: auto
}

.bird-gallery .bird-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0;
  float: left;
  border-radius: 50%;
  border-style: solid;
  border-width: 2px;
  border-color: #FFF
}

.bird-gallery .bird-name {
  display: block;
  font-size: 24px;
  color: #333
}

img {
  max-width: 100%
}
<div class="bird-gallery">
  <input type="text" class="bird-search" />
  <ul class="bird-list">

  </ul>
</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

Take a snapshot of an element using fixed element positioning

When it comes to extracting a sub image from a webpage using Selenium, I have found a reliable method. First, I capture a screenshot of the entire page and then extract the desired sub-image using the element's coordinates. Dimension elementDimension ...

How to ensure the table fits perfectly on the screen using JQueryMobile and Cordova?

I've been working on a JavaScript function that creates a dynamic page and displays data fetched from a database. However, I've encountered an issue where the displayed data overflows past the width of the mobile screen. Is there a way to ensure ...

Choose from a dropdown menu to either activate or deactivate a user account

I'm new to php and I need help getting this code to function properly delete.php <?php include_once 'dbconfig.php'; if($_POST['del_id']) { $id = $_POST['del_id']; $stmt=$db_con->prepare("UPDATE tbluse ...

I am trying to figure out how to style each box individually, but I'm running into some confusion. There are three different classes to choose from, with the child class

Struggling to style the ten boxes individually? Renaming each class of the box child didn't work as expected? Wondering how to select and style each box with a unique background color? Also facing issues with displaying the logo image on the navigatio ...

Infinite scroll causing Firebase ".length" function malfunction

My NextJs website is encountering errors related to Firebase infinite scroll. The issue seems to be with the .length property being undefined for some unknown reason. I am struggling to debug the code and make it work properly in Next.js. Any help would be ...

Animating an image inside a bordered div

I'm attempting to create an animated effect where an image moves randomly within the boundaries of a div container. While I've come across solutions for animating within borders, none have specifically addressed keeping the image inside the div. ...

Generating an HTML table from information stored in a text file

As a beginner in web design, I am looking to create an HTML table using data from a text file. I'm unsure of the best approach to take, so any guidance or pointers would be greatly appreciated. ...

What is the best way to prevent the contents of the First Column from spilling over into the Second Column using CSS

Currently, I am working on the CSS for two column sections: section one and section two. My goal is to have the first column of section one occupy the available space without overflowing into the second column, as illustrated in this image. The content hi ...

Design a sophisticated background using CSS

I am wondering if there is a way to create a CSS3 background similar to the one shown in the image link above. The gradient should smoothly transition from white to black, spanning across a header div, and remain consistent regardless of screen width (alwa ...

JQUERY inArray failing to find a match

I'm at my wit's end working with inArray and I'm really hoping for some help. I am using AJAX to fetch userIDs from my database, which come through as a JSON-encoded array. However, no matter what I try, I always get a -1 when trying to mat ...

Ember JS: Master of Controlling

I am working with the following controllers: clusters_controller.js.coffee Portal.DashboardClustersController = Ember.ArrayController.extend dashboard_controller.js.coffee Portal.DashboardController = Ember.ArrayController.extend In my template, I am ...

Executing Node.js Function from an External File Dynamically

Is it possible to run a Node function from an external file that may be subject to change? main.js function read_external(){ var external = require('./external.js'); var result = external.result(); console.log(result); } setInterva ...

Tabbed horizontal slider

I am trying to combine two scripts in order to create a tab-based system with a scrollbar located at the bottom of the content. I have merged the following Ajax tabs: with this slider: However, when I open the slider's tab, I am unable to move the s ...

Creating a custom variable assignment in Bootstrap within the custom.scss file

I've been searching for a solution to this problem for half a day now. My goal is to change the value of the $primary variable from the default $blue to the $orange variable. I tried following the instructions in the documentation: $primary: red; @imp ...

modify the URL records within the GetJson function

My current address is "http://localhost:8000/index", and when I execute the following jQuery code: $.getJSON("1",function(data) { .....code }); It redirects to "http://localhost:8000/index/1". This works fine for now. ...

An AngularJS project utilizing exclusively EJB

Can an AngularJS application be built directly with EJB without needing to expose them as REST services? Many examples on the internet show that eventually REST services are required to provide data to AngularJS. This means that EJB methods must be expos ...

Calculating the percentage difference between two dates to accurately represent timeline chart bar data

I am in the process of creating a unique horizontal timeline chart that visually represents the time span of milestones based on their start and finish dates. Each bar on the timeline corresponds to a milestone, and each rectangle behind the bars signifies ...

What is the method for sending a down arrow key in Capybara?

My unique requirement involves a specialized listbox automation that would benefit from simulating a down arrow keystroke and then pressing enter. The code snippet for pressing enter looks like this: listbox_example = find(input, "listbox-example") listb ...

Tips for utilizing the @Input directive within the <router-outlet></router-outlet> component

I am new to Angular 4 and recently discovered that in Angular, data can be passed from a parent component to a child component using @Input like this: <child [dataToPass]="test"></child> My question is, how can I achieve the same functionalit ...

What causes arrays in JavaScript to not be sorted in either ascending or descending order based on dates?

I'm attempting to organize my array of objects that contain a date property. I need to arrange the array based on ascending or descending dates. I've attempted the following approach: https://jsfiddle.net/rxaLutgn/1/ function sort_by(field, rev ...