Navbar is not in the center [duplicate] - html

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 1 year ago.
I have a problem with my navbar. I want to make a fully responsive website. For now, everything besides the navbar works. When I try to stretch the website, the content below the navbar stays at the center, but the navbar is moving to the left. I also want my navbar to be "fixed" and scroll with the page. I would like to see the logo to the left and menu items to the right, but not sure how can I do that. Can you help?
Talking about that page: https://fajferekpl.github.io/fjfr/
.grid {
display: grid;
grid-template-columns: repeat(8, 1fr);
gap: 10px;
}
.site-nav {
width: 100%;
position: fixed;
z-index: 99;
background-color: var(--background-color);
height: 5rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-logo {
grid-column: 1/4;
grid-row: 1;
color: var(--first-color);
padding: 20px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet"/>
<nav class="site-nav grid" id="header">
<div>.fjfr()</div>
<div class="nav-menu">
<ul>
<li>.home()</li>
<li>.about()</li>
<li>.portfolio()</li>
<li>.skills()</li>
<li>.contact()</li>
<li class="icon-navbar"><i class="fab fa-github"></i></li>
<li class="icon-navbar"><img src="assets/img/sun1.png" id="icon-sun"></li>
</ul>
</div>
<div class="hamburger-menu">
<div class="bar"></div>
</div>
</nav>

I used position: relative; on your .site-nav class to center the menu contents. Because a position: fixed; works similarly to absolute you can set top and left styles to align your fixed header into the center also.
.grid {
display: grid;
grid-template-columns: repeat(8, 1fr);
gap: 10px;
}
.site-nav {
width: 100%;
position: relative; /* Center navbar */
z-index: 99;
background-color: var(--background-color);
height: 5rem;
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-logo {
grid-column: 1/4;
grid-row: 1;
color: var(--first-color);
padding: 20px;
}
/* Fixed navbar on scroll changes */
.site-nav-colored {
width: 100%;
top: 0;
left: 22vw;
position: fixed;
z-index: 99;
background-color: var(--background-color);
height: 5rem;
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: 0 1px 4px var(--first-color);
}
<nav class="site-nav grid" id="header">
<div>.fjfr()</div>
<div class="nav-menu">
<ul>
<li>.home()</li>
<li>.about()</li>
<li>.portfolio()</li>
<li>.skills()</li>
<li>.contact()</li>
<li class="icon-navbar"><i class="fab fa-github"></i></li>
<li class="icon-navbar"><img src="assets/img/sun1.png" id="icon-sun"></li>
</ul>
</div>
<div class="hamburger-menu">
<div class="bar"></div>
</div>
</nav>

I believe it is just a positioning & width issue. I centered it with the following...
With fixed positioning, you have to be sure to tell it where to be positioned. Positioning it 50% from the left (aka 50% to the right), but then adding a transform: translate back 50% (aka another 50% to the left) will center it. This is because it moved it 50% from the far left corner of the navbar (not from the center of it), and then you have to move it 50% backward to make it true center.
.site-nav-colored {
width: 100%;
position: fixed;
z-index: 99;
background-color: var(--background-color);
height: 5rem;
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: 0 1px 4px
var(--first-color);
left: 50%;
transform: translate(-50%);
}
Also, the width of the nav bar is being affected by this section. It can only go up to 1060px.
media screen and (min-width: 950px)
.grid {
width: 100%;
max-width: 1060px;
margin: 0 auto;
}
So, if you want it to extend the full width of the screen then the nav would have to be outside of the div with class="grid" in your HTML.

Related

Layout where the logo is exactly in the center and two icons of different size are at the left and right [duplicate]

This question already has answers here:
Keep the middle item centered when side items have different widths
(12 answers)
Align 3 unequal blocks left, center and right
(4 answers)
Closed 8 months ago.
I'm trying to duplicate a very basic layout where the logo is exactly in the center and two icons of different size are at the left and right of the header bar.
What I need is (1) but what I get is (2):
.showcase header {
position: absolute;
top: 0;
left: 0;
width: 100%;
padding: 0px 0px;
z-index: 1000;
display: flex;
align-items: center;
justify-content: space-between;
}
<section class="showcase">
<header>
<div class="toggle">toggle</div>
<h2 class="logo">LOGO</h2>
<div class="booknow">averyverylongbooknow</div>
</header>
</section>
You could use CSS Grid along with Flexbox. What I did below is to give every column 1/3 of the parent's width, then position them as I want inside that given espace.
.showcase header {
position: absolute;
top: 0;
left: 0;
width: 100%;
z-index: 1000;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
align-items: center;
padding: 0 1rem;
box-sizing: border-box;
}
.logo {
display: flex;
justify-content: center;
}
.booknow {
display: flex;
justify-content: flex-end;
}
<section class="showcase">
<header>
<div class="toggle">toggle</div>
<h2 class="logo">LOGO</h2>
<div class="booknow">averyverylongbooknow</div>
</header>
</section>
I'd try:
header > * {
width: 33%;
text-align: center;
}
Please try this make you to Get a result
justify-content: space-around;
showcase header {
position: absolute;
top: 0;
left: 0;
width: 100%;
padding: 0px 0px;
z-index: 1000;
display: flex;
align-items: center;
justify-content: space-around;
}

Cannot align vertical elements

At first, the icons and label are centred correctly. But I need to make the anchor display: inline-block; to make the clickable area the full size of the grid it is in. Only the label itself is centred, while the icon is above the label.
I tried using vertical-align: middle; and align-items: centre;.
How can I make the icon and label vertically aligned?
#import url('https://fonts.googleapis.com/css2?family=Quicksand:wght#600&display=swap');
body {
display: grid;
grid-template-rows: 15% 70% 15%;
grid-auto-columns: 1fr 1fr;
gap: 0% 0%;
margin: 0;
padding: 0;
font-family: 'Quicksand', sans-serif;
background-color: rgb(224, 224, 224);
height: 100vh;
}
#header {
display: grid;
grid-template-columns: 25% 50% 25%;
grid-template-rows: 1fr;
gap: 0px 0px;
grid-auto-flow: row;
grid-template-areas: "back welcome logout";
grid-area: 1 / 1 / 2 / 2;
position: fixed;
width: 100%;
height: 15%;
background-color: rgb(255, 255, 255);
align-items: center;
text-align: center;
}
#back {
grid-area: back;
}
#welcome {
grid-area: welcome;
}
#logout {
grid-area: logout;
}
#content {
grid-area: 2 / 1 / 3 / 2;
}
#footer {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 1fr;
gap: 0px 0px;
grid-auto-flow: row;
grid-template-areas: "home contact";
grid-area: 3 / 1 / 4 / 2;
text-align: center;
position: fixed;
width: 100%;
bottom: 0;
height: 15%;
background-color: rgb(255, 255, 255);
}
#home {
grid-area: home;
}
#contact {
grid-area: contact;
}
/* centre alignment for icon and label */
#back,
#logout,
#home,
#contact {
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: rgb(58, 58, 255);
display: inline-block;
width: 100%;
height: 100%;
}
/* clickable size for icon and label */
#header a,
#header span,
#footer a,
#footer span {
font-size: 1.5em;
text-decoration: none;
}
/* hover for icon and label */
#header a:hover,
#footer a:hover {
background-color: rgb(216, 237, 255);
cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="/images/favicon.ico">
<link rel="stylesheet" href="https://fonts.sandbox.google.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD#20..48,100..700,0..1,-50..200" />
</head>
<body>
<div id="header">
<a href="#" id="back">
<span class="material-symbols-outlined">
arrow_back
</span><br/> Back
</a>
<h1 id="welcome">Timeline</h1>
<a href="#" id="logout">
<span class="material-symbols-outlined">
logout
</span><br/> Logout
</a>
</div>
<div id="content">
<p>hello</p>
</div>
<div id="footer">
<a href="#" id="home">
<span class="material-symbols-outlined">
home
</span><br> Home
</a>
<a href="#" id="contact">
<span class="material-symbols-outlined">
chat
</span><br> Contact
</a>
</div>
</body>
</html>
You should use flexbox when it comes to alignment of items. It's easy to use, and super useful when you completely control it.
You should take a look at the documentation here
First, you need to correct the HTML structure of the <a>s.
Remove the <br> tags and put the labels in their own <span>s.
<a href="#" id="home">
<span class="material-symbols-outlined">
home
</span>
<span>Home</span>
</a>
Now their styling is easier. The following properties are what you need to set for the links' rule:
Change display: inline-block to display: flex.
display: flex will make the <a>s hold the <span>s in a responsive container while assuming a block level element role that sits nicely in the width computed for it.
flex-direction: column will stack the <span>s vertically.
justify-content: center to center items vertically.
In vertical placement (i.e flex-direction: column), the align-items and justify-content properties get swapped because you are changing the main axis from horizontal to vertical. For this, we use justify-content: center rather than align-items: center to center the items vertically.
You may get rid of the other properties as they are not necessary and introduce problems and unneeded complexity.
The new rule set:
#back,
#logout,
#home,
#contact {
display: flex;
flex-direction: column;
justify-content: center;
width: 100%;
height: 100%;
color: rgb(58, 58, 255);
border: 1px solid;
}
Because the icons and labels are now in different <spans>, you can style them to take up full width and derive their height from their own content like this:
#back span,
#logout span,
#home span,
#contact span {
display: block;
width: 100%;
height: auto;
}
I also noticed that your header bar would change its height, getting shorter with screen resize while I opened dev tools, so you may correct this by setting a fixed height rather than a percentage height.
#header {
...
height: 100px;
...
}
Personally, I prefer to use flex when it is about trying to align vertically. Here you have an example on how to use it:
div {
margin-bottom: 10px
}
.vertical {
display: flex;
align-items: center;
height: 175px;
width: 175px;
background-color: yellow;
}
.horizontal {
display: flex;
justify-content: center;
height: 175px;
width: 175px;
background-color: red;
}
.both {
display: flex;
justify-content: center;
align-items: center;
height: 175px;
width: 175px;
background-color: blue;
color: white;
}
<div class="vertical">
vertical
</div>
<div class="horizontal">
horizontal
</div>
<div class="both">
horizontal and vertical
</div>

How to align the icons on the right to the center of the navigation bar?

I want to align the social media icon on the right in the center of the navigation bar along with the navigation link and logo.
I'm trying to do something like this
navigation bar
Link to code
You can create a wide div that is centered, create a div containing the icons, put it in the centerd div and then use in css float: right;
you can use something like that:
<style>
* {
padding: 0;
margin: 0;
}
.header {
width: 100%;
height: 60px;
background-color: black;
display: flex;
justify-content: center;
}
.centered {
width: 900px;
height: 60px;
background-color: red;
display: flex;
justify-content: center;
}
.text {
width: 300px;
height: 60px;
background-color: yellow;
}
.logo {
width: 200px;
height: 60px;
display: flex;
flex-direction: row;
position: absolute;
left: calc(50% + 260px);
}
.logo div {
height: 45px;
width: 45px;
border-radius: 7px;
background-color: yellow;
position: relative;
margin-top: 7px;
margin-left: 14px;
}
</style>
<div class="header">
<div class="centered">
<div class="text">
</div>
<div class="logo">
<div></div>
<div></div>
<div></div>
</div>
</div>
</div>
What you're trying to do is probably aligning your content to the vertical center.
To do that set your navbar display to grid and set grid-template-column according to the structure of the content within your navbar and also place all your social icons within a div that's a direct child of the navbar.
And lastly on the div containing your social-icons, set the properties;
.social_box{
display: flex;
justify-self: center;
align-content: space-around;
justify-content: center;
}

The Nav bar is positioned as fixed. The image below it moves to the upper right of nav bar overlapping it. The image should remain at the bottom

Image overlaps Flex Navbar[Image overlap NavBar]I have a Navbar that is fixed and a Flexbox. The image below called prestige appears to the right side overlapping the Navbar. The image needs to remain at the bottom of the page.
<div class="nav">
<img class="logo" src="images/food.png" alt="Food">
<div class="link">Home<div class="link">
<div class="link">Gallery<div class="link">
<div class="link">Services<div class="link">
<div class="link">Contact<div class="link">
</div>
<img class="cover" src="images/prestige.jpeg" alt="Prestige Food">
Need the prestige image to NOT overlap on top of Flexbox Navbar but expand the entire width of the page at the bottom.
.nav {
display: flex;
position: fixed;
top: 0px;
width: 100%;
padding: 0.1%;
z-index: 3;
}
.link {
display: flex;
align-items: flex-start;
justify-content: center;
height: 50px;
}
.nav a:link {
text-decoration: none;
color: blue;
margin-left: 1em;
margin-right: 1em;
}
.cover {
grid-column: 1 / 7;
grid-row: 3 / 5;
}`

Flexbox changes size after reload

I'm trying to create simple page header with a logo on the left-hand side and links divided evenly on the right-hand side. Like this render from Adobe Xd presenting expected header design. To do this I have written this code (snippet is missing the image and is a bit too narrow for this margin):
header {
margin: 0 200px;
height: 75px;
border-bottom: #707070 solid 2px;
display: flex;
flex-direction: row;
}
header .logo {
flex-grow: 2;
flex-basis: 0;
padding: 5px 0;
}
header nav {
flex-grow: 1;
flex-basis: 0;
padding-bottom: 20px;
display: flex;
justify-content: space-between;
}
header nav a {
align-self: flex-end;
}
<header>
<div class="logo">
<a href="home.html">
<img alt="Logo Hufca" src="logo_hufca_zlote.svg" />
</a>
</div>
<nav>
Działalność
Jednostki
Dokumenty
Kontakt
</nav>
</header>
This solution works as expected in certain cases on Chrome and Opera. The layout is correct after the first load of page (screenshot from Chrome after the first page load). It also scales adequately to viewport resizing. Then, after clicking some links, <nav> become narrower and flex is not flexible anymore - doesn't change size when viewport does (screenshot from Chrome after the bug occures).
On Firefox and Edge this solution doesn't work at all - bug occurs all the time.
You can test this page here. Even more confusing is fact that the footer (made with flexbox) works propoerly on the same page. How can I repair my code to make header looks as expected?
You are working with a static margin. Its better to use a relative CSS rule to achieve the space between content and borders. Also you dont need to set flex-grow & flex-basis.
But the main issue was that the img { height: 100% } caused that the img element rise to its full height. You have to give the header .logo a max-height attribute.
Also its better to achieve the space between the nav elements with padding.
Here is a possible solution:
header {
margin: 0 auto;
max-width: 90%;
height: 75px;
border-bottom: #707070 solid 2px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
header .logo {
padding: 5px 0;
height: 100%;
max-height: 65px;
}
img {
height: 100%;
}
header nav {
padding-bottom: 20px;
display: flex;
justify-content: space-between;
}
header nav a {
align-self: flex-end;
padding-right: 10px;
}
header nav a:last-child {
padding-right: 0;
}
<header>
<div class="logo">
<a href="home.html">
<img alt="Logo Hufca" src="https://wip.733mdh.pl/user/themes/motyw-hufca/images/logo_hufca_zlote.svg">
</a>
</div>
<nav>
Działalność
Jednostki
Dokumenty
Kontakt
</nav>
</header>
header {
height: 75px;
border-bottom: #707070 solid 2px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
header .logo {
flex-grow: 2;
flex-basis: auto;
padding: 5px 0;
}
header nav {
flex-grow: 1;
flex-basis: auto;
padding-bottom: 20px;
display: flex;
justify-content: space-between;
}
header nav a {
align-self: flex-end;
}
<header>
<div class="logo">
<a href="home.html">
<img alt="Logo Hufca" src="https://picsum.photos/200/50" />
</a>
</div>
<nav>
Działalność
Jednostki
Dokumenty
Kontakt
</nav>
</header>