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;
}
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>
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;
}
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;
}`
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>