Adjust header display for smaller screens

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

I want the search bar to go underneath on smaller screens, like this example:

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

Using Ajax to implement a content slider with lightSlider

Seeking assistance in developing a dynamic content slider utilizing the LightSlider plugin. Currently, I have a script that fetches content from a database table (outputting JSON to HTML) and displays 6 div's of content per slide. However, I aim to di ...

Converting CSS specific to a particular vendor: A step-by-step

Looking for a method to adapt a CSS file containing only -moz-css-property references to include compatibility with Chrome, Safari, and other browsers. Ideally seeking a user-friendly web application that can perform this task seamlessly. ...

What is the best way to replicate tags in selenium when an element has multiple class names?

Extracting information from a webpage where a significant amount of text was concealed behind the "see more" tab. Using selenium to click on all such buttons and then extracting data with beautifulsoup. However, some of these buttons have additional space ...

Troubleshooting problem with Angular 2 in Internet Explorer related to the use of [style]="

I've encountered a challenge while using angular 2 (2.0.0-beta.17). The app works perfectly on most browsers, but as expected, IE 11 is causing trouble. The scripts included in the head for angular are: <script type='text/javascript' sr ...

Facing difficulties in retrieving my static files with Express and Node.js

I'm puzzled as to why express isn't able to access my static file to display my CSS. Here is the code snippet from express.js: app.use(express.static(__dirname + '/public')); Contents of style.css: <style type="text/css"> bod ...

Leveraging package.json information within HTML using ReactJS

Currently, I am facing a situation where I need to incorporate the name attribute from the package.json file into my HTML code in order to use it as the title of the document. While I am aware that this can be achieved through the use of the react-helmet ...

Ensure that the assistant stays beneath the cursor while moving it

I am currently working on creating a functionality similar to the 'Sortable Widget', but due to some constraints, I cannot use the premade widget. Instead, I am trying to replicate its features using draggable and droppable elements: $(".Element ...

The battle between nested flexbox heights and max-heights

Note: I am observing some unusual behavior on Chrome 72. FireFox 64 seems to be working fine! I am experiencing an issue with a nested flexbox. In the code snippet below, I have added an artificial XL height to .root.container in order to achieve the des ...

Update the display of the mouse pointer

I use CSS to set a custom cursor image: #scroll_up{ display: block; position: absolute; width: 960px; height: 300px; cursor: url(../imgs/cursor_up.png), auto; } The above style is assigned to a div element so that the cursor appea ...

Exploring the utilization of server-side Web Sockets functionality

Recently, I've been diving into the world of Web Sockets and I'm eager to understand how server-side implementation works. While I feel comfortable with writing client-side code using onmessage() in HTML5, JavaScript, etc., I find myself unsure a ...

Issue with Bootstrap tab display of content

I'm having trouble with the tabs in my page. When I click on each tab, the content doesn't display correctly. Here is my code: <div class="w470px exam" style="border: 1px solid #ddd; margin-top: 30px;"> <ul id="myTab" class="nav nav ...

Having trouble with jQuery show/hide not functioning properly?

My website has a sub-menu section with a shopping cart icon. When a user hovers over the icon, it reveals a hidden cart section. The user can then move the mouse into the cart section to remove items. The problem I'm facing is that if a user displays ...

Utilizing Google Chrome Developer Tools for JQuery console debugging

Just started using jQuery and was expecting to see [<li>my name</li>, in the Google Chrome console but got: [li, prevObject: r.fn.init(1)] 0 : li length : 1 prevObject : r.fn.init(1) proto : Object(0) <html> ...

The 502 Bad Gateway error strikes again on Google Drive

I have a website with numerous photos stored on Google Drive. The image tags in my code look like this: <img src="https://googledrive.com/host/<foldId>/A14.jpg"> Unfortunately, many of the photos are not loading and instead showing a 502 Bad ...

Retrieving information from PHP using AJAX

As a newcomer to the world of AJAX, I am faced with the task of retrieving data from a PHP file and storing it in a JavaScript variable. Despite exploring several examples, I have not been able to find a satisfactory solution. Here is a simplified HTML cod ...

Aligning divs, prevent duplicate HTML within multiple divs

Currently, I am attempting to troubleshoot a jsfiddle issue. The main problem lies in my desire for the first two divs (with class="fbox") to be aligned next to each other on the same level. Furthermore, I am looking to enable dragging the image into the ...

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= ...

Monitoring user clicks using JavaScript code implemented

Currently, I am utilizing a JavaScript file to load on certain pages in order to track various events. Within this JavaScript file, there is a collection of selectors that have listeners attached to them. When these selectors are clicked, the tracking AP ...

Incorporating JavaScript unit tests into an established website

I am facing a challenge with testing my JavaScript functions in the context of a specific webpage. These functions are tightly integrated with the UI elements on the page, so I need to be able to unit-test the entire webpage and not just the functions them ...

What are some effective strategies for enhancing the performance of React user interfaces?

I'm currently dealing with a performance challenge in one of my React applications. What are some best practices to enhance the responsiveness of the User Interface? One approach could be to minimize the usage of conditional expressions like { carIsR ...