Я новичок в веб-разработке и в настоящее время разрабатываю веб-сайт-портфолио на основе django и начальной загрузки 5.3.3, и у меня проблема с боковой панелью, прилипающей к верхней части страницы, а не к верхней части экрана пользователя (мне это нужно). быть видимым пользователю независимо от того, где он находится на странице). Я пробовал использовать класс начальной загрузки sticky-top
, но, похоже, он не внес никаких изменений в свое поведение.
Вот мой код:
<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<meta name = "viewport" content = "width=device-width, initial-scale=1">
<title>My Portfolio</title>
<link href = "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel = "stylesheet" integrity = "sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin = "anonymous">
<link rel = "stylesheet" href = "https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
</head>
<body>
<div class = "container-fluid sticky-top" style = "background-color: rgb(255, 255, 255);"> <!--rgb(52, 52, 52)-->
<div class = "row flex-nowrap">
<!-- Start Navbar -->
<div class = "col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-dark">
<div class = "d-flex flex-column align-items-center align-items-sm-start px-3 pt-2 text-white min-vh-100">
<div class = "dropdown pb-4">
<a href = "#" class = "d-flex align-items-center text-white text-decoration-none " id = "dropdownUser1" data-bs-toggle = "dropdown" aria-expanded = "false">
<img src = "https://github.com/mdo.png" alt = "hugenerd" width = "30" height = "30" class = "rounded-circle">
{% if user.is_authenticated %}
<span class = "d-none d-sm-inline mx-1">{{user.username}}</span>
{% else %}
<span class = "d-none d-sm-inline mx-1">Aleksandr Gruzdev</span>
{% endif %}
</a>
<!-- <ul class = "dropdown-menu dropdown-menu-dark text-small shadow">
<li><a class = "dropdown-item" href = "#">New project...</a></li>
<li><a class = "dropdown-item" href = "#">Settings</a></li>
<li><a class = "dropdown-item" href = "#">Profile</a></li>
<li>
<hr class = "dropdown-divider">
</li>
<li><a class = "dropdown-item" href = "#">Sign out</a></li>
</ul> -->
</div>
<ul class = "nav nav-pills flex-column mb-sm-auto mb-0 align-items-center align-items-sm-start" id = "menu">
<li class = "nav-item">
<a href = "/" class = "nav-link align-middle px-0">
<span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-house"></i> Home</span>
</a>
</li>
<li>
<a href = "/#about" class = "nav-link px-0 align-middle mt-1">
<span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-person"></i> About</span></a>
</li>
<li>
<a href = "#" class = "nav-link px-0 align-middle mt-1">
<span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-images"></i> Resume</span></a>
</li>
<li>
<a href = "#" class = "nav-link px-0 align-middle mt-1">
<span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-hdd-stack"></i> Services</span></a>
</li>
<li>
<a href = "#projects" data-bs-toggle = "collapse" class = "nav-link px-0 align-middle dropdown-toggle mt-1" style = "color: rgb(20, 157, 221);">
<span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-card-list"></i> Projects</span> </a>
<ul class = "collapse nav flex-column ms-1" id = "projects" data-bs-parent = "#menu">
<li class = "w-100">
<a href = "blogpost" class = "nav-link px-0" style = "color: rgb(20, 157, 221);"> <span class = "d-none d-sm-inline">Blog Post</span> </a>
</li>
<li>
<a href = "/lekschat" class = "nav-link px-0" style = "color: rgb(20, 157, 221);"> <span class = "d-none d-sm-inline">LeksChat™</span> </a>
</li>
<li>
<a href = "#game" data-bs-toggle = "collapse" class = "nav-link px-10 align-right dropdown-toggle" style = "color: rgb(20, 157, 221);">
<span class = "ms-1 d-none d-sm-inline"><i class = "bi bi-card-list"></i> Space Runner Game</span> </a>
<ul class = "collapse nav flex-column ms-1" id = "game" data-bs-parent = "#projects">
<li>
<a href = "#" class = "nav-link px-0" style = "color: rgb(20, 157, 221);"> <span class = "d-none d-sm-inline">Game</span> </a>
</li>
<li class = "w-100">
<a href = "#" class = "nav-link px-0" style = "color: rgb(20, 157, 221);"> <span class = "d-none d-sm-inline">Leaderboard</span> </a>
</li>
<li>
<a href = "#" class = "nav-link px-0" style = "color: rgb(20, 157, 221);"> <span class = "d-none d-sm-inline">Personal Best</span> </a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href = "#" class = "nav-link px-0 align-middle mt-1">
<span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-envelope"></i> Contact</span></a>
</li>
<li>
{% if user.is_authenticated %}
<a href = "logout" class = "nav-link px-0 align-middle mt-1">
<span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-door-closed"></i> Log out</span></a>
{% if user.is_superuser %}
<a href = "http://127.0.0.1:8000/admin/" target = "_blank" rel = "noopener noreferrer" class = "nav-link px-0 align-middle mt-1"><span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-door-closed"></i> Admin Panel</span></a>
{% endif %}
{% else %}
<a href = "register" class = "nav-link px-0 align-middle mt-1">
<span class = "ms-1 d-none d-sm-inline" style = "font-size: x-large; color: rgb(20, 157, 221);"><i class = "bi bi-box-arrow-in-right"></i> Login or Signup</span></a>
{% endif %}
</li>
</ul>
<hr>
</div>
</div>
<!-- End navbar -->
<div class = "col py-3">
<main class = "main">
{% block content %}
{% endblock %}
</main>
</div>
</div>
</div>
<script src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity = "sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin = "anonymous"></script>
</body>
</html>
Я нашел этот вопрос уже заданным в прошлом, но решение не помогло мне, а также человеку, который задал вопрос: Как сделать боковую панель «липкой» в Bootstrap 5.2 На других сайтах были запутанные ответы.
Я удалил sticky-top
из container-fluid
. Я также удалил flex-nowrap
из row
. Я добавил следующее в sidebar
, чтобы fixed
и scrollable
отображались на экране любого размера: position-fixed
top-0
bottom-0
start-0
overflow-auto
.
Я добавил ms-auto
в основной контейнер. Я также обновил ширину col
для разных размеров экрана, но ваш текущий code
не подходит для просмотра на мобильных устройствах. Поэтому, пожалуйста, настройте боковую панель для просмотра на мобильных устройствах.
Я решил вашу текущую проблему. Обратите внимание на следующее code
:
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8" />
<meta name = "viewport" content = "width=device-width, initial-scale=1" />
<title>My Portfolio</title>
<link
href = "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel = "stylesheet"
integrity = "sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin = "anonymous"
/>
<link
rel = "stylesheet"
href = "https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"
/>
</head>
<body>
<div class = "container-fluid" style = "background-color: rgb(255, 255, 255)">
<!--rgb(52, 52, 52)-->
<div class = "row">
<!-- Start Navbar -->
<div class = "col-sm-4 col-md-3 col-xl-2 px-sm-2 px-0 bg-dark position-fixed top-0 bottom-0 start-0 overflow-auto">
<div
class = "d-flex flex-column align-items-center align-items-sm-start px-3 pt-2 text-white min-vh-100"
>
<div class = "dropdown pb-4">
<a
href = "#"
class = "d-flex align-items-center text-white text-decoration-none"
id = "dropdownUser1"
data-bs-toggle = "dropdown"
aria-expanded = "false"
>
<img
src = "https://github.com/mdo.png"
alt = "hugenerd"
width = "30"
height = "30"
class = "rounded-circle"
/>
{% if user.is_authenticated %}
<span class = "d-none d-sm-inline mx-1">{{user.username}}</span>
{% else %}
<span class = "d-none d-sm-inline mx-1">Aleksandr Gruzdev</span>
{% endif %}
</a>
<!-- <ul class = "dropdown-menu dropdown-menu-dark text-small shadow">
<li><a class = "dropdown-item" href = "#">New project...</a></li>
<li><a class = "dropdown-item" href = "#">Settings</a></li>
<li><a class = "dropdown-item" href = "#">Profile</a></li>
<li>
<hr class = "dropdown-divider">
</li>
<li><a class = "dropdown-item" href = "#">Sign out</a></li>
</ul> -->
</div>
<ul
class = "nav nav-pills flex-column mb-sm-auto mb-0 align-items-center align-items-sm-start"
id = "menu"
>
<li class = "nav-item">
<a href = "/" class = "nav-link align-middle px-0">
<span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-house"></i> Home</span
>
</a>
</li>
<li>
<a href = "/#about" class = "nav-link px-0 align-middle mt-1">
<span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-person"></i> About</span
></a
>
</li>
<li>
<a href = "#" class = "nav-link px-0 align-middle mt-1">
<span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-images"></i> Resume</span
></a
>
</li>
<li>
<a href = "#" class = "nav-link px-0 align-middle mt-1">
<span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-hdd-stack"></i> Services</span
></a
>
</li>
<li>
<a
href = "#projects"
data-bs-toggle = "collapse"
class = "nav-link px-0 align-middle dropdown-toggle mt-1"
style = "color: rgb(20, 157, 221)"
>
<span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-card-list"></i> Projects</span
>
</a>
<ul
class = "collapse nav flex-column ms-1"
id = "projects"
data-bs-parent = "#menu"
>
<li class = "w-100">
<a
href = "blogpost"
class = "nav-link px-0"
style = "color: rgb(20, 157, 221)"
>
<span class = "d-none d-sm-inline">Blog Post</span>
</a>
</li>
<li>
<a
href = "/lekschat"
class = "nav-link px-0"
style = "color: rgb(20, 157, 221)"
>
<span class = "d-none d-sm-inline">LeksChat™</span>
</a>
</li>
<li>
<a
href = "#game"
data-bs-toggle = "collapse"
class = "nav-link px-10 align-right dropdown-toggle"
style = "color: rgb(20, 157, 221)"
>
<span class = "ms-1 d-none d-sm-inline"
><i class = "bi bi-card-list"></i> Space Runner Game</span
>
</a>
<ul
class = "collapse nav flex-column ms-1"
id = "game"
data-bs-parent = "#projects"
>
<li>
<a
href = "#"
class = "nav-link px-0"
style = "color: rgb(20, 157, 221)"
>
<span class = "d-none d-sm-inline">Game</span>
</a>
</li>
<li class = "w-100">
<a
href = "#"
class = "nav-link px-0"
style = "color: rgb(20, 157, 221)"
>
<span class = "d-none d-sm-inline">Leaderboard</span>
</a>
</li>
<li>
<a
href = "#"
class = "nav-link px-0"
style = "color: rgb(20, 157, 221)"
>
<span class = "d-none d-sm-inline">Personal Best</span>
</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href = "#" class = "nav-link px-0 align-middle mt-1">
<span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-envelope"></i> Contact</span
></a
>
</li>
<li>
{% if user.is_authenticated %}
<a href = "logout" class = "nav-link px-0 align-middle mt-1">
<span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-door-closed"></i> Log out</span
></a
>
{% if user.is_superuser %}
<a
href = "http://127.0.0.1:8000/admin/"
target = "_blank"
rel = "noopener noreferrer"
class = "nav-link px-0 align-middle mt-1"
><span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-door-closed"></i> Admin Panel</span
></a
>
{% endif %} {% else %}
<a href = "register" class = "nav-link px-0 align-middle mt-1">
<span
class = "ms-1 d-none d-sm-inline"
style = "font-size: x-large; color: rgb(20, 157, 221)"
><i class = "bi bi-box-arrow-in-right"></i> Login or
Signup</span
></a
>
{% endif %}
</li>
</ul>
<hr />
</div>
</div>
<!-- End navbar -->
<div class = "col-sm-8 col-md-9 col-xl-10 py-3 ms-auto">
<main class = "main">{% block content %} {% endblock %}</main>
</div>
</div>
</div>
<script
src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity = "sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin = "anonymous"
></script>
</body>
</html>