What is the best approach to ensure mobile responsiveness and properly space the TV show div?

How can I center the searched images of shows and add spacing below each image? The 12-column grid is not behaving properly and the images are aligned correctly on desktop but not on mobile. Any suggestions on how to fix this using CSS and Bootstrap? The grid seems to be malfunctioning. Can someone please assist me?

const form = document.querySelector('#search-form');
const container = document.querySelector('#container');

form.addEventListener('submit',async function(e){
    e.preventDefault();
    
    const searchTerm = form.elements.query.value;

    const config = { params: { q:searchTerm}}
    const res = await axios.get(`http://api.tvmaze.com/search/shows`,config);

    clear();
    displayShows(res.data);

    form.elements.query.value='';
})

const displayShows = (shows) =>{
    for (let res of shows){
        
        if(res.show.image){
            const div = addShow(res);
            container.appendChild(div);
        }
        
    }
}

const addShow = (res) => {
            
            const div=document.createElement('DIV');

            const img=document.createElement('IMG');
            img.src = res.show.image.medium;

            const spanName=document.createElement('P');
            spanName.textContent=res.show.name;

            div.append(img);

            return div;
}

function clear(shows){
    container.innerHTML = '';
}
*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

body{
    background-color: #8EC5FC;
    background-image: linear-gradient(120deg, #fc8eed 0%, #E0C3FC 50%, #ffffff 100%);
    font-family: "Poppins", sans-serif;
    min-height: 100vh;
}

header{
    font-size: 1.5rem;
    color: #960189;;
}

header,form {
    min-height: 20vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

form input, form button {
    padding: 0.4rem;
    font-size: 1.6rem;
    border: none;
    background: white;
    margin-right: 10px;
}

form input{
    width: 27%;
    border-radius: 10px;
}


form button {
    color: white;
    background:   #e44ad7;
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.3s ease;
}

form button:hover {
    background: white;
    color: #e44ad7;
}

.container{
    margin: 5%;
}

.container  div{
    display: inline;
    padding: 3%;
    
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60020f0f14131412011020554e504e504d0205140153">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <link rel="stylesheet" href="./TV_ShowSearch.css">
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <title>TV SHOW SEARCH</title>
</head>
<body>
    <header>
        <h1>TV SHOW SEARCH</h1>
    </header>
    

    <form autocomplete="off" id="search-form">
        <input type="text" placeholder="TV Show Title"  name="query">
        <button id="search-btn">Search</button>
    </form>

    <div class="container align-items-center" id="container">
        
    </div>
    
    <script src="./TV_ShowSearch.js"></script>
</body>
</html>

enter image description here

Answer №1

To implement responsive design, consider using the code snippet below within a media query in your CSS:

@media only screen and (max-width: 600px) { 
  .container div {
  padding: 3%;
  width: 210px;
  margin: auto;
 }
}

Answer №2

After experimenting with your CSS, I was able to achieve the following modifications:

const form = document.querySelector('#search-form');
const container = document.querySelector('#container');



form.addEventListener('submit',async function(e){
    e.preventDefault();
    
    const searchTerm = form.elements.query.value;

    const config = { params: { q:searchTerm}}
    const res = await axios.get(`http://api.tvmaze.com/search/shows`,config);

    clear();
    displayShows(res.data);

    form.elements.query.value='';
})

const displayShows = (shows) =>{
    for (let res of shows){
        
        if(res.show.image){
            const div = addShow(res);
            container.appendChild(div);
        }
        
    }
}

const addShow = (res) => {
            
            const div=document.createElement('DIV');

            const img=document.createElement('IMG');
            img.src = res.show.image.medium;

            const spanName=document.createElement('P');
            spanName.textContent=res.show.name;

            div.append(img);

            return div;
}

function clear(shows){
    container.innerHTML = '';
}
*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

body{
    background-color: #8EC5FC;
    background-image: linear-gradient(120deg, #fc8eed 0%, #E0C3FC 50%, #ffffff 100%);
    font-family: "Poppins", sans-serif;
    min-height: 100vh;
}

header{
    font-size: 1.5rem;
    color: #960189;;
}

header,form {
    min-height: 20vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

form input, form button {
    padding: 0.4rem;
    font-size: 1.6rem;
    border: none;
    background: white;
    margin-right: 10px;
}

form input{
    width: 27%;
    border-radius: 10px;
}


form button {
    color: white;
    background:   #e44ad7;
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.3s ease;
}

form button:hover {
    background: white;
    color: #e44ad7;
}

.container{
    margin: 5%;
}

.container  div{
    display: inline;
    padding: 3%;
    
}

@media screen and (max-width: 642px) {
  form input{
      width: calc(27% + 100px);
  }
}

@media screen and (max-width: 400px) {
  form input, form button{
      width: calc(50% + 100px);
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e4c41415a5d5a5c4f5e6e1b001e001e034c4b5a4f1d">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <link rel="stylesheet" href="./TV_ShowSearch.css">
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <title>TV SHOW SEARCH</title>
</head>
<body>
    <header>
        <h1>TV SHOW SEARCH</h1>
    </header>
    

    <form autocomplete="off" id="search-form">
        <input type="text" placeholder="TV Show Title"  name="query">
        <button id="search-btn">Search</button>
    </form>

    <div class="container align-items-center" id="container">
        
    </div>
    
    <script src="./TV_ShowSearch.js"></script>
</body>
</html>

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

send the value of a variable from a child component to its parent

I have created a typeahead component within a form component and I am trying to pass the value of the v-model from the child component to its parent. Specifically, I want to take the query model from the typeahead component and place it in the company mode ...

Bootstrap: Altering grid column layout for medium and small/extra-small screens

Hello everyone, I'm having a bit of trouble working with Bootstrap. My goal is to create a container with a row that contains three columns of equal width: <div class="row"> <div class="col-md-4" style="background:red;">logo</div& ...

To style a text box next to text within a paragraph in CSS, simply create an empty box

I have some CSS classes defined as follows: .p_box { position: relative; } .p_box span { background-color: white; padding-right: 10px; } .p_box:after { content:""; position: absolute; bottom: 0; left: 0; right: 0; top: ...

Send back alternate HTML content if the request is not made via AJAX

Last time I asked this question, I received several negative responses. This time, I will try to be more clear. Here is the structure of a website: Mainpage (Containing all resources and scripts) All Other pages (HTML only consists of elements of that ...

Finding the variance in the given situation is as simple as following these steps

To find the variance between the "Total Marks" in a question and the input texts for each answer, we need to consider specific scenarios. Firstly, if a question has only one answer, the text input should be displayed as readonly with the same value as the ...

How to Retrieve the Current div's ID in VueJS

When using the v-for directive to dynamically generate an id for my div, I need to pass this unique id to a specific function. <div v-for="(item, index) in items" :key="index" :id="'form' + index" > ...

Implementing method overrides in TypeScript class objects inherited from JavaScript function-based classes

I am facing a challenge with overriding an object method defined in a JavaScript (ES5) function-based class: var JSClass = function() { this.start = function() { console.log('JSClass.start()'); } } When I call the start() method, it pri ...

Executing a cloud function in Firebase from an Angular-Ionic application by making an HTTP request

I am a newcomer to GCP and app development, so please bear with me if this question seems mundane. Currently, I have an angular-ionic app that is connected to Firebase allowing me to interact with the Firestore database. Now, my challenge is to invoke a ht ...

Display exclusively the provided value

Can someone please help me with printing in PDF style and displaying only the inputted value without showing the textbox or dropdown? Check out this image for reference I would like to achieve something similar, how can I do that? Any assistance would be ...

Problem with the mobile site width due to viewport misalignment

Our website is currently experiencing an issue with the viewport, but it seems to only occur on mobile devices: If you visit the site on your mobile device, you may notice a large gap on the right side of the page. Strangely, this problem does not appear ...

Is there a method in Kalendae js that automatically triggers the closing of the calendar after a date has been selected?

Currently, I am using Kalendae from https://github.com/ChiperSoft/Kalendae and I have to say it is fantastic. The best part is that I don't have to worry about dependencies. By default, the calendar pops up when a text box is selected and updates the ...

Error encountered while using Google Translate with XMLHttpRequest (Missing 'Access-Control-Allow-Origin' header)

Trying to access a page that utilizes Google Translate is resulting in the following error: XMLHttpRequest cannot load http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit. No 'Access-Control-Allow-Origin' heade ...

Modify session variable upon link click

Here is an extension of the question posed on Stack Overflow regarding creating a new page for different PHP ORDER BY statements: Create a new page for different php ORDER BY statement? The task at hand requires changing a session variable and refreshing ...

Cannot find JS variable after loop has completed

I am struggling to understand why the value of my variable is not changing in my function. Here is my code snippet: var count = function(datain){ let temparr = [], countobj = {}; $.each(datain, function(key, val) { console.log(temparr); cou ...

How can I tally the frequency of characters in a given string using Javascript and output them as numerical values?

I am in the process of tallying the frequency of each individual character within a given string and representing them as numbers. For example, let's consider the string "HelloWorld". HELLOWORLD There is one H - so 1 should be displayed with H remov ...

The server encountered an unexpected error while processing the request, possibly due to a

My JavaScript file includes an interval function that calls the following code: setInterval(function() { $.getJSON('/home/trackUnreadMsgs', function(result) { $.each(result, function(i, field) { var temp = "#messby" + result[i].from; ...

What is the best way to verify if a class attribute includes a specific class or not?

Below is the code snippet provided. The code fetches all classes and then checks for the presence of the 'mat-checked' class. pmanage.no_additional_cost().last().invoke('prop', 'class').then((Class) => { let ...

Manipulating a DOM element in Angular 2 to alter its class attribute

I am a beginner in angular2. Here is my code: import { Component, OnInit } from '@angular/core'; @Component({ selector: 'main', template: ` <div class="current"> </div> ` }) export class MainComponent impl ...

What is the best way to create a blurred effect on an image during loading, and then transition to a sharp image once it is fully loaded?

I'm currently working on a project where I want the images to display as a blurred preview initially, and then become clear once fully loaded. This is similar to the feature seen on Instagram, where the app shows a blurred image before displaying the ...

What to do when faced with an NPM issue: Resolving the error "Unable to assign properties to null (setting 'parent')"

The issue arises in NPM when working within a monorepo that utilizes the NPM workspaces feature. Within my monorepo, I have five packages: a, b, c, d, and e. Additionally, I have a package located in a separate repository that is not part of the workspace, ...