javascript - The side menu bar(sticky) is overflowing unless I scroll - Stack Overflow
I am building a React app with a MainLayout component that sets consistent padding for all pages using utility classes (e.g., Tailwind CSS). Here is my MainLayout code:
import React from "react";
import Navbar from "./Navbar";
import { Outlet } from "react-router-dom";
function MainLayout() {
return (
// <div className="px-4 md:px-24 lg:px-32 xl:px-42 2xl:px-60">
<div className="px-4 md:px-8 lg:px-16 lx:px-32 2xl:px-64">
<Navbar />
<Outlet />
</div>
);
}
export default MainLayout;
On one of my pages, PostListPage, I have a FilterSideMenu component that appears alongside a list of posts. Here's the PostListPage layout:
import React, { useState } from "react";
import PostList from "../components/PostList";
import FilterSideMenu from "../components/FilterSideMenu";
function PostListPage() {
const [open, setOpen] = useState(false);
return (
<>
<div className="mb-10 mt-6">
<h1 className="text-2xl mb-6">Example Blog</h1>
<button
onClick={() => setOpen((prev) => !prev)}
className="bg-secondaryColor px-4 py-2 md:hidden mt-6 mb-6 rounded-lg dark:text-black"
>
{open ? "Close" : "Filter or Search"}
</button>
<div className="flex flex-col-reverse gap-8 md:flex-row justify-between">
<div>
<PostList />
</div>
<div className={`${open ? "block" : "hidden"} md:block`}>
<FilterSideMenu />
</div>
</div>
</div>
</>
);
}
export default PostListPage;
And here's the FilterSideMenu:
import React from "react";
import { Link } from "react-router-dom";
import Search from "./Search";
function FilterSideMenu() {
return (
<div className="px-6 h-max sticky top-10 dark:bg-gray-600 bg-gray-100 p-4 rounded-2xl">
<div className="flex flex-col gap-8">
<div>
<span className="font-medium mb-2 block">Search</span>
<Search />
</div>
<div className="flex flex-row md:gap-4 md:flex-col gap-32">
<div>
<span className="font-medium">Filter</span>
<div className="flex flex-col gap-2 mt-2 text-sm">
<label
htmlFor="newest"
className="flex items-center gap-2 cursor-pointer"
>
<input
type="radio"
id="newest"
name="sort"
value="newest"
className="w-4 h-4 rounded-sm border-[1.5px] appearance-none checked:bg-secondaryColor border-black dark:border-gray-300"
/>
Newest
</label>
<label
htmlFor=""
name="sort"
value="popular"
className="flex items-center gap-2 cursor-pointer"
>
<input
type="radio"
className="w-4 h-4 rounded-sm border-[1.5px] appearance-none checked:bg-secondaryColor border-black dark:border-gray-300"
/>
Most Popular
</label>
<label
htmlFor=""
name="sort"
value="trending"
className="flex items-center gap-2 cursor-pointer "
>
<input
type="radio"
className="w-4 h-4 rounded-sm border-[1.5px] appearance-none checked:bg-secondaryColor border-black dark:border-gray-300"
/>
Trending
</label>
<label
htmlFor=""
name="sort"
value="oldest"
className="flex items-center gap-2 cursor-pointer "
>
<input
type="radio"
className="w-4 h-4 rounded-sm border-[1.5px] appearance-none checked:bg-secondaryColor border-black dark:border-gray-300"
/>
Oldest
</label>
</div>
</div>
<div className="">
<span className="font-medium">Categories</span>
<div className="flex flex-col underline gap-2 mt-4 text-sm">
<Link to="/posts?cat=all">All</Link>
<Link to="/posts?cat=categ1">Categ1</Link>
<Link to="/posts?cat=categ1">Categ2</Link>
<Link to="/posts?cat=categ1">Categ3</Link>
<Link to="/posts?cat=categ1">Categ4</Link>
</div>
</div>
</div>
</div>
</div>
);
}
export default FilterSideMenu;
The issue arises when I visit the /posts page. The sidebar (FilterSideMenu) initially appears outside the viewport horizontally, but after I scroll a bit, it adjusts itself and comes inside the viewport. The expected behavior is that it should respect the padding defined in MainLayout (px-4 md:px-8 lg:px-16 lx:px-32 2xl:px-64) right from the beginning.
Verified that the FilterSideMenu component is wrapped inside the MainLayout's padding. Checked the CSS classes and styles for conflicting margins or padding. Used browser developer tools to observe layout behavior and noticed that the issue resolves after a slight scroll, indicating a potential reflow/repaint issue.
The FilterSideMenu should respect the padding applied by MainLayout immediately when the page loads, without requiring a scroll.
I am building a React app with a MainLayout component that sets consistent padding for all pages using utility classes (e.g., Tailwind CSS). Here is my MainLayout code:
import React from "react";
import Navbar from "./Navbar";
import { Outlet } from "react-router-dom";
function MainLayout() {
return (
// <div className="px-4 md:px-24 lg:px-32 xl:px-42 2xl:px-60">
<div className="px-4 md:px-8 lg:px-16 lx:px-32 2xl:px-64">
<Navbar />
<Outlet />
</div>
);
}
export default MainLayout;
On one of my pages, PostListPage, I have a FilterSideMenu component that appears alongside a list of posts. Here's the PostListPage layout:
import React, { useState } from "react";
import PostList from "../components/PostList";
import FilterSideMenu from "../components/FilterSideMenu";
function PostListPage() {
const [open, setOpen] = useState(false);
return (
<>
<div className="mb-10 mt-6">
<h1 className="text-2xl mb-6">Example Blog</h1>
<button
onClick={() => setOpen((prev) => !prev)}
className="bg-secondaryColor px-4 py-2 md:hidden mt-6 mb-6 rounded-lg dark:text-black"
>
{open ? "Close" : "Filter or Search"}
</button>
<div className="flex flex-col-reverse gap-8 md:flex-row justify-between">
<div>
<PostList />
</div>
<div className={`${open ? "block" : "hidden"} md:block`}>
<FilterSideMenu />
</div>
</div>
</div>
</>
);
}
export default PostListPage;
And here's the FilterSideMenu:
import React from "react";
import { Link } from "react-router-dom";
import Search from "./Search";
function FilterSideMenu() {
return (
<div className="px-6 h-max sticky top-10 dark:bg-gray-600 bg-gray-100 p-4 rounded-2xl">
<div className="flex flex-col gap-8">
<div>
<span className="font-medium mb-2 block">Search</span>
<Search />
</div>
<div className="flex flex-row md:gap-4 md:flex-col gap-32">
<div>
<span className="font-medium">Filter</span>
<div className="flex flex-col gap-2 mt-2 text-sm">
<label
htmlFor="newest"
className="flex items-center gap-2 cursor-pointer"
>
<input
type="radio"
id="newest"
name="sort"
value="newest"
className="w-4 h-4 rounded-sm border-[1.5px] appearance-none checked:bg-secondaryColor border-black dark:border-gray-300"
/>
Newest
</label>
<label
htmlFor=""
name="sort"
value="popular"
className="flex items-center gap-2 cursor-pointer"
>
<input
type="radio"
className="w-4 h-4 rounded-sm border-[1.5px] appearance-none checked:bg-secondaryColor border-black dark:border-gray-300"
/>
Most Popular
</label>
<label
htmlFor=""
name="sort"
value="trending"
className="flex items-center gap-2 cursor-pointer "
>
<input
type="radio"
className="w-4 h-4 rounded-sm border-[1.5px] appearance-none checked:bg-secondaryColor border-black dark:border-gray-300"
/>
Trending
</label>
<label
htmlFor=""
name="sort"
value="oldest"
className="flex items-center gap-2 cursor-pointer "
>
<input
type="radio"
className="w-4 h-4 rounded-sm border-[1.5px] appearance-none checked:bg-secondaryColor border-black dark:border-gray-300"
/>
Oldest
</label>
</div>
</div>
<div className="">
<span className="font-medium">Categories</span>
<div className="flex flex-col underline gap-2 mt-4 text-sm">
<Link to="/posts?cat=all">All</Link>
<Link to="/posts?cat=categ1">Categ1</Link>
<Link to="/posts?cat=categ1">Categ2</Link>
<Link to="/posts?cat=categ1">Categ3</Link>
<Link to="/posts?cat=categ1">Categ4</Link>
</div>
</div>
</div>
</div>
</div>
);
}
export default FilterSideMenu;
The issue arises when I visit the /posts page. The sidebar (FilterSideMenu) initially appears outside the viewport horizontally, but after I scroll a bit, it adjusts itself and comes inside the viewport. The expected behavior is that it should respect the padding defined in MainLayout (px-4 md:px-8 lg:px-16 lx:px-32 2xl:px-64) right from the beginning.
Verified that the FilterSideMenu component is wrapped inside the MainLayout's padding. Checked the CSS classes and styles for conflicting margins or padding. Used browser developer tools to observe layout behavior and noticed that the issue resolves after a slight scroll, indicating a potential reflow/repaint issue.
The FilterSideMenu should respect the padding applied by MainLayout immediately when the page loads, without requiring a scroll.
Share Improve this question asked 21 hours ago Sowmith SripadiSowmith Sripadi 1 New contributor Sowmith Sripadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 0The issue arises because the sticky property of FilterSideMenu doesn’t properly calculate its position until after a reflow. Fix this by wrapping the content in a consistent container and ensuring FilterSideMenu has a fixed width to prevent layout shifts.
Updated code below
function MainLayout() {
return (
<div className="relative px-4 md:px-8 lg:px-16 xl:px-32 2xl:px-64">
<Navbar />
<Outlet />
</div>
);
}
FilterSideMenu
<div className="px-6 h-max sticky top-10 dark:bg-gray-600 bg-gray-100 p-4 rounded-2xl w-full md:w-[250px]">
This ensures the layout respects padding, without requiring a scroll.
- 微软发布Surface平板:定义并示范Win8用户体验
- 研究机构:平板四年内将成主流计算设备
- 微软反攻打响跨界战争:Win8移动领域伸橄榄枝
- linux - How to use GNU ld with rustc-compiled obj file without cargo? - Stack Overflow
- Unable to deploy flex consumption function app using azure bicep - Stack Overflow
- ST_CONTAINS() giving FALSE even if Point lies within polygon. google-bigquery - Stack Overflow
- python - NaN values in Pandas are not being filled by the interpolate function when it's applied to a full dataframe - S
- asynchronous - Parallelization by threads vs Parallelization by processes on backend - Stack Overflow
- Receiver incorrectly registered? Android, Kotlin - Stack Overflow
- c++ - Microbenchmark - backward iteration results in fewer cache misses - Stack Overflow
- Any way to open an SSIS project with a newer version of Visual Studio? - Stack Overflow
- python - Unet isn't working orand i'm not using it correctly - Stack Overflow
- shell - how to change my CONDA_DEFAULT_ENV in Linux? - Stack Overflow
- python - pickle port? loading a pickle with data when I've redefined the object code - Stack Overflow
- c++ - Vscode doesn't pass args to program when debugging - Stack Overflow
- apache - is it possible to graceful single process in passenger rails app? - Stack Overflow
- arkit - Detecting and Using 2 Different Vertical Planes - Stack Overflow