Tips for ensuring the border matches the size of the image

I am in the process of creating a website that includes a filter option. My plan is to have the filters displayed on the left side and the items on the right side.

To achieve this layout, I have implemented a scrollable div for the items. However, I noticed that the borders do not align perfectly with the images as shown in the sample image below. While I haven't written the script yet, here is the code snippet I have prepared:

https://i.stack.imgur.com/YQMqe.png

<html>

<head>
  <title></title>

  <style>
    #vezivanje {
      background-color: hotpink;
      height: 80px;
      display: flex;
      align-items: center;
      border-top-left-radius: 20px;
      border-top-right-radius: 20px;
      font-size: 20px;
    }
    
    img {
      height: auto;
      max-width: 80%;
    }
    
    #filter {
      position: fixed;
      width: 300px;
      height: 900px;
      border-right: solid;
      border-color: #9f2b68;
      border-right-width: 2px;
      padding-left: 15px;
      padding-top: 30px;
      margin: 10px;
    }
    
    #container {
      position: relative;
      display: grid;
      float: right;
      overflow: scroll;
      width: 1560px;
      height: 850px;
      margin: 10px;
      grid-template-columns: 1fr 1fr 1fr;
      grid-gap: 1vh;
    }
    
    .item {
      border-style: solid;
      border-color: #9f2b68;
      margin: 10px;
      border-width: 2px;
    }
    
     ::-webkit-scrollbar {
      display: none;
    }
    
    body {
      background-color: #e2e3e3;
    }
  </style>

</head>

<body>
  <div id="sve">
    <div id="vezivanje">
      <span class="upustvo">
            <a style="text-decoration:none"><span style="color:white;font-weight:bold">Domaci zadatak</span></a>
      </span>
    </div>

    <div id="filter">
      <div id="uzrast">
        <h2>Uzrast:</h2>
        <p><input type="radio">0+</p>
        <p><input type="radio">3+</p>
        <p><input type="radio">6+</p>
        <p><input type="radio">10+</p>
      </div>

      <div id="cena">
        <h2>Cena:</h2>
        <p>0<input type="range">10.000</p>
      </div>
    </div>

    <div id="container"></div>
  </div>
  <script>
    //--------------------------

    let container = document.querySelector("#container");
    let items = [];

    function itemInfo(image, age, price) {
      this.image = image;
      this.age = age;
      this.price = price;
    };
    items.push(new itemInfo("https://yekupi.blob.core.windows.net/ekupirs/1200Wx1200H/EK000359292_1.image", "0+", "2000 RSD"));
    items.push(new itemInfo("https://poklonizabebu.rs/wp-content/uploads/2022/02/Muzicka-igracka-kuca-Brown-2.jpg", "0+", "1500 RSD"));
    items.push(new itemInfo("https://pertinitoys.com/fajlovi/product/master-wheels-trotinet-p-0282-master-wheels-web-s_5f3fb5d70b71e.jpg", "3+", "1500 RSD"));
    items.push(new itemInfo("https://www.lego.com/cdn/cs/set/assets/blt70237dec0eef084a/10696.jpg?fit=bounds&format=jpg&quality=80&width=1600&height=1600&dpr=1", "3+", "5000 RSD"));
    items.push(new itemInfo("https://img.goglasi.com/img/225234594", "3+", "3000 RSD"));
    items.push(new itemInfo("https://www.bcgroup-online.com/upload/m/84401-lego-duplo-10915-kamion-sa-slovima-abecede.jpg", "3+", "2000 RSD"));
    items.push(new itemInfo("https://www.decjisajt.rs/files/watermark/files/thumbs/files/images/product/2018/01/29/thumbs_1200/thumbs_w/drvena-muzicka-igracka-velika-gitara-91701_1200_1200px_w.jpg", "3+", "9000 RSD"));

    items.forEach(i => {
      container.innerHTML += "<div class = 'item'><img src = " + i.image + ">" +
        "<p>" + i.age + " " + i.price + "</p>"
      "</div>"
    })
  </script>

</body>

</html>

Answer №1

Your images will always have a 20% gap because the max-width is set to 80%. This solution was provided by @ishiro, and it allows you to achieve the desired result.

  1. The class .item has been set with width: 80%.
  2. The img tag has been set with max-width: 100%.

With this setup, the 20% space disappears, and the border aligns perfectly with the image without changing its size.

//--------------------------

let container = document.querySelector("#container");
let toys = [];

function toyInfo(image, age, price) {
  this.image = image;
  this.age = age;
  this.price = price;
}
toys.push(
  new toyInfo(
    "https://yekupi.blob.core.windows.net/ekupirs/1200Wx1200H/EK000359292_1.image",
    "0+",
    "2000 RSD"
  )
);
toys.push(
  new toyInfo(
    "https://poklonizabebu.rs/wp-content/uploads/2022/02/Muzicka-igracka-kuca-Brown-2.jpg",
    "0+",
    "1500 RSD"
  )
);
toys.push(
  new toyInfo(
    "https://pertinitoys.com/fajlovi/product/master-wheels-trotinet-p-0282-master-wheels-web-s_5f3fb5d70b71e.jpg",
    "3+",
    "1500 RSD"
  )
);
toys.push(
  new toyInfo(
    "https://www.lego.com/cdn/cs/set/assets/blt70237dec0eef084a/10696.jpg?fit=bounds&format=jpg&quality=80&width=1600&height=1600&dpr=1",
    "3+",
    "5000 RSD"
  )
);
toys.push(
  new toyInfo("https://img.goglasi.com/img/225234594", "3+", "3000 RSD")
);
toys.push(
  new toyInfo(
    "https://www.bcgroup-online.com/upload/m/84401-lego-duplo-10915-kamion-sa-slovima-abecede.jpg",
    "3+",
    "2000 RSD"
  )
);
toys.push(
  new toyInfo(
    "https://www.decjisajt.rs/files/watermark/files/thumbs/files/images/product/2018/01/29/thumbs_1200/thumbs_w/drvena-muzicka-igracka-velika-gitara-91701_1200_1200px_w.jpg",
    "3+",
    "9000 RSD"
  )
);

toys.forEach((t) => {
  container.innerHTML +=
    "<div class='item'><img src=" +
    t.image +
    ">" +
    "<p>" +
    t.age +
    " " +
    t.price +
    "</p>";
  ("</div>");
});
#binding {
  background-color: hotpink;
  height: 80px;
  display: flex;
  align-items: center;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  font-size: 20px;
}

img {
  max-width: 100%;
  height: auto;
}

#filter {
  position: fixed;
  width: 300px;
  height: 900px;
  border-right: solid;
  border-color: #9f2b68;
  border-right-width: 2px;
  padding-left: 15px;
  padding-top: 30px;
  margin: 10px;
}

#container {
  position: relative;
  display: grid;
  float: right;
  overflow: scroll;
  width: 1560px;
  height: 850px;
  margin: 10px;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 1vh;
}

.item {
  width: 80%;
  border-style: solid;
  border-color: #9f2b68;
  margin: 10px;
  border-width: 2px;
}

::-webkit-scrollbar {
  display: none;
}

body {
  background-color: #e2e3e3;
}
<div id="all">
  <div id="binding">
    <span class="instructions">
      <a style="text-decoration:none"><span style="color:white;font-weight:bold">Homework Assignment</span></a>
    </span>
  </div>

  <div id="filter">
    <div id="age">
      <h2>Age:</h2>
      <p><input type="radio">0+</p>
      <p><input type="radio">3+</p>
      <p><input type="radio">6+</p>
      <p><input type="radio">10+</p>
    </div>

    <div id="price">
      <h2>Price:</h2>
      <p>0<input type="range">10.000</p>
    </div>
  </div>

  <div id="container"></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

What is the best way to prevent a Bootstrap modal from refreshing the page when clicking an ASP.NET button?

I've encountered an issue while trying to display data in a Bootstrap modal view. The data is fetched from a database and successfully shown, but only for a brief moment before the page automatically refreshes. Below is the HTML code: <div class=" ...

Modifying a css class via javascript

Is it possible to set an element's height using CSS to match the window inner height without directly modifying its style with JavaScript? Can this be achieved by changing a CSS class with JavaScript? One attempted solution involved: document.getEle ...

jPlayer - Utilize the setMedia() function to define the media source URL

Having some trouble using jPlayer on Firefox 3.6 (Ubuntu) function loadmedia() { $('#jquery_jplayer_1').jPlayer('setMedia', { mp3: 'media/audio/04-Piste_4_1.mp3', }); } $(document).ready(function () { $('#jque ...

Ways to display a vertical scrollbar within a select box

I'm currently working with an Angular select box and I'm looking to display a scroll bar if there are more than 5 data entries. <select class="form-control" data-ng-model='projectListData.appVersion' ng-selected ng-options="v.versio ...

The movement of the Bootstrap navbar brand causes a shift in adjacent elements

Is there a way to use the bootstrap navbar brand without it affecting the layout of other elements? I'd like to have the icon aligned to the left and the navbar elements perfectly centered. Alternatively, is there a way to include the element as a n ...

Error found in Paper.js at line 81: Uncaught TypeError - Unable to access properties of undefined (specifically '1') while utilizing Material UI and Joy UI

I am encountering an issue while using react js with material UI and JoyUI, where I am getting a blank screen. Below is the code snippet causing the problem: import React from 'react'; import { CssVarsProvider } from '@mui/joy/styles'; ...

Unexpected display issues with CSS shapes in Bootstrap framework

Been experimenting with creating custom shapes in Bootstrap - they display correctly in HTML and CSS, but Bootstrap distorts them. It seems like there are no conflicting classes. Could it be necessary to use '!important' on some of the elements? ...

Tips on transmitting and receiving substantial JSON information

As a newbie in the full-stack development world, I am currently on a quest to find an efficient method for transmitting and retrieving large data between my React front-end and Express back-end while keeping memory usage to a minimum. My project involves b ...

updating the count of items in a react virtual list

Popular libraries such as react-virtualized, react-window, and react-virtuoso all include an item count property similar to the one shown in the code snippet below from material-ui. However, this property is typically located within the return statement. ...

Alignment of React page gets messed up upon inclusion of Gallery component

I have searched for solutions online, but nothing seems to work for me. I am facing an issue with my webpage that has multiple sections, and I want to display them one after the other vertically. Here is the code snippet: import React from 'react&ap ...

Converting an array to a JSON object using JavaScript

I need help with converting the following array into a JSON object: var input = [ 'animal/mammal/dog', 'animal/mammal/cat/tiger', 'animal/mammal/cat/lion', 'animal/mammal/elephant', 'animal/ ...

adjusting the height of a div using jQuery UI's layout feature

Recently, I have been playing around with the adjustable grids plugin (jquery ui layout) to set the width of each div using the plugins. However, I've encountered a challenge when it comes to adjusting the height of inner divs within the layout. Speci ...

What is the proper way to utilize a class with conditional export within the Angular app.module?

This query marks the initiation of the narrative for those seeking a deeper understanding. In an attempt to incorporate this class into app.module: import { Injectable } from '@angular/core'; import { KeycloakService } from 'keycloak-angul ...

When it comes to adjusting the height of an element, there are two ways to go about it: using $(element).height

function adjustHeight(){ var headerHeight=$(element).find('.header').outerHeight(); console.log(headerHeight); var temp=$(window).height()-headerHeight; console.log(temp); $('.users ...

Collecting all the <td> elements within a single row

I am having trouble creating a table dynamically using JSON data. The issue is that all <td> elements are being placed in the same <tr> instead of two separate rows. Here is my JavaScript code: $('button').click(function() { var str ...

Arranging nested divs in relation to one another in a specific position

This code represents a chart in HTML. The goal is to have the second (middle) div start where the first (top) div ends, which has a width of 75px. To achieve this, the margin-left for the middle div is set to 75px in the following styles. Following the sam ...

Ensuring Proper Highlighting for HTML Select Options

I am working on a project involving an HTML Drop-down menu. Check out the code I have so far: http://jsfiddle.net/dineshk/u47xjqLv/ HTML Code <select> <option class="first" style="display:none;">HH</option> <option class= ...

Keep moving the box back and forth by pressing the left and right buttons continuously

Looking to continuously move the .popup class left and right by clicking buttons for left and right movement. Here is the js fiddle link for reference. This is the HTML code structure: <button class="right">Right</button> <button class="l ...

Generate an li element that is interactive, containing both text and a span element

I am dealing with a JSON array that looks like this: var teamDetails=[ { "pType" : "Search Engines", "count" : 5}, { "pType" : "Content Server", "count" : 1}, { "pType" : "Search Engines", "count" : 1}, { "pType" : "Business", "count" : 1,}, { "pTyp ...

Using dryscrape for web scraping: encountering an issue when selecting a radio button with CSS

I am attempting to extract data from a dynamically updated table on a webpage using dryscrape. The web page in question is located at . My code functions correctly with tables that are generated when the page is loaded initially. However, I now need to upd ...