This question already has answers here:
Align 3 unequal blocks left, center and right
(4 answers)
Closed last month.
I have 3 divs that contain text. The text has different lengths, meaning that if I put these three divs into another div, if I justify-content: center this div, the middle one out of the three divs won't be in the center of the page.
What I want to achieve is to have the middle div in the center of the webpage and the first div to be to left of it and the third one to the right, with no space between (except padding).
Edit: I don't know if I maybe was unclear about what I am asking: I want to center .container-footer-3 but .footer-company ("Center") should be in the center of the page, not in the center of the div.
-> In other words, how do I center the parent div, containing all these 3 divs but not the center of the div to the center page, instead "Center" div to the center of the page?
.container-footer-3 .nav {
display: inline-table;
padding: 0 20px 0 20px;
}
.footer-nav {
display: flex;
justify-content: center;
}
.footer-company{
position: absolute;
left: 50%;
margin-left: -50px;
width: 100px;
height: 100px}
<div class="footer-nav">
<div class="container-footer-3">
<div class="footer-explore nav">
<h20>Left</h20>
<ul>
</ul>
</div>
<div class="footer-company nav">
<h20>Center</h20>
<ul>
</ul>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight</h20>
<ul>
</ul>
</div>
</div>
</div>
If you want to use flex, then you can make all three divs share the same width by adding flex-basis: 100% to the children within the flex container. I simplified your HTML to better show how it can be done. 100% just states that they are all equally treated. It's not like 100% of width.
You will then need to add an extra div inside .footer-explore if you want to align it to the right.
Do note, I added the outline to make the alignments more clear.
.footer-nav {
display: flex;
justify-content: center;
align-items: top;
}
.nav {
padding: 0 20px 0 20px;
flex-basis: 100%;
}
* {
outline: 1px solid grey;
}
<div class="footer-nav">
<div class="footer-explore nav">
<h20>Left</h20>
</div>
<div class="footer-company nav">
<h20>Center</h20>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight RightRightRightRight</h20>
</div>
</div>
You can also use a grid, and set the middle column to adapt to it's content, and the two divs on the side to fill up the rest of the space (using 1fr). Again, you need to add an extra div inside .footer-explore if you want to align it to the right. I added an outline here too for clarity.
.footer-nav {
display: grid;
grid-template-columns: 1fr min-content 1fr;
}
.nav {
padding: 0 20px 0 20px;
}
* {
outline: 1px solid grey;
}
<div class="footer-nav">
<div class="footer-explore nav">
<h20>Left</h20>
</div>
<div class="footer-company nav">
<h20>Center</h20>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight RightRightRightRight</h20>
</div>
</div>
There's already a lot of answers, but I figured I'd give my 2 cents seeing as none of the answers actually show an example where the leftmost and rightmost columns aren't expanded to full width.
In a grid-layout, you can define the 3 columns with:
grid-template-columns: minmax(max-content, 1fr) auto minmax(max-content, 1fr);
The first and the last column (left and right) will have their column stretch to fit its content width, while the layout stays a 3-column layout.
The middle column will just have a value of auto.
You can then specify each grid-item's positioning with justify-self, which works in both flex- and grid-layouts - I used flex in my example.
This solution isn't fully responsive, but you can specify e.g. a max-width-value on the grid-items which will make it responsive for most screens, not small screens such as mobile devices though.
.container-footer-3 {
display: grid;
grid-template-columns: minmax(max-content, 1fr) auto minmax(max-content, 1fr);
justify-items: center;
grid-gap: 40px;
}
.nav {
padding: 0 20px 0 20px;
border: 1px solid black;
display: flex;
flex-direction: column;
max-width: 150px;
}
.footer-explore {
justify-self: right;
}
.footer-company {
justify-self: center;
}
.footer-socials {
justify-self: left;
}
<div class="footer-nav">
<div class="container-footer-3">
<div class="footer-explore nav">
<h20>Left</h20>
<ul>
</ul>
</div>
<div class="footer-company nav">
<h20>Center</h20>
<ul>
</ul>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight</h20>
<ul>
</ul>
</div>
</div>
</div>
<div class="footer-nav">
<div class="container-footer-3">
<div class="footer-explore nav">
<h20>Left Left Left Left Left</h20>
<ul>
</ul>
</div>
<div class="footer-company nav">
<h20>Center Center Center</h20>
<ul>
</ul>
</div>
<div class="footer-socials nav">
<h20>Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right </h20>
<ul>
</ul>
</div>
</div>
</div>
#grid {
display: grid;
grid-template-rows: 1fr;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
width: 100vw;
height: 100vh;
}
#div1 {
grid-area: 1 / 1 / 2 / 2;
background-color: rgba(29, 240, 35, 0.5);
}
#div2 {
grid-area: 1 / 2 / 2 / 3;
background-color: rgba(213, 209, 50, 0.5);
}
#div3 {
grid-area: 1 / 3 / 2 / 4;
background-color: rgba(158, 43, 127, 0.5);
}
<div id="grid">
<div id="div1">div1</div>
<div id="div2">div2</div>
<div id="div3">div3</div>
</div>
I don't know if that correspond exactly to what you are looking, but css grid could be your answer
A flex box solution is as follows:
The left and right div are allowed to grow using flex-grow:1; The center is given a flex-grow of 0 so it shrinks. If we set the left and right divs with a flex-basis of 100% then they'll expand to equal size. I've put a red line where the center of the screen is so you can see the center div is actually centered.
.container-footer-3 {
display: flex;
gap: 0.5rem;
}
.container-footer-3 > div {
flex-grow:1;
outline:1px solid blue;
flex-basis:100%;
}
.container-footer-3 > div:first-child {
text-align:right;
}
.container-footer-3 > div:nth-child(2) {
flex-grow:0;
flex-basis:0;
}
.centerline {
border:1px solid red;
height:50px;
width:0px;
position:absolute;
left:50%;
top:0;
}
<div class="footer-nav">
<div class="container-footer-3">
<div class="footer-explore nav">
<h20>Left</h20>
</div>
<div class="footer-company nav">
<h20>Center</h20>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight</h20>
</div>
</div>
</div>
<div class='centerline'></div>
Related
I have 4 items inside a flex container. I want to make the 4 of them the same width and height, but always having them in 2 different rows (so a 2 X 2 grid).
Since the children are in different flex containers, they do not obey flex-grow: 1; Even the children of the same row do not obey the rule. And if I put them in the same container, they put themselves in the same row, and I need the 2 X 2 grid.
You can find a codepen here with the same code: https://codepen.io/mongolhippie/pen/yLYQbVd?editors=1100
.tile {
display: flex;
flex-direction: column;
border: 2px solid #A97C50;
border-radius: 20px;
padding: 20px;
margin: 25px;
/* THESE 3 OPTIONS TO CONTROLL HAVING THE SAME SPACE FOR EVERY TILE */
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
flex: 1;
}
.tile p {
font-size: min(calc( 1.125vw + 1.2rem ), 3.9rem);
}
.flex-center{
display: flex !important;
justify-content: center !important;
align-items: center;
}
.manual-link {
text-decoration: none !important;
color: var(--brown-dark);
}
.manual-link:hover {
text-decoration: none !important;
color: var(--brown-dark);
}
.icon-and-title{
display: flex;
align-items: center;
}
.icon-and-title p{
font-size: min(calc( 1.125vw + .9rem ), 3.9rem);
}
.icon-and-title img{
max-width: 100%;
height: auto;
transition: all .3s;
max-height: 80px;
margin-right: 20px;
margin-bottom: 10px;
}
<div class="flex-center">
<a class="manual-link" href="/manuals/manual-it.pdf">
<div class="tile">
<div class="icon-and-title">
<img src="https://i.pinimg.com/originals/06/bc/e8/06bce81285badba0c3becd273ca67f95.png" alt="">
<p>ADMIN</p>
</div>
<div class="links-manuals">
<p>For the administrator of the app</p>
</div>
</div>
</a>
<a class="manual-link" href="/manuals/manual-developers.pdf">
<div class="tile">
<div class="icon-and-title">
<img src="https://i.pinimg.com/originals/06/bc/e8/06bce81285badba0c3becd273ca67f95.png" alt="">
<p>DEVELOPERS</p>
</div>
<div class="links-manuals">
<p>To upgrade the Code</p>
</div>
</div>
</a>
</div>
<div class="flex-center">
<a class="manual-link" href="/manuals/manual-design.pdf">
<div class="tile">
<div class="icon-and-title">
<img src="https://i.pinimg.com/originals/06/bc/e8/06bce81285badba0c3becd273ca67f95.png" alt="">
<p>DESIGNERS</p>
</div>
<div class="links-manuals">
<p>Style guide</p>
</div>
</div>
</a>
<a class="manual-link" href="/manuals/ngo.pdf">
<div class="tile">
<div class="icon-and-title">
<img src="https://i.pinimg.com/originals/06/bc/e8/06bce81285badba0c3becd273ca67f95.png" alt="">
<p>NGOs</p>
</div>
<div class="links-manuals">
<p>The project</p>
</div>
</div>
</a>
</div>
Which is the secret to having them in a 2 X 2 disposition with the same width and height??
Thanks a lot!
Simply move from Flexbox to CSS GRID.
So change the CSS like this:
.flex-center{
display: grid;
grid-template-columns: 1fr 1fr;
}
Since you are handling your layout in 2 different rows we'll play with the grid-template-columns.
The fr unit tells the grid to have to "cells" with the same width.
Better renaming the class from .flex-center to something else since you'll use CSS GRID.
Here the updated working Codepen
Flebox are very good to make a 1D flexible container (vertically or horizontally) and can also handle multi-lines when they is no more space for all elements (instead of overflow the container)
BUT They is another display type used mainly for 2D flexible grid. And it's called grid, you should look at this guid in order to use it right.
As you want by default a 2D (2x2) grid, it'll be way easier to use it, This is a very basic grid you can set in order to have both columns and rows to take half of available space (50%)
.container {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 50% 50%;
}
Hi I am trying to align the bottombar elements so that they are in 2 columns on the side of 102. I was wondering if there is a way to fix it as they are all floating on the right at the moment. I am a beginner html css programmer and I am not very experienced yet. Ill appreciate any help you can give me!
CSS
/*bottom navbar*/
.bottomnav{
width: 100%;
background-color: rgb(248, 138, 180);
display: flex;
flex-direction: row;
}
.navbarlogo2{
display: block;
margin-left: auto;
margin-right: auto;
width: 10%;
text-decoration: none;
}
/*bottombar*/
.nav {
display: flex;
flex-direction: row;
}
.left, .right {
flex: 1;
}
HTML
<div class="bottomnav">
<ul class="bottomlogo">
<li class="navbarimg2"><img class="navbarlogo2" src="img/LOGO.png"></li>
</ul>
<div class='nav'>
<div class='left'>
<ul>
<li>About Us</li>
<li>Affiliates</li>
</ul>
</div>
<div class='right'>
<ul>
<li>TOS</li>
</ul>
</div>
</div>
</div>
END RESULT
WANTED RESULT
I made things like that. CSS Grid is one of the new HTML5 standard you should take a look. In your case, use a grid is better choice against flex because you're looking for a table-like structure.
I choosed to split your needs in 2 parts:
Center your logo
Make a 2 columns grid for your links
Centering your logo
We need to center an element and prevent it to interfere with our incoming links grid. So we'll set our container with a position: relative and place the img tag in position: absolute. Note the image's top right bottom left properties are now relative to the first parent positioned as relative.
And so we only need to make some simple maths. Note the calc() function, we don't want to center the top left corner of your logo but the center. So we need to remove the half of the defined logo's width.
navbarlogo2 {
left: calc(50% - 60px);
}
Make a 2 columns grid for your links
In order make a grid, you have to display your container as grid and set its grid-template-columns to 1fr 1fr. You can translate fr with the word fraction. So here, we're asking for a row split in 2 fractions.
Because we want a place for our logo, we're adding a gap (grid-cap) in out container to make some space between our 2 columns.
Learn more about the fr unit here.
body {
margin:0
}
.bottomnav {
width: 100%;
background-color: rgb(248, 138, 180);
position: relative;
}
.navbarlogo2 {
display: block;
margin-left: auto;
margin-right: auto;
width: 120px;
text-decoration: none;
position: absolute;
filter: brightness(10);
top: 15px;
left: calc(50% - 60px) /*center top left corner then remove half logo width (120px)*/
}
/*bottombar*/
.nav {
display: grid;
grid-gap: 120px;
grid-template-columns: 1fr 1fr;
}
.nav ul {
padding-left: 0;
}
.nav ul li {
list-style: none;
text-align: center;
padding-left: 0;
}
.left,
.right {
flex: 1;
}
<div class="bottomnav">
<div class="bottomlogo">
<img class="navbarlogo2" src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg">
</div>
<div class='nav'>
<div class='left'>
<ul>
<li>About Us</li>
<li>Affiliates</li>
</ul>
</div>
<div class='right'>
<ul>
<li>TOS</li>
<li>Fourth </li>
</ul>
</div>
</div>
</div>
This question already has answers here:
Targeting flex items on the last or specific row
(10 answers)
Wrapping flex items in the last row [duplicate]
(1 answer)
Closed 3 years ago.
I have a list of items I want to display with CSS. Originally, it was only two items side-by-side on one line but now I want to make it responsive for larger screens so I want to make it display 3 items on one line instead. My old code looks like this with justify-content:space-between. It looks good with an odd number of items to display.
.flex-container-old{
margin-top: 50px;
background: magenta;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.box-old{
width: 40%;
border: 1px solid black;
margin-bottom: 20px;
height: 300px;
background: orange;
}
.wrapper{
margin: 0 auto;
width: 80%;
}
body{
background:#D3D3D3;
}
<div class="wrapper">
<div class="flex-container-old">
<div class="box-old">
</div>
<div class="box-old">
</div>
<div class="box-old">
</div>
<div class="box-old">
</div>
<div class="box-old">
</div>
</div>
</div>
So naturally I extended it to three items in one row by modifying the width property only to end up with the below.
.flex-container-new{
background: lightblue;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.box {
width: 30%;
border: 1px solid black;
margin-bottom: 20px;
height: 300px;
background: orange;
}
.wrapper{
margin: 0 auto;
width: 80%;
}
<div class="wrapper">
<div class="flex-container-new">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
</div>
My problem in the case of the above code with three items on one line is I want the last item in last row to be pushed to the left, aligned with the middle item in the row above it. Sadly bootstrap is not an option. This is for learning purposes. It there a way I can achieve the above with just CSS? Many thanks in advance.
This is easier to control using CSS Grid because we can dictate both the x and y axis. With Flexbox, you can only reliably control the x axis. If you haven't heard about the fr unit, it's defined by Mozilla as follows:
The fr, which is short for “fraction”, is a unit which represents a fraction of the available space in the grid container.
Another nice thing about using Grid is that we can drop the height and margin-bottom set in .box and also the flex-wrap rule. Everything about the layout of this grid, from the height of the cells to the grid-gap spacing between them, is all defined in the parent.
.grid-container-new {
background: lightblue;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 300px);
grid-gap: 20px;
}
.box {
border: 1px solid black;
background: orange;
}
<div class="grid-container-new">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
jsFiddle
#michael-benjamin Center and right align flexbox elements almost what I want, but with 2 differences:
Div have to take empty space at left side too (when it be wide)
Div have to collapse text when there is no empty space around at all.
What I have?
A is main centered object and BBBBBBBBBBBBBBB is right object.
This is fine till A will grow:
What I want?
Is it possible to do with flex/grid/table-tr-td/any other css tricks?
What I tried?
CSS Positioning Properties - can't stop when grow to B
Flex Auto Margins & Invisible Flex Item (DOM element) - invisible item don't give empty space at left side
CSS Grid Layout - 1fr don't give empty space at left side
CSS grid can do it like below:
.container {
margin: 10px;
display: grid;
grid-template-columns: 1fr minmax(0,max-content) 1fr;
color: #fff;
}
.container::before {
content:"";
}
.b {
margin-left: auto;
background: grey;
}
.a {
margin: auto;
background: blue;
overflow:hidden;
text-overflow:ellipsis;
max-width:100%;
}
body {
background:linear-gradient(red,red) center/ 2px 100% no-repeat;
}
<div class="container">
<div class="a">AA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
<div class="container">
<div class="a">AA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
<div class="container">
<div class="a">AAAAAAAAAAAAAA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
<div class="container">
<div class="a">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
<div class="container">
<div class="a">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</div>
<div class="b">BBBBBBBBBBBBBBBB</div>
</div>
I am making a music playback controller, and the container has 3 sections: left, center, and right. However, since the left and right sides have different widths, the center section isn't in the true center of the div, but I need it to be. I am using flexbox's space-between option to layout the items.
#container {
display: flex;
justify-content: space-between;
background-color: lightgrey;
}
#container > div {
height: 100px;
border: 2px dashed red;
/*This is only for looks*/
text-align: center;
padding: 5px;
}
<div id="container">
<div>Left Side</div>
<div>I want this centered</div>
<div>Right Side (Extra text for extra length)</div>
</div>
You can use margins to approximate centering. But in order to get perfect centering with flexbox that's consistent across a variety of viewports, you'll have to slightly modify your HTML somewhat.
You need to turn the direct children of #container into flex containers themselves with a display:inline-flex declaration and give them a flex value of 1 and justify-content: center.
From there, you add your content into child divs. To get alignment on the left and right divs, use margin-right: auto and margin-left: auto, respectively.
#container {
display: flex;
background-color: lightgrey;
}
.flex {
flex: 1;
display: inline-flex;
justify-content: center;
}
.flex > div {
height: 100px;
border: 2px dashed red;
text-align: center;
padding: 5px;
}
.left div {
margin-right: auto;
}
.right div {
margin-left: auto;
}
<div id="container">
<div class="left flex">
<div>Left Side</div>
</div>
<div class="center flex">
<div>I want this centered</div>
</div>
<div class="right flex">
<div>Right Side (Extra text for extra length)</div>
</div>
</div>