How to make footer stay at bottom of the page? - css

Im trying to build a simple single page react project where data is fetched from a public API and then it renders the data to the site. When styling my project I stumbled across a problem where my footer component doesnt stay at the bottom of the site.
I googled the solutions with stylings like :
.footer__container {
margin: auto;
width: 100%;
position: absolute;
right: 0;
left: 0;
bottom: 0;
background-color: #efefef;
text-align: center;
}
This works good if data is not rendered into the site : Correct
But when data is rendered to the website, the footer component stays at the same place. When scrolling down, it doesnt move along. Example
Without position: absolute; and styling
.footer__container {
margin: auto;
height: 60px;
background-color: #efefef;
margin-top: 2.5rem;
text-align: center;
}
it stays at the bottom when data is rendered to the site. But the start view is this : Without data rendered.
App.js :
function App() {
return (
<div className="App">
<Header/>
<Form/>
<Footer/>
</div>
);
}
Form.jsx
return (
<div className="form__section">
<form onSubmit={handleFetch}>
<input
value={word}
onChange={(e) => setWord(e.target.value)}
id='enter__word'
className='form__input'
></input>
<button>Search</button>
</form>
<div className='word__section'>
{synonyms.map((synonym) => (
<div className="word__container" key={synonym.word}>
<ul>
<li>
{synonym.word}
</li>
</ul>
</div>
))}
</div>
</div>
)

Please remove the position: absolute; and add the min-height: calc(100vh - HEADER_HEIGHT - FOOTER_HEIGHT) to form__section.
If you have something like CodeSandbox, I will modify it there.

Related

My CSS is not working properly in vs code

I have written a simple navbar that has a scroll animation.
Example:
<div className={"nav active"}>
Here CSS on active is not working. Even if I write .active or nav.active in my CSS file, I have no result. In my code I have used a on scroll event listener so when I scroll down, the navbar appears black and when I go on the navbar, it disappears. But the problem is that CSS is working in nav, but not working in
active and as a result black color in active is not appearing.
const [show , handleshow]= useState(false);
const transitionNavBar = () => {
if (window.scroll > 100){
handleshow(true);
}else{
handleshow(false);
}
};
useEffect(() => {
window.addEventListener("scroll",transitionNavBar);
return () => window.removeEventListener("scroll", transitionNavBar);
}, []);
return (
<div className={`nav ${show && "active"}`}>
<div className="nav_contents">
<img className='nav_logo'
src="http://assets.stickpng.com/images/580b57fcd9996e24bc43c529.png"
alt=""
/>
<img className='nav_avatar' src="https://i.pinimg.com/originals/0d/dc/ca/0ddccae723d85a703b798a5e682c23c1.png"
alt=""
/>
</div>
</div>
)
}
.nav{
position: fixed;
top: 0;
padding: 20px;
width: 100%;
height: 30px;
z-index: 1;
}
.nav.active{
background-color: #111;
}
.nav_contents{
display: flex;
justify-content: space-between;
}
.nav_logo{
position: fixed;
top: 10px;
left: 0%;
width: 80px;
object-fit: contain;
padding-left: 20px;
cursor: pointer;
}
.nav_avatar{
position: fixed;
cursor: pointer;
right: 20px;
width: 30px;
height: 30px;
}
Instead of this
<div className={`nav ${show && "active"}`}>
Try this:
<div className={`nav ${show ? "active":""}`}>
I think you need to write your CSS in a seperate file, if you are not using styled components for example.
Write your CSS classes inside a seperate file and then import the css file inside your JSX/JS.
For example:
import "./navbar.css"
Edit: Sorry, just reread and noticed your nav is working.
As the other posts suggest, you would probably need to use a conditional operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
I don't know what was a problem but i uninstall ns install vs code again my issue was solved I really don't know how it worked but it worked.

Issue with ng-zorro's layout - autocomplete box does not stay in position when scrolling

so I am having an issue with setting up NG-ZORROs layout
I have the following layout
HERE IS A STACKBLITZ EXAMPLE OF THE SETUP
Where the HEADER should be fixed in position
The SIDE NAVIGATION (on the left) should also be fixed in position when the user is scrolling the content, but it also has an internal scroll if the menu items dont fit the screen
The CONTENT should have internal scroll so that
// WRAPPER
.wrapper {
height: 100vh;
}
// HEADER
.header {
color: white;
background-color: rgb(24, 132, 255);
height: 45px;
display: flex;
justify-content: center;
position: fixed;
z-index: 999999;
width: 100%;
}
// SIDE NAV MENU
[nz-menu] {
line-height: 64px;
height: 100vh;
overflow: auto;
}
[nz-menu]::-webkit-scrollbar {
display: none;
}
// CONTENT
.input {
border: solid 1px rgb(24, 132, 255);
}
.before {
margin-bottom: 20px;
display: flex;
justify-content: center;
background-color: rgb(184, 216, 208);
opacity: 0.3;
height: 300px;
align-items: center;
}
.after {
margin-top: 20px;
display: flex;
justify-content: center;
background-color: rgb(184, 185, 216);
opacity: 0.3;
height: 800px;
align-items: center;
}
// HERE IS WHERE THE ISSUE IS
nz-content {
padding: 0 50px;
margin-top: 64px;
// WE NEED THE OVERFLOW SO THAT ONLY THE CONTENT IS SCROLLABLE AND THE REST OF THE ELEMENTS ARE NOT
overflow: auto;
}
<nz-layout class="wrapper">
<!-- HEADER -->
<header class="header">
HEADER
</header>
<nz-layout class="content">
<!-- SIDE NAVIGATION -->
<nz-sider nzCollapsible nzWidth="200px">
<ul nz-menu nzMode="inline" nzTheme="dark" class="menu-children">
<li nz-menu-item>
<span>Menu 0</span>
</li>
<li nz-submenu *ngFor="let item of menuItems" (nzOpen)="openMap.menu+item"
(nzOpenChange)="openHandler('menu'+ item)" nzTitle="{{'Menu' + item}}">
<ul>
<li *ngFor="let submenuItem of menuItems" nz-menu-item>
Menu {{submenuItem}}
</li>
</ul>
</li>
<li nz-menu-item (click)="openHandler('menu11')">
<span>Templates</span>
</li>
</ul>
</nz-sider>
<!-- CONTENT -->
<nz-content>
<!-- ONLY CONTENT IN THE NZ-CONTENT SHOULD BE SCROLLABLE -->
<p class="before">
CONTENT BEFORE SEARCHBAR
</p>
<!-- SEARCHBAR + AUTOCOMPLETE -->
<nz-input-group class="search-wrapper" nzSize="large">
<!-- INPUT -->
<input
class="input"
type="text"
nz-input
placeholder="Click here and scroll..."
[nzAutocomplete]="auto"
/>
<!-- AUTOCOMPLETE -->
<nz-autocomplete #auto>
<nz-auto-option [nzDisabled]="true">
<span class="no-results"> No Results </span>
</nz-auto-option>
</nz-autocomplete>
</nz-input-group>
<p class="after">
CONTENT AFTER SEARCHBAR
</p>
</nz-content>
</nz-layout>
</nz-layout>
My issue is that in order to achieve that i have set up the CONTENT with overflow: auto and everything works as expected... until I added a search bar with an autocomplete box.
Now when you click the search box and scroll on the content the autocomplete box stays in position instead of sticking to the input field of the the search bar - see image
HERE IS A STACKBLITZ EXAMPLE OF THE SETUP
I was able to do some hacky workarounds, but they dont work 100%.
Does anyone know why setting up the nz-content with overflow:auto breaks the autocomplete box position ?!
Ok, after a lot more research I was able to find a working solution to the problem.
It turned out to be the following
In the usage of tooltip (including popconfirm、popover), body element's scroll event will update the position of tooltip. It will never update the position of tooltip if the scroll event happens in a custom element.You can add cdkScrollable directive to achieve the goal. Take notice that you need to import relative package import {ScrollingModule} from '#angular/cdk/scrolling';, for more information you can visit scrolling/api.
source - ng-zorro (bottom of the page)
I also found this stackoverflow question about Material Design Autocomplete
So based on those two, what ended up doing is I imported the ScrollingModule to my module like so
import { ScrollingModule } from '#angular/cdk/scrolling';
#NgModule({
imports: [
// ...
ScrollingModule,
],
// ...
})
export class MyAppModule { }
and then just applying the cdkScrollable to NZ-CONTENT
<nz-content cdkScrollable> CONTENT GOES HERE </nz-content>
Here is a link with the working solution - line 37

Position element in relation to a list element

I'm trying to make a pop-up options box appear next to each one of the elements of a file list. This window show the options available for that files.
I'm using an onClick function that toggle classes between one hidden and the other visible. My problem is that the box is placed always in the same place, despite the file you click, so it seems like you are always opening the options from the first file.
The React code:
return (
<div className='file-row'>
<div className='file-row-1'>
<div className='checkbox-iconFile-container'>
<input className='checkbox-folder' type="checkbox"/>
<p id={props.id} className='file-name'><img className='resourceIcon'alt='tipo de documento' src={resourceIcon}></img>{newStr}</p>
</div>
<p className='file-date'>{date}</p>
<p className='groups-file-text'>UBIP,...</p>
</div>
<div className='div-hidden-options'>
<ModalResources></ModalResources>
<div className='moreInfo-dots-container' onClick={showFileOptions}>
<img className='moreInfo-dots' alt='Opciones' src={dots}></img>
</div>
<div id='file-options' className='div-file-options-hidden'>
<p>Modificar tĂ­tulo</p>
<p>Modificar archivo</p>
<p>Mover recurso</p>
<p>Eliminar recurso</p>
</div>
</div>
</div>
)
The showFileOptions function :
function showFileOptions () {
let optionsDiv = document.getElementById('file-options');
optionsDiv.classList.toggle('div-file-options-show');
}
And the CSS:
.div-hidden-options {
width: 15%;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.div-file-options-hidden {
visibility: hidden;
position: absolute;
}
.div-file-options-show {
visibility: visible;
position: absolute;
margin-left: 15%;
background-color: white;
border: 1px solid #DDDDDD;
padding: 5px;
border-radius: 5px;
margin-top: -36px;
}
And an image of the position that the box takes regardless of the file we click:
And this is the desirable behaviour of the box when the second file is clicked:

Reactjs & materialize - stop fixed div when it reaches the footer

I'm working on a project, and would like to have a side panel in a fixed position on the screen (just below the navbar) until it reaches the top of the footer so the two don't overlap. I've found some suggestions using Jquery, but this project is in react and I am using the materialize css framework. Here is the code I am working with in App.js...
<div className="App">
<Navbar />
<div className="row" id="landingcontainer">
<div className="col s3" id="sidebar">
<Sidebar />
</div>
</div>
<Footer />
</div>
And here is what my css looks like:
#landingcontainer {
height: 120vh;
position: relative;
}
#sidebar {
position: fixed;
height: 85vh;
background-color: plum;
color: white;
top: 12vh;
right: 5px;
}
I've also made a sandbox for this: https://codesandbox.io/s/dawn-snow-3cmdv
Right now the when the user scrolls all the way to the bottom, the sidebar overlaps the footer.
Thanks!!
Why are you using materialize?
Just use this: https://material-ui.com/components/drawers/,
at least you'll avoid that kind of problems.
If you want to keep using materialize,
just, tell me why are you using position:fixed ?
Are you aware of that position:fixed make an element
to stay always in the same place even if the page is scrolled?
Are you sure you didn't want to do this :
#landingcontainer {
height: 120vh;
position: relative;
margin-bottom:0;
}
#sidebar {
position: absolute;
height: 100%;
background-color: plum;
color: white;
top: 0;
right: 0;
}

Sticky footer in ember

Hey I create Ember application , And I try to set Sticky Footer.
I try to do this by this tutorial Sticky Footer On CSS-Tricks , Cause it's work for me once.
But with Ember its dosen't work
My css:
.mainFooter {
height:100px;
color:white;
background-color: #0c2635;
text-align:center;
}
.wrapper {
min-height: 100%;
margin-bottom: -100px;
}
.wrapper:after {
content: "";
display: block;
}
.mainFooter, .wrapper:after {
height: 100px;
}
My HTML:
<footer class="mainFooter">
SOME TEXT
</footer>
As I said, it's dosent work.
I watch the source code via the Inspector and I saw that Ember added its own wrapper to content the I put in the application.hbs file, this wrapper have a class named ember-view so I try to do:
.ember-view {
min-height: 100%;
}
But it's dosent work either, the footer displayed in the middle of the page.
Someone maybe try this and successid?
I would like to know about a solution to this problem.
I don't know how to fake an Ember app in jsfiddle/codeopen so I upload the app to my server, url: http://drawyourgif.pe.hu/dist/
EDIT
According to the solution that kumkanillam sugest I did so:
Application.hbs:
{{outlet "modal"}}
{{partial "header"}}
<div id="gif" class="wrapper">
{{outlet}}
</div>
{{partial "footer"}}
app.js
App = Ember.Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
rootElement: '#gif',
Resolver
});
And I get this error in the console:
ember.debug.js:43272Uncaught TypeError: Cannot read property 'tagName' of undefined
What I did wrong?
.ember-view will be included for all ember component by default so it's not good to apply css property for this class.
there may be many ways but the below should help.
You can wrap your application.hbs to render inside your page-wrap div.
for this you need to include the below line in
index.html
<div id="app-name" class="wrapper">
{{content-for "body"}}
</div>
application.hbs
<h1> Content </h1>
{{outlet}}
<div id="footer">
<p>I'm the Sticky Footer. inside application.hbs</p>
</div>
Configure rootElement in app.js. that will force entire app to include it in app-name div.
app.js
App = Ember.Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
rootElement: '#app-name',
Resolver
});
app.css
#wrapper {
min-height: 100%;
overflow: auto;
}
#footer {
position: absolute;
bottom: -3px;
left: 2px;
right: 2px;
height: 50px;
background-color: rgba(0, 0, 0, 0.8);
color: #fff;
}
Final Update:
You don't need to change anything in app.js. just look at the sample twiddle. I think this will help you
SAMPLE TWIDDLE
Here is a solution that uses flexbox. You may not want to use flexbox because you're unfamiliar with it, but I'll submit this answer for later google searches.
Here's a codepen with very little content in the main body: http://codepen.io/anon/pen/KgXgjV
Here's the same css with much more content in the main area: http://codepen.io/anon/pen/dpVpBK
Here is an example of why position: absolute doesn't work: https://ember-twiddle.com/f620502b8172f4181c9d58503e02e39c?openFiles=templates.application.hbs%2C
HTML
<html>
<body>
<div id="root" class="ember-application">
<div id="ember332" class="ember-view">
<div class='main-content'>
<h1>Welcome to Ember Twiddle</h1>
<br />
<p>
this page has very little content
</p>
</div>
<div id="footer">
<p>I'm the Sticky Footer. inside application.hbs</p>
</div>
</div>
</div>
</body>
</html>
CSS
/* this is just to reset codepen */
/* probably not necessary on ember */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* end reset */
html, body, #root {
height: 100%;
}
.ember-view {
display: flex;
flex-direction: column;
min-height: 100vh;
background: lightblue;
}
.main-content {
flex: 1;
}
#footer {
width: 100%;
height: 50px;
background-color: grey;
}

Resources