Align items to the right within a flexbox - html

I am using Bootstrap 4 and this CSS class to vertically align my items:
.vertical-align {
display: flex;
flex-direction: row;
}
.vertical-align > [class^="col-"],
.vertical-align > [class*=" col-"] {
display: flex;
align-items: center;
justify-content: flex-start;
}
With this, all this content is aligned to the left. How can I align, for example, the list (ul> li) to the right? Nothing I've tried is working.
Here is a working demo:
.vertical-align {
display: flex;
flex-direction: row;
}
.vertical-align > [class^="col-"],
.vertical-align > [class*=" col-"] {
display: flex;
align-items: center;
justify-content: flex-start;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row vertical-align">
<div class="col-md-3" style="background-color: #f1f1f1;">
<div class="logo">
<img src="http://placehold.it/120x50" alt="" class="img-responsive">
</div>
</div>
<div class="col-md-6" style="background-color: #ffccff;">
<div class="catergorias">
<ul class="list-inline categorias_topo">
<li class="list-inline-item">
Categoria 1
</li>
<li class="list-inline-item">
Categoria 2
</li>
<li class="list-inline-item">
Categoria 3
</li>
<li class="list-inline-item">
Categoria 4
</li>
</ul>
</div>
</div>
<div class="col-md-3" style="background-color: #f1f1f1;">
BUSCA
</div>
</div>
</div>
I also have a JSFiddle: https://jsfiddle.net/xcottg08/

You should use justify-content: flex-end; instead of justify-content:flex-start to justify content to the right side.
You must specify it within these rules
.vertical-align > [class^="col-"],
.vertical-align > [class*=" col-"] {
However, I would create a new class like .vertical-align-right and one extra class to the container with list (the div above .catergorias with class .col-md-6), thus the code would look as follow:
.vertical-align-right > .list,
justify-content: flex-end;
}

Related

Flex: 1 Doesn't Fill Angular Layout Div Vertically

I'm working on a sidebar using Angular and Flex. It's app-sidebar within the following layout Angular component:
<div fxlayout="row" fxFill>
<app-sidebar fxLayout="column" fxFlex="10%"></app-sidebar>
<div fxLayout="column" fxFlex="100">
<app-top-navbar class="navbar-component" fxLayout="column"></app-top-navbar>
<div class="content" fxLayout="column" fxFlex>
<router-outlet></router-outlet>
</div>
</div>
</div>
I'm trying to get the sidebar to fill the page vertically, the list items within evenly spaced between that expanse. But I'm not having any luck... I've made a simplified version in this JSFiddle. You can see that it stretches horizontally (great, as I want it to fill that 10% of the greater layout), but it doesn't go to the bottom of the page.
I'm new to Angular and Flex, so any help would be appreciated. Here's the Fiddle code (if it makes a difference, anchors are used, because I also have icons not included):
:host {
display: flex;
flex-direction: column;
flex: 1 1 100%;
}
#sidebar-cont-div{
display: flex;
flex:1;
flex-direction: column;
}
#navbar-content-container{
display: flex;
flex: 1;
flex-direction: column;
}
#main-navbar {
background-color: red;
border-radius: 50px 20px 20px 50px;
text-align:center;
}
#nav-list {
padding: 0;
list-style: none;
display: flex;
flex-direction: column;
justify-content: space-around;
flex: 1;
margin: auto;
min-width:100%;
height:100%;
}
<div id = "sidebar-cont-div">
<nav id="main-navbar">
<div id="navbar-content-container">
<a href="#">
<h1 id="sidebar-logo">Project<br>Dash</h1>
</a>
<ul id="nav-list">
<li class="sidebar-item" role="presentation">
<a href="#">
<span>Overview</span>
</a>
</li>
<li class="sidebar-item" role="presentation">
<a href="#">
<span>Project Record</span>
</a>
</li>
</ul>
</div>
</nav>
</div>
please try like this
body , html {
height:100% ;
margin : 0px ;
}
#sidebar-cont-div{
display: flex;
flex-direction: column;
height:100%
}
#main-navbar {
background-color: #ffffff;
background-image: linear-gradient(180deg,rgba(127,162,137,0.82) 20%,rgba(39,86,94,0.92));
border-radius: 50px 20px 20px 50px;
text-align:center;
display: flex;
flex-direction: column;
flex: 1 1 auto;
}
You can use the rest of the style as it is.By using the styles above your side nav will strech vertically.
working sample in stackblitz
https://stackblitz.com/edit/js-aqedvi

Flex elements don't work. How can i fix my code to solve that problem?

Why can't I use justify-content: space-between or space-around?
.mobile-page-wrapper {
display: flex;
flex-direction: column;
justify-content: space-between;
}
.mobile-nav {
display: flex;
justify-content: space-between;
}
<div class="mobile-page-wrapper">
<div class="mobile-nav">
<img src="./img/mobile-nav-logo.svg" alt="LOGO" class="logo-mobile">
<ul class="mobile-soc">
<li><img class="mobile-fb" src="./img/Facebook.svg" alt="Facebook"></li>
<li><img class="mobile-vk" src="./img/VK.svg" alt="Vk"></li>
<li><img class="mobile-inst" src="./img/Instagram.svg" alt="Instagram"></li>
</ul>
</div>
<!-- /.mobile-nav -->
<div class="main-idea-mobile">
<h1>SSDSDSDS<br>DASDASD</h1>
</div>
<!-- /.main-idea-mobile -->
</div>
<!-- /.mobile-page-wrapper -->
I would like to use justify-content: space-between; with flex-direction: column but it doesn't work. Plz help me to solve that problem.
If you wish for there to be more space between the items in mobile-wrapper you need to set its height property.
display: flex; justify-items: space-between; seems to be working on mobile-nav because mobile-nav is a div which is a block level element and therefore has a default width of 100%, and the default flex-direction is row.
I have attached your snippet below with the height of mobile-wrapper set to 100vh as an example.
.mobile-page-wrapper {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100vh;
}
.mobile-nav {
display: flex;
justify-content: space-between;
}
<div class="mobile-page-wrapper">
<div class="mobile-nav">
<img src="./img/mobile-nav-logo.svg" alt="LOGO" class="logo-mobile">
<ul class="mobile-soc">
<li><img class="mobile-fb" src="./img/Facebook.svg" alt="Facebook"></li>
<li><img class="mobile-vk" src="./img/VK.svg" alt="Vk"></li>
<li><img class="mobile-inst" src="./img/Instagram.svg" alt="Instagram"></li>
</ul>
</div>
<!-- /.mobile-nav -->
<div class="main-idea-mobile">
<h1>SSDSDSDS<br>DASDASD</h1>
</div>
<!-- /.main-idea-mobile -->
</div>
<!-- /.mobile-page-wrapper -->
(I noticed that when running the snippets from your post and my answer they look the same in the small window, you can see the difference if you click full page)

Flexbox help needed

I am trying to space my navigation items in my header evenly for desktop display using flexbox, justify content: space-around.
However it seems as though having .header also set to flex is interfering, as I am able to implement space-around when I comment out .header as display flex.
Tried everything I can think of, so now I come to the experts!
Any ideas greatly appreciated. Thank you.
.header {
margin-bottom: 15%;
display: flex;
justify-content: space-between;
}
.header-ul {
display: flex;
justify-content: space-between;
}
<section class="home">
<header class="header">
<h1 class="h1">made tight</h1>
<i class="fa fa-bars is-hidden-tablet" aria-hidden="true"></i>
<!--non-mobile nav-->
<ul class="header-ul is-hidden-mobile">
<li href='#'>Home</li>
<li href='#'>Studio</li>
<li href='#'>Services</li>
<li href='#'>Careers</li>
<li class='contact-li' href='#'>Contact</li>
</ul>
</header>
You can add margin-left to ul.header-ul.is-hidden-mobile li
.header {
margin-bottom: 15%;
display: flex;
justify-content: space-between;
}
.header-ul {
display: flex;
justify-content: space-between;
}
ul.header-ul.is-hidden-mobile li {
margin-left:30px;
}
<section class="home">
<header class="header">
<h1 class="h1">made tight</h1>
<i class="fa fa-bars is-hidden-tablet" aria-hidden="true"></i>
<!--non-mobile nav-->
<ul class="header-ul is-hidden-mobile">
<li href='#'>Home</li>
<li href='#'>Studio</li>
<li href='#'>Services</li>
<li href='#'>Careers</li>
<li class='contact-li' href='#'>Contact</li>
</ul>
</header>
You can use flex-grow: 1 to make the nav fill the remaining space.
.header {
margin-bottom: 15%;
display: flex;
justify-content: space-between;
}
.header-ul {
display: flex;
justify-content: space-between;
flex-grow: 1;
}
<section class="home">
<header class="header">
<h1 class="h1">made tight</h1>
<i class="fa fa-bars is-hidden-tablet" aria-hidden="true"></i>
<!--non-mobile nav-->
<ul class="header-ul is-hidden-mobile">
<li href='#'>Home</li>
<li href='#'>Studio</li>
<li href='#'>Services</li>
<li href='#'>Careers</li>
<li class='contact-li' href='#'>Contact</li>
</ul>
</header>

justify-content property is not working as expected

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>

Keeping middle item centered horizontally

I'm trying to make my first custom header.
I want the middle item (eg. the logo) to stay centered regardless of how much content is on either side.
I tried using z-index but I believe that was wrong.
HTML:
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
justify-content: space-around;
}
.flex-item {
text-align: center;
}
.image {
z-index: -1;
}
<div id="header_container">
<div id="header">
<ul class="flex-container">
<li class="flex-item">Menu items 1 BHello | seven | eight | nineteen | Fitiyytwooooo
</li>
<li class="image">
<img src="http://www.plankdesign.com/wp-content/uploads/2011/01/html4.jpg" style="width:204px;height:128px;">
</li>
<li class="flex-item">Menu items 2 Blah | Blah | Blah</li>
</ul>
</div>
</div>
Here's my jsfiddle demo.
Try like this: Demo
.image img {
z-index: 1000;
width:18%;
height:128px;
position:absolute;
left:50%;
margin-left:-9%; /* half of the width */
}
HTML:
<li class="image">
<img src="http://www.plankdesign.com/wp-content/uploads/2011/01/html4.jpg" />
</li>
Check this out, you may want to modify the code(col-md-x) as per your requirement.