How can Media Queries help make a search box adapt to different screen sizes?

Currently, this is my code snippet. My goal is to make it drop below my logo on the left side as the screen size decreases. Thank you in advance for any suggestions!

@media screen and (max-width:959px){

        #wrapper{
            width: 100%;
        }

        #Search{
            width:40em;
            height: 3em;
        }

    }

    @media screen and (max-width:640px)
    {

        #Search{
            width:100%;

        }
    }



    <!DOCTYPE html>
    <html>
    <head>
        <title>Shiny Science</title>
        <link rel="stylesheet" type="text/css" href="css.css">
        <script src="js.js"></script>
        <meta charset="utf-8">
        <meta name="viewport" content = "width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div id="wrapper">
        <header>
            <img id="mainLogo" src="images/mainlogo.jpg">
            <select id="Select">
                <option value="0">Menu</option>
                <option value="1">Home</option>
                <option value="2">Contact Us</option>
                <option value="3">About Us</option>
            </select>

        </header>
    <main>
        <h1>Shiny Science</h1>
        <p id="description">Metusque innabilis, aestu manebat cum cuncta grandia sectamque iuga recessit cuncta. Metusque innabilis, aestu manebat cum cuncta grandia sectamque iuga recessit cuncta</p>
        <div id="searchbox">
        <img src="images/monkey.jpg" id="secondaryLogo" alt="logo maybe">
        <input type="text" name="txtSearch" placeholder="Topic Name" id="Search" ><input type="button" name="btnSearch" value="Search" id="searchButton">
        <img src="images/turtle.png" alt="somethingkidscience" id="secondaryLogo2">
        </div>
    </main>
    <footer>
        <div id="ep1">
        <h2>Episode 1</h2>
        <p class="caption">Metusque innabilis, aestu manebat cum cuncta grandia sectamque iuga recessit cuncta</p>
        <iframe  src="https://www.youtube.com/watch?v=W3s7UWpo35s?autoplay=1"><p>Your browser doesn't support iframe</p></iframe>
    </div>
    <div id="ep2">
        <h2>Episode 2</h2>
        <p class="caption">Metusque innabilis, aestu manebat cum cuncta grandia sectamque iuga recessit cuncta</p>
        <iframe src="https://www.youtube.com/watch?v=W3s7UWpo35s"><p>Your browser doesn't support iframe</p></iframe>
    </div><br>
    <a href="http://www.youtube.com">View all episodes</a>
    </footer>

    </div>

    </body>
    </html>
    

Apologies for any confusion, here are the HTML and CSS components I am currently utilizing.


    //CSS Styles
    body{
        font-family: Arial, Verdana, sans-serif;
        font-size: 1.3em; 
        background: linear-gradient(red, yellow) repeat;
        text-align: center;
        color: #fff;
    }
    header{
        display: flex;
        justify-content: space-around;
        flex-wrap: nowrap;
        align-items: center;
        padding-top: 1em;
    }

    main,footer,header{
        border-bottom: 2px solid red;
        padding-bottom: 2em;
        text-align: center;
    }

    caption{
        font-size: 0.5em;
    }

    #ep1, #ep2{
        display: inline-block;
        margin-left: 5em;
        margin-right: 5em;
    }

    wrapper{
        margin: auto;
        width: 80%;
        background: linear-gradient(cyan,red);
    }

    secondaryLogo{
        width: 15%;
    }
    searchbox{
        display: flex;
        justify-content: space-around;
        flex-wrap: nowrap;
        align-items: center;
    }

    Search{
        width:40em;
        height: 3em;
    }

    contactHeader{
        padding-top: 1em;
        display: flex;
        justify-content: center;
        flex-wrap: nowrap;
        align-items: center;
        margin-bottom: 2em;
        overflow:auto;
    }

    h1, description{
        margin: auto;
        width: 80%;
    }

    input[type=text], textarea{
        width:50%;
        float: left;
    }

    input[type=submit]{

    }

    searchButton{
        height: 3em;
    }


    mainLogo{
        width: 30%;
        margin-left: 25%;
    }

    secondaryLogo2{
        float: right;
        width: 15%;
    }

    us{
        width: 10%;
    }

    label{
        float: left;
        text-align: right;
        width: 35%;
        margin-right: 5%;
    }

    select{
        font-size: 1em;
        text-align: center;
    }

    contactForm{
        width: 49%;
        border-right: 2px solid white;
        float: left;
        height: 30%;
    }
    contactMethod{
        float: left;
        text-align: left;
        padding-left: 3em;
    }

    @media screen and (max-width:959px){
    
        wrapper{
            width: 100%;
        }

        Search{
            width:40em;
            height: 3em;
        }

    }

    
    media screen and (max-width:640px)
    {

        Search{
            width:100%; 
            height:150px;
            float:left;
    }

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

I have been experimenting with various queries, but currently, only the changes to the wrapper are effective when reducing the screen size.

Answer №1

Make sure to place the following code snippet inside your @media screen and (max-width:640px) declaration:

@media screen and (max-width:640px)
{
    #searchbox { display:block }
    #secondaryLogo { display:block; width:auto }
}

To ensure that the child divs are displayed as blocks, it is essential to disable the flexbox property for #searchbox. Otherwise, they will continue to be treated as flex children. When dealing with smaller screen sizes, there may be other issues in your code, but this particular problem was addressed as per your request :)

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 navigate to a specific element using jQuery?

I am having an issue with scrolling to a specific div at the top of my page. Even though I have buttons for other sections, when I scroll to the bottom and click on the button, it does not take me to the top of the page as intended. You can view the CodeP ...

Apply the :after pseudo-class to the specified element

I am struggling with implementing an overlay effect on two images on my website. My goal is to add an overlay when the images are clicked, and I have tried creating this effect using the after pseudo class. Unfortunately, the overlay does not seem to wor ...

What is the best way to align background shapes within a specific section on a webpage using CSS?

I've been attempting to strategically place multiple shapes in the background using CSS, but I'm facing challenges. While I was successful with my hero section at the top of the page, positioning elements in a section halfway down has proven prob ...

Adding a drop shadow effect to borders using CSS is a great way to

I created this unique button design using Figma and now I want to implement it in CSS. https://i.sstatic.net/b6hNx.png Upon closer inspection, you'll notice that I included a highlight with a white shadow on the border. However, when I try to repli ...

Create a Bootstrap div containing a button that adjusts to different screen sizes on the

Struggling with an issue in Bootstrap on how to create an input group with responsive content and a button on the right side. I want the button to always be on the right side and also responsive. I attempted to achieve this with the code below, but it does ...

Tips for maintaining the fixed size of a table header and preventing it from resizing in width

My goal was to create a table using divs with fixed headers while scrolling vertically. However, I encountered an issue where the header width seemed to shrink and became misaligned with the rows. Even setting the width to 100% did not resolve this probl ...

Error encountered when attempting to call a JavaScript function by clicking on a link within a dropdown menu due to an unexpected end of

Here is the code snippet from my HTML file: <html> <head> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css"> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/boots ...

Display a CSS animation upon hovering the mouse

I'm looking to create a unique animation that involves drawing angled and straight lines, along with displaying text from left to right upon hovering over a button. I am relatively new to this concept and would appreciate any guidance. Additionally, I ...

How to position an element to the right in Bootstrap 4 without causing it to move to a new line immediately?

As a newcomer to Bootstrap, I have come across only one individual who has raised this issue before. Unfortunately, they did not receive a satisfactory solution, prompting me to bring up the question once more. My dilemma involves two button groups. When ...

``I am experiencing an issue with the framework7 slider range feature not functioning properly on the

Issue with framework7 slider range not functioning in Google Chrome but working in other browsers. https://i.sstatic.net/ItY8U.jpg Here is the code snippet: <div class="item-content"> <div class="item-media"><i class="i ...

Issues encountered while trying to style an unordered list using the table-cell property

I am having trouble formatting the "How it works" section on my website. When the text is too long, the numbers on the left side grow alongside it as shown in number 4. Is there a way to keep the numbers fixed on the left while allowing the text to expand ...

Ways to prioritize the parent element's style over the child element's style attribute

I am looking to customize the appearance of an unfamiliar HTML code by changing its text color to cyan, regardless of its current color. I attempted the following: <p style="color:cyan !important;"> <span style="color:red;">Hello World</ ...

Troubleshooting problems with the CSS code for a progress bar in Gmail

I recently came across a unique progress bar design on Gmail and wanted to implement something similar. I found some code snippets on this webpage: . However, I encountered an issue where the progress bar was only displaying in Internet Explorer but not in ...

Mastering the art of carousel div creation with Bootstrap

Is there a way to create a carousel in Bootstrap 3 where only one div slides at a time, instead of three? I attempted to use divs instead of images in the traditional carousel structure, but it's not functioning as expected. I'm looking for some ...

Personalized Grid Design for Showcasing Athletic Teams

I am looking to create a custom grid layout that represents the team's Players, separated by their positions (GK, Defense, Midfielders, and Forwards), similar to the image below. The layout should also be responsive like in image 1. Currently, the re ...

The size of the dropdown adjusts according to the changes in the page size

Can anyone help me with an issue I'm facing in my form? Whenever I zoom in or out on my browser, the select element within the form gets resized and changes its position. Any suggestions would be greatly appreciated. Below is the code snippet for refe ...

Inconsistent animations on Firefox with animate.css

Rock, Paper, Scissors! Greetings, I've been honing my coding skills by developing an HTML game utilizing these fantastic CSS3 animations paired with jQuery. Unfortunately, I've encountered a frustrating issue with Firefox as it only plays the a ...

Proper alignment of content in HTML using Bootstrap following the integration of .json data

After aligning the emergency hotlines properly, I am struggling to position the text under the image to display the data in three rows. I attempted col-4 but encountered issues with JSON. My objective is to show the image followed by the numbers directly b ...

What is the method for including line numbers alongside a highlighted code section in highligh.js?

I want to ensure that the text rows are aligned perfectly with the numbers in the yellow vertical bar, without causing any issues with the mouse pointer as it moves across the rows. Adjusting margin or padding also affects the position of the mouse pointer ...

Problem with NextJS: CSS styles not being applied globally to elements

Recently, I created a blog using nextJS and loaded markdown files as posts on the main page. To style my website, I used css modules along with a global.css file. However, I've encountered an issue where some properties from the global.css file are no ...