Horizontaly center an image in flex display? - css

I wan't to center an image in my flex navbar but i can't, i already tried this: How to center an image within a flex item (cell), but doesn't work.
nav {
width: 100%;
display: flex;
justify-content: space-between;
border-radius: 1.5em;
}
nav #this img {
height: 5em;
width: 5em;
}
.item {
width: 45%;
display: flex;
justify-content: space-around;
line-height: 3.2em;
font-size: 1.75em;
}
.login {
width: 20%;
display: flex;
justify-content: space-around;
align-self: center;
}
<nav>
<div class="item">
<div> Content </div>
<div> Content </div>
</div>
<div id="myimage">
<img src="https://via.placeholder.com/150x50">
</div>
<div class="login">
<div> Content </div>
<div> Content </div>
<div> Content </div>
<div> Content </div>
<div> Content </div>
</div>
</nav>

I just update your CSS with few updates. Try this I hope it'll help you out. Thanks
nav {
width: 100%;
display: flex;
justify-content: space-between;
border-radius: 1.5em;
}
nav #this img {
height: 5em;
width: 5em;
}
.item {
width: 45%;
display: flex;
justify-content: space-around;
font-size: 1.75em;
}
.login {
width: 20%;
display: flex;
justify-content: space-around;
align-self: center;
}
.flexItem {
flex: 1;
flex-wrap: wrap;
align-items: center;
}
<nav>
<div class="item flexItem">
<div> Content </div>
<div> Content </div>
</div>
<div id="myimage">
<img src="https://via.placeholder.com/150x50">
</div>
<div class="login flexItem">
<div> Content </div>
<div> Content </div>
<div> Content </div>
<div> Content </div>
<div> Content </div>
</div>
</nav>

I dont know exactly what are you doing with this:
nav #this img {
height: 5em;
width: 5em;
}
i think you wanted something like this:
nav #myimage img {
height: 5em;
width: 5em;
}
To center in flexbox, use the justify-content to the primary axis and align-items to the secondary in the flexbox parent element.
in this case you can do it at nav and align the 3 directly children divs at center.
nav {
width: 100%;
display: flex;
justify-content: space-between;
border-radius: 1.5em;
align-items: center;
}
instead if you want to positionate the image in its parent div, its the same, set display: flex and align items to the parent element (#myimage):
#myimage{
display:flex;
align-items:center;
}

nav {
width: 100%;
display: flex;
justify-content: space-between;
border-radius: 1.5em;
}
nav #this img {
height: 5em;
width: 5em;
}
.item {
width: 45%;
display: flex;
justify-content: space-around;
line-height: 3.2em;
font-size: 1.75em;
}
.login {
width: 20%;
display: flex;
justify-content: space-around;
align-self: center;
}
#myimage {
display: flex;
justify-content: center;
align-items: center;
}
<nav>
<div class="item">
<div> Content </div>
<div> Content </div>
</div>
<div id="myimage">
<img src="https://via.placeholder.com/150x50">
</div>
<div class="login">
<div> Content </div>
<div> Content </div>
<div> Content </div>
<div> Content </div>
<div> Content </div>
</div>
</nav>

Related

How to make right elements space-between?

It doesn't work when I set justify-content: space-between on rightContainer, I want Apple on the top and Sony on the bottom.
<div className={styles.mainContainer}>
<div className={styles.leftContainer}>
<div className={styles.profilePicture} />
<p className={styles.profileUsername}>Test Name</p>
</div>
<div className={styles.rightContainer}>
<p>Apple</p>
<p>Sony</p>
</div>
</div>
css
.mainContainer {
display: flex;
width: 100%;
background-color: cadetblue;
}
.leftContainer {
width: 100%;
background-color: red;
text-align: left;
}
.rightContainer {
width: 100%;
background-color: blue;
text-align: right;
justify-content: space-between;
}
justify-content: space-evenly is only applicable to a flexbox. You can use a vertical flexbox to apply space-evenly.
.mainContainer {
display: flex;
width: 100%;
background-color: cadetblue;
}
.leftContainer {
width: 100%;
background-color: red;
text-align: left;
}
.rightContainer {
width: 100%;
background-color: blue;
text-align: right;
justify-content: space-evenly;
display: flex; /*Create a flexbox*/
flex-direction: column; /*Vertical flexbox*/
}
<div class="mainContainer">
<div class="leftContainer">
<div class="profilePicture" />
<p class="profileUsername">Test Name</p>
</div>
<div class="rightContainer">
<p>Apple</p>
<p>Sony</p>
</div>
</div>

CSS Flex: what's the most accurate way for making a grid-like layout?

By design flex is (initially) a single lane of content and wasn't made specifically for grids.
I currently use flex-wrap: wrap; but it's not really made for making grids -- even though it's probably the first option you try to make one.
Moreover I think it's not the only way to create grid-like layouts.
So is this the most accurate (proper) way? of creating a grid in flex?
Or are there better alternatives?
Edit (after 2 answers were posted): just to clarify, I'm not looking for display: grid; I'm asking what is the most proper/accurate way of doing it in flex. (See css flex grid)
(Of course display: grid is a proper way for making grids with CSS grid. That is not what the question is asking.)
My advise is to go with display: grid.
To get started you have to define a container element as a grid with
display: grid, set the column and row sizes with grid-template-columns
and grid-template-rows, and then place its child elements into the
grid with grid-column and grid-row.
This is an useful post about https://css-tricks.com/snippets/css/complete-guide-grid/
And this is a complete example:
The style:
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
background-color: red;
padding: 1px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid #000;
padding: 20px;
font-size: 30px;
text-align: center;
}
and the layout:
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
</div>
so i came up with this its not perfect but you could tweak it to make it what you want i think...https://codepen.io/colinthedev/pen/ExKwVVZ
document.getElementsByTagName("h1")[0].style.fontSize = "6vw";
body {
font-family: system-ui;
background: #f06d06;
color: white;
text-align: center;
display: flex;
flex-direction: column;
margin: 0;
padding: 0;
}
.mainWrapper {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: flex-start;
}
.flexWrapOne {
display: flex;
flex-direction: row;
width: inherit;
margin: 0 .5rem 0 1rem;
justify-content: center;
align-items: center;
}
.item1 {
display: flex;
order: 1;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 15rem;
width: 20rem;
margin: .5rem;
background: #000;
}
.item2 {
display: flex;
order: 2;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 10rem;
width: 10rem;
margin: .5rem;
background: #0D89F2;
}
.item3 {
display: flex;
order: 3;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 15rem;
width: 33rem;
margin: .5rem;
background: #89F20D;
}
.flexWrapTwo {
display: flex;
flex-direction: column;
}
.item4 {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 10rem;
width: 20rem;
margin: .5rem;
background: #000;
}
.item6 {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 10rem;
width: 10rem;
margin: .5rem;
background: #0D89F2;
}
#media screen and (max-width: 1430px) {
.flexWrapTwo {
flex-direction: row;
}
.item4 {
order: 2;
}
.item6 {
order: 1;
}
}
#media screen and (max-width: 1250px) {
.mainWrapper {
display: flex;
flex-direction: column;
justify-content: center;
}
.flexWrapOne {
flex-direction: column;
width: inherit;
}
}
<h1>👋 Hello World!</h1>
<div class="flexWrapOne">
<h1 class="item1"> ITEM-1 </h1>
<h1 class="item2"> ITEM-2 </h1>
<h1 class="item3"> ITEM-3 </h1>
</div>
<div class="mainWrapper">
<div class="flexWrapOne">
<h1 class="item1"> ITEM-4 </h1>
<h1 class="item2"> ITEM-5 </h1>
<h1 class="item3"> ITEM-6 </h1>
</div>
<div class="flexWrapTwo">
<h1 class="item4"> ITEM-7 </h1>
<h1 class="item6"> ITEM-8 </h1>
</div>
</div>
<!-- Same as above just copy pasted -->
<div class="flexWrapOne">
<h1 class="item1"> ITEM-1 </h1>
<h1 class="item2"> ITEM-2 </h1>
<h1 class="item3"> ITEM-3 </h1>
</div>
<div class="mainWrapper">
<div class="flexWrapOne">
<h1 class="item1"> ITEM-4 </h1>
<h1 class="item2"> ITEM-5 </h1>
<h1 class="item3"> ITEM-6 </h1>
</div>
<div class="flexWrapTwo">
<h1 class="item4"> ITEM-7 </h1>
<h1 class="item6"> ITEM-8 </h1>
</div>
</div>

Div with position absolute is wider than its container

In the following code:
HTML
<div class="cards-wrapper">
<div class="cards_item">
<div class="card">
<div class="card__header">
<div class="=card__title">
<div>title</div>
</div>
</div>
<div class="card__content">
<p class="card__description">Some text</p>
<p class="card__frequency">more text</p>
</div>
<div class="card__footer">
<div class="card__tick"><input type="hidden" value="0"><label class="checkbox-container"><input type="checkbox" class="list-checkbox" value="false"><span class="checkmark"></span></label></div>
<div class="card__see">Even more</div>
</div>
</div>
</div>
</div>
SCSS
.cards-wrapper {
display: flex;
flex-wrap: wrap;
box-sizing: border-box;
outline: 2px solid blue
}
.cards_item {
display: flex;
justify-content: space-between;
box-sizing: border-box;
width: 100%;
padding: 0.5rem;
}
.card {
position: relative;
flex: 0 1 100%;
justify-self: stretch;
box-sizing: border-box;
padding: 0.625rem;
overflow: hidden;
&__header {
display: flex;
flex-direction: row;
align-items: center;
}
&__title {
margin: 0;
}
&__frequency {
margin-bottom: 80px;
}
&__footer {
position: absolute;
bottom: 0.8rem;
display: inline-flex;
align-items: center;
justify-content: space-between;
width: 100%;
outline: 2px solid red;
}
}
Why does .card__footer div with a width of 100% wider than its container?
It fits in the container if you change its width to 90%.
But something seems to be not right here.
Can someone help me please with this?
I see that the container div has padding to it.
can you try this width value on your footer?
width: calc(100% - 1.25rem);
since you are using scss
calc(100% - 2*0.625rem)

Align items to left while flex direction is row-reverse in Flex

I want to display items in reverse order horizontally with left alignment
here is my working fiddle, but items are aligned to right.
#main {
width: 400px;
height: 400px;
border: 1px solid #c3c3c3;
display: flex;
flex-direction: row-reverse;
align-items: flex-start;
}
#main div {
width: 50px;
height: 50px;
}
<div id="main">
<div style="background-color:coral;">A</div>
<div style="background-color:lightblue;">B</div>
<div style="background-color:khaki;">C</div>
<div style="background-color:pink;">D</div>
<div style="background-color:lightgrey;">E</div>
<div style="background-color:lightgreen;">F</div>
</div>
can someone help me, thanks in advance.
You need to specify justify-content: flex-end;
#main {
width: 400px;
height: 400px;
border: 1px solid #c3c3c3;
display: flex;
flex-direction: row-reverse;
align-items: flex-start;
justify-content: flex-end;
}
#main div {
width: 50px;
height: 50px;
}
<div id="main">
<div style="background-color:coral;">A</div>
<div style="background-color:lightblue;">B</div>
<div style="background-color:khaki;">C</div>
<div style="background-color:pink;">D</div>
<div style="background-color:lightgrey;">E</div>
<div style="background-color:lightgreen;">F</div>
</div>

Make flexbox children with same height

I have a flexbox parent setted with flex-direction: row.
Inside this parent, I have two children. I would like them to have the same height!
Inside this children I have dynamic content (with variable height).
The way I'm doing, if I add text on the right child, the left one will grow.
But If the left child grows, the right one stays small.
Should not they behave in the same way?
Here is a fiddle: https://jsfiddle.net/4g6uevok/8/
HTML:
<div id="main">
<div class="left">
<div class="title">MY TITLE:</div>
<div class="left-area">
<div class="left-area-row">
<div class="left-area-row-titulo">#1</div>
<div class="left-area-row-info">A</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">#2</div>
<div class="left-area-row-info">B</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">#3</div>
<div class="left-area-row-info">AC</div>
</div>
</div>
</div>
<div class="right">
<div class="title">SECOND TITLE:</div>
</div>
</div>
CSS:
#main {
width: 100%;
height:auto;
margin-top: 30px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: stretch;
background-color: red;
}
.left{
width: 400px;
display: flex;
flex-direction: column;
background:lime;
align-items: stretch;
}
.title {
width: 100%;
font-size: 1.5em;
color:#525252;
display: flex;
justify-content: center;
margin-bottom: 20px;
font-weight: bold;
font-family: "emir-bold";
}
.left-area {
width: 100%;
display: flex;
flex-direction: column;
}
.left-area-row {
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.left-area-row-titulo {
width: 49.5%;
display: flex;
align-items: center;
justify-content: flex-start;
background-color: #819196;
color: white;
padding: 6px;
margin:0 2px 4px 0;
}
.left-area-row-info {
width: 49.5%;
display: flex;
align-items: center;
justify-content: center;
background: #CCCCCC;
padding: 6px;
margin:0 0 4px 2px;
}
.right {
width: calc(100% - 430px);
height: 100%;
display: flex;
flex-direction: column;
background:orange;
align-items: stretch;
}
Flex items are aligned to strech by default. Your height:100% value in .right class preventing it to take whole height so try to remove height:100% to the .right element
#main {
width: 100%;
height: auto;
margin-top: 30px;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: stretch;
background-color: red;
}
.left {
width: 400px;
display: flex;
flex-direction: column;
background: lime;
align-items: stretch;
}
.title {
width: 100%;
font-size: 1.5em;
color: #525252;
display: flex;
justify-content: center;
margin-bottom: 20px;
font-weight: bold;
font-family: "emir-bold";
}
.left-area {
width: 100%;
display: flex;
flex-direction: column;
}
.left-area-row {
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
}
.left-area-row-titulo {
width: 49.5%;
display: flex;
align-items: center;
justify-content: flex-start;
background-color: #819196;
color: white;
padding: 6px;
margin: 0 2px 4px 0;
}
.left-area-row-info {
width: 49.5%;
display: flex;
align-items: center;
justify-content: center;
background: #CCCCCC;
padding: 6px;
margin: 0 0 4px 2px;
}
.right {
width: calc(100% - 430px);
display: flex;
flex-direction: column;
background: orange;
align-items: stretch;
}
<div id="main">
<div class="left">
<div class="title">MY TITLE:</div>
<div class="left-area">
<div class="left-area-row">
<div class="left-area-row-titulo">
#1
</div>
<div class="left-area-row-info">A</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">
#2
</div>
<div class="left-area-row-info">BA</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">
#3
</div>
<div class="left-area-row-info">C</div>
</div>
<div class="left-area-row">
<div class="left-area-row-titulo">
#4
</div>
<div class="left-area-row-info">D</div>
</div>
</div>
</div>
<div class="right">
<div class="title">SECOND TITLE:</div>
</div>
</div>

Resources