I am having some trouble positioning an element to its parent absolutely while keeping it responsive. Lets just say I have a responsive <section> that is 66% of the screen width and centered. I have a <nav> that is supposed to be stuck to the side of it at all times. However once your screen size is less than 992px, the width of that <section> is now 100%. The <nav> that is supposed to be always on the side is now supposed to be stuck to the top of the <section>...
All of this I can do and make it work properly... until you keep shrinking the size of the screen down where the <li> 's in the <nav> have to stack on top of eachother. When this happens the <nav> is now covering part of the <section> instead of remaining aligned with it.
I have made a codepen with an example of this. I gave the elements background colors so its easier to see whats happening. Any help or suggestions would be appreciated. Is there a way I can do this without controlling it with multiple media queries?
HTML
<section class="col-8-12 offset-2">
<nav class="to-do-list">
<ul>
<li>Add Some1 Info</li>
<li>Add Some2 Info</li>
<li>Add Some3 Info</li>
<li>Add Some4 Info</li>
</ul>
</nav>
<div>
<h1>Some Title Here</h1>
<p>Some1 Stuff Here</p>
<p>Some2 Stuff Here</p>
<p>Some3 Stuff Here</p>
<p>Some4 Stuff Here</p>
</div>
</section>
CSS
html { background-color: #1394cb; }
.col-8-12 { width: 66.66666667%; }
.offset-2 { margin-left: 16.66666667%; }
section { float: left; margin-top: 250px; background-color: #0d2c41; position: relative; }
nav.to-do-list { position: absolute; left: -177px; top: 0; background-color: #FFF; max-width: 180px; }
div>h1, div>p { color: #FFF; padding-left: 15px; }
ul>li { list-style: none; margin-right: 30px; }
#media only screen and (max-width: 992px){
.col-8-12 { width: 100%; }
.offset-2 { margin-left: 0; }
nav.to-do-list { left: 0; top: -50px; min-height: 50px; display: inline-block; max-width: none; width: 95%; margin-left: 2.5%; }
ul>li { display: inline-block; }
}
Simply use relative positioning at lower resolutions. Using absolute will break your layout because the element is removed from the regular flow and therefore doesn't affect other elements around it.
With minor other modifications:
html { background-color: #1394cb; }
.col-8-12 { width: 66.66666667%; }
.offset-2 { margin-left: 16.66666667%; }
section { float: left; margin-top: 250px; ; position: relative; }
section > div {background: #0d2c41; padding: 10px 0;}
nav.to-do-list { position: absolute; left: -177px; top: 0; background-color: #FFF; max-width: 180px; }
div>h1, div>p { color: #FFF; padding-left: 15px; }
ul>li { list-style: none; margin-right: 30px; }
#media only screen and (max-width: 992px){
.col-8-12 { width: 100%; }
.offset-2 { margin-left: 0; }
nav.to-do-list { position: relative; left: 0; min-height: 50px; display: inline-block; max-width: none; width: 95%; margin-left: 2.5%; }
ul>li { display: inline-block; }
}
Related
This question already has answers here:
How do you get the footer to stay at the bottom of a Web page?
(32 answers)
Closed 2 years ago.
Trying to make the footer in my html css website stick down but nothing works. I've tried changing the position to absolute and fixed and setting bottom: 0 and doing everything but nothing works. Also, is there a better way to make my logo aligned in the middle? Heres my css:
.footer{
background-color: #d62929;
clear: both;
width: 100%vw;
display:block;
overflow: hidden;
padding-top:10px;
padding-bottom: 10px;
min-height: 100%vw;
}
.contact{
margin-left: 30px;
margin: 0 auto;
display:block;
float: left;
padding-right: 50px;
}
.info{
margin-left: 30px;
margin: 0 auto;
padding-left: 30px;
display:block;
float: left;
padding-right: 50px;
}
.account{
margin-left: 30px;
margin: 0 auto;
padding-left: 30px;
display:block;
float: left;
padding-right: 50px;
}
a{
text-decoration:none;
color: black;
font-family: times new roman;
font-size: 18px;
text-align: center;
}
ul{
list-style: none;
text-align: left;
}
.logo_footer{
float: left;
padding: 40px 0;
margin-left: 20px;
margin-right: 40px;
}
h1{
color: white;
font-size: 24;
}
li{
padding: 5px;
}
Heres my html for the footer:
<div>
<footer class="footer">
<img src="{{url_for('static', filename='Logo.png')}}" style="height:108px;width:100px;" class="logo_footer" alt="logo"></a>
<div class="contact">
<h1>Contact us</h1>
<ul>
<li>Facebook</li>
<li>Instagram</li>
<li>Telegram</li>
</ul>
</div>
<div class="info">
<h1>Information</h1>
<ul>
<li>About Us</li>
<li> Contact Us</li>
<li>Return Policy</li>
<li>Delivery</li>
</ul>
</div>
<div class="account">
<h1>Account</h1>
<ul>
<li>Log in</li>
<li> Register</li>
<li> My cart</li>
</ul>
</div>
</footer>
</div>
You can make position:fixed; instead of position:absolute; This will make it fixed to the bottom. if there are any other div or something that's causing an overlay issue, use z-index:5;
I used postion:relative on wrapper div and postion: sticky on footer.
.sectionWrapper {
position: relative;
}
.header {
height: 10vh;
width: 100%;
background: red;
}
.body {
height: 100vh;
width: 100%;
background: blue;
border: 1px solid black;
}
.footer {
height: 20vh;
width: 100%;
background-color: green;
position: sticky;
bottom: 0%;
}
<div class="sectionWrapper">
<section class="header">Header</section>
<section class="body">Body 1</section>
<section class="body">Body 2</section>
<section class="body">Body 3</section>
<section class="footer">footer</section>
</div>
There are multiple ways for that.
Min-height:
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
.footer {
position: relative;
min-height: 100%;
Margin-top, here you do need to specify footer height:
* {
margin: 0;
padding: 0;
}
html,
body,
.footer {
height: 100%;
}
.footer__content {
box-sizing: border-box;
This the best, because the height of the footer doesn't matter:
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
.footer {
display: table;
height: 100%;
This way is a bit different from others because it uses CSS calc() function, and you need to know exact footer height:
* {
margin: 0;
padding: 0;
}
.footer__content {
min-height: calc(100vh - 80px);
}
This is the most correct way, however it works only in modern browsers, as in the 3rd example, the height doesn't matter:
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
.footer {
display: flex;
flex-direction: column;
In my project, I am using this to solve same task, it's the easiest solution that I found in Internet:
body {
position: relative;
min-height: 100vh;
}
.footer {
position: absolute;
bottom: 0px;
}
Here is important to use min-height property in body and not the height one, because actual height of your page can be more that user's screen size.
This solution makes your footer to snap not to screen bottom, but to page bottom.
I'm trying to learn HTML/CSS and working on a nav bar, however, I am experiencing a scaling problem. This is the website in full screen.
This is the website minimized a bit.
Then this is the website minimized all the way.
As you can tell when I scale the website around into different scales then the proportions mess up and things begin to overlap. I have tried making the children absolute while keeping the containers relative. I am also using em's for measurement and not using pixels. What can I do to keep everything proportional while scaling?
This is the js fiddle
https://jsfiddle.net/2w1r136j/2/
HTML
<div class="container">
<header>
<nav>
<img class="logo" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Westworld_Logo.svg/2000px-Westworld_Logo.svg.png" alt="logo">
<div class="leftNavContainer">
Home
Story
</div>
<div class="rightNavContainer">
Characters
Create
</div>
</nav>
</header>
</div>
CSS
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #222;
font-size: 1em;
}
.container {
width: 100%;
height: 100%;
position: absolute;
}
header {
background: white;
height: 3.5em;
}
.logo {
height: 4.5em;
width: 4.5em;
position: absolute;
left: 50%;
margin-left: -50px !important; /* 50% of your logo width */
display: block;
margin-top: 0;
}
.leftNavContainer {
position: absolute;
float: left;
}
.leftNavContainer a {
position: relative;
display: inline;
margin-right: 2em;
margin-left: 2em;
}
.rightNavContainer {
float: right;
}
.rightNavContainer a {
position: relative;
display: inline;
margin-right: 2em;
margin-left: 2em;
}
Well Media queries might work, but a much better implementation would be using Flexbox or better CSS Grid.
I've updated the fiddle with a flexbox implementation.
https://jsfiddle.net/khpv2azq/3/
HTML
<head>
<title>
Westworld
</title>
<link rel="stylesheet" href="css/style.css">
</head>
<div class="container">
<header>
<nav>
<div class="leftNavContainer">
Home
Story
</div>
<img class="logo" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8a/Westworld_Logo.svg/2000px-Westworld_Logo.svg.png" alt="logo">
<div class="rightNavContainer">
Characters
Create
</div>
</nav>
</header>
</div>
CSS
* {
box-sizing: border-box;
}
body {
margin: 0;
background: #222;
font-size: 1em;
}
nav{
display: flex;
justify-content: space-between;
}
.container {
width: 100%;
height: 100%;
}
header {
background: white;
height: 3.5em;
}
.logo {
height: 4.5em;
width: 4.5em;
position: absolute;
left: 50%;
margin-left: -50px !important;
/* 50% of your logo width */
display: block;
margin-top: 0;
}
.leftNavContainer {
}
.leftNavContainer a {
position: relative;
display: inline;
margin: 4px;
}
.rightNavContainer {
}
.rightNavContainer a {
position: relative;
display: inline;
margin: 4px;
}
Also MDN resource for Flex box -
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox
Hope this help! 😇
You can use media queries to change sizes at breakpoints
ex:
#media only screen and (max-width: 600px) {
body {
font-size: .7em;
}
}
https://jsfiddle.net/2w1r136j/7/
However, you might consider using the media queries to incorporate a responsive design which will work for mobile.
A common idiom is to collapse the menu items into full width elements, and to bump up the font size.
something like: https://jsfiddle.net/2w1r136j/40/
I'm trying to put a pseudo element as a background image that simply crops at the document's width. However, when I put a background image in it keeps expanding the width of the document, making some other divs really wide.
Specifically, you can see from the screenshot that the nav is extending its width to accommodate the pseudo element's background image.
My attempt:
I've seen it done, but am not what the difference is, as the code in the actual pseudo element is the same. An example:
HTML:
<nav class="navbar navbar-default navbar-fixed-top navbar-custom">
<div class="container nav-down">
<div class="navbar-header page-scroll">
<a class="navbar-brand" href="/"><img src="/static/ScoopsLogo.png" alt="${out.global.siteName} Logo"></a>
<p class="navbar-blog">Blog</p>
</div>
</div>
<div id="scroll-progress">
<div id="scroll-progress-bar"></div>
</div>
</nav>
<section id="home">
<div class="home-1">
<div class="home1-1">
<h1>Sound smart at the dinner table</h1>
<h5>Learning made easy & fun</h5>
</div>
</div>
<div class="home-lessons fade-in-up-static delay-2">
<!-- List of Lessons -->
<h5 class="text-center">NEW SCOOPS</h5>
<div class="lessons-list">
</div>
</div>
</section>
CSS:
.navbar {
min-height: 50px;
margin-bottom: 20px;
}
.navbar-default {
background-color: #f8f8f8;
border-color: #e7e7e7;
}
.navbar-fixed-top {
top: 0;
border-width: 0 0 1px;
}
.container {
max-width: 600px;
}
.navbar-custom {
.navbar-header {
width: inherit;
max-width: inherit;
}
.navbar-brand {
img {
width: auto;
height: 100%;
}
}
.navbar-blog {
float: right;
padding: 10px;
margin: 3px 10px 0 0;
}
}
.home-lessons {
width: 100%;
padding: 3%;
#media (min-width: $screen-md-min) {
margin-bottom: 20px;
padding: 20px 40px;
}
h5 {
margin-top: -100px;
}
&::before {
content: '';
background-image: url(/static/Scoops-Icons.png);
background-size: cover;
width: 500px;
height: 500px;
display: block;
position: absolute;
margin-left: 200px;
margin-top: -200px;
}
}
I just answered a very similar question that you put a bounty on over here. In this case, I think that your best bet would be to either enforce a max width on your background-carrying pseudo-element, or drop the overflow-x on your document.
Below is a snipper of your CSS with the fix:
.home-lessons {
width: 100%;
padding: 3%;
#media (min-width: $screen-md-min) {
margin-bottom: 20px;
padding: 20px 40px;
}
h5 {
margin-top: -100px;
}
&::before {
content: '';
background-image: url(/static/Scoops-Icons.png);
background-size: cover;
width: 500px;
height: 500px;
display: block;
position: absolute;
margin-left: 200px;
margin-top: -200px;
max-width: calc(100% - 200px); /* HERE IS THE FIX */
}
}
If that didn't work, the quick fix would be to go for this:
html {
overflow-x: hidden;
}
I've seen some people overkill it slightly by using, html, body as their CSS selectors, this is up to you.
Parent element should use a position: relative whilst the child element uses top: 0 and left: 0.
Like this:
.parent {
position: relative;
}
.child {
top: 0;
left: 0;
}
I have two lists that's location I would like to move as the screen size gets smaller. When the screen reaches 327px in length, I would like the "headlogo" to be centered on the screen, and everything else exactly as it is... how do I do this?
#charset "utf-8";
/* CSS Document */
body {
margin: 0;
padding: 0;
background-color: #DDDCDC;
}
ul {
list-style-type: none;
}
li {
display: inline;
padding: 10px;
}
.secondlist {
margin-top: 30px;
}
#headlogo {
width: 80px;
margin-left: 10px;
margin-top: 10px;
}
.firstlist {
position: relative;
float: left;
}
.secondlist {
position: absolute;
right: 0;
margin-right: 50px;
margin-top: 50px;
z-index: 2;
}
li#about {
width: 5px;
}
#media only screen and (max-width: 610px) {
.firstlist {
position: relative;
float: left;
}
.secondlist {
position: relative;
right: 0;
margin-right: 50px;
margin-top: 50px;
z-index: 2;
text-align: center;
}
li {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
li.listli {
line-height: 30px;
}
#headlogo {
width: 80px;
margin-left: 10px;
margin-top: -22px;
}
}
#media only screen and (max-width: 424px) {
#headlogo {
margin-top: -3px;
}
}
#media only screen and (max-width: 394px) {
#headlogo {
margin-top: 7.5px;
}
}
#media only screen and (max-width: 327px) {
#centerme {
display: inline-block;
position: relative;
top: 30px;
}
.firstlist {
float: none;
display: block;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}
li.listli {
position: relative;
top: -10px;
}
}
<div id="centerme">
<ul class="firstlist">
<a class="alist" href="http://www.coopertimewell.com">
<li class="listl" id="image">
<img id="headlogo" src="images/logotestme.png" />
</li>
</a>
</ul>
</div>
<ul class="secondlist">
<a class="alist" href="#">
<li class="listli" id="home"><strong>HOME</li></a>
<a class = "alist" href = "http://www.coopertimewell.com/about"><li class = "listli" id = "about">ABOUT ME</li></a>
<a class = "alist" href = "http://www.coopertimewell.com/work"><li class = "listli" id = "work">PORTFOLIO</li></a>
<a class = "alist" href = "http://www.coopertimewell.com/contact"><li class = "listli" id = "contact">CONTACT</li></a>
</ul>
If you would only center it horizontally u have to edit #center in the 'max-width: 327px' query to the following:
#centerme {
display: block;
position: relative;
top: 30px;
width: 100%;
text-align: center;
}
I made the #center div a 100% width and set the text-align to center, which affects the image also. So this way you can center the image horizontally.
Or if you would center it horizontally and vertically you can use:
#centerme {
display: block;
position: relative;
top: 50%;
transform: translateY(50%);
-webkit-transform: translateY(50%);
width: 100%;
text-align: center;
}
With the same code from above I added
top: 50%;
transform: translateY(50%);
to center it horizontally.
Goodluck!
This should centre it below 327px;
#media (max-width: 327px){
#headlogo{
display: block;
margin: 0 auto
}
}
Check this out for more info
You can do
#media (max-width: 327){
#headlogo{
//with your code her//
}
}
Or you can use a new css file with media added
<link rel="stylesheet" media="screen and (max-width: 327px)" href="small.css/>
With this line you can add a complete new css if your screen will be 327px or smaller.
I'm trying to have an image next to my vertical navbar but it's being displayed under it.
This is how I'm doing it
body
{
font-family: 'Open Sans', sans-serif;
background-color: #333;
}
.element
{
display:inline-block;
float:left;
}
#wrapper
{
position:absolute;
height:100%;
width:200px;
bottom:0px;
background-color:#0F4D92;
}
nav
{
top: 50%;
margin-top: -75px;
position: fixed;
}
ul
{
list-style-type: none;
width: 200px;
height: 40px;
}
<div class="element" id="wrapper">
<nav>
<ul>
<li>Home</li>
<li>Withdraw</li>
<li>Deposit</li>
</ul>
</nav>
</div>
<img class="element" src="../bg.jpg"/>
Yet, this is how it looks:
As you can see the image is behind the navbar. I can't just use a margin because I need an element with the size of the gray space so I can have the image in the center of that space. So how can I fix this?
Gathering from what you want to achieve, I took the liberty to change up your markup and made you a working copy. Less complicated than what you were trying to do I think. I hope this helps in some way.
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
height: 100%;
}
body {
font-family: 'Open Sans', sans-serif;
background-color: #333;
}
.element {
position: relative;
height: 100%;
width: 100%;
font-size: 0;
}
nav,
.imgWrapper {
display: inline-block;
height: 100%;
}
nav {
width: 20%;
background-color: #0F4D92;
font-size: 1rem;
}
nav > * {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.imgWrapper {
vertical-align: top;
width: 80%;
}
.imgWrapper > img {
width: 100%;
height: 100%;
}
ul {
list-style-type: none;
width: 200px;
height: 40px;
}
<div class="element">
<nav>
<ul>
<li>Home
</li>
<li>Withdraw
</li>
<li>Deposit
</li>
</ul>
</nav>
<div class="imgWrapper">
<img src="http://placehold.it/2000x2000" alt="" />
</div>
</div>
Your wrapper for the navbar is position: absolute; which means that none of the other elements will interact with it. Maybe try using position: relative; instead.
By the way you are doing it one solution is to give .element a left margin, but you will always have to set a margin if you want to display something next to your navbar (because its absolute positioned):
.element
{
display:inline-block;
float:left;
margin-left: 210px;
}
Wrapper position causing this problem. You must change it to relative. You also need to change the following:
##wrapper {
position: relative;
}
#wrapper {
float: left;
width: 250px;
overflow: hidden;
}
.element {
float: right;
}
#wrapper,
.element {
display: inline-block;
width: 100%;
}