I have the problem that as soon as I use an iPad / tablet and no PC, the content / text disappears behind the sidebar.
How can I make sure that the sidebar works as a “borderline” and that no text / content is displayed behind the sidebar?
I use bootstrap 5.
My html code:
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
<ul class="nav flex-column">
<li class="nav-item">
{% if app.request.get('_route') == 'app_dashboard' %}
<a class="nav-link active" aria-current="page" href="{{ path('app_dashboard') }}">
<span data-feather="home"></span>
Test
</a>
{% else %}
<a class="nav-link" aria-current="page" href="{{ path('app_dashboard') }}">
<span data-feather="home"></span>
Test
</a>
{% endif %}
</li>
<li class="nav-item">
{% if app.request.get('_route') == '' %}
<a class="nav-link active" aria-current="page" href="#">
<span data-feather="file"></span>
Test
</a>
{% else %}
<a class="nav-link" aria-current="page" href="#">
<span data-feather="file"></span>
Test
</a>
{% endif %}
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file-plus"></span>
Test
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="users"></span>
Test
</a>
</li>
</ul>
</div>
</nav>
</div>
My own sidebar css:
.sidebar {
position: fixed;
top: 0;
bottom: 0;
left: 0;
padding: 45px 0 0;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 5%), 0 2px 10px 0 rgb(0 0 0 / 5%);
width: 220px;
}
.sidebar .active {
border-radius: 5px;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 16%), 0 2px 10px 0 rgb(0 0 0 / 12%);
}
problem:
Screenshot
This happens because you haven't split your sidebar and your content. You've basically just placed it onto your page without considering that the sidebar & content will be 2 different components. Since you're using Bootstrap I would recommend using Grid System. Like this:
.sidebar {
position: fixed;
top: 0;
bottom: 0;
left: 0;
padding: 45px 0 0;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 5%), 0 2px 10px 0 rgb(0 0 0 / 5%);
width: 220px;
}
.sidebar .active {
border-radius: 5px;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 16%), 0 2px 10px 0 rgb(0 0 0 / 12%);
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="col-md-12 col-sm-12">
<div class="row">
<div class="col-md-4 col-sm-4">
<nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar">
<div class="position-sticky pt-3">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{{ path('app_dashboard') }}">
<span data-feather="home"></span> Test
</a>
<a class="nav-link" aria-current="page" href="{{ path('app_dashboard') }}">
<span data-feather="home"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">
<span data-feather="file"></span> Test
</a>
<a class="nav-link" aria-current="page" href="#">
<span data-feather="file"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file-plus"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="users"></span> Test
</a>
</li>
</ul>
</div>
</nav>
</div>
<div class="col-md-8 col-sm-8">
<h1>Hello World</h1>
</div>
</div>
</div>
</div>
Something else to note, for medium to smaller devices you will have to apply different styling's so this is where the power of bootstraps grid system will come in handy. Grid Options
Related
I'm having issues with this: I'm making sort of a tabs nav and I should accomplish the arrow down to go up on click, when the submenu is collapsed. I have the material icon, but the thing is I can only do it with html5 and css3
<nav class="nav my-4">
<li class="nav-link px-5 active fw-bold border-bottom border-2">
<a class="text-decoration-none small" href="#">Título 1</a>
</li>
<li class="nav-link px-5">
<a class="text-decoration-none text-grey-20 small" href="#">Título 2</a>
</li>
<li class="nav-link px-5">
<a class="text-decoration-none text-grey-20 small" href="#">Título 3</a>
</li>
<li class="nav-link px-5">
<a class="text-decoration-none dropdown dropdown-toggle small mw-ch-17" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Título 4
<!-- <span class="material-icons-outlined chevron-down">pand_more</span> -->
<svg .class="chevron-down" width="13" height="8" viewBox="0 0 13 8" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1.47146 0.294922L6.06146 4.87492L10.6515 0.294922L12.0615 1.70492L6.06146 7.70492L0.0614624 1.70492L1.47146 0.294922Z" fill="#666666"/>
</svg>
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<li><a class="dropdown-item" href="#">Ítem 1</a></li>
<li><a class="dropdown-item" href="#">Ítem 2</a></li>
<li><a class="dropdown-item" href="#">Ítem 3</a></li>
</ul>
</li>
<li class="nav-link px-2">
<a href="#" type="button">
<span class="material-icons text-grey-20">
keyboard_arrow_right
</span>
</a>
</li>
</nav>
This the CSS:
.mw-ch-17 {
max-width: 17ch;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.nav-link:hover {
background-color: #E7F0F4;
border-radius: 5px 5px 0 0;
}
.nav-link:active {
background-color: #CFE0E8;
border-radius: 5px 5px 0 0;
}
.dropdown-toggle::after {
display: inline-block;
margin-left: 0.255em;
vertical-align: 0.255em;
content: url("data:image/svg+xml,%3Csvg width='13' height='9' viewBox='0 0 13 9' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11.09 8.20508L6.5 3.62508L1.91 8.20508L0.5 6.79508L6.5 0.795078L12.5 6.79508L11.09 8.20508Z' fill='%23000000'/%3E%3C/svg%3E");
border-top: 0;
border-right: 0;
border-bottom: 0;
border-left: 0;
}
I was thinking maybe there's a way to change the fill color with CSS, but when I try with fill: black!important, doesn't work.
Expected: Arrow down at .collapse and arrow up at .collapse .show
And also change the font and icon color on hover and active
I currently have 2 problems with my sidebar.
The upper part is cut off on mobile devices.
The sidebar is unfolded by default on mobile devices. It shouldn’t be like this. And when it is folded, the content of the page does not shift to the left. However, the content should shift to the left.
I use bootstrap 5.
.sidebar {
position: fixed;
top: 0;
bottom: 0;
left: 0;
padding: 45px 0 0;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 5%), 0 2px 10px 0 rgb(0 0 0 / 5%);
width: 220px;
}
.sidebar .active {
border-radius: 5px;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 16%), 0 2px 10px 0 rgb(0 0 0 / 12%);
}
#media (max-width: 767px) {
.main {
padding-right: 40px;
padding-left: 220px;
/* 180 + 40 */
}
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<div class="container-fluid">
<div class="col-md-12 col-sm-12">
<div class="row">
<div class="col-md-4 col-sm-4">
<nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar">
<div class="position-sticky pt-3">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{{ path('app_dashboard') }}">
<span data-feather="home"></span> Test
</a>
<a class="nav-link" aria-current="page" href="{{ path('app_dashboard') }}">
<span data-feather="home"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">
<span data-feather="file"></span> Test
</a>
<a class="nav-link" aria-current="page" href="#">
<span data-feather="file"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file-plus"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="users"></span> Test
</a>
</li>
</ul>
</div>
</nav>
</div>
<div class="col-md-8 col-sm-8">
{% block body %}{% endblock %}
</div>
</div>
</div>
</div>
You can use CSS Media Queries to determine what action to do based on the screen size. Here, you would want to change the position/size of the sidebar based on the screen size.
#media (max-width: 600px) {
*This is for phones, for example (you might need to find more accurate pixel counts)*
}
Edit:
I just saw that you already have Media Queries. Try changing the size and position too, though, not just the padding.
Edit:
Here's a quick demo. This should be more than enough to solve your issue. Here's another link to help you. I would appreciate if you marked this answer as correct. Thanks!
function openNav() {
const a = document.getElementById("sidebarMenu");
const b = document.getElementsByClassName("CLASS NAME HERE");
a.style.width = "220px";
for(var i = 0; i < b.length; i++) {
b[i].style.width = "100%";
}
}
function closeNav() {
const a = document.getElementById("sidebarMenu");
const b = document.getElementsByClassName("CLASS NAME HERE");
a.style.width = "0";
for(var i = 0; i < b.length; i++) {
b[i].style.width = "0";
}
}
.sidebar {
position: fixed;
top: 0;
bottom: 0;
left: 0;
padding: 45px 0 0;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 5%), 0 2px 10px 0 rgb(0 0 0 / 5%);
width: 220px;
}
.sidebar .active {
border-radius: 5px;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 16%), 0 2px 10px 0 rgb(0 0 0 / 12%);
}
.closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#media (max-width: 767px) {
.sidebar {
padding: 120px 0 0;
}
}
<span onclick="openNav()">open</span>
<div class="container-fluid">
<div class="col-md-12 col-sm-12">
<div class="row">
<div class="col-md-4 col-sm-4">
<nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
<ul class="nav flex-column">
×
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{{ path('app_dashboard') }}">
<span data-feather="home"></span> Test
</a>
<a class="nav-link" aria-current="page" href="{{ path('app_dashboard') }}">
<span data-feather="home"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">
<span data-feather="file"></span> Test
</a>
<a class="nav-link" aria-current="page" href="#">
<span data-feather="file"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file-plus"></span> Test
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="users"></span> Test
</a>
</li>
</ul>
</div>
</nav>
</div>
<div class="col-md-8 col-sm-8">
</div>
</div>
</div>
</div>
I have an app using Bootstrap 5.0 laid out as in the dashboard example. In the main content column, I have a heading followed by a set of tabs. Each tab contains a block of preformatted text in a pre tag. The preformatted text can be both wide and tall, so scroll bars are needed in both directions. I have set up a codepen to demonstrate the issue.
By default, the pre element is the full height of the content and the vertical scroll bar is attached to the page instead of the pre element. This means that in order to access the hortizontal scroll bar, users have to scroll the page down to the very bottom so they lose track of what they were looking at when they needed to scroll horizontally.
What I really want is to limit the height of the pre block to the vertical space available on the page so that both the vertical and horizontal scroll bars are attached to the pre element itself. I have managed to almost achieve what I want by setting the max-height of the pre as follows:
.scrollable-pre {
max-height: calc(100vh - 16em);
}
However, this is brittle because if add anything to the heading that increases its height, the hard coded 16em will have to be adjusted.
How do I get the pre to automatically size to the available height?
body {
font-size: .875rem;
}
.feather {
width: 16px;
height: 16px;
vertical-align: text-bottom;
}
/*
* Sidebar
*/
.sidebar {
position: fixed;
top: 0;
/* rtl:raw:
right: 0;
*/
bottom: 0;
/* rtl:remove */
left: 0;
z-index: 100;
/* Behind the navbar */
padding: 48px 0 0;
/* Height of navbar */
box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
}
#media (max-width: 767.98px) {
.sidebar {
top: 5rem;
}
}
.sidebar-sticky {
position: relative;
top: 0;
height: calc(100vh - 48px);
padding-top: .5rem;
overflow-x: hidden;
overflow-y: auto;
/* Scrollable contents if viewport is shorter than content. */
}
.sidebar .nav-link {
font-weight: 500;
color: #333;
}
.sidebar .nav-link .feather {
margin-right: 4px;
color: #727272;
}
.sidebar .nav-link.active {
color: #2470dc;
}
.sidebar .nav-link:hover .feather,
.sidebar .nav-link.active .feather {
color: inherit;
}
.sidebar-heading {
font-size: .75rem;
text-transform: uppercase;
}
/*
* Navbar
*/
.navbar-brand {
padding-top: .75rem;
padding-bottom: .75rem;
font-size: 1rem;
background-color: rgba(0, 0, 0, .25);
box-shadow: inset -1px 0 0 rgba(0, 0, 0, .25);
}
.navbar .navbar-toggler {
top: .25rem;
right: 1rem;
}
.navbar .form-control {
padding: .75rem 1rem;
border-width: 0;
border-radius: 0;
}
.form-control-dark {
color: #fff;
background-color: rgba(255, 255, 255, .1);
border-color: rgba(255, 255, 255, .1);
}
.form-control-dark:focus {
border-color: transparent;
box-shadow: 0 0 0 3px rgba(255, 255, 255, .25);
}
.pre-container {}
.scrollable-pre {
max-height: calc(100vh - 16em);
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<header class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<a class="navbar-brand col-md-3 col-lg-2 me-0 px-3" href="#">Company name</a>
<button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
<div class="navbar-nav">
<div class="nav-item text-nowrap">
<a class="nav-link px-3" href="#">Sign out</a>
</div>
</div>
</header>
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="position-sticky pt-3">
<ul class="nav flex-column">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">
<span data-feather="home"></span> Dashboard
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file"></span> Orders
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="shopping-cart"></span> Products
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="users"></span> Customers
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="bar-chart-2"></span> Reports
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="layers"></span> Integrations
</a>
</li>
</ul>
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>Saved reports</span>
<a class="link-secondary" href="#" aria-label="Add a new report">
<span data-feather="plus-circle"></span>
</a>
</h6>
<ul class="nav flex-column mb-2">
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file-text"></span> Current month
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file-text"></span> Last quarter
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file-text"></span> Social engagement
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
<span data-feather="file-text"></span> Year-end sale
</a>
</li>
</ul>
</div>
</nav>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Dashboard</h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group me-2">
<button type="button" class="btn btn-sm btn-outline-secondary">Share</button>
<button type="button" class="btn btn-sm btn-outline-secondary">Export</button>
</div>
<button type="button" class="btn btn-sm btn-outline-secondary dropdown-toggle">
<span data-feather="calendar"></span>
This week
</button>
</div>
</div>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="pre-container">
<pre class="scrollable-pre">
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
</pre>
</div>
</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">...</div>
</div>
</main>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
I have tried to implement the following solution but have doing so seems to break my website. The logo I have for the website is the same color as the navbar background, so I am trying to switch to a black logo once the navbar shows up. However the website hangs on the spinner loader and fails to load in when the code block is active. Commenting out the code fixes this and the website behaves normally. Any ideas what I am missing here? Here is the relevant code:
// Javascript
// jQuery to collapse the navbar on scroll
$(window).on('scroll load', function() {
if ($(".navbar").offset().top > 20) {
$(".fixed-top").addClass("top-nav-collapse");
//$("img.logo-image").attr('src', "images/master_logo_black.svg");
} else {
$(".fixed-top").removeClass("top-nav-collapse");
//$("img.logo-image").attr('src', "images/master_logo_gold.svg")
}
});
//jQuery to change to logo colour THIS IS THE ISSUE
$(document).ready(function() {
$(window).on("scroll", function () {
if($(this).scrollTop() > 20){
$(".logo-image img").attr("src", "images/master_logo_black.svg");
} else {
$(".logo-image img").attr("src", "images/master_logo_gold.svg");
}
})
});
.navbar-custom {
padding-top: 0.375rem;
padding-bottom: 0.375rem;
background-color: #FFBF00;
box-shadow: 0 0.0625rem 0.375rem 0 rgba(0, 0, 0, 0.1);
font: 600 0.875rem/0.875rem "Open Sans", sans-serif;
transition: all 0.2s;
}
.navbar-custom .navbar-brand {
font-size: 0;
}
.navbar-custom .navbar-brand.logo-image img {
width: 20rem;
height: 5rem;
-webkit-backface-visibility: hidden;
}
.navbar-custom.top-nav-collapse {
padding: 0.375rem 1.5rem 0.375rem 2rem;
box-shadow: 0 0.0625rem 0.375rem 0 rgba(0, 0, 0, 0.1);
background-color: #FFBF00;
}
.navbar-custom.top-nav-collapse .navbar-brand.logo-image img {
fill: rgba(0, 0, 0, 1)
}
<body data-spy="scroll" data-target=".fixed-top">
<!-- Preloader -->
<div class="spinner-wrapper">
<div class="spinner">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
<!-- end of preloader -->
<!-- Navbar -->
<nav class="navbar navbar-expand-md navbar-dark navbar-custom fixed-top">
<!-- Text Logo - Use this if you don't have a graphic logo -->
<!-- <a class="navbar-brand logo-text page-scroll" href="index.html">redacted</a> -->
<!-- Image Logo -->
<a class="navbar-brand logo-image" href="index.html"><img class="logo-image" src="images/master_logo_gold.svg" alt="alternative"></a>
<!-- Mobile Menu Toggle Button -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-awesome fas fa-bars"></span>
<span class="navbar-toggler-awesome fas fa-times"></span>
</button>
<!-- end of mobile menu toggle button -->
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link page-scroll" href="#header">HOME <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link page-scroll" href="#about">ABOUT</a>
</li>
<li class="nav-item">
<a class="nav-link page-scroll" href="#research">RESEARCH</a>
</li>
<li class="nav-item">
<a class="nav-link page-scroll" href="#experience">EXPERIENCE</a>
</li>
<li class="nav-item">
<a class="nav-link page scroll" href="blog.html">BLOG</a>
</li>
<li class="nav-item">
<a class="nav-link page-scroll" href="#contact">CONTACT</a>
</li>
</ul>
<span class="nav-item social-icons">
<span class="fa-stack">
<a href="https://www.linkedin.com/in/redacted/">
<span class="hexagon"></span>
<i class="fab fa-linkedin-in fa-stack-1x"></i>
</a>
</span>
<span class="fa-stack">
<a href="https://github.com/redacted">
<span class="hexagon"></span>
<i class="fab fa-github fa-stack-1x"></i>
</a>
</span>
</span>
</div>
</nav>
<!-- end of navbar -->
<!-- end of navbar -->
Seems to work as shown below. Try changing the image src on scroll as shown in the example.
// Javascript
// jQuery to collapse the navbar on scroll
const $window = $(window);
$window.on('scroll', function() {
let scroll = $window.scrollTop();
if (scroll > 20) {
$(".fixed-top").addClass("top-nav-collapse");
//$("img.logo-image").attr('src', "images/master_logo_black.svg");
} else {
$(".fixed-top").removeClass("top-nav-collapse");
//$("img.logo-image").attr('src', "images/master_logo_gold.svg")
}
});
.navbar-custom {
padding-top: 0.375rem;
padding-bottom: 0.375rem;
background-color: #FFBF00;
box-shadow: 0 0.0625rem 0.375rem 0 rgba(0, 0, 0, 0.1);
font: 600 0.875rem/0.875rem "Open Sans", sans-serif;
transition: all 0.2s;
}
.fixed-top {
position: fixed;
top: 0;
left :0;
width: 100%;
}
.navbar-custom.top-nav-collapse {
padding: 0.375rem 1.5rem 0.375rem 2rem;
box-shadow: 0 0.0625rem 0.375rem 0 rgba(0, 0, 0, 0.1);
background-color: red;
}
.logo-img {
width: 50px;
height: 20px;
background: red;
}
.top-nav-collapse .logo-img {
background: green;
}
.nav-item {
display: inline-block;
margin: .2rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-md navbar-dark navbar-custom fixed-top">
<!-- Text Logo - Use this if you don't have a graphic logo -->
<!-- <a class="navbar-brand logo-text page-scroll" href="index.html">redacted</a> -->
<!-- Image Logo -->
<a class="navbar-brand logo-image" href="index.html">
<div class="logo-img"></div>
</a>
<!-- end of mobile menu toggle button -->
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link page-scroll" href="#header">HOME <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link page-scroll" href="#about">ABOUT</a>
</li>
<li class="nav-item">
<a class="nav-link page-scroll" href="#research">RESEARCH</a>
</li>
<li class="nav-item">
<a class="nav-link page-scroll" href="#experience">EXPERIENCE</a>
</li>
<li class="nav-item">
<a class="nav-link page scroll" href="blog.html">BLOG</a>
</li>
<li class="nav-item">
<a class="nav-link page-scroll" href="#contact">CONTACT</a>
</li>
</ul>
<span class="nav-item social-icons">
<span class="fa-stack">
<a href="https://www.linkedin.com/in/redacted/">
<span class="hexagon"></span>
<i class="fab fa-linkedin-in fa-stack-1x"></i>
</a>
</span>
<span class="fa-stack">
<a href="https://github.com/redacted">
<span class="hexagon"></span>
<i class="fab fa-github fa-stack-1x"></i>
</a>
</span>
</span>
</div>
</nav>
<!-- end of navbar -->
<!-- end of navbar -->
<div style="height:10000px"></div>
I have a bootstrap 4 navbar that I'd like to add a hover affect to each link. I've added the following css to overwrite the standard styles which gives 'kinda' what I'm after. When hovering over a link the border-bottom shows but also pushes the text up when hovering. It is also not on the bottom of the navbar itself.
As I'm using the img to set the height of the row the navbar sits in and then splitting the height by 50% I wonder if that is what I am up against?
As you can see from the image, i'd like the orange border be at the bottom of the navbar. As a unintended concequence of what I've done so far when hovering over any link the whole text moves up by 5px or so then drops when not hovering.
This is the code making the navbar...
<div class="container-fluid p-0">
<div class="row no-gutters shadow">
<div class="col-sm-12 col-md-12 col-lg-1">
<img src="<?php echo URLROOT.'/public/img/msplogo.jpg'; ?>" class="header-logo mx-auto d-block"/>
</div>
<div class="col-sm-12 col-md-12 col-lg-11">
<div class="row no-gutters h-50">
<div class="d-none d-lg-block col-12">
<nav class="navbar navbar-expand-lg navbar-dark bg-msp-darkblue h-100">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</nav>
</div>
</div>
</div>
And this is the CSS...
/* Header Logo */
.header-logo {
width:100%;
max-width: 160px;
}
/* Small devices (landscape phones, less than 768px) */
#media (max-width: 768px) {
.header-logo {
width:100%;
max-width: 80px;
margin:10px;
}
}
/* Large viewport navbar */
.nav-link {
font-family: 'Open Sans', sans-serif;
font-size: 0.8vw;
font-weight: bolder;
text-transform: uppercase;
}
.nav-link:hover {
border-bottom: 5px solid #ff7240;
color:#fff!important;
}
Could anyone point me in the right direction?
Regards
Matt
To prevent the navbar from growing when you hover over the link, specify a transparent border-bottom of the same size as the :hover border.
.nav-link {
border-bottom: 5px solid transparent;
}
.nav-link:hover {
border-bottom: 5px solid red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar" style="border: 1px solid gray">
<ul class="navbar-nav">
<li>
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
</ul>
</nav>
After a bit of digging I found I needed to add h-100 to the ul. This then made the link fill it's parent element and then in the css use height:100% on the .nav-link
Completed code is:
<div class="container-fluid p-0">
<div class="row no-gutters shadow">
<div class="col-sm-12 col-md-12 col-lg-1">
<img src="<?php echo URLROOT.'/public/img/msplogo.jpg'; ?>" class="header-logo mx-auto d-block"/>
</div>
<div class="col-sm-12 col-md-12 col-lg-11">
<div class="row no-gutters h-50">
<div class="d-none d-lg-block col-12">
<nav class="navbar navbar-expand-lg navbar-dark bg-msp-darkblue h-100 py-0">
<ul class="navbar-nav h-100 mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</div>
And the CSS - I had to add a padding-top to the link to move it back the middle as well.
/* Header Logo */
.header-logo {
width:100%;
max-width: 160px;
}
/* Small devices (landscape phones, less than 768px) */
#media (max-width: 768px) {
.header-logo {
width:100%;
max-width: 80px;
margin:10px;
}
}
/* Large viewport navbar */
.nav-link {
font-family: 'Open Sans', sans-serif;
font-size: 0.8vw;
font-weight: bolder;
text-transform: uppercase;
border-bottom: 5px solid transparent;
height:100%
padding-top:27px;
}
.nav-link:hover {
border-bottom: 5px solid #ff7240;
color:#fff!important;
}