I need help with the code of a project I've been working on. I can't make my navigation bar fixed so it always appears on the top of the viewport. I understand the rules of CSS position and I've been looking at examples and tutorials, but for some reason, I'm stuck.
I tried to make the position fixed for the navbar and relative to the container, along many other changes. Every thing I tried, it messes up my content. I guess this is one of those moments when you need help.
This is the link to the project and the code of the navbar without any position rules.
https://codepen.io/aitormorgado/pen/MWayXPy
#title-wrapper {
display: flex;
justify-content: center;
align-items: center;
font-family: "Aclonica", serif;
color: #281c1c;
font-size: 6rem;
text-transform: uppercase;
margin: 3rem 1rem;
}
li {
list-style: none;
}
#header-img {
width: 6rem;
padding-right: 1.5rem;
}
#nav-bar ul {
display: flex;
flex-direction: column;
align-items: center;
font-family: "Libre Baskerville", serif;
background-color: #990000;
color: #e0e0e0;
border-top: 1px solid black;
border-bottom: 1px solid black;
text-transform: uppercase;
font-size: 4rem;
}
#nav-bar ul li {
border: 2px solid black;
width: 100%;
text-align: center;
}
#nav-bar ul li a {
text-decoration: none;
color: inherit;
padding: 1.4rem;
display: block;
}
#nav-bar ul li:hover {
background: #cc3300;
}
A million thanks for your help!
as per your question, I just chnaged a small thing in your code and now your header is fixed at the top while scrolling,
all you need to do is use of
#header {
position: sticky;
top: 0;
z-index: 99999;
background-color: #E0E0E0;
}
on your header. and the background color is given so that rest of the bottom elements will not appear below it while scrolling,
If need something else, or I have not understood your question, then feel free to share.
just check the codepen here.
Related
I have almost finished my web dashboard which can be seen here. Here is the source code. The last few bits I would like to improve are the mobile adaptation of the navbar and the footer. I'm I struggling to position elements properly, and on some devices, it looks worse than on others. I'm not the best when it comes to CSS yet, so I need a little bit of help here.
Few issues I have is the title not being positioned correctly:
.
I would like it to be positioned so the distance between the top and bottom is equal. And I want it to be at the same distance from the left as the burger button is from the right.
Same problem with the footer. it doesn't look very well organized:
or even worse:
I want these elements to be on an equal distance between top and bottom and never overlap, preferably all in one line. I'm sure there is a number of solutions here, but any solution that is simple and makes it looks more organized will be appraciated.
One thing I need to mention is that HTML elements are defined in Python code in Plotly Dash environment, but I'm pretty sure it makes no difference.
I'm attaching some of the Plotly HTML and CSS code here, but the full code is here:
HTML Code of the navbar:
app.layout = html.Div([
html.Nav([
html.Div("Covid-19 global data Dashboard", className="dashboard-title"),
html.A(
id="toggle-button",
children=[
html.Span(className="bar"),
html.Span(className="bar"),
html.Span(className="bar"),
],
href="#",
className="toggle-button"),
html.Div(
id="navbar-links",
children=html.Ul(
children=[
html.Li(html.A("Home", href=homeURL)),
html.Li(html.A('Source Code', href=sourceCodeURL)),
html.Li(html.A("CSV Data", href=sourceDataURL))]),
className="navbar-links active"
)]
HTML Code of the footer:
html.Footer([
html.Div("created by Sebastian Meckovski", id='footer-text'),
html.Div([
html.P(['Find Me On:'], id='find-me-on'),
html.A([html.Img(src=app.get_asset_url('linkedInLogo.png'), style={'height': '2rem'})],
href=linkedInURL),
html.A([html.Img(src=app.get_asset_url('facebookLogo.png'), style={'height': '2rem'})],
href=facebookURL)
], id='footer-links')
CSS Desktop view:
body {
background-color: var(--LightBlue);
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background-color: var(--DarkBlue);
color: white;
}
.container {
position: relative;
min-height: 100% ;
}
.dashboard-title{
font-size: 1.2rem;
margin: .5rem;
}
.navbar-links ul{
margin: 0;
padding: 0;
display: flex;
}
.navbar-links li {
list-style: none;
}
.navbar-links li a{
text-decoration: none;
color: white;
padding: 1.5rem;
display: block;
}
.navbar-links li a:hover{
background-color: var(--DarkBlueHover);
}
.toggle-button{
position: absolute;
top: .8rem;
right: 1rem;
display: none;
flex-direction: column;
justify-content: space-between;
width: 30px;
height: 21px;
}
.toggle-button .bar{
height: 3px;
width: 100%;
background-color: white;
border-radius: 10px;
}
#footer {
position: absolute;
display: flex;
justify-content: space-between;
align-items: center;
bottom: 0;
width: 100%;
background: var(--DarkBlue);
color: white;
height: 3.5rem;
}
#footer-links{
display: flex;
}
#find-me-on{
padding-right: 20px;
font-size: .8rem;
}
#footer-text {
margin: .5rem;
font-size: .8rem;
}
Mobile View:
#media only screen and (max-width: 500px) {
.dashboard-title {
font-size: 1rem;
padding-right: 80px;
}
.toggle-button {
display: flex;
}
.navbar-links {
display: flex;
width: 100%;
}
.navbar{
align-items: center;
flex-direction: column;
min-height:45px;
}
.navbar-links ul {
width: 100%;
flex-direction: column;
}
.navbar-links li{
text-align: center;
}
.navbar-links li a{
padding: .5rem 1rem;
}
.navbar-links.active {
display: none;
}
H2{
font-size: 15px;
}
#footer-text {
margin: .5rem;
font-size: .8rem;
}
#find-me-on{
padding-right: 15px;
}
}
For .navbar class set justify-content property to center, because if you set flex-direction to column, it "rotates" view object, so align-items starts working horizontally.
so:
.navbar{
justify-content: center;
flex-direction: column;
min-height: 45px;
}
And now for navbar horizontal centering: because you set .toggle-button to be absolute, it is set 1rem from right side, while .dashboard-title is centered inside of its parent. To fix this you can simply change margin inside of .navbar class to 1rem, it's now at 0.5rem; Also make sure that .navbar is not centered horizontally by its parent.
After these corrections navbar looks like this:
And for the footer it's also flexbox case. Your images are inside of a element, so you have to vertically center a content.
Image below is only in preview debugging purposes, set your CSS where you previously did.
a{
display: flex;
align-items: center; // Now all <a> element children are centered vertically.
}
I'm fairly new to coding and new to this website c:
I've been following the SheCodes courses and I'm currently doing the Responsive one because I want to move my coding career in that direction.
Anyways, I'm working on my own website to display my Illustration works and whatnot https://mariafloscher.netlify.app/ but the menu isn't hovering the way I want it, I want the whole section to look white and not only the area surrounding the words.
Here's the GitHub repository https://github.com/mariafloscher/cv-and-portfolio
Here's the HTML portion of the menu:
<div id="menu">
<div>homepage</div>
<div>about</div>
<div>work experience</div>
<div>works</div>
<div>commissions' & hiring info</div>
<div>contact</div>
</div>
And here's the CSS
#menu {
display: flex;
justify-content:space-evenly;
color: #f8f8f8;
background-color: #202020;
padding: 5px 0px 5px 0px;
height: fit-content;
}
.menu-item {
color: #f8f8f8;
background-color: #202020;
text-align: center;
display: inline-block;
text-decoration: none;
width: -moz-fit-content;
}
.menu-item:hover {
background-color: #f8f8f8;
color: #202020;
}
.menu-link {
color: #f8f8f8;
background-color: #202020;
text-align: center;
text-decoration: none;
}
.menu-link:hover {
background-color: #f8f8f8;
color: #202020;
text-decoration: none;
max-width: max-content;
}
Thanks in advance <3 Have a great day and a good start of the week :D!!
Move menu-item from a to div and in css
On menu item add flex: 1. That will make sure that all items are stretched to max available space
Add .menu-item a and .menu-item:hover a to handle menu items properly
Side note:
You can add column-gap: 10px to #menu or padding: 0 5px on .menu-item in order add more space between elements. Use both and you will see the difference.
Here is full example
#menu {
display: flex;
justify-content: space-evenly;
color: #f8f8f8;
background-color: #202020;
height: fit-content;
}
.menu-item {
flex: 1;
background-color: #202020;
padding: 5px;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
.menu-item a {
color: #f8f8f8;
display: inline-block;
text-decoration: none;
}
.menu-item:hover {
background-color: #f8f8f8;
}
.menu-item:hover a {
color: #202020;
}
<div id="menu">
<div class="menu-item">homepage</div>
<div class="menu-item">about</div>
<div class="menu-item">work experience</div>
<div class="menu-item">works</div>
<div class="menu-item">commissions' & hiring info</div>
<div class="menu-item">contact</div>
</div>
First of all I think you don't need the <div> tag around your <a> tags.
<div id="menu">
homepage
about
work experience
works
commissions' & hiring info
contact
</div>
If you want to see hover animation over the whole menu-item, you have to remove padding form #menu and add it instead to .menu-item. Also use flex: 1 1 auto; if you using display: flex; for #menu if you.
#menu {
display: flex;
justify-content:space-evenly;
color: #f8f8f8;
background-color: #202020;
height: fit-content;
}
.menu-item {
flex: 1 1 auto;
padding: 5px 0;
color: #f8f8f8;
background-color: #202020;
text-align: center;
display: inline-block;
text-decoration: none;
width: -moz-fit-content;
}
.menu-item:hover {
background-color: #f8f8f8;
color: #202020;
}
.menu-link {
color: #f8f8f8;
background-color: #202020;
text-align: center;
text-decoration: none;
}
.menu-link:hover {
background-color: #f8f8f8;
color: #202020;
text-decoration: none;
max-width: max-content;
}
I'm not the greatest with HTML, but I'm also not the worse, as in I know how to locate and change things with instructions, but that's about it.
Is anyone able to help me please?
I am trying to get these two menu's separated/spaced out, ideally the main menu links to the left, and the social media links to the right, I have included two images below that hopefully explains what I'm looking to achieve.
This is how it looks on my site
This is how I want it to look
Hopefully I have put the right bit of code below, as I initially said I'm not great with HTML at all.
# 7. Navigation
-----------------------------------------------*/
#social-menu .social-links a {
color: $(topmenu.text.color);
}
#social-menu .social-links a:hover {
color: $(topmenu.text.hover.color);
}
.menu-wrap {
font-family: 'Poppins', sans-serif;
font-weight: 400;
font-size: 12px;
text-transform: uppercase;
letter-spacing: 2px;
width: 100%;
z-index: 7;
word-break: break-word !important;
-webkit-font-smoothing: subpixel-antialiased;
text-align: center;
}
#menu-secondary {
max-width: 1180px;
margin: 0 auto;
border-top: 1px solid;
border-bottom: 1px solid;
}
#nav-secondary {
margin-bottom: 50px;
}
#nav-primary {
position: static;
height: 50px;
background: $(topmenu.bg.color);
}
.wrapper-nav-primary {
max-width: 92%;
margin: 0 auto;
position: fixed;
width: 100%;
background: $(topmenu.bg.color);
padding: 0 4%;
z-index: 999;
}
#menu-primary {
width: 70%;
float: left;
visibility: hidden;
}
#social-menu {
float: right;
margin-top: 18px;
}
.menu-wrap .widget {
display: inline-block;
}
.menu-wrap li {
display: inline-block;
position: relative;
}
.menu-wrap li,
.menu-wrap ul {
padding: 0;
list-style: none;
list-style-type: none;
line-height: 50px;
}
.menu-wrap li a {
margin: 0 30px;
font-weight: 400 !important;
}
#nav-primary li a {
color: $(topmenu.text.color);
}
#nav-primary li a:hover {
color: $(topmenu.text.hover.color);
}
Thank you in advance.
My guess is that you can add the following to the #social-menu. If you can supply the HTML implementation, and not only the CSS it will be easier to determine.
position: absolute;
right: 0px;
Thank you both again for your help, I feel very silly as the solution was simply going to the Blogger layout section and simply moving a box!
I spent the whole of yesterday trying to sort this, following various tutorials and guides changing the HTML and adding CSS haha.
The red HTML/JavaScript box in the primary menu section should be in the social media section below
I am currently a student learning HTML and CSS to become a web developer and i am having some trouble with getting my navigation buttons to properly display as the page window is resized. I want them to stack on top of one another like blocks but instead they overlap as you can see. Webpage Example
I'm sure i'm missing something obvious, but i would greatly appreciate the help.
Thanks!
body {font-family: verdana, arial, sans-serif;
background-color: #523925; }
header { text-align: center;
padding-top: 50px;
background-image: url(coffeemug.jpg);
height: 120px;
font-size: 400%;
font-family: verdana pro black;
font-weight: bold; }
nav { text-align: center;
margin-top: 20px;
max-height: 150px; }
nav a { text-align: center;
padding: 10px 70px;
border-style: solid; border-width: 4px; border-color: #16181D;
margin-left: 5px;
color:#523925;
font-size: 120%; }
footer { text-align: center;
margin-top: 20px;
font-size: small; }
#wrapper { width: 95%;
margin-left: auto;
margin-right: auto;
background-color: #EAB987; }
.charpic {max-width: 100%; }
#media only screen and (max-width: 1024px) {
nav a { font-size: 100%;
padding: 8px 50px; }
}
By default, a tags have display: inline, which lets them display side by side and fit as ends up not respecting margins very well. As noted in a previous answer, changing them to display: block will get them to respect margins, but prevent them from sitting side by side unless you add additional logic.
Probably the simplest change to get the behavior that I think you're looking for is to make them display: inline-block, which will continue to lay them out side by side but treat them as block elements that won't overlap as they end up wrapping. This would look like:
nav a {
text-align: center;
padding: 10px 70px;
border-style: solid; border-width: 4px; border-color: #16181D;
margin-left: 5px;
color:#523925;
display: inline-block;
font-size: 120%;
}
You could also do this by using flexbox, making nav a flex parent with display: flex and then using flex properties to determine layout. This would be a bit more of a change, but I think probably get you a nicer solution. As an example, if you wanted to have them overflow when necessary and be centered each line, you could do:
nav {
display: flex;
justify-content: center;
}
try adding display: block; to the a element, like so:
nav a { text-align: center;
padding: 10px 70px;
border-style: solid; border-width: 4px; border-color: #16181D;
margin-left: 5px;
color:#523925;
display: block;
font-size: 120%; }
I am a novice at this(css) and I am messing around trying to build a small website with html and css. I am having aa issue where on my page to the left I have my links to external pages. And in the middle of my page I have a paragraph in a section but this paragraph seems to interfere with my links. some of the links wont let me click on them.
This is my css, Can sombody tell me what I am doing wrong here please?
body {
color: white;
background-color: black;
font-family: Verdana, Geneva, sans-serif;
}
header {
border: 5px solid blue;
}
table, th, td {
border: 1px solid white;
}
section {
font-family: arial;
font-size: 15px;
text-align: center;
padding: 70px;
left: 0;
margin: auto;
margin-top: -100px;
position: absolute;
top: 50%;
width: 100%;
}
nav ul {
padding: 15px;
line-height: 3em;
list-style-type: none;
}
nav ul li a {
color: white;
color: rgba(255, 255, 255, 0.5);
text-decoration: none;
}
nav ul li:hover {
list-style-image: url(download.png);
}
nav ul li a:hover {
color: rgba(255, 255, 255, 1);
}
http://codepen.io/davidatthepark/pen/jVLNbv?editors=1100
Check out my link above. I put the borders in just so you can visualize what is going on. I'm sure you guessed it already but the section was covering the nav. A good way to put elements side by side is to use display: inline-block. Make sure to look up the display selector because it is crucial to writing CSS. All I did was make the width of the nav 30%, the section to 65% so that they don't flow over 100%, and then give both of them display: inline-block. Hope that makes sense!