Flexbox Ordering. Splitting a column - flexbox

I want to achieve the following, changing my page layout for mobile/desktop using Flexbox.
I am trying to achieve the following using Flexbox ordering:
http://i.stack.imgur.com/iBXpJ.jpg
Hope this makes sense?

Here is a way of doing this using flexbox. Here is the working demo.
<div class="main-container">
<div class="item menu">Menu</div>
<div class="item sidebar">Sidebar</div>
<div class="item content">Content</div>
</div>
.main-container {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.item {
border: 5px solid gray;
}
.menu {
flex-basis: 200px;
width: 200px;
}
.sidebar {
flex-grow: 1;
width: 200px;
}
.content {
flex-basis: 100%;
width: calc(100% - 200px);
}
#media (max-width: 600px) {
.main-container {
flex-wrap: no-wrap;
height: 100%;
}
.item {
width: 100%;
}
.sidebar, .content {
flex-basis: auto;
}
}

Related

Align one element of a container at the top left corner and the other at the bottom right using flexbox?

The element in the image is styled using absolute positioning but when I resize the screen I have to slightly adjust the positioning of both the 'For Sale' element and the '$400,000' element so I am curious as to if there is a way to achieve the same layout using flexbox?
Here is my attempt (https://codepen.io/ob98/pen/eYVPJLJ)
<div class = 'container'>
<p class='item1'>Top Left</p>
<p class='item2'>Bottom Right</p>
</div>
css:
.container{
display: flex;
background: red;
width: 500px;
height: 200px;
color: white;
}
.item1{
align-self: flex-start;
}
.item2{
align-self: flex-end;
justify-self: flex-end; /* If align-self moved this to the bottom of the container vertically, I am thinking that this should move it to the end/right side of the container horizontally, but that is not working */
}
Just went to https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self and saw that "In flexbox layouts, this property is ignored (more about alignment in Flexbox)", so I guess that explains why that is not working but I don't really understand why this property is being ignored... Anyway, is there a way to achieve this layout using flexbox or do I have to stick to absolute positioning? Thanks for any input.
Add to .container the declaration justify-content: space-between, to make both your p go to the container edges, and that's done!
.container {
display: flex;
justify-content: space-between;
background: red;
width: 500px;
height: 200px;
color: white;
}
.item1 {
align-self: flex-start;
}
.item2 {
align-self: flex-end;
justify-self: flex-end;
}
<div class = 'container'>
<p class='item1'>Top Left</p>
<p class='item2'>Bottom Right</p>
</div>
Adding justify-content to container and just using the item2 class like this would help.
.container{
display: flex;
justify-content: space-between;
background: red;
width: 500px;
height: 200px;
color: white;
}
.item2{
align-self: flex-end;
}
Can be the answer but im not sure. When i ran it worked.
.bg-flex {
width: 100vw;
height: 100vh;
background-image: url('https://media.istockphoto.com/photos/the-city-of-london-skyline-at-night-united-kingdom-picture-id1312550959');
display: flex;
flex-direction: column;
}
.for-sale {
height: 50%;
display: flex;
flex-direction: row;
align-items: flex-start;
justify-content: flex-start;
}
.k-400 {
height: 50%;
display: flex;
flex-direction: row;
align-items: flex-end;
justify-content: flex-end;
}
.example {
width: 300px;
height: 100px;
background-color: aqua;
}
<div class="bg-flex">
<div class="for-sale">
<div class="example">
</div>
</div>
<div class="k-400">
<div class="example">
</div>
</div>
</div>

how to close mobile navigation menu on click outside responsive menu

I have a menu on my website and it works ok on laptop browser. But on the mobile version, the menu opens and close when i chose a option. But when i click outside de menu it doesn't close.
I would like to know how can i do so that when the user clicks or taps outside the menu the menu closes.
Can someone help please?
My code is:
const toggleButton = document.getElementsByClassName('toggle-button')[0]
const navbarLinks = document.getElementsByClassName('navbar-links')[0]
const links = navbarLinks.querySelectorAll("a")
toggleButton.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
});
for(let link of links){
link.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
});
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.container-header nav{
margin-top:0em;
}
.position-sticky {
position: sticky!important;
}
body {
margin: 0;
padding: 0;
position: relative;
}
.navbar {
margin: 0;
padding: 0;
display: flex;
position: relative;
justify-content: space-between;
align-items: center;
color: white;
background-color:#5aaddd;
box-shadow: rgba(0, 0, 0, 0.35) 0px 0px 20px 0px;
}
#logo{
height: 60px;
width: 85px;
}
#logo img{
margin-left: 35%;
margin-top: 8%;
height: 80%;
display: flex;
padding: 0;
}
.brand-title {
font-size: 1.5rem;
margin: .5rem;
}
.navbar-links {
height: 100%;
}
.navbar-links ul {
display: flex;
margin: 0;
padding: 0;
}
.navbar-links li {
list-style: none;
}
.navbar-links li a {
display: block;
text-decoration: none;
color: white;
padding: 1rem;
font-family: 'Signika Negative', sans-serif;
font-size:15pt;
}
.navbar-links li:hover {
background-color: #555;
}
.toggle-button {
position: absolute;
top: 1.3rem;
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;
}
#media (max-width: 800px) {
.navbar {
flex-direction: column;
align-items: flex-start;
}
.position-sticky {
position: sticky!important;
}
.toggle-button {
display: flex;
}
.navbar-links {
display: none;
width: 100%;
}
.navbar-links ul {
width: 100%;
flex-direction: column;
transition: transform 200ms;
}
.navbar-links ul li {
text-align: center;
}
.navbar-links ul li a {
padding: .5rem 1rem;
}
.navbar-links.active {
display: flex;
}
}
<nav class="navbar">
<div id="logo"><img src="/images/logo/logo.png"> </div>
<a href="#" id="toggle" class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div id="topbar" class="navbar-links">
<ul>
<li>Sobre</li>
<li>Oferta</li>
<li>Implementação</li>
<li>Segurança</li>
<li>FAQ</li>
<li>Imprensa</li>
</ul>
</div>
</nav>

Flexbox text alignment on mobile

So, I am using flexbox for some columns. Basically column 1 is left-aligned, column two i centered, and column three is right-aligned. No big deal, or so I thought. In Chrome, everything looks fine, even when looking at things through Chrome's Inspect tool...but on my iPhone I can't get the right column to justify-content.
My code looks like:
<div id="nav">
<div class="wrap">
<div>
<p><i class="far fa-dot-circle"></i> Watch</p>
</div>
<div>
<p><img src="<?php bloginfo('template_directory'); ?>/images/logo.png" alt="" title="" /></p>
</div>
<div>
<p><i class="fas fa-donate"></i> Give</p>
</div>
</div>
</div>
The css looks like:
#nav div {
align-items: center;
display: flex;
flex: 1;
font-family: 'Open Sans', sans-serif;
font-size: 14px;
letter-spacing: 2px;
line-height: 18px;
padding: 10px 20px 0;
text-transform: uppercase;
}
#nav > .wrap > div:nth-child(1) {
background: #00ff00;
justify-content: left;
}
#nav > .wrap > div:nth-child(2) {
background: #ff0000;
justify-content: center;
}
#nav > .wrap > div:nth-child(3) {
background: #0000ff;
justify-content: right;
}
#nav img {
max-width: 175px;
width: 100%;
}
#nav i {
color: #fff;
margin-right: 5px;
}
#nav a {
color: #fff;
text-decoration: none;
}
Here is the Fiddle: https://jsfiddle.net/1kw5g3h7/
I added the following code this morning and this works:
#nav > .wrap > div:nth-child(3) p {
text-align: right;
width: 100%;
}
I don't like this approach, it seems like to me the justify-content just isn't working is there a way to fix this using flexbox? I'm trying to understand why my original code isn't working on my iPhone? Maybe it's because I have Font Awesome icons in front of the text, I just don't know. Any ideas?
Thanks,
Josh
I didn't realize until now that I hadn't ever posted my final code. This is what I ended up with: https://jsfiddle.net/tc36e24s/2/
HTML:
<div id="head">
<div id="nav">
<i class="ion-android-close"></i>
<ul>
<li>Watch</li>
<li>Give</li>
<li><img src="images/logo.png" alt="Logo" title="Logo" /></li>
<li>Visitors</li>
<li>Contact</li>
</ul>
</div>
<div id="logo">
<p><img src="images/logo.png" alt="Logo" title="Logo" /></p>
<div onclick="openNav()" class="open">
<i class="ion-android-menu"></i>
</div>
</div>
</div>
CSS
body {
background: #009151;
}
#head {
display: flex;
flex-flow: column;
height: 100vh;
}
#nav ul {
flex: 0 1 auto;
}
#nav ul img {
max-width: 175px;
width: 100%;
}
#nav ul i {
color: #9ac2e7;
margin-right: 5px;
}
#nav li {
font-family: 'Open Sans', sans-serif;
letter-spacing: 2px;
line-height: 18px;
padding: 10px 20px 0;
text-align: center;
text-transform: uppercase;
}
#logo {
display: flex;
}
#logo img {
max-width: 200px;
margin: 20px 30px;
width: 100%;
}
#media screen and (max-width: 800px) {
#head .open {
color: #9ac2e7;
font-size: 30px;
margin: 20px 30px;
text-align: right;
width: 100%;
}
#head .open:hover {
cursor: pointer;
}
#nav {
background: #9ac2e7;
display: none;
height: 100%;
left: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 1;
}
#nav ul {
align-items: center;
display: flex;
flex-flow: column;
height: 100%;
justify-content: center;
width: 100%;
}
#nav ul a {
color: #000;
text-decoration: none;
}
#nav li {
font-size: 20px;
padding: 10px 0;
}
#nav li:nth-child(3) {
display: none;
}
#nav .close {
color: #000;
font-size: 30px;
position: absolute;
right: 30px;
top: 20px;
}
}
#media screen and (min-width: 801px) {
#nav .close {
display: none;
}
#nav[style] {
display: block !important;
}
#nav ul {
align-items: center;
display: flex;
}
#nav ul a {
color: #9ac2e7;
text-decoration: none;
}
#nav li {
flex: 1;
font-size: 14px;
}
#logo {
display: none;
}
}
JS
function openNav() {
document.body.style.overflow="hidden";
document.getElementById("nav").style.display = "block";
}
function closeNav() {
document.body.style.overflow="scroll";
document.getElementById("nav").style.display = "none";
}
I added some JS to make my menu work on mobile, but other than that this is code I went with...which is pretty much the same as before except I added two additional links (one on each side of the logo).
Thanks,
Josh

Inner div oscillates when using a CSS transition on the parent div

Please run my code sample and hover over the black square, why does the inner div, the white border div, oscillate/shake? How do I make it stop doing that?
body {
min-height: 100vh;
display: flex;
}
.main-container {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
}
.transition-container {
background: black;
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
margin: 20px;
transition-duration: 1.25s;
transition-property: margin, width, height;
}
.transition-container:hover {
margin: 0;
width: 120px;
height: 120px;
}
.inner-container {
border: 4px solid white;
width: 60px;
height: 60px;
}
<div class="main-container">
<div class="transition-container">
<div class="inner-container">
</div>
</div>
</div>
Goal
On hover of my outer div (<div class="transition-container">), I want it to grow and I want to use a transition to make it smooth
I don't want the growing div to take up any more space. Currently, to achieve this, I shrink the margins
Adding transition-timing-function: steps(10, end); fixed my contrived example. Source.
body {
min-height: 100vh;
display: flex;
}
.main-container {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
}
.transition-container {
background: black;
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
margin: 20px;
transition-duration: 1.25s;
transition-property: margin, width, height;
transition-timing-function: steps(10, end);
}
.transition-container:hover {
margin: 0;
width: 120px;
height: 120px;
}
.inner-container {
border: 4px solid white;
width: 60px;
height: 60px;
}
<div class="main-container">
<div class="transition-container">
<div class="inner-container">
</div>
</div>
</div>
Changing the border width from 4px to 4.5px on <div class="inner-container"> fixed it. Changing it to any decimal seems to fix it. For instance if I use 5.5px or 3.5px that fixes it too. I don't understand why though 🤷‍♂️, would love to know why!
I had to use this fix in our application as the transition-time-function fixed didn't work in our more complicated layout.
body {
min-height: 100vh;
display: flex;
}
.main-container {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
}
.transition-container {
background: black;
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
margin: 20px;
transition-duration: 1.25s;
transition-property: margin, width, height;
}
.transition-container:hover {
margin: 0;
width: 120px;
height: 120px;
}
.inner-container {
border: 4.5px solid white;
width: 60px;
height: 60px;
}
<div class="main-container">
<div class="transition-container">
<div class="inner-container">
</div>
</div>
</div>

classed image not aligning vertically

My flex container holds three divs,
every div has property of flex-grow
I have a classed image that does not work on align-items: center;
this is the flex container
.top-container {
display: flex;
width: 100% !important;
height: 70px;
}
and this is the container inside
.top-notifications {
flex-grow: 6;
background-color: #F8F9F9;
text-align: right;
align-items: center;
}
and the image class is
.user-picture {
width: 35px;
height: 35px;
border-radius: 100%;
-moz-border-radius: 100%;
-webkit-border-radius: 100%;
}
every elements in boxes are aligned centered but the classed image
body {
margin: 0;
}
.top-container {
display: flex;
width: 100% !important;
height: 70px;
}
.logo {
background-color: #EAFAF1;
display:flex;
align-items:center;
}
.top-menu {
background-color: #FEF9E7;
display:flex;
align-items:center;
}
.top-notifications {
flex-grow: 1;
display:flex;
justify-content: flex-end;
align-items: center;
background-color: #F8F9F9;
}
/* top menu */
.top-menu ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #FEF9E7;
}
.top-menu li {
float: left;
}
.top-menu li a {
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
margin: 5px;
}
.top-menu li a:hover {
background-color: #85C1E9;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
margin: 5px;
}
/* end top menu */
.user-picture {
width: 35px;
height: 35px;
border-radius: 100%;
-moz-border-radius: 100%;
-webkit-border-radius: 100%;
}
<div class="top-container">
<div class="logo"><img src="images/logo.png" class="logo" /> </div>
<div class="top-menu">
<ul>
<li><a class="active" href="#home">Dashboard</a></li>
<li>Users</li>
<li>Settings</li>
</ul>
</div>
<div class="top-notifications">
Notifications<i class="fa fa-bell-o fa-lg"></i>
Reports<i class="fa fa-flag fa-lg"></i>
<img src="images/boy.png" class="user-picture" />
</div>
</div>
align-items:center; is used on the flex parent, not on the flex item itself. See css flex guide

Resources