Adjust header display for smaller screens

Looking for help with adjusting my header design on small screens:

https://i.sstatic.net/x8BTo.png

I want the search bar to go underneath on smaller screens, like this example: https://i.sstatic.net/x4RFi.png

I've tried changing the position to relative at a certain screen width but it didn't work.

  <div className="header">
    <div className="header__logo">
      <img src={logo} alt=""></img>
    </div>
    <div className="header__search">
      <SearchBar />
    </div>
    <div className="header__right">
      <FiUser className="header__icon" />
      <Cart />
    </div>
  </div>



/*
   header
*/
.header {
  width: 100%;
  height: 60px;
  border-bottom: 1px solid #e4e4e4;
  display: flex;
  justify-content: center;
}

.header__logo {
  width: 120px;
  height: 60px;
  line-height: 65px;
}

.header__logo img {
  width: 120px;
  background-size: contain;
}

.header__search {
  width: 50%;
  height: 60px;
  line-height: 60px;
  margin: 0 20px 0 20px;
  text-align: center;
}

.header__right {
  width: 120px;
  height: 60px;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
}

.header__icon {
  height: 30px;
  width: 30px;
  margin-top: 15px;
  cursor: pointer;
}

I'm capable of coding it myself, just need guidance on how to achieve this. Any help is appreciated!

Answer №1

  1. To tackle this issue, one approach could be to include two search bars within the same file. Then, based on screen sizes, you can hide one and display the other.

Here's an example:

<select class="for-desktop"> ... </select>
<select class="for-mobile"> ... </select>

You can achieve this by utilizing CSS as shown below:

@media(max-width:768px){
     .for-desktop{
        display:none;
      }
 }

 @media(min-width:768px){
     .for-mobile{
        display:none;
      }
 }

By implementing this method, you can simplify the process without much hassle.

  1. Another strategy is to employ CSS-Grid for the entire header layout and position the select at the bottom (this may require writing some efficient code).

  2. A quick workaround is to apply a hack by setting position:relative on the parent element and position:absolute; solely for the select when the screen size is smaller, adjusting the positioning of the select accordingly.

For instance, you can refer to the following example:

.header {
  position:relative;
}

.header__search {
  position:absolute;
  bottom: -60px;
  left:0;
  right:0;
}

(Minor adjustments may be needed in the positioning of the search item)

Answer №2

If you're looking to freshen up your CSS, consider utilizing CSS grid to relocate the search bar on your webpage

For further guidance, I suggest checking out this resource

Cheers!

-- UPDATE ---

If not, you have the option to hide the current bar once the screen size surpasses a certain threshold and display a new "div" or "copy" below (this could lead to interesting animations)

Alternatively, you could employ JavaScript to move this element to a different position as needed

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

Tips for creating a 3D illusion by coding a div or object to rotate around another object or div

I have reached a technical and intellectual roadblock. The issue at hand is this: I need the text to encircle an image and create the illusion that it is moving in front of or behind the image, while keeping the image static. Currently, 1) the rotating te ...

Navigate the speech bubble tail by moving or rotating it with your mouse

For my project, I am working on creating a dynamic speech bubble that can be dragged around the corner of the bubble with the mouse. The goal is for the tip of the speech bubble to rotate automatically based on its position. Below is a snippet of my code: ...

Updating part of a page while also changing the navigation

Apologies in advance, as this is my first attempt at coding a website. I have a specific need where I want to update only one div on my web page when a link in the navigation bar is clicked. <body> <div id="wrapper"> <header id= ...

Deactivating a widget on one of my web pages

I'm looking to disable a widget on certain pages. Does anyone know how to do this? One idea I had was to add a condition in the HTML (using Django or PHP?) of the widget that would make it only appear if the site URL is not the one where I don't ...

Fade between two elements using jQuery

Can someone assist me with adding a fade effect between elements in my code? Any advice or suggestions would be greatly appreciated! $("#example p:first").css("display", "block"); jQuery.fn.timer = function() { if(!$(this).children("p:last-child").i ...

Error: Attempting to access property 'question' of an undefined value

Trying to render information from a local .json file using Javascript functions, I encountered an error in the console for const answer despite being defined. I temporarily commented it out to test the function, only to receive the same TypeError for quest ...

Browser tries to interpret Javascript code as a file organization system

Encountering an issue while trying to load my website. When I open index.html in my browser, I receive a problem loading page message. This response is puzzling as it does not indicate a file loading error. Here's a snippet of the response: Firefox c ...

Retrieving text content from an HTML element with Jsoup for web page manipulation

My task involves extracting text content from a specific HTML element <span class="adr" style="float: none !important;"> <span class="street-address" style="float: none !important;">18, Jawaharlal Nehru Road, </span> ...

I'm trying to figure out how to retrieve data from a JQuery autocomplete response that contains an array. Specifically, I want

https://i.stack.imgur.com/YpuJl.pngThis form field is for entering text input. <span> <img src="images/author2.jpg" width="50" /> //The profile picture displayed here is static but in the database I have a dynamic image. <input class="sea ...

Issue with HTML and CSS Grid alignment coupled with CSS syntax error on RBRACE

I'm in the process of updating my website indexes and decided to incorporate grids. However, when attempting to implement grids using Dreamweaver, I encountered several issues. Firstly, an error message appeared stating "Expected RBRACE at line 72, co ...

By default, the sidebar is open on desktop devices while closed on mobile devices

I am struggling to figure out how to set my sidebar/navigation content, using Bootstrap, to be expanded by default on desktop and closed by default on mobile with the icon only showing on mobile devices. Despite multiple attempts, I cannot seem to make thi ...

Can the z-index of a div be altered by a checked checkbox?

I've been trying to figure out how to make a PayPal button only clickable after the user confirms the Terms of Service. My idea is to place another div over the PayPal button with an unchecked checkbox, opacity, and z-index. When the user checks the c ...

Color highlighting in dropdown HTML items

I'm trying to create a dropdown menu with alternating colors for the items. Can someone please provide the CSS code needed to style dropdown list items using ODD and EVEN? ...

Exploring Partial Views in Bootstrap Single Page View and AngularJS

Currently, I am utilizing Bootstrap's single page view design, specifically the one found at this link: http://www.bootply.com/85746. As my code in the view has grown to nearly 500 lines and is expected to expand further, I am seeking a method to crea ...

What is causing nested divs to generate a scrollbar?

I am struggling with a situation involving 3 layers of nested divs: <div class="outer"> <div class="inner"><div class="item"></div></div> </div> In this scenario, the .inner div is set to position absolute and each d ...

A time-tracking counter that tallies the amount of time users spend on the webpage

I'm currently working on a code that tracks the time spent on a page starting from when it was clicked. My challenge now is figuring out how to capture the time when the submit button is clicked and store it in the database. < script > var ...

Animation of CSS with an image depicting a leaf swaying gently in the breeze

This project was a personal challenge for me, and I am quite satisfied with the approach I have developed. However, I am open to exploring alternative methods if there are any. The website I am working on features a logo that includes an image of a leaf. ...

Selecting a webpage from a dropdown list to display its specific content within an iframe

I have a dropdown menu similar to this: <form name="change"> <SELECT NAME="options" ONCHANGE="document.getElementById('frame1').src = this.options[this.selectedIndex].value"> <option value="">Choose</option> <opti ...

Mobile devices seem to be constantly refreshing website images

I have a landing page consisting of multiple sections with images as the background, each section occupying the full screen. In one specific section, the images change every 5 seconds. The website works smoothly on desktop, but I encounter issues on mobil ...

What is the best way to place text side by side with an image?

How can I align text below a picture like this? https://i.stack.imgur.com/WcNRa.jpg Here is the code snippet: https://jsfiddle.net/vzjj9eLy/ HTML: <span class="test"> <img src="http://cdn.sstatic.net/Sites/stackoverflow/img/<a href="/cd ...