I'm trying to learn more about flex.
I want to add more columns to a flex layout but I can only get two columns.
When I try to add a third column, the div's just stack.
Here's the two col version and my attempt to get a third column...
http://brinsterinc.com/footer.html
http://brinsterinc.com/footer1.html
Here's the CSS...
footer[is=footer] {
background: hsl(var(--tertiary));
display: grid;
gap: 3rem;
grid:
"menu"
"menu1"
"menu2"
"policies"
"copyright";
padding: clamp(4rem,8vw,8rem) max((100% - 80rem) / 2,4vw);
}
footer[is=footer]>div>ul {
all: unset;
display: flex;
flex-flow: column wrap;
gap: 1rem;
}
footer[is=footer]>div:nth-of-type(1) {
display: flex;
flex-direction: column;
gap: 2rem 4rem;
grid-area: menu;
}
footer[is=footer]>div:nth-of-type(2) {
display: flex;
flex-direction: column;
gap: 2rem 4rem;
grid-area: menu1;
}
footer[is=footer]>div:nth-of-type(3) {
display: flex;
flex-direction: column;
gap: 2rem 4rem;
grid-area: menu2;
}
footer[is=footer]>div:nth-of-type(4) {
grid-area: policies;
}
footer[is=footer]>p {
all: unset;
color: hsl(var(--on-tertiary));
grid-area: copyright;
}
#media (min-width: 50em) {
footer[is=footer] {
grid: "menu menu1 menu2" "copyright policies" / -webkit-max-content 1fr;
grid:
"menu menu1 menu2"
"copyright policies" / max-content 1fr;
place-items: start;
}
footer[is=footer]>div:nth-of-type(4)>ul {
-moz-column-gap: clamp(1rem,4vw,2rem);
column-gap: clamp(1rem,4vw,2rem);
flex-direction: row;
}
}
Put a flex wrapper round the items you want to put in a column. I think the grid markup is causing some problems though, you're allocating them to different grid areas and without setting grid-template-rows or grid-auto-rows, the child divs will stack on top of each other.
footer[is=footer] {
background: hsl(var(--tertiary));
/* display: grid;
gap: 3rem;
grid:
"menu"
"menu1"
"menu2"
"policies"
"copyright";
padding: clamp(4rem, 8vw, 8rem) max((100% - 80rem) / 2, 4vw); */
}
.flexContainer {
display: flex;
}
.flexContainer div {
flex: 1 0 auto;
}
footer[is=footer]>div>ul {
/* all: unset;
display: flex;
flex-flow: column wrap;
gap: 1rem; */
}
footer[is=footer]>div:nth-of-type(1) {
/* display: flex;
flex-direction: column;
gap: 2rem 4rem;
grid-area: menu; */
}
footer[is=footer]>div:nth-of-type(2) {
/* display: flex;
flex-direction: column; */
/* gap: 2rem 4rem;
grid-area: menu1; */
}
footer[is=footer]>div:nth-of-type(3) {
/* display: flex;
flex-direction: column; */
/* gap: 2rem 4rem;
grid-area: menu2; */
}
footer[is=footer]>div:nth-of-type(4) {
/* grid-area: policies; */
}
footer[is=footer]>p {
/* all: unset;
color: hsl(var(--on-tertiary));
grid-area: copyright; */
}
/* #media (min-width: 50em) {
footer[is=footer] {
grid: "menu menu1 menu2""copyright policies" / -webkit-max-content 1fr;
grid:
"menu menu1 menu2"
"copyright policies" / max-content 1fr;
place-items: start;
}
footer[is=footer]>div:nth-of-type(4)>ul {
-moz-column-gap: clamp(1rem, 4vw, 2rem);
column-gap: clamp(1rem, 4vw, 2rem);
flex-direction: row;
}
} */
<footer is="footer">
<div class="flexContainer">
<div>
<ul>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
</ul>
</div>
<div class="oakhide">
<ul>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
</ul>
</div>
<div>
<ul>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
</ul>
</div>
</div>
<div>
<ul>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
<li>
<a href="">
link
</a>
</li>
</ul>
</div>
<p><small>Copyright © 2022</small></p>
</footer>
Related
In menu item, i am using display: Flex
on hover there is space between items
Used: Align-content: stretch but no result
see image: space showing
My Code
.menu {
display: flex;
justify-content: space-between;
}
.menu li {
display: flex;
padding: 10px;
}
.menu li:hover {
background-color: #ddd;
}
.menu li a {
display: flex;
flex-direction: column;
align-items: center;
}
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css"
/>
<ul
class="menu dflx border-top-light px-10 container py-10 justify-between items-center"
>
<li>
<i class="fa-solid fa-magnifying-glass"></i> Explore
</li>
<li>
<i class="fa-solid fa-location-dot"></i> Attractions
</li>
<li>
<i class="fa-regular fa-star"></i> Latest Deals
</li>
<li>
<i class="fa-solid fa-bars"></i> Menu
</li>
</ul>
Just use flex: 1; justify-content: center; in .menu li
Working example
.menu {
display: flex;
justify-content: space-between;
}
.menu li {
display: flex;
padding: 10px;
flex: 1;
justify-content: center;
}
.menu li:hover {
background-color: #ddd;
}
.menu li a {
display: flex;
flex-direction: column;
align-items: center;
}
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css"
/>
<ul
class="menu dflx border-top-light px-10 container py-10 justify-between items-center"
>
<li>
<i class="fa-solid fa-magnifying-glass"></i> Explore
</li>
<li>
<i class="fa-solid fa-location-dot"></i> Attractions
</li>
<li>
<i class="fa-regular fa-star"></i> Latest Deals
</li>
<li>
<i class="fa-solid fa-bars"></i> Menu
</li>
</ul>
My first question here and your help would be much appreciated
I have a ul width display: grid inside a div with overflow: hidden.
The ul's purpose is to function as a slidable element, so its width is not explicitly specified, but rather, defined by its contents. Each li element inside the list contains text of variable length, which should not be truncated nor break on a 2nd line.
The li count inside the list may vary, from 4 to up to 12 elements.
My question is how can I create a grid that all its cells have the width of the widest li? (which, in turn, is defined by the longest text)
Please keep in mind that the grid is always going to have 2 rows
Here is a simplified version of what I am trying to accomplish:
.container {
overflow: hidden;
background: #fafafa;
width: 760px;
margin: 0 auto;
padding: 24px;
}
ul {
overflow: auto;
list-style: none;
padding: 0;
display: grid;
grid-gap: 8px;
grid-auto-flow: column;
grid-auto-columns: 1fr;
grid-template-rows: repeat(2, 1fr);
}
li {
border: 1px solid #eee;
border-radius: 20px;
padding-inline: 12px;
display: flex;
gap: 4px;
align-items: center;
}
a {
white-space: nowrap;
}
<div class='container'>
<ul>
<li>
<img src='https://unsplash.it/50/50'>
<a>text 1</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 2</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 3</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Fourth text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Test 5 is is a huge text and all other cells should follow its size</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 6</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>This is also a large text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 8</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 9</a>
</li>
</ul>
</div>
.container {
overflow: hidden;
background: #fafafa;
width: 760px;
margin: 0 auto;
padding: 24px;
}
ul {
overflow: auto;
list-style: none;
padding: 0;
display: grid;
grid-gap: 8px;
grid-auto-flow: column;
grid-templates-column: auto auto auto auto;
grid-template-rows: auto auto;
}
li {
border: 1px solid #eee;
border-radius: 20px;
padding-inline: 12px;
display: flex;
gap: 4px;
align-items: center;
}
a {
white-space: nowrap;
}
<div class='container'>
<ul>
<li>
<img src='https://unsplash.it/50/50'>
<a>text 1</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 2</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 3</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Fourth text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Test 5 is is a huge text and all other cells should follow its size</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 6</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>This is also a large text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 8</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 9</a>
</li>
</ul>
</div>
.container {
overflow: hidden;
background: #fafafa;
width: 760px;
margin: 0 auto;
padding: 24px;
}
ul {
overflow: auto;
list-style: none;
padding: 0;
display: grid;
grid-gap: 8px;
grid-auto-flow: column;
grid-auto-columns: 1fr;
grid-template-rows: repeat(2, 1fr);
}
li {
border: 1px solid #eee;
border-radius: 20px;
padding-inline: 12px;
display: flex;
gap: 4px;
align-items: center;
}
a {
white-space: nowrap;
}
<div class='container'>
<ul>
<li>
<img src='https://unsplash.it/50/50'>
<a>text 1</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 2</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 3</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Fourth text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Test 5 is is a huge text and all other cells should follow its size</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 6</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>This is also a large text</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 8</a>
</li>
<li>
<img src='https://unsplash.it/50/50'>
<a>Text 9</a>
</li>
</ul>
</div>
The problem is this image, I want to make it circular, I used border-radius:50% and set equal height and width. I searched for the question and found this one How to make a circular image in css and I tried to make something similar but I used flexbox.
<div className='nav-bar'>
<ul className='nav-links'>
<li className='nav-link'>
<Link >
Home
</Link>
</li>
<li className='nav-link'>
<Link >
Add question
</Link>
</li>
<li className='nav-link'>
<Link >
Leaderboard
</Link>
</li>
</ul>
<ul className='nav-user-sign-out'>
<li className='nav-item'>
<p>Welcome, </p>
</li>
<li className='nav-item'>
<div className='avatar'>
<img src={} alt='' />
</div>
</li>
<li className='nav-item'>
<button className='sign-out'>
Sign out
</button>
</li>
</ul>
</div>;
.nav-bar {
display: flex;
align-items: center;
justify-content: space-between;
}
.nav-links {
display: flex;
}
Link {
padding: 10px;
}
.nav-user-sign-out {
display: flex;
align-items: center;
justify-content: space-around;
}
.sign-out {
padding: 10px;
}
.avatar {
width: 30px;
height: 30px;
border-radius: 50%;
overflow: hidden;
}
img {
height: 100%;
width: 100%;
}
Either make height and width of fixed and equal value or if you are using percentage confirm to that image has same width and height.
I just made a codesandbox with your code, and it's working without an issue, maybe the issue is with another CSS class that declared somewhere else, or maybe the object-fit property on the image. I'll put the link to my sandbox here, feel free to go ahead and try it with your code. I added object-fit to the image styles there.
working example
.nav-bar {
display: flex;
align-items: center;
justify-content: space-between;
}
.nav-links {
display: flex;
}
Link {
padding: 10px;
}
.nav-user-sign-out {
display: flex;
align-items: center;
justify-content: space-around;
}
.sign-out {
padding: 10px;
}
.avatar {
width: 30px;
height: 30px;
border-radius: 50%;
overflow: hidden;
}
img {
height: 100px;
width: 105px;
border-radius: 50%;
}
<div className='nav-bar'>
<ul className='nav-links'>
<li className='nav-link'>
<Link >
Home
</Link>
</li>
<li className='nav-link'>
<Link >
Add question
</Link>
</li>
<li className='nav-link'>
<Link >
Leaderboard
</Link>
</li>
</ul>
<ul className='nav-user-sign-out'>
<li className='nav-item'>
<p>Welcome, </p>
</li>
<li className='nav-item'>
<div className='avatar'>
<img src="https://media.sproutsocial.com/uploads/2015/02/Facebook_Embed.png" alt=''width ="200" />
</div>
</li>
<li className='nav-item'>
<button className='sign-out'>
Sign out
</button>
</li>
</ul>
</div>
I am new to css. I will like to center brand logo and also the links in the navbar.
HTML
<nav class="navbar navbar-expand-md center">
<a class="navbar-brand" href="http://www.dimsum.dk" target="_blank">
<img src={% static 'media/HIDDEN DIMSUM logo final.png'%} width="100" alt="">
</a>
<button class="navbar-toggler navbar-dark" type="button" data-toggle="collapse" data-target="#main-navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="main-navigation">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Book Table</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.dimsumbox.dk" target="_blank">
Order Dimsum Box</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://www.dimsumshop.dk" target="_blank">
Order Takeaway</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" target="_blank">
About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" target="_blank">
Contact</a>
</li>
</ul>
</div>
</nav>
CSS
.navbar {
background: #d48498;
}
.nav-link {
color: black;
font-size: 24px;
font-weight: bold;
}
.navbar.center .nav-item {
text-align: center;
display: inline-block;
vertical-align: middle;
}
This is how the navbar looks right now
I want to have the logo centered and the links appearing in a new line also centered so it looks like
Logo
Link 1 Link 2 Link 3 Link 4
I have tried justify center in css but it doesn't work.
Add this bit to your css
#media (min-width: 768px) {
.navbar {
flex-flow: wrap;
}
.navbar-brand {
width: 100%;
text-align: center;
}
.navbar-brand img {
height: 50px; /*Set a logo size*/
width: auto;
}
.navbar-collapse {
justify-content: center;
}
}
use justify content: center and display: flex in css
display: flex;
justify-content: center;
for reference https://www.w3schools.com/cssref/css3_pr_justify-content.asp
add this css after bootstrap.css:
.navbar-brand {
display: flex;
width: 100%;
justify-content: center !important;
}
.navbar {
flex-wrap: wrap !important;
justify-content: center !important;
}
.navbar-nav{
width:100%;
justify-content:center !important
}
I want to style my unordered list like this one below
Link to what i want to achive
My structure looks like:
<div class="tabs">
<ul class="tabs__labels">
<li class="0">
<a class="active" href="#">
<img/>
<div>something</div>
</a>
</li>
<li class="1">
<a class="" href="#">
<img/>
<div>something1</div>
</a>
</li>
...
</ul>
</div>
Is it a good idea to name the list classes like 0,1 etc. and style them separately to somehow achive this effect? How to use flexbox to get similar effect? Should i group them into rows?
Would this be a start?
.tabs__labels {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
list-style: none;
margin: 0;
padding: 0;
}
.tabs__labels li {
flex-basis: 45%;
margin: 20px 0;
padding: 0;
}
.tabs__labels li:nth-child(odd) {
display: flex;
justify-content: flex-end;
}
.tabs__labels li:nth-child(even) {
display: flex;
}
.tabs__labels li:nth-child(3),
.tabs__labels li:nth-child(4) {
flex-basis: 35%;
}
.tabs__labels li a {
display: flex;
justify-content: center;
align-items: center;
height: 40px;
width: 40px;
border-radius: 50%;
border: 1px solid red;
text-decoration: none;
}
.tabs__labels li:nth-child(1)::before {
content: 'some text';
text-decoration: underline;
margin-right: 10px;
}
.tabs__labels li:nth-child(2)::after {
content: 'some text';
text-decoration: underline;
margin-left: 10px;
}
<div class="tabs">
<ul class="tabs__labels">
<li>
<a class="active" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
<li>
<a class="" href="#">S
</a>
</li>
</ul>
</div>