Dealing with Content Alignment
When it comes to aligning content, there are various methods at your disposal. Options like flexbox
, grid
, or utilizing classes for centering items through translations can be explored. In this scenario, employing flexbox
seems like a suitable choice due to its user-friendly nature.
For more insights, refer to: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Regarding Page Reloading
I strongly advise against reloading the page using POST-Requests
.
If absolute necessity demands it, one could establish the "active" class logic based on the URL
. This simple if-else
logic would ideally reside within the page controller or relevant component in use.
In scenarios where page reload is not mandatory, toggling classes via JavaScript remains a viable option. However, do note that such changes will not persist post-reload. For single-page applications, this might suffice. While storing selection states in cookies or sessions is plausible theoretically, it may not be the ideal approach.
Another approach involves incorporating links with route symbols like #
within the href
. This enables state retention within the URL
sans page reloading, facilitating direct scrolling or jumping to referenced elements.
The example demonstrated below illustrates how selection status is lost after a reload:
const menuItems = document.querySelectorAll('.menu a.btn');
const selectItem = (i, menuItems) => {
for (let j = 0; j < menuItems.length; j++) {
menuItems[j].classList.remove('btn-success');
}
menuItems[i].classList.add('btn-success');
}
for (let i = 0; i < menuItems.length; i++) {
menuItems[i].addEventListener('click', (e) => {
e.preventDefault();
selectItem(i, menuItems);
return false;
});
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<nav class="menu" role="navigation">
<a href="books/all" class="btn btn-md btn-success" role="button">All books</a>
<a href="books/fiction" class="btn btn-md" role="button">Fiction</a>
<a href="books/biography" class="btn btn-md" role="button">Biography</a>
<a href="books/new" class="btn btn-md" role="button">New</a>
</nav>
An alternative method you could implement is as follows:
const menuItems = document.querySelectorAll('.menu a.btn');
const selectItem = (i, menuItems) => {
for (let j = 0; j < menuItems.length; j++) {
menuItems[j].classList.remove('btn-success');
}
menuItems[i].classList.add('btn-success');
}
for (let i = 0; i < menuItems.length; i++) {
menuItems[i].addEventListener('click', (e) => {
selectItem(i, menuItems);
});
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<nav class="menu" role="navigation">
<a id="#books/all" href="#books/all" class="btn btn-md btn-success" role="button">All books</a>
<a id="#books/fiction" href="#books/fiction" class="btn btn-md" role="button">Fiction</a>
<a id="#books/biography" href="#books/biography" class="btn btn-md" role="button">Biography</a>
<a id="#books/new" href="#books/new" class="btn btn-md" role="button">New</a>
</nav>
The #
symbol can also be utilized in CSS
by means of the :target
selector.