I am trying to display these images horizontally in a flex box with justify-content: space-around.
It does not work, any ideas?
The only way that I was able to display it in a spaced manner was with padding.
Why doesn't justify-content work?
h3 {
font-weight: lighter;
font-size: 16px;
margin-top: 0;
margin-bottom: 0;
padding: 0 0 10px 0;
font-family: Proxima Nova;
letter-spacing: 1.92px;
color: #FFFFFF;
opacity: 0.7;
}
.flex-container {
flex-direction: column;
display: flex;
width: 100vw;
height: auto;
}
.trusted-by-container {
display: flex;
flex-flow: column;
align-items: center;
top: 781px;
}
<div class="trusted-by-container">
<div class=t itle>
<h3>TRUSTED BY</h3>
</div>
<ul class="trusting-companies-container">
<li class="company-item"><img src="./images/monday.svg" alt="monday" </li>
<li class="company-item"><img src="./images/intel.svg" alt="intel" </li>
<li class="company-item"><img src="./images/johnson.svg" alt="johnson" </li>
<li class="company-item"><img src="./images/handy.svg" alt="handy" </li>
<li class="company-item"><img src="./images/flexport.svg" alt="flexport" </li>
</ul>
</div>
Flex items will take the width of their content, leaving no extra space for keyword alignment properties (such as justify-content) to work.
Create some extra space; add width: 100%. (Also, close your img elements.)
h3 {
font-weight: lighter;
font-size: 16px;
margin-top: 0;
margin-bottom: 0;
padding: 0 0 10px 0;
font-family: Proxima Nova;
letter-spacing: 1.92px;
color: #FFFFFF;
opacity: 0.7;
}
.flex-container {
flex-direction: column;
display: flex;
width: 100vw;
height: auto;
}
.trusted-by-container {
display: flex;
flex-flow: column;
align-items: center;
top: 781px;
}
.trusting-companies-container {
width: 100%; /* new */
display: flex;
justify-content: space-around;
}
<div class="trusted-by-container">
<div class=t itle>
<h3>TRUSTED BY</h3>
</div>
<ul class="trusting-companies-container">
<li class="company-item"><img src="./images/monday.svg" alt="monday"></li>
<li class="company-item"><img src="./images/intel.svg" alt="intel"></li>
<li class="company-item"><img src="./images/johnson.svg" alt="johnson"></li>
<li class="company-item"><img src="./images/handy.svg" alt="handy"></li>
<li class="company-item"><img src="./images/flexport.svg" alt="flexport"></li>
</ul>
</div>
.companies-evenly {
justify-content: space-evenly;
}
.trusting {
padding: 0;
margin: 0;
list-style: none;
display: flex;
}
<div class="trusted-by-container">
<div class=title>
<h3>TRUSTED BY</h3>
</div>
<ul class="trusting companies-evenly">
<li class="company-item"><img src="./images/monday.svg" alt="monday" </li>
<li class="company-item"><img src="./images/intel.svg" alt="intel" </li>
<li class="company-item"><img src="./images/johnson.svg" alt="johnson" </li>
<li class="company-item"><img src="./images/handy.svg" alt="handy" </li>
<li class="company-item"><img src="./images/flexport.svg" alt="flexport" </li>
</ul>
</div>
Related
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 10 months ago.
As you can see the menu points are not centered vertically like the social media points, could someone please tell me how to do that, it's the first page I am working on. I would also like to scale down the space between the social media links, would be very grateful if someone could help me!
.menu-link {
font-family: 'Montserrat', sans-serif;
font-weight: 500;
font-size: 22px;
color: #262a2b;
text-decoration: none;
height: 45px;
}
#social-media {
width: 100px;
height: 100px;
margin: 0;
padding: 0;
}
.menu-link:hover {
color: #0088a9;
}
.active-menu-links {
color: white;
}
#normal-header {
position: fixed;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
width: 100%;
margin: auto;
font-size: 19px;
min-height: 100px;
}
#header-img {
height: 110px;
padding-top: 20%;
padding-bottom: 20%;
margin-left: 20%;
}
#nav-bar {}
#nav-bar ul {
list-style: none;
display: flex;
flex-flow: row;
}
#nav-bar li {
padding: 10px;
margin: 12px;
}
#nav-bar ul,
a {
text-decoration: none;
}
<nav id="nav-bar">
<ul>
<li class="nav-link" id="menu-item"> Home </li>
<li class="nav-link" id="menu-item"> About </li>
<li class="nav-link" id="menu-item"> Roadmap </li>
<li class="nav-link" id="menu-item"> Ecosystem </li>
<li class="nav-link" id="menu-item"> Team
<li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Instagram"></a>
</li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Twitter"></a>
</li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Discord"></a>
</li>
-->
</ul>
</nav>
I hope someone can help me. Thanks in advance!
So, I can't identify your problem because the snippet not working well as missing of files
But you can try to add align-items: center; to the #nav-bar ul selector
Or you can add display: flex align-items: center; to the #nav-bar li
You have several options:
Set line-height property of li or li a to the overall height
Set vertical-align: middle; (this doesn't always work for me)
Use paddings and margins (not recommended)
Set display: flex; and align-items: center to your li tag, but it can potentially break your layout
Since you made a snippet I tried and yes, you need line-height.✌️
To explain: If you use line-height with the same height as it's element. The two will match and make a easy menu button. Some side padding and/or margin, and you are done.
.menu-link {
font-family: 'Montserrat', sans-serif;
font-weight: 500;
font-size: 22px;
color: #262a2b;
text-decoration: none;
height: 45px;
line-height: 45px;
}
#social-media {
width: 100px;
height: 100px;
margin: 0;
padding: 0;
}
.menu-link:hover {
color: #0088a9;
}
.active-menu-links {
color: white;
}
#normal-header {
position: fixed;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
width: 100%;
margin: auto;
font-size: 19px;
min-height: 100px;
}
#header-img {
height: 110px;
padding-top: 20%;
padding-bottom: 20%;
margin-left: 20%;
}
#nav-bar {}
#nav-bar ul {
list-style: none;
display: flex;
flex-flow: row;
}
#nav-bar li {
padding: 10px;
margin: 12px;
}
#nav-bar ul,
a {
text-decoration: none;
}
<nav id="nav-bar">
<ul>
<li class="nav-link" id="menu-item"> Home </li>
<li class="nav-link" id="menu-item"> About </li>
<li class="nav-link" id="menu-item"> Roadmap </li>
<li class="nav-link" id="menu-item"> Ecosystem </li>
<li class="nav-link" id="menu-item"> Team
<li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Instagram"></a>
</li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Twitter"></a>
</li>
<li>
<a class="nav-link" href="xxx"><img id="social-media" src="xxx" alt="Discord"></a>
</li>
-->
</ul>
</nav>
The items are evenly spaced to flex containers in all menus except the Chinese calendars tab, I don't understand why.
They were aligned perfectly, but when I added links to the submenu items, it spread out randomly. I'm almost certain I have to add something in a{} to correct this, but I've tried the following, but no fix:
Tried erasing the links. No fix.
Tried copying the code from the submenu into a{} in CSS, worsens the problem.
* {
font-family: arial, sans-serif;
box-sizing: border-box;}
html, body {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: black;
}
.nav {
position:fixed;
top:0;
left:0;
background-color: rgba(255,255,255,.8);
border-radius: 0px;
border: none;
width: 100%;
margin: 0;
padding: 25px 0;
flex-direction: row;
display: flex;
align-items: center;
justify-content: space-evenly;
}
.item {
color: black;
font-weight: bold;
text-transform: uppercase;
}
.submenu {
display: none;
flex-wrap: wrap;
align-items: center;
align-text: center;
position: absolute;
padding-top: 107px;
padding: 10px;
left: 0;
right: 0;
text-transform: uppercase;
z-index: 1;
background-color: #2F4F4F;
color: white;
justify-content: space-evenly;
}
.submenu li {
margin-left: 6%;
width: 19%;
padding: 5px;
}
.item.has-children:hover .submenu {
display: flex;
align-items: center;
flex-direction: row;
justify-content: space-evenly;
flex-wrap: wrap;
flex: 1 1 calc(25% - 80px);
color: black;
background-color: rgba(255,255,255,.8);
}
ul {
list-style: none;
padding: 0;
}
<nav>
<ul class="nav">
<li class="item">
<a href="../index.html">
<img src="../Images/Navigation/Intak Logo 25px High.png" alt="Home" />
</a>
</li>
<li class="item has-children" style="color:#4D4D4D;">Printing
<ul class="submenu">
<li>Labels & Stickers</li>
<li>Banners</li>
<li>A-Frame</li>
<li>Menu Boards</li>
<li>Takeout Menus</li>
<li>Business Cards</li>
<li>Dine-In Menus</li>
<li>Posters</li>
<li>Envelopes</li>
<li>Chinese Wedding Cards</li>
<li>Flyers</li>
<li>Letterheads</li>
<li>Brochures</li>
<li>Vinyl</li>
<li>NCR Forms</li>
<li>Catalogues</li>
</ul>
</li>
<li class="item has-children">Graphic Design
<ul class="submenu">
<li>Logo Design</li>
<li>Ads/Flyers/Promotions</li>
<li style="text-align: center;">Menu Boards<br>
(Digital & Boards)</li>
<li style="text-align: center;">Restaurant Menus<br>
(Takeout & Dine-In)</li>
</ul>
</li>
<li class="item has-children">Chinese Calendars
<ul class="submenu">
<li>Cane Wallscroll Calendars</li>
<li>Wall Calendars</li>
<li>Mini Calendars</li>
<li>Desk Calendars</li>
<li>Special Desk Calendars</li>
<li>Packet Calendars</li>
<li>More Calendars</li>
</ul>
</li>
<li class="item">FAQS</li>
<li class="item">Contact Us</li>
</ul>
</nav>
Expected results is all nav submenu items to be aligned
You just have to change justify-content: space-evenly; to justify-content: left; for the .item.has-children:hover .submenu style.
The submenu style should be like this:
.item.has-children:hover .submenu {
display: flex;
align-items: center;
flex-direction: row;
justify-content: left;
flex-wrap: wrap;
flex: 1 1 calc(25% - 80px);
color: black;
background-color: rgba(255,255,255,.8);
}
As for the Chinese Calendar's submenu, there are only 7 items, which cause it to have 4 on the first row and 3 on the second row, thus causing them to look like they're not aligned because the space-evenly style cause them to be centered with even space.
It isn't related to the links.
By setting them to left, it will prevent the white space on the left messing up with the position of the 2nd row.
Before and after (with red border visualizing the boxes)
justify-content: space-evenly;
justify-content: left;
The nav bar and submenu are perfectly aligned when the view port is full width of the screen. However, when I get to 720px where the nav item "Chinese Calendars" becomes two lines, it creates a gap between the nav bar and the submenu drop down.
I've tried shifting code around and removing things, but can't figure it out. I tried float: right;, creates an overlap instead of falling right below.
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: black;
}
nav {
position: sticky;
position: -webkit-sticky;
top: 0;
}
.nav {
position: sticky;
left: 0;
background-color: rgba(255, 255, 255, .8);
border-radius: 0px;
border: none;
width: 100%;
padding: 10px 0;
flex-direction: row;
display: flex;
align-items: center;
justify-content: space-evenly;
}
.item {
color: black;
font-weight: bold;
text-transform: capitalize;
width: 25%;
text-align: center;
}
.submenu {
display: none;
flex-wrap: wrap;
margin-top: 29px;
align-items: center;
align-text: center;
position: absolute;
padding: 10px;
font-size: small;
left: 0;
right: 0;
text-transform: capitalize;
z-index: 1;
background-color: #2F4F4F;
color: white;
justify-content: left;
}
.submenu li {
margin-left: 6%;
width: 19%;
padding: 5px;
}
.submenuActive {
display: flex;
cursor: pointer;
align-items: center;
flex-direction: row;
justify-content: left;
flex-wrap: wrap;
flex: 1 1 calc(25% - 80px);
color: black;
background-color: rgba(255, 255, 255, .9);
}
/*.item.has-children:hover .submenu {
display: flex;
align-items: center;
flex-direction: row;
justify-content: left;
flex-wrap: wrap;
flex: 1 1 calc(25% - 80px);
color: black;
background-color: rgba(255,255,255,.8);
}*/
ul {
list-style: none;
padding: 0;
}
.logo {
display: block;
margin-top: 50px;
margin-bottom: 25px;
}
.logo img {
display: block;
margin: auto;
max-width: 70%;
}
<a class="logo" href="index.html">
<img src="../../Images/Navigation/Intak Logo 40px High.png" alt="Home" />
</a>
<nav>
<ul class="nav">
<li class="item">
<a href="../index.html">
<img src="../../Images/Navigation/Intak Nav Mark -01.png" alt="Home" />
</a>
</li>
<li class="item has-children">Printing
<!-- <ul class="submenu">
<li>Labels & Stickers</li>
<li>Banners</li>
<li>A-Frame</li>
<li>Menu Boards</li>
<li>Takeout Menus</li>
<li>Business Cards</li>
<li>Dine-In Menus</li>
<li>Posters</li>
<li>Envelopes</li>
<li>Chinese Wedding Cards</li>
<li>Flyers</li>
<li>Letterheads</li>
<li>Brochures</li>
<li>Vinyl</li>
<li>NCR Forms</li>
<li>Catalogues</li>
</ul> -->
</li>
<li class="item has-children">Graphic Design
<!-- <ul class="submenu">
<li>Logo Design</li>
<li>Ads/Flyers/Promotions</li>
<li style="text-align: center;">Menu Boards<br>
(Digital & Boards)</li>
<li style="text-align: center;">Restaurant Menus<br>
(Takeout & Dine-In)</li>
</ul>-->
</li>
<li class="item has-children" style="color:#4D4D4D;">Chinese Calendars
<ul class="submenu">
<li>Cane Wallscroll Calendars</li>
<li>Wall Calendars</li>
<li>Mini Calendars</li>
<li>Desk Calendars</li>
<li>Special Desk Calendars</li>
<li>Lucky Money Envelopes</li>
<li>More Calendars</li>
</ul>
</li>
<!-- <li class="item">FAQS</li> -->
<li class="item">Contact Us</li>
</ul>
</nav>
I need it so regardless of viewport size, the submenu is always touching the navigation bar
https://jsfiddle.net/nqjv3xs5/1/#&togetherjs=lgy6QvwL4g
Greater than 720px:
Less than 720px:
I am attempting to list a series of same sized elements. I want these elements to display with even spacing on the right and left (vertically centered?), and evenly spaced between each other. The biggest problem is that the list needs to be able to adjust to screen size changes and number of element changes. As such the width and elements per line need to update as necessary. The bottom row should also ideally align with those above it.
This is the closest that I have been able to get so far.
HTML
<div class="outer">
<div class="inner">
</div>
</div>
... repeated as any times as there are blocks.
<div class="outer">
<div class="inner">
</div>
</div>
CSS
body {
text-align: justify;
margin:0;
width: auto;
}
.outer {
background:blue;
width: 100px;
height: 90px;
display: inline-block;
text-align: center;
}
.inner {
background:red;
width: 90px;
height: 90px;
display: inline-block;
text-align: center;
}
JSFiddle
Sounds like a job for flexbox. One of these work for you? https://codepen.io/anon/pen/VEpbjv
HTML
<ul class="flex-container space-between">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>
<ul class="flex-container space-around">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>
<ul class="flex-container space-evenly">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>
CSS
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
}
.flex-start {
justify-content: flex-start;
}
.flex-end {
justify-content: flex-end;
}
.flex-end li {
background: gold;
}
.center {
justify-content: center;
}
.center li {
background: deepskyblue;
}
.space-between {
justify-content: space-between;
}
.space-between li {
background: lightgreen;
}
.space-around {
justify-content: space-around;
}
.space-around li {
background: hotpink;
}
.space-evenly {
justify-content: space-evenly;
}
.space-evenly li {
background: #bada55;
}
.flex-item {
background: tomato;
padding: 5px;
width: 60px;
height: 50px;
margin: 5px;
line-height: 50px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
I have the following scenario:
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
flex-wrap:wrap;
justify-content: space-around;
}
.flex-item {
background: tomato;
padding: 5px;
width: 150px;
height: 75px;
margin-top: 10px;
line-height: 75px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
}
<ul class="flex-container">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>
http://codepen.io/HugoGiraudel/pen/LklCv
I want one thing changed:
The left and right divs should not have a space between the parent div and itself.
When I do justify-content: space-between, that problem is solved but then the divs are no longer centered.