I'm writing an app that replicates the look/feel of a desktop OS, and my navbar in the bottom of the page shrinks in height when the page is resized in height. It shrinks in such a way that it becomes unusable at a point.
I've already tried some CSS properties such as using: position: fixed, position: relative, and position: absolute. position: absolute is the one that has been the best try out of all of them. If I use any others, the navbar would stick at the top no matter what you change in the style.
body {
/* these are for the navbar */
top: 100%;
margin: 0px;
padding: 0px;
/* normal styles */
font-family: "MS Sans Serif";
color: white;
background-color: #008080;
font-size: 12px;
/* without this, the page would go blank
overflow: hidden;
}
ul {
/* this is what works best */
position: absolute;
width: 100%;
top: 96.17%;
/* normal navbar styling.. */
list-style-type: none;
margin: 0px;
padding: 0px;
overflow: hidden;
background-color: #ffffff;
}
I expected the navbar to move up normally as needed, keeping its properties. Instead, the navbar moves up while the page is resized, but it becomes thinner and thinner until it is unusable.
Here is a sample gif: enter image description here
ul {
display: flex;
list-style: none;
align-items: center;
flex-flow: row;
background: red;
position: sticky;
max-width: 100vw;
width: 100%;
color: wheat;
height: 50px;
justify-content: space-around;
}
#media screen and (max-width: 768px) {
ul {
transition: .2s all linear;
height: 45px;
max-width: -webkit-fill-available;
width: 100%;
}
}
<ul>
<li>Home</li>
<li>About</li>
<li>Help</li>
<li>Contact</li>
</ul>
Related
My button's text leaks out of the container if I zoom it. When I zoom the button's text flows out of the container. What I want is for it to adjust its font size and stay in the container and don't leak out.
.wrapper a {
position: relative;
display: block;
width: 8.5vw;
height: 7vh;
font-size: 18px;
font-family: sans-serif;
text-decoration: none;
color: #05386b;
border: 2px solid #05386b;
letter-spacing: 2px;
text-align: center;
position: relative;
transition: all .35s;
}
.wrapper a span {
position: relative;
z-index: 2;
}
.wrapper a:after {
position: absolute;
content: "";
top: 0;
left: 0;
width: 0;
height: 100%;
background: #EDF5E1;
transition: all .35s;
}
.wrapper a:hover {
color: #8ee4af;
}
.wrapper a:hover:after {
width: 100%;
}
<div className="wrapper">
<Link to="/orders"><span>Returns <br/> <b>& Orders</b></span></Link>
</div>
You could take a look at the #media css rule. It can be used to create responsive webpages.
For example the following increases the font size by 2 pixels if the screen size is smaller than 786px.
#media only screen and (max-width: 786px) {
.wrapper a {
font-size: 20px;
}
}
To prevent the content from overflowing all together you might want to take a look at the overflow css property. It controls the way content overflows from the containing area. Might not be applicable to your case though as you are talking about making a button here.
This is the first time I'm getting this type of problem. I'm trying to make responsive navigation bar. At a certain width, I want my nav links div to go to right side bar. But the nav links div is not taking full viewport height even after giving 100vh. Here is the code -
HTML
<header>
<nav>
<div class="logo">Logo</div>
<div class="nav-items">
<li><a href = '#'>Link-1</a></li>
<li><a href = '#'>Link-2</a></li>
<li><a href = '#'>Link-3</a></li>
</div>
</nav>
</header>
SCSS
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
header {
background: blue;
nav {
height: 65px;
max-width: 1340px;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
margin: auto;
padding: 1rem;
.logo {
font-size: 2rem;
color: black;
}
.nav-items {
li {
display: inline;
a {
color: black;
text-decoration: none;
margin-left: 2rem;
}
}
}
}
}
#media screen and (max-width: 768px) {
header nav .nav-items {
position: absolute;
right: 0;
height: 100vh;
width: 50%;
border: 1px solid black;
}
}
Link to codepen -
https://codepen.io/yell0wflash/pen/JjWGwJa
Set position to fixed, add top:0; also height:100vh and left or right to 0 in the media query
This is because your has align-items:center; change this based on media query.
#media screen and (max-width: 768px) {
header nav {
align-items:unset;
}
}
The height is 100vh, but you have set align-items:center, so the element is offset to align with the other flex element.
Add
header nav {
align-items: flex-start;
}
To your media query
Now as your nav element has padding you need to accommodate that with
height: calc(100vh - 16px);
I'm trying to create a dropdown menu for one of the items in my nav bar. I based the code on this W3Schools example. Upon hover, the menu appears below the nav bar (as it should be) but it is 1) stacked horizontally rather than vertically and 2) appears to the far right on the page. I've look at similar questions here but haven't been able to figure out the problem in my code. Any help would be appreciated.
/* nav */
nav {
display: flex;
flex-wrap: wrap;
padding: .25rem 0;
color: #ffffff;
font: 30px 'Roboto', sans-serif;
margin: auto;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
overflow: hidden;
}
nav a {
display: block;
margin: 0 40px;
}
/* dropdown container */
.dropdown {
float: none;
position: relative;
overflow: visibile;
}
/* dropdown button */
.dropdown .dropbtn {
display: flex;
font-size: 30px;
border: none;
outline: none;
color: #ffffff;
padding: inherit;
background-color: inherit;
font-family: inherit;
margin: auto;
}
/* dropdown content (hidden by default */
.dropdown-content {
display: none;
position: absolute;
margin-top: 10px;
background-color: #ffffff;
width: 250px;
left: calc(50% - 125px);
}
.dropdown-content>a {
color: black;
text-align: left;
border-bottom: 1px solid #009EDB;
}
/* show dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
float: left;
margin: 0;
}
<nav class="justify-content-center">
About
<section class="dropdown">
<button class="dropbtn">
Work
</button>
<section class="dropdown-content">
Articles and Presentations
From Process to Flow Series
</section>
</section>
Github
Trailhead
</nav>
Your dropdown is structured of anchors (links, <a> tags), which naturally are inline elements. That means that naturally these elements are located as part of page or line flow. To make them appear vertical, you need to change them to be "block" elements, which you use by adding display: block to the styling on the dropdown a elements:
nav a {
margin: 0 40px;
display: block;
}
The 'margin' was already present in this particular element.
I've also removed all the "!important" from your styling because it's bad practice and wasn't helping at all. Since you're missing a background, I restyled the triggering element to have red text so it doesn't seem like a random white space was triggering the dropdown.
That being said, I don't see any "styled far right" behavior for the drop down. The menu is displayed directly under the triggering element (with a 40px margin, which if you have a really small screen might make it seem like it's super far right.)
/* nav */
nav {
display: flex;
flex-wrap: wrap;
padding: .25rem 0;
color: #ffffff;
font: 30px 'Roboto', sans-serif;
margin: auto;
justify-content: center;
overflow: hidden;
}
nav a {
margin: 0 40px;
display: block;
}
/* dropdown container */
.dropdown {
float: none;
overflow: hidden;
}
/* dropdown button */
.dropdown .dropbtn {
display: flex;
font-size: 30px;
border: none;
outline: none;
color: red;
padding: inherit;
background-color: inherit;
font-family: inherit;
margin: auto;
}
/* dropdown content (hidden by default */
.dropdown-content {
display: none;
position: absolute;
background-color: inherit;
width: 100%;
}
/* show dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
<nav class="justify-content-center">
About
<section class="dropdown">
<button class="dropbtn">Work</button>
<section class="dropdown-content">
Articles and Presentations
From Process to Flow Series
</section>
</section>
Github
Trailhead
</nav>
Problem number 1 was solved through Rody of the Frozen Peas answer.
For Problem number 2:
You want to align the center of dropdown-content relative to it's parent.
For that you want to shift dropdown-content to the left by half of it's width and then shift it a bit to the right by half of the width of the dropdown. Also the dropdown element needs to be relatively positioned otherwise the dropdown-content would be positioned realtive to the document. To make the dropdown-content visible you need to make dropdowns and the nav bars overflow visible.
nav {
overflow: visible;
}
.dropdown {
position: relative;
overflow: visible;
}
.dropdown-content {
position: absolute;
width: 250px;
left: calc(50% - 125px);
}
The reason this works is that you align the center of the dropdown-content with the left of dropdown by specifying left: -125px as you're shifting it to the left by half of the width of dropdown-content. To then align it with the center of dropdown you need to add 50% as it is absolutely positioned and will therefore use the parents width as reference and 50% of the parents width is the parents center.
Context: I have a container using display: flex and I am centering one svg element vertically in the center of the page. The only other things on the page are A) a fixed header that gets taken out of the document flow and isn't taking up space and B) a footer with position: absolute that should also not be taking up space. The centering works correctly on every browser but mobile chrome/mobile ios. Vertical centering appearing correctly on firefox mobile.
Problem: The child element of the flex container is not being centered on IOS Safari and Chrome Mobile. It seems like the address bar is throwing off the alignment for some reason. Can anyone offer any insight or help me with a way to debug the issue? I cannot replicate the problem in chrome dev tools.
Any help appreciated. Thank you community.
.logo-flexcontainer {
align-items: center;
display: flex;
height: 100vh;
margin: 0 auto;
width: 90%;
#media (min-width: 1024px) {
width: 50%;
}
}
.logo-flexcontainer > div {
flex: 1 0 auto;
}
#mobile-navbar {
display: flex;
align-items: center;
position: fixed; /* Set the navbar to fixed position */
width: 100%; /* Full width */
height: 50px;
top: 0; /* Position the navbar at the top of the page */
transition: transform .1s ease, background-color .5s ease;
&.hidden {
transform: translate3d(0,-100%,0);
}
&.scroll-background {
background-color: rgba(0, 0, 0, 0.8)
}
#media (min-width: 1024px) {
display: none;
}
#page-title {
margin: 5px 0 0 5%;
h3 {
font-size: 6vw;
text-shadow: 1px 1px #000;
#media (min-width: 600px) {
font-size: 2rem;
}
a {
text-decoration: none;
}
}
}
#hamburger {
position: absolute;
width: 50px;
height: 50px;
top: 5px;
right: 10px;
cursor: pointer;
span {
display: block;
position: absolute;
height: 6px;
width: 100%;
background: #FFFFFF;
box-shadow: 1px 1px #000;
&:nth-child(1) {
top: 6px;
}
&:nth-child(2) {
top: 18px;
}
&:nth-child(3) {
top: 30px;
}
}
}
}
footer {
position: absolute;
text-align: center;
left: 0;
bottom: 0;
height: 100px;
width: 100%;
svg {
height: 90%;
#media (max-width: 550px) {
height: 80%;
}
}
}
<div class="logo-flexcontainer">
<div>
<svg id="logo" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 598.6 323.5">
<g>svg code goes here...</g>
</svg>
</div>
</div>
<footer>
social icons here, code not important
</footer>
Here is a live link to the project here: project link See if you can replicate the problem on your Android or iPhone device.
Two pictures of homepage on chrome mobile and firefox mobile.
The left is the incorrect orientation and the right is the expected result.
I've been reading a lot and I now realize this is a well documented issue. My initial instinct was correct, chrome mobile address bar adds 60px to the document flow. When you create an element with 100vh, it actually extends 60px past the bottom of the screen of the phone. To me it appears to be a bug but the chrome dev team considers it a feature. Calculating Viewport Height on Chrome Android with CSS
Here is Google's official statement on this: https://developers.google.com/web/updates/2016/12/url-bar-resizing
My apologies for posting this question in lieu of this new information.
I'm required to make sure my nav bar remains on the right of my header such that the user utilises the horizontal scroll bar to access the navigation options which aren't visible.
Currently I've got 5 elements in my nested and once the user resizes the browser the nav menu jumps/moves down.
See fiddle here
HTML:
<header> <!-- Navigation menu bar -->
<nav>
<ul> <!-- Navigation menu bar options. These are fixed in terms of content. -->
<li>My Profile</li>
<li>Log Out</li>
<li>FAQs</li>
<li>Extras</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
CSS:
header {
background-color: #3366FF;
width: 100%;
height: 86px;
position: fixed;
top: 0;
left: 0;
z-index: 100;
opacity: 0.90%;
}
#logo {
margin: 0px;
float: left;
width: 200px;
height: 86px;
background: url("../images/logo.png") no-repeat center;
}
nav {
float: right;
padding: 20px;
}
#menu-icon {
display: hidden;
width: 100px;
height: 86px;
background: url(http://www.w3newbie.com/wp-content/uploads/icon.png);
padding: 0;
margin: 0;
}
a:hover#menu-icon {
border-radius: 2px 2px 0 0;
}
ul {
list-style: none;
}
nav ul li {
text-align: center;
display: inline-block;
padding: 10px;
}
nav ul li a:hover {
color: #363636;
text-decoration: none;
}
I would just put a min-width on your header element, stopping it from ever becoming too narrow for the list elements to fit, yet retaining the flexibility.
I tried min-width:660px; and it seemed to work fine to me.
header {
background-color: #3366FF;
width: 100%;
min-width: 660px;
height: 86px;
position: fixed;
top: 0;
left: 0;
z-index: 100;
opacity: 0.90%;
}
ALTERNATELY you can change your nav css to
nav {
text-align:right;
padding: 20px;
}
..And then change your header's "height" to "min-height" instead.
You have to remove the natural flexibility of the menu by adding a specific width. That way, regardless of the size of the browser window, the menu will remain the width you want it to be and the user will have to scroll to see it.
You can use JS to dynamically do this by setting the width of the header to be a certain amount of pixels, for e.g., greater than whatever is the width of the window upon loading of the document and resizing of the window.
You could use CSS Table display to sort the layout in the header and stop the menu from wrapping underneath. (added bonus: this avoids floats altogether)
header {
display: table;
}
header #logo, header nav {
display: table-cell;
border:1px solid red;
}
https://jsfiddle.net/S5bKq/1277/
As for the responsive menu itself - a quick internet search for 'responsive menus' will net you several examples/tutorials.