Как зафиксировать элемент Header в React
Если делать position: sticky, мой дочерний элемент с именем класса .blot с стилем position: absolute, постоянно работает
repository: https://github.com/halowddyep/42cups
JS:
import styles from "./Navbar.module.css";
import { NavLink } from "react-router-dom";
import logo from "../../assets/images/logo.jpg";
import blot from "../../assets/images/blot.png";
import bag from "../../assets/images/bag.png";
import { useAppSelector } from "../../hooks/hooks";
const Navbar: React.FC = () => {
const sum = useAppSelector((state) => state.items.sum);
const numberOfProducts = useAppSelector(
(state) => state.items.numberOfProducts
);
return (
<header className={styles.header}>
<NavLink to="/" className={styles.logoBlock}>
<img
src={logo}
height="80px"
width="80px"
className={styles.logo}
alt="logo"
></img>
</NavLink>
<div className={styles.navigationBlock}>
<NavLink to="/assortment" className={styles.navItem}>
<p>Ассортимент</p>
<img
className={styles.blot}
src={blot}
height="100%"
width="100%"
alt="blot"
></img>
</NavLink>
<NavLink to="/franchise" className={styles.navItem}>
<p>Франшиза</p>
<img
className={styles.blot}
src={blot}
height="100%"
width="100%"
alt="blot"
></img>
</NavLink>
<NavLink to="/company" className={styles.navItem}>
<p>О компании</p>
<img
className={styles.blot}
src={blot}
height="100%"
width="100%"
alt="blot"
></img>
</NavLink>
<NavLink to="/contact" className={styles.navItem}>
<p>Контакты</p>
<img
className={styles.blot}
src={blot}
height="100%"
width="100%"
alt="blot"
></img>
</NavLink>
</div>
<NavLink to="/cart" className={styles.cartBlock}>
<img src={bag} width="30px" height="30px" alt="bag"></img>
<div className={styles.info}>
{sum} ₽ <br></br>
{numberOfProducts} товаров
</div>
</NavLink>
</header>
);
};
export default Navbar;
CSS:
.header {
font-family: "Trebuchet-MS", sans-serif;
display: flex;
width: 100%;
height: 100px;
background-color: #fff;
color: #000;
font-weight: 600;
box-shadow: 0 14px 19px rgb(0 0 0 / 20%);
/* position: sticky; */
top: 0;
}
.logoBlock {
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
width: 20%;
height: 100%;
}
.logoBlock .logo {
display: flex;
align-items: center;
justify-content: center;
}
.navigationBlock {
display: flex;
width: 60%;
height: 100%;
}
.navItem {
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
width: 25%;
height: 100%;
color: #000;
position: relative;
}
.navItem p {
z-index: 2;
}
.navItem:hover {
color: #fff;
}
.navItem:hover .blot {
z-index: 1;
}
.cartBlock {
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
width: 20%;
height: 100%;
color: #000;
}
.header a {
text-decoration: none;
}
.blot {
position: absolute;
z-index: -1;
}
.info {
margin-left: 10px;
font-weight: 400;
}