How to fix fixed link taking up full width of page? - html

I have a fixed anchor tag linked to a heading on my website. This anchor tag has an arrow icon that is meant to be a quick way to get to the top of the page. I wanted to position it at the bottom right, but the link is taking up full width at the bottom of the page unless I use "width: fit-content;'. I tried all the display options and float only works on the icon and not the link. It makes the arrow icon float to the position I want but the link width is still taking up all the pages width. Does anyone know how I can fix the width of the link and position it to the bottom right side of the page? Thank you in advance.
NOTE-
I am using bootstrap 5
#quick-anchor-top {
font-size: 25px;
padding: 15px 25px 15px 25px;
border-radius: 50px;
color: rgb(0, 0, 0);
background-color: rgba(182, 20, 20, 0.800);
transition: all 0.4s ease;
margin: 20px;
}
#quick-anchor-top:hover {
transition-duration: 0.4s;
color: white;
background-color: rgba(0, 0, 0, 0.800);
}
<a id="quick-anchor-top" href="#header-title-1" class="fixed-bottom float-end"> <i class="fa-solid fa-arrow-up"></i></a>

You can use predefined bootstrap 5 classes to position elements on the site. Use position-fixed and bottom-0, end-0 to set your element to the bottom right corner.
Just change your classes in the anchor element like this:
<a id="quick-anchor-top" href="#header-title-1" class="position-fixed bottom-0 end-0"><i class="fa-solid fa-arrow-up"></i></a>
Here is an example: https://jsfiddle.net/5j7do14t/
More: https://getbootstrap.com/docs/5.0/utilities/position/

Always try to share your full code so others can understand better and help you in a faster way. As I can see your code is missing an important point which is the position.
Since you didn't share the full code I will try to explain to you how to do a Scroll back to top button in a very easy way.
First of all, you need the button itself, and secondly, do your style, and finally, the JavaScript code that will handle the scroll.
So let's start.
1. Create the button that will take you to the top of the page when clicked on.
<button id="quickAnchorTop" title="Go to top"><i class="fa-solid fa-arrow-up"></i></button>
For the naming, it's better to use the camelCase since you will use its ID in the CSS and in the JS code, so we can call your button (quickAnchorTop instead of quick-anchor-top).
2. Now the most important part, is the CSS to style your button.
#quickAnchorTop {
display: none; /* It should be hidden by default */
position: fixed; /* Fixed/sticky position */
bottom: 20px; /* Place the button at the bottom of the page */
right: 20px; /* Place the button 20px from the right */
z-index: 99; /* Make sure it does not overlap */
border: none; /* Remove borders */
outline: none; /* Remove outline */
background-color: blue; /* Set a background color */
color: white; /* Text color */
cursor: pointer; /* Add a mouse pointer on hover */
padding: 16px; /* Some padding */
border-radius: 10px; /* Rounded corners */
font-size: 18px; /* Font size */
}
#quickAnchorTop:hover {
background-color: #555; /* Add a dark-grey background on hover */
}
3. Finally, use JavaScript to handle the scroll and show your button.
// Get the button from the current page:
let myBackToTopButton = document.getElementById("quickAnchorTop");
// When you scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
myBackToTopButton .style.display = "block"; // To show your button
} else {
myBackToTopButton .style.display = "none"; // To hidd your button
}
}
// When you click on the button, scroll to the top of the document
function topPageFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
In the above JS code, first, you will get your button by the ID you used which is (quickAnchorTop), then you will have a function to handle the scroll and to show or hide your button.
Now make sure to update your button code and use this final form as you added the onclick event to your button:
<button onclick="topPageFunction()" id="quickAnchorTop" title="Go to top"><i class="fa-solid fa-arrow-up"></i></button>
Hopefully this will help you.

Related

How can I make a button that changes the text inside when clicked?

I want to make a button that changes the text inside the button by pressing the button, but I don't know how! :(
I used :hover, but when I move the mouse pointer away, it goes back to its previous state.
There is the possibility of solving it with the pseudo-class :hover and the use of data attributes. The idea of this solution is that you hide the original button text, add an empty content and then use hover over the element to show the content of the data attribute.
I'll show you how in the following example:
body {
background-color: #F2CD5C;
text-align: center;
}
.container {
margin-top: 30vh;
}
/*
MARK BUTTON:
In the button styles, it is necessary to hide the original text that we generated, to create the correct spacing and the data attribute text can overlap
The text color must be the same as the button background.
Position must be relative.
*/
.button {
position: relative;
padding: 0.5em 1em;
border: 2px solid #fff;
border-radius: 15px;
font-size: 2em;
color: black;
background: black;
}
/*
MARK USE :before and :after
Setup pseudo-element ::before with content: ""; and position must be absolute and setup with the original position text inside the button.
Write ::after with the exact text inside button = Click me! with the same position and setup to ::before pseudo-element
*/
.button::before {
content: "";
position: absolute;
left:0;
width: 100%;
text-align: center;
opacity: 0;
color: white;
}
.button::after {
content: "Click me!";
position: absolute;
left:0;
width: 100%;
text-align: center;
opacity: 1;
color: white;
}
.button::before {
content: attr(data-hover);
}
.button:hover:before{
opacity: 1;
}
.button:hover:after{
opacity: 0;
}
<div class="container">
<button class="button" type="button" data-hover="Hello world!">Click me!</button>
</div>
The code uses pseudo-elements ::before and ::after to display different text when the button is hovered over. The text "Click me!" is set as the content for the ::after pseudo-element, while the ::before pseudo-element gets its content from the "data-hover" attribute. When the button is hovered over, the ::before pseudo-element's opacity becomes 1 and the ::after pseudo-element's opacity becomes 0, effectively hiding and showing different text on the button.
I hope this can help you solve your question. Anyway, this solution is not clean, we should handle the DOM using JavaScript.
Reference Links
Using data attributes
I only know how to do this using javascript, hope that helps.
HTML
<button class="btn">Hey, click me!</button>
JS
first I store the button in a variable
var button = document.querySelector(".btn")
then I add an event listener to the button with the "click" event, which will make the function "function()" be executed whenever the button is clicked
button.addEventListener("click", function(){})
now, I define the function to change the text of the button using "this" to access the button of the function and ".textContent" to change the text that was inside
button.addEventListener("click", function(){
this.textContent = "Hey, you clicked me!"
})
Click "run" for a preview
var button = document.querySelector(".btn")
button.addEventListener("click", function(){
this.textContent = "Hey, you clicked me!"
})
<button class="btn">Hey, click me!</button>

How to hide nav bar on scroll and show a sidebar?

I am using bootstrap to code the frontend of a website. What I hope to achieve is that when I scroll the navbar vanishes and the sidebar pops up. I have been stuck on this for ages so If anyone has any idea to let me know.
This should answer the first piece of the question, in terms of hiding the navbar on scroll. I would get away from using bootstrap in this instance. It is easier to build your navbar from scratch, style it with css, and then use javaScript to manipulate it dynamically, such as hiding it. Once this is working. I can help you to get the sidebar to present on scroll. The link should give you an idea where to do with this.
https://www.w3schools.com/howto/howto_js_navbar_hide_scroll.asp
It is my opinion that the following block of javaScript will help you the most. you did not leave a code block, so it is only an assumption, based on what the common layout is. Again, I would move away from bootstrapping the menu bar, since you are wanting to customize features within it. You will see an explanation of the site that I listed below:
The first block would be your navbar div:
<div id="navbar">
Home
News
Contact
</div>
You can set this up and represent it however you would choose too. It should be in a standalone HTML file, lets say index.html for these purposes.
The next block of code is the CSS, as it would pertain to the above codeblock. Again, this can be shaped however you would like it to be, but for these purposes it is simply giving a blueprint. This should also be in a standalone css file.
#navbar {
background-color: #333; /* Black background color */
position: fixed; /* Make it stick/fixed */
top: 0; /* Stay on top */
width: 100%; /* Full width */
transition: top 0.3s; /* Transition effect when sliding down (and up) */
}
/* Style the navbar links */
#navbar a {
float: left;
display: block;
color: white;
text-align: center;
padding: 15px;
text-decoration: none;
}
#navbar a:hover {
background-color: #ddd;
color: black;
}
The final code block is your script. The JS that will dynamically change the navbar is contained within. As you can see below, they are setting the variable globally, though not always the best way, and then creating a function expression, this has to do with hoisting, and then simply hide the navigation bar. Please let me know if you need any further assistance to help you understand this.
js.file
var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
document.getElementById("navbar").style.top = "0";
} else {
document.getElementById("navbar").style.top = "-50px";
}
prevScrollpos = currentScrollPos;
}

Custom scrollbar but keep disappearing effect

Is there any way to just change the colour of a scrollbar in CSS, but keep the native 'disappear when not scrolling' effect. Basically I just want to turn the native scrollbar blue instead of its default black/dark-grey, but whenever I apply code like this
::-webkit-scrollbar {
width:5px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: blue;
border-radius:5px;
opacity:0.5;
}
The scrollbar looks how I want it too, but its persistent, instead of disappearing when i'm not scrolling. Is there any way I can keep that effect on a custom scrollbar?
EDIT - As requested my current browser is google chrome 73.0.3683.103
The most you can do using only css and webkit is to use the :hover/:active selectors to display or hide the scrollbar. The thing is, this will work on hover/selection and not on a finger swipe or a mouse wheel. Also this webkit property will not work on firefox or edge.
::-webkit-scrollbar-thumb {
background: transparent;
border-radius: 5px;
opacity: 0;
}
::-webkit-scrollbar-thumb:hover {
background: blue;
border-radius: 5px;
opacity: 0.5;
}
Info on webkit scrollbar
This question has a nice example of a smooth transition on hover
A late answer hopefully it still helps.
I don't think you can do this with pure CSS, (but i could be wrong)
You can use some jQuery to help you. I have a working fiddle here: https://jsfiddle.net/kingafrojoe/Le253gdw/29/
In your CSS .has-scroll to the scrollbar selectors as below
/* Add a css class to your scroll selectors */
.has-scroll::-webkit-scrollbar {
width: 15px;
}
.has-scroll::-webkit-scrollbar-track {
background: transparent;
}
.has-scroll::-webkit-scrollbar-thumb {
background: blue;
border-radius: 5px;
opacity: 0.5;
}
#tall {
height: 500px;
width: 100%;
background: #00ffff;
display: block;
}
In your HTML you will need a wrapper div wrapping the whole body of your document.
The body class also gets the class has-scroll jQuery will control this class.
<body class="has-scroll">
<div id="site">
<div id="tall"> I am tall content</div>
<!-- ALL other page HTML -->
</div>
</body>
</html>
Then some jQuery to detect the height of the content and the window.
If the content is taller than the window then there needs to be a scrollbar, else the scrollbar can do default behavior
<script type="text/javascript">
jQuery(window).load(function(){
var _body = $('body');
var _site = $('#site');
$(window).resize(function(){
show_scroll();// call the function on the window resize
});
show_scroll();// call the function
function show_scroll(){
// if the content wrapper is taller than the window add the has-scroll class,
// else remove the has scroll class to return default scroll behavior.
if(_site.outerHeight()>$(window).outerHeight()){
_body.addClass('has-scroll');
}else{
_body.removeClass('has-scroll');
}
}
});
</script>

Css overlay with background color inherited from theme

I'm trying to put an overlay on my page that will cover all the content.
The problem is that my site allows you to change the theme colors, and the overlay div does not inherit the color from the theme.
The only way to change it is using background-color: rgba( x, y, z, 1.0)
Here is my css for the overlay
.bio-overlay {
position: fixed; /* Sit on top of the page content */
display: block;
width: 100%; /* Full width (cover the whole page) */
height: 100%; /* Full height (cover the whole page) */
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 2048;
cursor: pointer; /* Add a pointer on hover */}
Is there a way to do this with css? If not I'll settle for some clever javascript. Thanks
In theory, if the theme is class-based, you can steal, or even make another class that only has a background color attached to it, and apply it to the overlay as well. This would make it so if the theme changed, the overlay color would change.
.backgroundColor {
background-color: rgba(x, y, z, 1.0);
}
<div class="bio-overlay backgroundColor">
Content
</div>
Another solution is, if the overlay is a child of the theme, you can select the overlay and apply a background color to it that way.
.themeClass .bio-overlay {
background-color: rgba(x, y, z, 1.0);
}
This will basically overload the .bio-overlay class and if the themeClass is a parent, it'll also apply the background color.
EDIT
To answer your comment, yes. In the stylesheet YOU edit, do something like this...
.whatevertheme .bio-overlay {
background-color: _whatever_;
}
.whatevertheme2 .bio-overlay {
background-color: _whatever2_;
}
....
It doesn't matter what file the style is in, all that matters is that it is included in the page. As I said in the comment, the only real downside is you'll break theme separation.

Is it possible to show a div on click using the :active selector in CSS?

I'm looking to show a div on click. The goal is to use pure CSS only, no jQuery.
Working FIDDLE Demo
Consider that you want something like this:
We write our markup as simple as possible. One element for container, one element for our link and one another element for popup:
<!-- [container] -->
<div class="link-with-popup">
<!-- link -->
<div class="link">CSS</div>
<!-- [popup] -->
<div class="popup">
<div class="box">CSS Description</div>
</div>
<!-- [/popup] -->
</div>
<!-- [/container] -->
Here is our layer structure in picture:
CONTAINER
Let's write CSS for our container.
.link-with-popup {
/* for visualizing */
background: yellow;
/* we need relative, because childs are absolute */
position: relative;
margin-bottom: 10px;
height: 30px;
width: 400px;
}
[!] Note that we make our container relative. Because the children will be in absolute mode.
LINK
We create our link as an absolute element from left, just as shown in the figure above.
.link {
background: blue;
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 100px;
z-index: 10;
}
POPUP
The dimention of popup element is same as the container, so we set all top, left, right, bottom properties to 0.
.popup {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: green;
z-index: 20;
}
[!] Note that z-index of popup element must be greater than link element.
.popup {
/* we won't show the popup yet */
display: none;
}
By now, we'll get this result (check it on jsFiddle):
Now we want the click for our link. This must be done with :active pseudo selector in CSS. But how we must show the poup? We have to get the next sibling element by the link. We use the + selector in CSS:
.link:active + .popup {
display: block;
}
See the result on jsFiddle. But the problem is that when user realize the mouse, the popup will disappear (as it display is set to none).
So we set the :hover rule for the popup and make it block.
.popup:hover {
display: block;
}
Check the jsFiddle demo. Now we get close enough. The only issue that the popup element, hide our link.
But it doesn't matter, because we won't set background for our popup (it will be transparent).
TEXT
For wanted text in popup element, we set this rules:
.popup .box {
position: absolute;
/* note that we make a gap from left to don't hide the link */
left: 130px;
top: 0;
right: 0;
bottom: 0;
background: #505050;
}
Check the jsFiddle demo. Now we have all things that we need.
Now it's time to make our popup element transparent (by setting the background as transparent or simply remove the background: green; rule):
.popup {
background: transparent;
}
And here is the final jsFiddle result. And if you add some extra CSS to it, it can be more stylish. Something like this that I've created.
Some important note to memorize:
In the final result, there is a gap between the link (blue one) and the popup (gray one). But the fact is that the gray element is not our popup. It's a child of popup and our popup is an 100% width and height element on the container.
Working FIDDLE Demo
Another way is to use the :target property (only works in moderns browsers).
Here's a qucik DEMO where I've hidden the div by applying opacity: 0; and the when you click the link the div changes to opacity: 1; The link and the div are matched using a hash in the url.
Here's the code from my example.
HTML
Click me
<br />
<div id="pop"></div>
CSS
#pop {
width: 100px;
height: 100px;
background: #000;
opacity: 0;
}
#pop:target {
opacity: 1;
}
There are some side effects though. The browser will jump/scroll down (not sure if it's possible to prevent this?) to the matched div and since we are using a hash in the url it will effect the browser history and, as mentioned above, it only works in modern browsers.
EDIT If you want to look into other hack/tricks for pure CSS click events, this is a good post - http://tympanus.net/codrops/2012/12/17/css-click-events/