How to bring content/element down when using position fixed - html

How do I move the content down when using position fixed on my nav-bar? My page goes to the top since I use position:fixed. I can use margin-bottom, but then my navbar isn't exactly at the top of the screen. (yes I have margin:0 and padding:0 inside *).
Link: https://codepen.io/Altinzzz/pen/ZEWawxV
Code
html
<div class="topnav" id="myTopnav">
Home
About Us
Contact
About
</div>
<div class = "introduction">
<span class = "introductionText"> Welcome </span>
<span class = "introductionText2"> About us </span>
</div>
css
*{
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.introduction{
text-align:center;
padding: 0;
}
.introduction span{
display: block;
animation-fill-mode: forwards;
}
.introductionText{
font-size: 80px;
background: white;
color:black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2{
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}

When you use position:fixed it takes the element out of the flow. Therefore the rest of the page starts at the top as if the fixed element doesn't even exist. This means that applying margin or any other CSS to it won't affect any other elements.
Instead you need to add space for it on the rest of the page, e.g. add padding or margin to the top of the next element, e.g.
.introduction{
text-align:center;
padding: 50px 0 0;
}
For easier maintenance, it might be better to add outer container for all pages to add this space, especially if the first element in each page is not the same e.g.
.page-container{
padding-top: 50px;
}
And the HTML would be something like:
<div class="topnav" id="myTopnav"> ... </div>
<div class="page-container">
... whatever elements you have...
</div>
Considerations for Responsive Design: Note that if the nav wraps, then a fixed padding won't be enough - in this case you might need to look at media queries or using the hamburger menu to keep the nav bar the same height to overcome this issue.
Working Example:
* {
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.page-container {
padding-top: 50px;
}
.introduction {
text-align: center;
padding: 0;
}
.introduction span {
display: block;
animation-fill-mode: forwards;
}
.introductionText {
font-size: 80px;
background: white;
color: black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2 {
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}
.contentforscroll {
background: lightblue;
height: 800px;
}
<div class="topnav" id="myTopnav">
Home
About Us
Contact
About
</div>
<div class="page-container">
<div class="introduction">
<span class="introductionText"> Welcome </span>
<span class="introductionText2"> About us </span>
</div>
<div class="contentforscroll">
<p>More content to make the page scroll so you see the fixed element</p>
</div>
</div>

add padding-top to introduction

Please give padding top to body (bigger than height of topnav)
And topnav should stick to top so top: 0 should be added;
Please check the following
*{
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
body {
padding-top: 50px;
}
.topnav {
overflow: hidden;
background-color: #312E2E;
position: fixed;
top: 0;
width: 100%;
z-index: 2;
float: left;
}
.topnav a {
float: left;
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: black;
color: white;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
#media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
#media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
.introduction{
text-align:center;
padding: 0;
}
.introduction span{
display: block;
animation-fill-mode: forwards;
}
.introductionText{
font-size: 80px;
background: white;
color:black;
letter-spacing: -50px;
font-weight: 400;
position: relative;
animation: text 3s 1;
}
.introductionText2{
font-size: 40px;
background: white;
font-weight: 400;
margin-bottom: 50px;
}
#keyframes text {
0%{
color: black;
margin-bottom: -80px;
letter-spacing: -10px;
}
25%{
margin-bottom: -80px;
letter-spacing: 25px;
}
80%{
letter-spacing: 0px;
}
100%{
letter-spacing: 0;
margin-bottom: -3px;
}
}
<div class="topnav" id="myTopnav">
Home
About Us
Contact
About
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<div class = "introduction">
<span class = "introductionText"> Welcome </span>
<span class = "introductionText2"> About us </span>
</div>

Thanks for the recommendations guys. All three are correct.
Adding padding-top: 50px; to .introduction worked, and so did adding padding-top: 50px; to the body and then putting top: 0;, inside topnav.

Related

Make sidebar responsive

I am working on a new site at the moment, and I'm wondering how I would go about making the sidebar responsive, meaning that:
it moves from the left to the top (not sticky);
the links turn into a hamburger icon;
when the hamburger icon is tapped, a full-screen menu appears (transparent colour), withthe available links centered on the screen, and an "X" to dismiss the menu in the top right;
both the hamburger icon and the logo are centered vertically in the nav, with the logo aligned to the left and the hamburger icon to the right;
Here's my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Maestoso Digital</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<nav id="nav">
<img id="logo" src="img/logo.png" alt="Maestoso Digital logo">
<ul id="nav-links">
<li id="active">Home</li>
<li>Hosting</li>
<li>Cloud Deployment</li>
<li>System Administration</li>
<li>Contact</li>
</ul>
<form action="customers.html">
<input id="customers-button" type="submit" value="Existing Customers" />
</form>
</nav>
<main>
<div id="header">
<img id="header-img" src="img/header.png" alt="Picture of data centre">
<h1 id="header-title">Welcome</h1>
</div>
<div id="content">
<h1>We are an internet service provider for the modern digital age!</h1>
<p>Maestoso Digital is an internet service provider offering bespoke, enterprise-grade hosting solutions and system administration services for all, but especially for musicians and musical organisations. Our numerous datacenters around the globe offer ultra-fast performance for whatever your needs may be, and our Network Operations Centre monitor your services 24/7 to ensure that they are always online.</p>
<footer>
<p>© Maestoso Digital. All rights reserved.</p>
</footer>
</div>
</main>
</body>
</html>
And here's my CSS:
#import url('https://fonts.googleapis.com/css2?family=Share+Tech&display=swap');
* {
box-sizing: border-box;
font-family: "Share Tech", sans-serif;
}
body {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #ed15ac;
}
a:hover {
font-weight: bold;
color: #ed1c51;
}
#content {
margin-top: 50px;
margin-left: 310px;
margin-right: 10px;
padding-bottom: 2.5rem;
}
p, ul, li {
font-size: 16pt;
}
#nav {
position: fixed;
display: flex;
flex-direction: column;
top: 0;
left: 0;
width: 250px;
height: 100%;
background-color: #dbdbdb;
align-items: center;
z-index: 10;
}
#media screen and (max-width: 700px) {
#nav {
width: 100%;
height: auto;
position: relative;
padding-bottom: 10px;
float: right;
}
#nav a {float: left;}
div#content {margin-left: 0; padding: 5px;}
}
#media screen and (max-width: 400px) {
#nav a {
text-align: center;
float: none;
}
}
nav img {
display: block;
margin-bottom: 35px;
padding-top: 20%;
max-width: 150px;
}
#nav-links {
list-style-type: none;
padding: 0;
margin: 0;
font-size: 16pt;
}
#nav-links:not(:last-child) {
margin-bottom: 50px;
}
p {
font-size: 16pt;
font-color: #1a1a1a;
}
h1 {
font-size: 24pt;
font-color: #000;
}
input {
background-color: #ed15ac;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
transition-duration: 0.4s;
}
input:hover {
background-color: #ed1c51;
color: white;
cursor: pointer;
}
#header {
position: relative;
}
#header-title {
position: absolute;
bottom: 5px;
left: 310px;
color: white;
opacity: 60%;
font-size: 120px;
text-shadow: 4px 4px 2px rgba(150, 150, 150, 1);
}
footer {
padding-top: 20px;
bottom: 0;
width: 100%;
height: 2.5rem;
text-align: center;
}
I have tried everything that I can think of, and just can't get it to work. Any suggestions would be hugely appreciated.
Yours, Antiquis
This is a very basic example of a toggleable menu, but you need at least a little bit of javascript to toggle an .open class on your navigation container. The .open adds display: block, while by default in the mobile css it's display:none so the menu is hidden at first.
Or if you want to animate the appearance of the menu you'd be toggling the visibility and height instead for example - there are different implementations for this, I suggest inspecting other website's menu codes at a small viewport size to see how they've implemented their mobile navs for ideas.
I have not put these styles inside media queries in my example so you can clearly see it in action, and not have to resize the window.
function navToggle() {
document.getElementById('nav-links').classList.toggle("open");
}
#import url('https://fonts.googleapis.com/css2?family=Share+Tech&display=swap');
* {
box-sizing: border-box;
font-family: "Share Tech", sans-serif;
}
body {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #ed15ac;
}
a:hover {
font-weight: bold;
color: #ed1c51;
}
#content {
margin-top: 50px;
margin-left: 310px;
margin-right: 10px;
padding-bottom: 2.5rem;
}
p,
ul,
li {
font-size: 16pt;
}
#nav {
position: fixed;
display: flex;
flex-direction: column;
top: 0;
left: 0;
width: 250px;
height: 100%;
background-color: #dbdbdb;
align-items: center;
z-index: 10;
}
nav img {
display: block;
margin-bottom: 35px;
padding-top: 20%;
max-width: 150px;
}
#nav-links {
list-style-type: none;
padding: 0;
margin: 0;
font-size: 16pt;
}
#nav-links:not(:last-child) {
margin-bottom: 50px;
}
p {
font-size: 16pt;
font-color: #1a1a1a;
}
h1 {
font-size: 24pt;
font-color: #000;
}
input {
background-color: #ed15ac;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
transition-duration: 0.4s;
}
input:hover {
background-color: #ed1c51;
color: white;
cursor: pointer;
}
#header {
position: relative;
}
#header-title {
position: absolute;
bottom: 5px;
left: 310px;
color: white;
opacity: 60%;
font-size: 120px;
text-shadow: 4px 4px 2px rgba(150, 150, 150, 1);
}
footer {
padding-top: 20px;
bottom: 0;
width: 100%;
height: 2.5rem;
text-align: center;
}
/*for purposes of demonstration, normally you stick this code in a media query, hide the hamburger toggle on desktop! */
#nav-links {
display: none;
}
#nav-links.open {
display: block;
}
#media screen and (max-width: 700px) {
#nav {
width: 100%;
height: auto;
position: relative;
padding-bottom: 10px;
float: right;
}
#nav a {
float: left;
}
div#content {
margin-left: 0;
padding: 5px;
}
}
#media screen and (max-width: 400px) {
#nav a {
text-align: center;
float: none;
}
}
<nav id="nav">
<img id="logo" src="img/logo.png" alt="Maestoso Digital logo">
<div class="hamburger-cont"><button id="hamburger-toggle" onclick="navToggle()">☰</button></div>
<ul id="nav-links">
<li id="active">Home</li>
<li>Hosting</li>
<li>Cloud Deployment</li>
<li>System Administration</li>
<li>Contact</li>
</ul>
<form action="customers.html">
<input id="customers-button" type="submit" value="Existing Customers" />
</form>
</nav>
<main>
<div id="header">
<img id="header-img" src="img/header.png" alt="Picture of data centre">
<h1 id="header-title">Welcome</h1>
</div>
<div id="content">
<h1>We are an internet service provider for the modern digital age!</h1>
<p>Maestoso Digital is an internet service provider offering bespoke, enterprise-grade hosting solutions and system administration services for all, but especially for musicians and musical organisations. Our numerous datacenters around the globe offer
ultra-fast performance for whatever your needs may be, and our Network Operations Centre monitor your services 24/7 to ensure that they are always online.</p>
<footer>
<p>© Maestoso Digital. All rights reserved.</p>
</footer>
</div>
</main>
#import url('https://fonts.googleapis.com/css2?family=Share+Tech&display=swap');
* {
box-sizing: border-box;
font-family: "Share Tech", sans-serif;
}
body {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #ed15ac;
}
a:hover {
font-weight: bold;
color: #ed1c51;
}
#content {
margin-top: 50px;
margin-left: 310px;
margin-right: 10px;
padding-bottom: 2.5rem;
}
p,
ul,
li {
font-size: 16pt;
}
#nav {
position: fixed;
display: flex;
flex-direction: column;
top: 0;
left: 0;
width: 250px;
height: 100%;
background-color: #dbdbdb;
align-items: center;
z-index: 10;
}
.nav-links {
position: relative;
top: 25%;
width: 100%;
text-align: center;
margin-top: 30px;
}
.nav-links li {
padding: 8px;
text-decoration: none;
font-size: 36px;
color: #818181;
display: block;
transition: 0.3s;
}
.navigation a:hover,
.navigation a:focus {
color: #f1f1f1;
}
#hamburger {
display: none;
}
.navigation .closebtn {
display: none;
position: absolute;
top: 20px;
right: 45px;
font-size: 60px;
}
#media screen and (max-width: 700px) {
#nav {
width: 100%;
height: auto;
position: relative;
padding-bottom: 10px;
float: right;
}
#nav a {
float: left;
}
div#content {
margin-left: 0;
padding: 5px;
}
}
#media screen and (max-width: 400px) {
#hamburger {
display: block;
}
.navigation {
height: 100%;
width: 100%;
display: none;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.9);
}
#nav a {
text-align: center;
float: none;
}
.navigation a {
font-size: 20px
}
.navigation .closebtn {
display: block;
font-size: 40px;
top: 15px;
right: 35px;
}
li {
padding: 10px;
}
#nav-links {
text-align: center;
margin-top: 40%;
}
#header {
position: relative;
top: 167px;
left: -303px;
}
}
nav img {
display: block;
margin-bottom: 35px;
padding-top: 20%;
max-width: 150px;
}
#nav-links {
list-style-type: none;
padding: 0;
margin: 0;
font-size: 16pt;
}
#nav-links:not(:last-child) {
margin-bottom: 50px;
}
p {
font-size: 16pt;
color: #1a1a1a;
}
h1 {
font-size: 24pt;
color: #000;
}
input {
background-color: #ed15ac;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
transition-duration: 0.4s;
}
input:hover {
background-color: #ed1c51;
color: white;
cursor: pointer;
}
#header {
position: relative;
}
#header-title {
position: absolute;
bottom: 5px;
left: 310px;
color: white;
opacity: 60%;
font-size: 95px;
text-shadow: 4px 4px 2px rgba(150, 150, 150, 1);
}
footer {
padding-top: 20px;
bottom: 0;
width: 100%;
height: 2.5rem;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Maestoso Digital</title>
<link rel="stylesheet" type="text/css" href="stack.css" />
</head>
<body>
<nav id="nav">
<img id="logo" src="img/logo.png" alt="Maestoso Digital logo">
<div id="myNav" class="navigation">
×
<ul id="nav-links">
<li id="active">Home</li>
<li>Hosting</li>
<li>Cloud Deployment</li>
<li>System Administration</li>
<li>Contact</li>
</ul>
</div>
<span style="font-size:30px;cursor:pointer" onclick="openNav()" id="hamburger">☰</span>
<form action="customers.html">
<input id="customers-button" type="submit" value="Existing Customers" />
</form>
</nav>
<main>
<div id="header">
<img id="header-img" src="img/header.png" alt="Picture of data centre">
<h1 id="header-title">Welcome</h1>
</div>
<div id="content">
<h1>We are an internet service provider for the modern digital age!</h1>
<p>Maestoso Digital is an internet service provider offering bespoke, enterprise-grade hosting solutions and system administration services for all, but especially for musicians and musical organisations. Our numerous datacenters around the globe offer
ultra-fast performance for whatever your needs may be, and our Network Operations Centre monitor your services 24/7 to ensure that they are always online.</p>
<footer>
<p>© Maestoso Digital. All rights reserved.</p>
</footer>
</div>
</main>
<script>
function openNav() {
document.getElementById("myNav").style.display = "block";
}
function closeNav() {
document.getElementById("myNav").style.display = "none";
}
</script>
</body>
</html>

My dropdown content won't show but it did before I moved my header

I had a simple website and everything worked perfectly but I wanted to make it more modern but I don't know what I changed that made my dropdown menu stop working.
I am new to coding btw and I use Visual Studio Code.
I haven't tried anything because I don't want to screw up my website.
My HTML and CSS code (see also jsfiddle):
<div class="header" id="header">
<span id="header-title">Nellfin Solutions</span>
<div class="dropdown">
<span id="menu">&#9776 Menu</span>
<div class="dropdown-content">
Home
Services
Products
About Us
Contact Us
</div>
</div>
</div>
#import url(http://fonts.googleapis.com/css?family=Monstserrat:400,700);
body {
background-image: url("backround.jpg");
background-repeat:no-repeat;
background-size:cover;
color: #555;
margin: 0;
font-family: 'Montserrat', sans-serif;
background-color: rgba(255, 255, 255, 0.979);
}
.header {
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
#header {
background-color: white;
height: 50px;
line-height: 50px;
position: fixed;
margin-top: 20px;
width: 80%;
transform: translate(12%, 0%)
}
#header-title {
float: right;
margin-right: 50px;
font-size: 20px;
color: orange;
}
.dropdown {
position: relative;
display: inline-block;
background-color: white;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #555;
min-width: 100px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
text-align: left;
overflow: auto;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
color: orange;
background-color: #6F6F6F;
}
#menu {
text-transform: uppercase;
letter-spacing: 0.1em;
margin: 10px;
margin-top: 0px;
color: orange;
font-weight: bold;
position: relative;
}
.body h1 {
position: relative;
padding-top: 100px;
padding-left: 200px;
text-transform: uppercase;
color: orange;
}
Because of the relative position in .body h1, it changes the z-index position. You can decrease the position of your .body h1 to below 0.
.body h1 {
z-index: -1;
}
Or you can remove the position for h1.
.body h1 {
position: relative(no need to use this css line)
}
Hover for dropdown doesn't work because you put body in front of it (you can use inspector to see that). so bring header to front by:
.header {
z-index: 1000;
}
Fiddle: fiddle

text stuckt to the header? [duplicate]

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
Closed 4 years ago.
I’m trying to move my text to the middle of my header on the image but as soon as I move it the whole header follows. Could someone try to help me solve this issue? As you see I’m trying to make it using margin-top but when I implement this the header follows. I have closed all the divs that affects the image.
html, body {
margin: 0;
padding: 0;
text-align: center;
width: 100%;
}
body {
font-family: 'Josefin Sans', sans-serif;
text-align: center;
}
header {
width: 100%;
height: 400px;
background: url(/assets/image/tjanst.jpg) no-repeat 50% 50%;
background-size: cover;
}
.content {
width: 94%;
margin: 4em auto;
font-size: 20px;
line-height: 30px;
text-align: justify;
}
.logo {
line-height: 60px;
position: fixed;
float: left;
margin: 16px 46px;
color: #fff;
font-weight: bold;
font-size: 20px;
letter-spacing: 2px;
}
nav {
position: fixed;
width: 100%;
line-height: 60px;
z-index: 10;
}
nav ul {
line-height: 60px;
list-style: none;
background: rgba(0, 0, 0, 0);
overflow: hidden;
color: #fff;
text-align: right;
margin: 0;
padding-right: 40px;
transition: 1s;
}
nav.black ul {
background: #000;
padding: 16px 40px;
}
nav ul li {
display: inline-block;
padding: 16px 40px;
}
.move-down h3{
margin-top: 200px;
max-width: 400px;
}
nav ul li a {
font-family: 'Josefin Sans', sans-serif;
font-size: 14px;
color: #fff;
font-weight: 600;
text-transform: uppercase;
text-decoration: none;
}
a {
color: #fff;
text-decoration: none;
}
.menu-icon {
line-height: 60px;
width: 100%;
background: #000;
text-align: right;
box-sizing: border-box;
padding: 15px 24px;
cursor: pointer;
color: #fff;
display: none;
}
#media(max-width: 786px) {
.logo {
position: fixed;
top: 0;
margin-top: 16px;
}
nav ul {
max-height: 20px;
background: #000;
}
nav.black ul {
background: #000;
}
.showing {
max-height: 34em;
}
nav ul li {
box-sizing: border-box;
width: 100%;
padding: 24px;
text-align: center;
}
.menu-icon {
display: block;
}
}
<header>
<nav>
<div class="menu-icon">
<i class="fa fa-bars fa-2x"></i>
</div>
<div class="logo wow tada">
</div>
<div class="menu">
<ul>
<li>START</li>
<li>TJÄNSTER</li>
<li>OM OSS</li>
<li>KONTAKT</li>
</ul>
</div>
</nav>
<div class="move-down wow fadeInUp"><h3> VILL GÖRA KUNDEN NÖJD</h3>
<h3> tel: 070719763 </h3></div>
</header>
how it looks now
https://i.stack.imgur.com/vOQGa.png
Well remove 'margin-top: 200px;' in the '.move-down h3' and add 'padding: 200px 0px 0px 0px;' or 'padding-top: 200px;'
Yes, Simple solution is.
Add this CSS
h3{
maring:0;
}
Hope this helps.
Instead of adding margin-top add top value to relatively positioned element.
.move-down h3 {
max-width: 400px;
position: relative;
top: 205px;
}
https://jsfiddle.net/6uedrb89/12/

How do I left align text in a menu and keep the text visible?

I want to left align text in a drop-down menu but I'm having some problems. I have this HTML
<nav>
<a>Vote</a>
<a>Search</a>
<a>About</a>
</nav>
and this CSS for the drop down menu
nav {
display: none;
width: 30rem;
padding: 0rem;
background-color: red;
position: absolute;
right: 0%;
top: 100%;
text-align: left;
}
.nav-open {
display: block;
}
nav a {
display: block;
width: 100%;
text-align: left;
padding: 1.4rem 1.6rem;
text-decoration: none;
cursor: pointer;
font-size: 1.2rem;
color: #000;
}
But as you can see, when you click the menu icon the text isn't even visible. Interestingly, when I change:
text-align: left;
to
text-align: center;
I can see the text again, but it is not aligned where i want it. How do I left align my text and keep it visible?
$('.menu-btn').click(function() {
$('nav').toggleClass('nav-open');
});
style* {
margin: 0 auto;
font-family: sans-serif;
}
body {
margin: 0 auto;
font-family: Benton Sans, -apple-system, BlinkMacSystemFont, Roboto, Helvetica Neue, Helvetica, Tahoma, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
}
header {
width: 100%;
background-color: orange;
text-align: center;
position: relative;
}
#pageTitle {
display: flex;
padding: 10px;
}
#pageTitle h2 {
justify-content: center;
/* align horizontal */
align-items: center;
width: 95%;
}
.menu-btn {
position: absolute;
display: inline-block;
cursor: pointer;
}
.menu-btn div {
position: absolute;
left: 100%;
top: 0%;
padding-right: 8px;
margin-top: -0.50em;
line-height: 1.2;
font-weight: 200;
vertical-align: middle;
z-index: 99;
}
.menu-btn span {
display: block;
width: 20px;
height: 2px;
margin: 4px 0;
background: #989da1;
z-index: 99;
}
nav {
display: none;
width: 30rem;
padding: 0rem;
background-color: red;
position: absolute;
right: 0%;
top: 100%;
text-align: left;
}
.nav-open {
display: block;
}
nav a {
display: block;
width: 100%;
text-align: left;
padding: 1.4rem 1.6rem;
text-decoration: none;
cursor: pointer;
font-size: 1.2rem;
color: #000;
}
nav a:hover {
background-color: #111;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div id="pageTitle">
<h2>Page Title</h2>
<div class="mobile-nav">
<button class="menu-btn" id="menu-btn">
<div></div>
<span></span>
<span></span>
<span></span>
</button>
<nav>
<a>Vote</a>
<a>Search</a>
<a>About</a>
</nav>
</div>
</div>
</header>
The problem comes from the rem unit you are using when giving width to your nav. You should use vw viewport width. This is relative from the screen width going from 0 to 100 turning the viewport width into a percentage.
Hope this helps
$('.menu-btn').click(function() {
$('nav').toggleClass('nav-open');
});
style* {
margin: 0 auto;
font-family: sans-serif;
}
body {
margin: 0 auto;
font-family: Benton Sans, -apple-system, BlinkMacSystemFont, Roboto, Helvetica Neue, Helvetica, Tahoma, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
}
header {
width: 100%;
background-color: orange;
text-align: center;
position: relative;
}
#pageTitle {
display: flex;
padding: 10px;
}
#pageTitle h2 {
justify-content: center;
/* align horizontal */
align-items: center;
width: 95%;
}
.menu-btn {
position: absolute;
display: inline-block;
cursor: pointer;
}
.menu-btn div {
position: absolute;
left: 100%;
top: 0%;
padding-right: 8px;
margin-top: -0.50em;
line-height: 1.2;
font-weight: 200;
vertical-align: middle;
z-index: 99;
}
.menu-btn span {
display: block;
width: 20px;
height: 2px;
margin: 4px 0;
background: #989da1;
z-index: 99;
}
nav {
display: none;
width: 100vw;
padding: 0rem;
background-color: red;
position: absolute;
right: 0%;
top: 100%;
text-align: left;
}
.nav-open {
display: block;
}
nav a {
display: block;
width: 100%;
text-align: left;
padding: 1.4rem 1.6rem;
text-decoration: none;
cursor: pointer;
font-size: 1.2rem;
color: #000;
}
nav a:hover {
background-color: #111;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div id="pageTitle">
<h2>Page Title</h2>
<div class="mobile-nav">
<button class="menu-btn" id="menu-btn">
<div></div>
<span></span>
<span></span>
<span></span>
</button>
<nav>
<a>Vote</a>
<a>Search</a>
<a>About</a>
</nav>
</div>
</div>
</header>
As others have pointed out already, the text is actually aligned left, but your screen size might prevent it from showing up due to the big width of your menu.
Try changing the width of your nav element to something relative to the page width, like 80%:
nav {
display: none;
width: 80%;
padding: 0rem;
background-color: red;
position: absolute;
right: 0%;
top: 100%;
text-align: left;
}
https://jsfiddle.net/1y8n08aq/1/

Why won't the z-index on my elements work?

I have my code set up so I have the hero image at the bottom and the overlay on top with the text and button in overlay. I also have the navigation bar with a z-index but for some reason the button for my resume in overlay isn't working.
HTML
<div id="header">
<a href="index.html"><div id="leftHeader">
<img src="assets/logo2.jpg" alt="Logo" style="width:65px;height:65px">
<h1>Amanda Farrington</h1>
</div>
<div id="nav">
<ul>
<li>About</li>
<li>Work</li>
<li>Contact</li>
<li>Notes</li>
</ul>
</div>
</div>
<div id="hero">
<div id="heroImage">
<img src="assets/trees.jpg" alt="trees" style="width:100%;height:10%">
</div>
<div id="overlay">
<h2>Amanda Farrington</h2>
<h3>Graphic Artist | Web Designer</h3>
View Resume
</div>
</div>
CSS
#header {
color: #D7DADB;
font-family: 'Roboto', sans-serif;
font-weight: 300;
font-size : 15px;
text-align: left;
width: 100%;
padding-left: 3em;
position: relative;
height: 15%;
box-sizing: border-box;
padding-top: 1em;
}
#header img
{
float: left;
padding-left: 3em;
}
h1{
width: 9em;
float: left;
padding-left: 0.5em;
color: #45CCCC;
padding-bottom: 1px;
}
#nav {
width: 50%;
margin:0;
padding:0;
text-align: right;
color: red;
font-size:20px;
float: right;
padding-right: 2em;
z-index: 99;
}
#nav ul {
padding: 1px;
}
#nav li {
display: inline;
padding: 38px;
}
#nav li a {
color: #2C3E50;
text-decoration: none;
}
#nav li a:hover {
color: #45CCCC;
}
/*----------hero image styles-------------*/
#hero{
padding-top: 25em;
width: 100%;
height: 30em;
position: relative;
z-index: -1;
}
#heroImage
{
top: 9%;
width: 100%;
z-index: 1;
position: absolute;
}
#overlay{
width: 34em;
top: -15%;
margin-left: 30%;
z-index: 2;
position: relative;
clear: left;
}
h2{
width: 100%;
position: relative;
font-family: 'Roboto', sans-serif;
font-weight: 300;
font-size: 60px;
float: center;
color: white;
opacity: 1.0;
text-shadow: 2px 2px 3px #000000;
text-align: center;
}
h3{
width: 100%;
position: relative;
font-family: 'Roboto', sans-serif;
font-weight: 300;
font-size: 30px;
color: #e5e5e5;
opacity: 1.0;
text-shadow: 2px 3px 2px #000000;
text-align: center;
}
a.down{
z-index: 100;
font-family: 'Roboto', sans-serif;
font-weight: 400;
text-decoration: none;
color: #181b1e;
background: #45CCCC;
position: relative;
padding: 0.6em 0.2em;
font-size: 1.2em;
-webkit-border-radius: 6px;
width: 30%;
position: relative;
display: block;
margin-left: auto;
margin-right: auto;
text-align: center;
}
a.down:hover{
text-decoration: underline;
color: white;
}
Because z-index works only on elements which are NOT set asposition: static. Bear in mind that every element is set as default to position:static.
Try set to position:absolute; or relative your element.
Also all other types of positioning, like position:fixed, position:sticky.
So I've taken a look at your code and the reason your button doesn’t work is because the div with the ID of #hero (which contains the button) is below the body because it has a z-index of -1.
Set the z-index for #hero to 0 or higher and the button will work.
#hero {
padding-top: 25em;
width: 100%;
height: 30em;
position: relative;
z-index: 0;
}
Check out this JS Fiddle I've created for you:
https://jsfiddle.net/8fqwr6ca/
Edit: Oh, and I forgot to mention–since you want the image to be below, set the #hero 's z-index to 1, set #heroImage to 0, and overlay to 2. That should do the trick (if what I think you want is correct).