Reputation: 95
I am fairly new to CSS and HTML.
And I understand that this question has been asked several times, yet I cannot seem to fix my current issue.
I have been working on little small projects like trying to recreate other websites with extra features.
My current task is recreating a front page search area, such as google.
I have been focusing in the responsiveness of websites when a browser resize occurs, so I have been using Flexbox as often as possible.
A main div with a container id, with a min-height of 100% and a relative position.
Inside the container div are nav(#nav), center(#center) and footer(#footer) elements and their respective id's.
I have made some research into the Footer and how to keep it at the bottom of a page while being responsive when shrinking the browser, but I seem to have come to a halt or possibly I haven't fully understood yet how to get the footer to stop at a certain element.
The issue is that I cannot get the footer to function correctly. I do not wish it to overlaps with the bottons and links in the main content. It stops at the search bar when resizing the browswer, but I would like it to stop when it meets the buttons.
As you will see from the block of code snippets in the JSbin link, I have an issue with the footer.
Current HTML + CSS files and output: JSBin
*{
margin: 0px;
padding: 0px;
}
html, body {
height: 100%;
}
#container {
min-height:100%;
position:relative;
}
a:visited{
color: #609;
}
/* ----- Navigation Styling ----- */
#nav {
display: flex;
border: 10px solid goldenrod; /*Colourful Borders*/
min-height:9vh;
justify-content:flex-end;
align-items: baseline;
background:#ff0;
padding:10px;
}
/*Nav Content*/
/* ----- Center Styling ----- */
#center {
width: 100%;
background-color: white;
min-height:10vh;
}
.ctr_img {
height: 92px;
width: 272x;
padding-top: 20px;
}
.ctr_img:hover {
-webkit-filter: invert(100%);
}
.img_mic {
padding: 0 8px;
float: right;
display:inline-block;
top: -30px;
position: relative;
}
.srch_bx {
border-style: hidden;
box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 0 0 0 1px rgba(0,0,0,0.08);
max-width: 40%;
min-height: 40px;
}
.srch_in {
width: 100%;
border-radius: 2px;
border: none;
overflow: auto;
outline: none;
left: 0px;
background: url(data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D) transparent;
font: 16px arial,sans-serif;
display: inline-block;
text-align: start;
}
.mic_set {
background-size: 30px 30px;
height: 100%;
width: 26px;
}
.btn_sbmt {
padding: 20px 50%;
min-height: 20px;
display: flex;
justify-content: center;
}
.btn_srch, .btn_lcky {
background-color: #f2f2f2;
border: 1px solid #f2f2f2;
border-radius: 2px;
color: #757575;
font-family: arial,sans-serif;
font-size: 1vw;
font-weight: bold;
margin: 11px 4px;
padding: 0 16px;
height: 36px;
}
.lang_diff{
max-height: 28px;
font-size: 0.9vw;
margin-bottom: 24px;
font-family: arial,sans-serif;
}
/* Footer Styling */
#footer {
position:absolute;
bottom:0;
height: 5.2em; /* Height of the footer */
width: 100%;
min-height: 10vh;
background-color: #f2f2f2;
line-height: 40px; /*Specifies the line height.*/
min-width: 400px;
}
.ctry {
display: flex;
margin-left: 30px;
text-align: left;
color: rgba(0,0,0,.54);
border-top: 1px solid #e4e4e4;
max-width: 150px;
}
.ctry_ftr {
font-size: 1vw;
}
.opt_ftr {
display: flex;
justify-content:space-between;
border: 1px solid #e4e4e4;
}
span.blft_ftr a, span.brght_ftr a, span.brght_stg a {
text-decoration: none;
color: #666;
margin-left: 16px;
font-size: 1vw;
}
.brght_ftr{
margin-right: 30px;
float: right;
}
.adv_ftr {
padding: 0 16px;
text-decoration: none;
display: inline-block;
}
<!DOCTYPE html>
<html>
<head>
<title>Testing Ground</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="reset.css" />
<link rel="icon" href="http://www.favicon.cc/logo3d/53653.png" />
<meta charset="UTF-8">
</head>
<body> <!--FLEX-->
<div id="container"> <!-- Added New for Footer behaviour -->
<nav id="nav" class="nav_cls">
<!--Currently at work-->
<p>Navigation Bar Content</p>
</nav>
<center id="center">
<a class="img_link" href="">
<img class="ctr_img" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/PM5544_with_non-PAL_signals.png/250px-PM5544_with_non-PAL_signals.png" alt="image">
</a>
<div class="in_forms">
<div class="srch_bx">
<form>
<input class="srch_in" type="text" name="search"/>
</form>
<div class="img_mic">
<a href="https://youtu.be/Ye8mB6VsUHw ">
<img class="mic_set" src="https://cdn.sesamestreet.org/sites/default/files/1496328210/Cookie_Big.png" />
</a>
</div>
</div>
</div>
<div>
<div class="btn_sbmt">
<input class="btn_srch" type="submit" value="Don't Hide me!" name="search"/>
<input class="btn_lcky" type="submit" value="Seriously, Don't!" name="luck"/>
</div>
</div>
<div class="lang_diff">
Links:
<a class="lang" href="">Link1</a>
<a class="lang" href="">Link2</a>
<a class="lang" href="">Link3</a>
<a class="lang" href="">Link4</a>
<a class="lang" href="">Link5</a>
</div>
</center>
<footer id="footer">
<div class="ctry">
<span class="ctry_ftr">
First half of a footer
</span>
</div>
<div class="opt_ftr">
<span class="blft_ftr">
<a class="adv_ftr" href="">Footer Link 1</a>
<a class="adv_ftr" href="">Footer Link 2</a>
<a class="adv_ftr" href="">Footer Link 3</a>
</span>
<span class="brght_ftr">
<a class="adv_ftr" href="">Footer Link 4</a>
<a class="adv_ftr" href="">Footer Link 5</a>
</span>
</div>
</footer>
</div>
</body>
</html>
Footer is displayed over content. I need it always on the bottom of the page
HTML, CSS - Sticky footer displays over content on screen resize
Footer is displayed over content. I need it always on the bottom of the page
Upvotes: 3
Views: 4823
Reputation: 2919
#footer
is positioned absolutely, which means that the parent container doesn't know how big it is (i.e. it is outside of the "document flow"). #container
's height is set to min-height: 100%
, so at a minimum, it needs to be the full height of the parent (in this case, the body), but if the child content is taller, it will stretch to accommodate that extra height.
Because #footer
is not considered when#container
is trying to figure out it's height, #container
is just making sure it can fit it's other position: static
or position:relative
children.
So, when you scale down the height of your browser so it is very short, #container
is too short - it doesn't leave space for the #footer
.
There are a few ways to take care of this, though it is tricky from a responsive perspective if the height of the #footer
is variable depending on the content and whether it wraps, etc.
If you know the height, you can add that amount of padding on the bottom of #container
(if box-sizing: border-box;
is turned on, it will not add onto the height: 100%
- it will be included) and it will reserve the space for the #footer
.
If you don't know the height, you could leverage display: flex;
to fill the full height and have the footer self-align to the bottom, without needing to use position: absolute;
. Flexbox has a bit of a learning curve though, I would recommend you read through some tutorials. http://flexboxfroggy.com/ is a fun game that can also help to learn it.
Upvotes: 1
Reputation: 90277
:)
Congratulations, first of all, for following the rules of asking on SO.
Your problem comes from placing your footer out of document flow (position:absolute
). It looks like you want it placed in document flow. So change position:absolute
into position:relative
(or static
).
To keep it at the bottom of the screen at all times, adjust your #container
to display:flex;flex-direction:column;min-height:100vh
and make #center
grow:
#footer {
position:static; /* this is default value of position
* so you could just remove `position:absolute`
* from your code.
*/
}
#container {
min-height: 100vh;
display: flex;
flex-direction: column;
}
#center {
flex-grow: 1;
}
See it working:
* {
margin: 0px;
padding: 0px;
}
html,
body {
height: 100%;
}
#container {
min-height: 100%;
position: relative;
}
a:visited {
color: #609;
}
/* ----- Navigation Styling ----- */
#nav {
display: flex;
border: 10px solid goldenrod;
/*Colourful Borders*/
min-height: 9vh;
justify-content: flex-end;
align-items: baseline;
background: #ff0;
padding: 10px;
}
/*Nav Content*/
/* ----- Center Styling ----- */
#center {
width: 100%;
background-color: white;
min-height: 10vh;
}
.ctr_img {
height: 92px;
width: 272x;
padding-top: 20px;
}
.ctr_img:hover {
-webkit-filter: invert(100%);
}
.img_mic {
padding: 0 8px;
float: right;
display: inline-block;
top: -30px;
position: relative;
}
.srch_bx {
border-style: hidden;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.16), 0 0 0 1px rgba(0, 0, 0, 0.08);
max-width: 40%;
min-height: 40px;
}
.srch_in {
width: 100%;
border-radius: 2px;
border: none;
overflow: auto;
outline: none;
left: 0px;
background: url(data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D) transparent;
font: 16px arial, sans-serif;
display: inline-block;
text-align: start;
}
.mic_set {
background-size: 30px 30px;
height: 100%;
width: 26px;
}
.btn_sbmt {
padding: 20px 50%;
min-height: 20px;
display: flex;
justify-content: center;
}
.btn_srch,
.btn_lcky {
background-color: #f2f2f2;
border: 1px solid #f2f2f2;
border-radius: 2px;
color: #757575;
font-family: arial, sans-serif;
font-size: 1vw;
font-weight: bold;
margin: 11px 4px;
padding: 0 16px;
height: 36px;
}
.lang_diff {
max-height: 28px;
font-size: 0.9vw;
margin-bottom: 24px;
font-family: arial, sans-serif;
}
/* Footer Styling */
#footer {
/* position: absolute;
bottom: 0; */
height: 5.2em;
/* Height of the footer */
width: 100%;
min-height: 10vh;
background-color: #f2f2f2;
line-height: 40px;
/*Specifies the line height.*/
min-width: 400px;
}
.ctry {
display: flex;
margin-left: 30px;
text-align: left;
color: rgba(0, 0, 0, .54);
border-top: 1px solid #e4e4e4;
max-width: 150px;
}
.ctry_ftr {
font-size: 1vw;
}
.opt_ftr {
display: flex;
justify-content: space-between;
border: 1px solid #e4e4e4;
}
span.blft_ftr a,
span.brght_ftr a,
span.brght_stg a {
text-decoration: none;
color: #666;
margin-left: 16px;
font-size: 1vw;
}
.brght_ftr {
margin-right: 30px;
float: right;
}
.adv_ftr {
padding: 0 16px;
text-decoration: none;
display: inline-block;
}
#footer {
line-height: 2.42em;
}
#container {
min-height: 100vh;
display: flex;
flex-direction: column;
}
#center {
flex-grow: 1;
}
<div id="container">
<!-- Added New for Footer behaviour -->
<nav id="nav" class="nav_cls">
<!--Currently at work-->
<p>Navigation Bar Content</p>
</nav>
<center id="center">
<a class="img_link" href="">
<img class="ctr_img" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/PM5544_with_non-PAL_signals.png/250px-PM5544_with_non-PAL_signals.png" alt="image">
</a>
<div class="in_forms">
<div class="srch_bx">
<form>
<input class="srch_in" type="text" name="search" />
</form>
<div class="img_mic">
<a href="https://youtu.be/Ye8mB6VsUHw ">
<img class="mic_set" src="https://cdn.sesamestreet.org/sites/default/files/1496328210/Cookie_Big.png" />
</a>
</div>
</div>
</div>
<div>
<div class="btn_sbmt">
<input class="btn_srch" type="submit" value="Don't Hide me!" name="search" />
<input class="btn_lcky" type="submit" value="Seriously, Don't!" name="luck" />
</div>
</div>
<div class="lang_diff">
Links:
<a class="lang" href="">Link1</a>
<a class="lang" href="">Link2</a>
<a class="lang" href="">Link3</a>
<a class="lang" href="">Link4</a>
<a class="lang" href="">Link5</a>
</div>
</center>
<footer id="footer">
<div class="ctry">
<span class="ctry_ftr">
First half of a footer
</span>
</div>
<div class="opt_ftr">
<span class="blft_ftr">
<a class="adv_ftr" href="">Footer Link 1</a>
<a class="adv_ftr" href="">Footer Link 2</a>
<a class="adv_ftr" href="">Footer Link 3</a>
</span>
<span class="brght_ftr">
<a class="adv_ftr" href="">Footer Link 4</a>
<a class="adv_ftr" href="">Footer Link 5</a>
</span>
</div>
</footer>
</div>
Another minor issue which needed addressing was using line-height
expressed in px
for #footer
while its height was expressed in em
, causing the page to develop a vertical scrollbar. line-height:2.42em
fixes it.
On a different note, it would be unfair of me to give you this answer without mentioning the major accessibility problem your example has from lowering font-size on narrow devices. Try opening the example page on any mobile and you'll understand what I mean. At all times, elements in your page should keep a font-size allowing users to read.
The main purpose of web pages is to present content. If you present the content in a form disabling user ability to receive the message, your page does not perform its function.
Upvotes: 4