I am using the CSS framework Bulma (first time), though my question might not be Bulma specific, I thought I'd include that just be clear.
I have a navigation bar that has a centered set of links, but also right-align elements. Here is a screenshot:
You can ignore the fixed leaves on the left. I want to know how I can get the cart and the login button to be right aligned whilst having the other bits centre aligned.
Here is a codepen of what I have tried. I just do not know of the proper way to have the car and the login right aligned. I mean I can position absolute them, but that sounds silly.
HTML CODE
<nav class="navbar is-fixed-top">
Products
Our Story
<div id="logo">Logo placeholder</div>
Blog
Contact Us
</nav>
CSS CODE
nav {
display: flex;
width: 100%;
height: 100px;
align-items: center;
justify-content: center;
}
nav a {
text-decoration: none;
color: #194522;
font-weight: bold;
margin: 0 40px;
display: flex;
align-items: center;
}
nav a:hover {
color: #abcf39;
}
How can I get my navigation like that?
Bulma has two regions in its navbar called navbar-startand navbar-end for control of alignment. Just add an additonal class (in my example: navbar-start--centered) to adapt the "start region" to your needs:
.navbar-start--centered {
flex-grow: 1;
justify-content: center;
}
Here a codepen to play with.
Look at it with a wide viewport - it is desktop only. If you want the start region in the viewports center, you could additionally position the "end region" absolutely.
.navbar-start--centered {
flex-grow: 1;
justify-content: center;
}
<nav class="navbar" role="navigation" aria-label="main navigation">
<div id="navbarBasicExample" class="navbar-menu">
<div class="navbar-start navbar-start--centered">
<a class="navbar-item" href="">Products</a>
<a class="navbar-item" href="">Our Story</a>
<a class="navbar-item" href="https://bulma.io">
<img src="https://bulma.io/images/bulma-logo.png" width="112" height="28">
</a>
<a class="navbar-item" href="">Blog</a>
<a class="navbar-item" href="">Contact Us</a>
</div>
<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
<a class="button is-primary">
<span class="icon">
<i class="fas fa-shopping-cart"></i>
</span>
<span>Cart</span>
</a>
<a class="button is-light">
Log in
</a>
</div>
</div>
</div>
</div>
</nav>
You can add an empty element to the left (as a placeholder) and one to the right (to hold the links) and set them to be flex:1.
Then use normal flex positioning to set the contents of the second (right) container to be right aligned.
nav {
display: flex;
width: 100%;
height: 100px;
align-items: center;
justify-content: center;
}
nav a {
text-decoration: none;
color: #194522;
font-weight: bold;
margin: 0 40px;
display: flex;
align-items: center;
}
nav a:hover {
color: #abcf39;
}
.nav-container{
display:flex;
flex:1;
justify-content: flex-end;
}
.nav-container a {
margin:0 10px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css" />
<nav class="navbar is-fixed-top">
<div class="nav-container"></div>
Products
Our Story
<div id="logo">Logo placeholder</div>
Blog
Contact Us
<div class="nav-container">
🛒
Login
</div>
</nav>
Related
I'm having difficulty vertically centering some SVGs from FontAwesome in a navigation component. I've recreated the problem in a much simpler example here. What I'm trying to achieve is to have the two icons and the text all vertically aligned regardless of their individual height. As you can see, the Apple logo is not quite at the same position as the other elements. I feel like maybe my problem lies in having everything nested in too many elements, but the SVGs also need to link to other pages, and I like to be able to have different spacing between the icons compared to between the icons and the title. Any advice is greatly appreciated!
.tool-bar {
background-color: gray;
display: flex;
height: 3em;
align-items: center;
}
.icons {
display: flex;
align-items: center;
margin-left: 0.5em;
}
.icons a {
margin-right: 0.2em;
display: flex;
align-items: center;
}
.icons a img {
height: auto;
}
<link href="https://pro.fontawesome.com/releases/v5.2.0/css/all.css" rel="stylesheet"/>
<div class="tool-bar">
<a class="title">Title</a>
<div class="icons">
<a href="https://www.google.com/" target="_blank">
<i class="fab fa-google"></i>
</a>
<a href="https://www.apple.com/" target="_blank">
<i class="fab fa-apple"></i>
</a>
</div>
</div>
Wrap the <i> element containing the fontawesome icons in a <div>.
I have done that here in this snippet and added a style background red to show the div rendering. The issue is that in fontawesome icons, the Google G logo and the Apple logo don't appear proportionate although their containing divs are identical in sizes. That's partly why these don't look visually aligned. You can see this if you run the snippet.
.tool-bar {
background-color: gray;
display: flex;
height: 3em;
align-items: center;
}
.icons {
display: flex;
align-items: center;
margin-left: 0.5em;
}
.icons a {
margin-right: 0.2em;
display: flex;
align-items: center;
}
.icons a img {
height: auto;
}
<link href="https://pro.fontawesome.com/releases/v5.2.0/css/all.css" rel="stylesheet"/>
<div class="tool-bar">
<a class="title">Title</a>
<div class="icons">
<a href="https://www.google.com/" target="_blank">
<div style="background-color: red;">
<i class="fab fa-google"></i>
</div>
</a>
<a href="https://www.apple.com/" target="_blank">
<div style="background-color: red;">
<i class="fab fa-apple"></i>
</div>
</a>
</div>
</div>
I'm trying to create previous / next buttons at the bottom of a set of images, to navigate to a previous and next project. I'd like the previous button with icon to align to the left and the next button (and icon) to align to the right.
I figured flex would be the correct way to do this using flex start for the previous button and flex-end for the next button but I can't get it to work and have tried a few tutorials, any pointers would be very helpful.
Also doing it in SASS to make it 'easier'... :)
Thank you
.item-pagination-prevnext {
display: flex;
background-color: grey;
padding: 10vw 0;
.item-pagination-link {
display: flex;
justify-content: flex-start;
align-items: center;
a {
cursor: pointer;
}
.item-pagination-icon {
position: relative;
padding: 0 25px;
}
.item-pagination-link-next {
display: flex;
justify-content: flex-end;
color: red;
align-items: center;
}
}
<div class="item-pagination-prevnext">
<!-- prev -->
<a class="item-pagination-link item-pagination-link-prev" href="#">
<div class="item-pagination-icon">
<i class="fas fa-arrow-left"></i>
</div>
<div class="pagination-title-wrapper">
<h4 class="item-pagination-title">Previous</h4>
</div>
</a>
<!-- next -->
<a class="item-pagination-link item-pagination-link-next" href="#">
<div class="pagination-title-wrapper">
<h4 class="item-pagination-title">Next</h4>
</div>
<div class="item-pagination-icon">
<i class="fas fa-arrow-right"></i>
</div>
</a>
</div>
You just need to add justify-content: space-between; to item-pagination-prevnext class.
I wanted the nav bar right at the top, to have the class with left on the left side, the class with middle right in the middle, and the class with right in the right side.
* {
padding: 0;
margin: 0;
}
ul {
list-style: none;
}
.flex-container {
width: 100%;
}
.flex-container ul {
display: flex;
align-items: center;
justify-content: center;
}
.flex-container li {
border: 1px solid red;
}
.flex-container nav ul .nytl {
width: 189px;
height: 26px;
}
.flex-container nav ul .first {
justify-content: flex-start;
}
hr {
margin-top: 10px;
}
<div class="flex-container">
<nav>
<ul>
<li class="left">
<a href="#"><img src="https://img.icons8.com/material-outlined/16/000000/menu.png">
</a>
</li>
<li class="left">
<a href="#"><img src="https://img.icons8.com/material-rounded/16/000000/search.png">
</a>
</li>
<li class="left">SPACE & COSMOS
</li>
<li class="middle"><img src="https://lco1220.github.io/nyt_article/images/nyt-logo.png" alt="NewYorkTimes-Logo" class="nytl"></li>
<li class="right"><button>Subscribe</button> .
</li>
<li class="right"><button>Login</button></li>
</ul>
</nav>
<hr>
</div>
Try using auto margins to push the left and right elements away from the middle element.
(Also, since you're using the HTML5 nav element and CSS3 properties, you really don't need a ul to structure your layout. You can simplify your code substantially.)
nav {
display: flex;
align-items: center;
}
nav > * {
border: 1px solid red;
}
.nytl {
margin: 0 auto;
width: 189px;
height: 26px;
}
hr {
margin-top: 10px;
}
* {
padding: 0;
margin: 0;
}
<nav>
<a href="#">
<img src="https://img.icons8.com/material-outlined/16/000000/menu.png">
</a>
<a href="#">
<img src="https://img.icons8.com/material-rounded/16/000000/search.png">
</a>
SPACE & COSMOS
<img src="https://lco1220.github.io/nyt_article/images/nyt-logo.png" alt="NewYorkTimes-Logo" class="nytl">
<button>Subscribe</button>
<button>Login</button>
</nav>
<hr>
Learn more about auto margins here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
Here's another flex method you may find useful:
Aligning Three Divs Horizontally Using Flexbox
You may encounter another problem now: Because flex features such as auto margins, justify-content and align-items work by distributing free space, your middle item may not be perfectly centered. See these posts for more details and solutions:
Keep the middle item centered when side items have different widths
Center and right align flexbox elements
I would utilise the space-between option that flex brings with the justify-content property. You have to be careful of the way the code is listed for SEO purposes as opposed to placing anywhere and have the css reposition it all. It should cascade in natural order first.
.flex-thirds {
display: flex;
justify-content: space-between;
}
.flex-thirds .col {
width: 32%;
}
.nytl {
margin: 0 auto;
width: 189px;
height: 26px;
}
<div class="flex-thirds">
<div class="col">
<a href="#"><img src="https://img.icons8.com/material-outlined/16/000000/menu.png">
</a>
<a href="#"><img src="https://img.icons8.com/material-rounded/16/000000/search.png">
</a>
SPACE & COSMOS
</div>
<div class="col">
<img src="https://lco1220.github.io/nyt_article/images/nyt-logo.png" alt="NewYorkTimes-Logo" class="nytl">
</div>
<div class="col">
<button>Subscribe</button>
<button>Login</button>
</div>
</div>
You can find more about justify-content here at css-tricks
I am trying to center my logo it is in a div with the id "logocont" here is my css code no matter how i tried is stay stuck at the left if i put display block my navigation bar goes in another line and logo will be centered.
#logocont {
display: inline-block;
float: none;
align-content: center;
text-align: center;
margin : 0 auto;
overflow : visible;
}
Note: I am using grid layout I created the grids in another css file and i am linking it.
Here is my HTML code:
<div class="gr-16" id="menu">
<div id="logocont" class="gr-2">
<a href="#">
<img id="logo" src="images/mylogo21.png" alt="Logo" runat="server" />
</a>
</div>
<div id="navbar" class="gr-5">
<nav>
<a href="#">
Home
</a>
<a href="#">
Programmers
</a>
<a href="#">
Request Program
</a>
</nav>
</div>
</div>
Use display inline-flex for your logo container
#logocont {
display: inline-flex;
align-items: center;
}
This example centers a div in a div:
<html>
<style>
#outer {
width: 100%;
text-align: center;
}
#inner {
display: inline-block;
}
</style>
<div id="outer">
<div id="inner">Foo foo</div>
</div>
I used the following CSS to make a vertically and horizontally aligned div accordingly to what i understood from your HTML. May you check if this helps? (link to JSFiddle: https://jsfiddle.net/est02o5m/1/)
CSS:
.gr-16 {
display: grid;
}
.gr-2 {
grid-column: 2;
background-color: red;
border: 1px solid black;
grid-column-start: 1;
grid-column-end: 2;
}
.gr-5 {
grid-column: 5;
grid-column-start: 3;
grid-column-end: 8;
}
#logocont {
width:100%;
height: 200px;
display: inline-flex;
float: none;
align-content: center;
text-align: center;
margin : 0 auto;
overflow : visible;
border: 2px solid black;
background: #ccc;
align-items: center;
justify-content: center;
}
If the css that i used doesn't correspond to what you're using may you complement it and update your question?
I fixed this problem by removing the nav tag from the div as well as the logo so my code became:
<div class="gr-16" id="menu">
<nav id="navbar">
Home
Programers
Request Program
</nav>
<a href="#" id="logocont">
<img id="logo" src="images/mylogo21.png" alt="Logo" runat="server" />
</a>
</div>
and changed the css to:
#logocont {
width : 100px;
display : block;
margin : 0 auto;
}
I'm trying to align "Previous" to the left, align "Next" to the right, and figure out how to center the page numbers.
I've been looking at tutorials and articles on flexbox but I'm having a hard time understanding it.
.nav-links {
display: flex;
}
.prev.page-numbers {
justify-content: flex-start;
}
.next.page-numbers {
justify-content: flex-end;
}
<div class="nav-links">
<a class="prev page-numbers">Previous</a>
<a class="page-numbers">1</a>
<a class="page-numbers">2</a>
<a class="page-numbers">3</a>
<a class="next page-numbers">Next</a>
</div>
It seems you want to "push" flex items in one direction or another. To do that, you should use auto margins instead of justify-content.
.nav-links {
display: flex;
}
.prev.page-numbers {
margin-right: auto; /* Push to the left */
}
.next.page-numbers {
margin-left: auto; /* Push to the right */
}
<div class="nav-links">
<a class="prev page-numbers">Previous</a>
<a class="page-numbers">1</a>
<a class="page-numbers">2</a>
<a class="page-numbers">3</a>
<a class="next page-numbers">Next</a>
</div>
The remaining flex items will be centered between the first and last ones, not necessarily centered relatively to the container. If you want the latter, you can make the first and last flex items be equally wide.
.nav-links {
display: flex;
}
.prev.page-numbers, .next.page-numbers {
flex: 1; /* Disttribute remaining space equally */
}
.next.page-numbers {
text-align: right;
}
<div class="nav-links">
<a class="prev page-numbers">Previous</a>
<a class="page-numbers">1</a>
<a class="page-numbers">2</a>
<a class="page-numbers">3</a>
<a class="next page-numbers">Next</a>
</div>
The justify-content property applies only to flex containers, although it aligns flex items.
In your code, because you're applying justify-content to flex items, it is being ignored.
Here are two working examples:
Example 1 - justify-content
.nav-links {
display: flex;
justify-content: space-between;
background-color: lightgray;
}
a {
flex: 0 0 10%;
text-align: center;
border: 1px solid black;
background-color: yellow;
}
<div class="nav-links">
<a class="prev page-numbers">Previous</a>
<a class=" page-numbers">1</a>
<a class="page-numbers">2</a>
<a class=" page-numbers">3</a>
<a class="next page-numbers">Next</a>
</div>
Example 2 - auto margins
.nav-links {
display: flex;
background-color: lightgray;
}
a {
flex: 0 0 10%;
text-align: center;
border: 1px solid black;
background-color: yellow;
}
a:first-child { margin-right: auto; }
a:last-child { margin-left: auto; }
<div class="nav-links">
<a class="prev page-numbers">Previous</a>
<a class=" page-numbers">1</a>
<a class="page-numbers">2</a>
<a class=" page-numbers">3</a>
<a class="next page-numbers">Next</a>
</div>
More details:
Methods for Aligning Flex Items along the Main Axis
justify-content only works in parents, not in children, so if you can wrap your .page-numbers then you just can simply set display:flex to parent and child div and flex:1 + justify-content to div child, which is parent of .page-numbers
div {
display: flex;
}
div div {
flex: 1;
justify-content: center
}
<div class="nav-links">
<a class="prev page-numbers">Previous</a>
<div>
<a class="page-numbers">1</a>
<a class="page-numbers">2</a>
<a class="page-numbers">3</a>
</div>
<a class="next page-numbers">Next</a>
</div>