Ways to eliminate extra spacing when search bar is expanded

My code is all contained within a header with the class 'example'. When the 'search' button is clicked, a div with the class 'search-bar' appears and its borders match those of the header. However, I want the search bar to align with the left edge of the screen. How can this be achieved?

const searchBar =
  document.querySelector('#search-bar');
const openBtn = document.querySelector('#open-btn');
const closeBtn = document.querySelector('#close-btn');
const brand = document.querySelector('#brand');

openBtn.addEventListener('click', (e) => toggleSlider());
closeBtn.addEventListener('click', (e) => toggleSlider());

function toggleSlider() {
  searchBar.classList.toggle('open');
  brand.classList.toggle('open');
}
.example {
  padding: 0 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  z-index: 100;
  transition: all 1s ease;
}

.brand.open {
  color: #fff;
}

.slide-area {
  width: 100%;
  padding: 10px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  height: 50px;
  overflow: hidden;
}

.search-bar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background-color: purple;
  position: absolute;
  padding: 10px;
  right: 0;
  top: 0;
  left: 0;
  bottom: 0;
  transform: translateX(100%);
  transition: all 1s ease;
}

.search-bar.open {
  transform: translateX(0);
}
<header class="example">
  <div class="slide-area">
    <div id="brand" class="brand">Menu Stuff
    </div>
    <button id="open-btn">Search
            </button>
    <div id="search-bar" class="search-bar">
      <button id="close-btn">X</button>
    </div>
  </div>
  <div class="btns">Log in</div>
</header>

Link to the working example in CodePen

Answer №1

Make the adjustment of replacing overflow: hidden with .example

const searchBar =
  document.querySelector('#search-bar');
const openBtn = document.querySelector('#open-btn');
const closeBtn = document.querySelector('#close-btn');
const brand = document.querySelector('#brand');

openBtn.addEventListener('click', (e) => toggleSlider());
closeBtn.addEventListener('click', (e) => toggleSlider());

function toggleSlider() {
  searchBar.classList.toggle('open');
  brand.classList.toggle('open');
}
body {
  margin: 0px;
}

.example {
  padding: 0 50px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  overflow: hidden;
}

.brand {
  z-index: 100;
  transition: all 1s ease;
}

.brand.open {
  color: #fff;
}

.slide-area {
  width: 100%;
  padding: 10px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 50px;
}

.search-bar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background-color: purple;
  position: absolute;
  padding: 10px;
  right: 0;
  top: 0;
  left: 0;
  bottom: 0;
  transform: translateX(100%);
  transition: all 1s ease;
}

.search-bar.open {
  transform: translateX(0);
}
<header class="example">
  <div class="slide-area">
    <div id="brand" class="brand">Menu Stuff
    </div>
    <button id="open-btn">Search
            </button>
    <div id="search-bar" class="search-bar">
      <button id="close-btn">X</button>
    </div>
  </div>
  <div class="btns">Log in</div>
</header>

Codepen link

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

Is there a method to give a webpage a subtle shimmering effect without utilizing CSS box-shadow?

Struggling to Develop a High-Performance Interface Feature I'm currently facing a challenge in coding an interface that requires a subtle and broad glow effect, similar to the example provided below: https://i.sstatic.net/E4ilD.jpg Exploration of ...

Centered header with underline styled using CSS pseudo element

I have centered heading text in a container, and I am using a border-bottom and pseudo element to create an underline effect. However, I am uncertain about how to make the underline stop at the end of the heading text. https://i.sstatic.net/FseHl.png Her ...

Attain three images with precise dimensions using CSS

Having trouble fitting 3 images in the same row? The issue arises when the images have different sizes, causing everything to shift. Any advice on how to address this problem? Thank you. HTML: <div class="first-slot"> <di ...

What is the best way to design an element in HTML/CSS with a curved semicircle border at the top?

I'm looking to design an HTML/CSS element that features a semicircle at the top-middle with a border. Inside this semicircle, I want to place a smaller circle containing a single character (similar to the X in the provided image). Additionally, there ...

Designing an interactive HTML table that adapts to various screen

Currently utilizing Bootstrap to create a responsive HTML table on smaller devices like the iPad, but seeking a more polished and professional alternative. Searching for a JQuery/JavaScript or CSS solution without relying on plugins. Would appreciate any ...

The waveform's bottom appears distorted when using Bootstrap

I'm currently experimenting with creating a header wave in Bootstrap CSS using SVG shape. However, I encountered an issue when implementing bootstrap into the HTML code - my design resembles the second image instead of the desired look of the first on ...

Struggling with the elimination of bullet points in CSS navigation bars

I'm having trouble removing the bullet points from my navigation bar. I've tried using list-style-type: none and even adding !important, but it doesn't seem to work. Strangely enough, everything looks fine in Chrome, but I get an error when ...

Is the float floating within another float?

This question may have a strange title, but I couldn't think of anything better to call it. So, here's the situation: I have a grid layout where I need my .search-wrapper to be 50% wide and floated to the right. In my demonstration on jsfiddle, ...

Understanding the distinction between .navbar and .navbar a in CSS

Can you explain the distinction between .navbar and .navbar a? .navbar { overflow: hidden; background-color: #333; font-family: Arial; } .navbar a { float: left; font-size: 16px; color: white; ...

Enhancing the Search Bar in Bootstrap 4 Dashboard Template: A Step-by-Step Guide

Recently got my hands on the Bootstrap 4 Dashboard template and decided to enhance it by adding a form to the top search bar. However, I encountered an issue where the search bar shrinks and loses its original design. If you want to check out the Bootstra ...

Generate additional tabs

Currently, I am in the process of working on a project where I have a bar that will contain dynamically filled tabs. My goal is to include a (+) image that becomes visible when the bar is full. When a user clicks on this image, the remaining tabs should ap ...

Resolving the Material-UI NextJS Button Styling Dilemma

I've encountered an issue with the styling of a few buttons in JS. When I apply a styling class using className, the formatting works fine on the initial render but loses its styling on subsequent refreshes. This problem is specific to two individual ...

Mobile-responsive iFrame content is designed to adjust and fit appropriately on

While my iframe is responsive on both mobile and website, I am facing an issue where the content overflows (both overflow X and Y) from the width and height of the iFrame. Here's the scenario: in the mobile view (simulated using Google Chrome to mimi ...

Boosting Your Theme - Eliminating Redundant Styles

Currently, I am facing more of a best practice issue rather than a coding one. I am in the process of creating a custom bootstrap theme inspired by https://github.com/HackerThemes/theme-kit. Although I have a functional theme that I am satisfied with, I am ...

menu fails to expand when clicked in mobile view

Could someone please help me troubleshoot why the menu icon is not expanding in mobile view? I'm not sure what I did wrong. Here is the link for reference: Snippet of HTML: <nav class="navbar navbar-default navbar-fixed-top" role="navigation"> ...

Tips on overriding the outline:none property in CSS

Is there a way to overwrite the 'outline: none' property in CSS? I have a parent class that has this property set, but I want to remove it in the child class. ...

Is it possible to show one element while hiding others upon clicking using JavaScript?

Concept My idea is to create a website with a navigation menu where only one section is visible at a time. Each section would become visible upon clicking a specific button in the navigation bar. Challenge I attempted to achieve this using the following ...

Is it possible for a website to be compromised by incorporating CSS within HTML code?

I'm currently working on developing a shopping cart component in React, but my supervisor has advised against using CSS directly in the HTML markup due to security concerns. While I understand the importance of good coding practices, I fail to see how ...

I am encountering an issue where I am unable to access properties of null while trying to read the 'pulsate' function within the

The current version of my material-ui library is as follows: "@mui/icons-material": "^5.5.1", "@mui/material": "^5.5.1", This is how I included the Button component : import Button from "@mui/material/Button&qu ...

Can a website be designed to mimic the style of the current operating system?

Is there a way to apply CSS styling to web site elements, such as buttons, that mimics the appearance of operating system (Windows, macOS, Linux) button styles? This would allow HTML buttons to automatically adjust their CSS style whenever I switch theme ...