Help with CSS flexbox

Hi, these two elements are inside a navbar with a display of flex. What is the best way to bring the logo on the left and the search bar at the center? also considering responsiveness. Thanks in advance.
No description
78 Replies
ἔρως
ἔρως4w ago
grid just this:
display: grid;
grid-template-columns: auto 1fr;
justify-content: center;
display: grid;
grid-template-columns: auto 1fr;
justify-content: center;
im not sure about the exact property for the last line, but you can click the tiny grid icon next to the display, and center the items
Gabriele
GabrieleOP4w ago
this should be applied only on the navbar parent container?
ἔρως
ἔρως4w ago
yes to the closest parent element that has the image and the search form
Gabriele
GabrieleOP4w ago
this is the result i got and its all on the left
No description
ἔρως
ἔρως4w ago
looks correct to me the image is on the left and the form is centered on the rest of the space in the element inspector, click on the grid button next to the element
Gabriele
GabrieleOP4w ago
No description
Gabriele
GabrieleOP4w ago
this is what i meant the search bar should be at the center of the page
ἔρως
ἔρως4w ago
that is different but do what i said
Gabriele
GabrieleOP4w ago
i took the screenshot in a bad way sorry lol
ἔρως
ἔρως4w ago
take a screenshot with this on im on mobile
Gabriele
GabrieleOP4w ago
No description
ἔρως
ἔρως4w ago
so, i got the wrong property for the last line on the right side, you should see a button next to the display grid click on it and then click on anything that has a symbol representing the center
Gabriele
GabrieleOP4w ago
i only see the button grid when inspecting in the html and also in the css in the dev tool but when i click i only get this
ἔρως
ἔρως4w ago
on the right side? on the css side? it should have a button, unless you use firefox
Gabriele
GabrieleOP4w ago
i have firefox yes
Gabriele
GabrieleOP4w ago
No description
Gabriele
GabrieleOP4w ago
this is what i see
ἔρως
ἔρως4w ago
you have "space-between" as the value
Gabriele
GabrieleOP4w ago
I was just trying but with center its the same thing
ἔρως
ἔρως4w ago
try align-items this is the part i never remember
Gabriele
GabrieleOP4w ago
align items center? nothing changes as well same result just the input bar got less thick
Chris Bolson
Chris Bolson4w ago
Do you want the search box centered in the viewport or centered in the remaining space? If you want it centered in the viewport you could try defining 3 columns:
.container{
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* increase the size of the middle column as required */
& > :nth-child(2){
text-align: center; /* or place-self:center*/
}
}
.container{
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* increase the size of the middle column as required */
& > :nth-child(2){
text-align: center; /* or place-self:center*/
}
}
Gabriele
GabrieleOP4w ago
the search box should be centered in the screen so i guess viewport also tried your solution and the result is the same again this one i mean the result is the same one as that one
ἔρως
ἔρως4w ago
i would do 1fr auto 1fr but that works too (or the meh repeat(1fr, 3)
Chris Bolson
Chris Bolson4w ago
yes, whichever, the idea is to add a "fake" column to the right
ἔρως
ἔρως4w ago
yeah i got it
Chris Bolson
Chris Bolson4w ago
Can you share the code? What we are saying should work
ἔρως
ἔρως4w ago
i would use auto so you can just set the alignment on the parent instead of a specific child
Chris Bolson
Chris Bolson4w ago
you are right, auto makes things simpler. You then don't even need to define center in any way
Chris Bolson
Chris Bolson4w ago
This is my quick demo based on the 3 columns (with auto 😉)
ἔρως
ἔρως4w ago
that too
Gabriele
GabrieleOP4w ago
how do i send the code to make it look like a snippet?
ἔρως
ἔρως4w ago
just send an example over codepen or jsfiddle dont paste the code here it's easier to experiment and debug, comparent to you sending lumps of code and us having to guess what it is doing
Jochem
Jochem4w ago
#Asking Good Questions has some info
Gabriele
GabrieleOP4w ago
give me a sec
Jochem
Jochem4w ago
But yeah, codepen is by far preferable
Gabriele
GabrieleOP4w ago
this is the code i have
Chris Bolson
Chris Bolson4w ago
change the second grid-template-columns value to "auto" as Epic suggested
Gabriele
GabrieleOP4w ago
if you mean removing the 1fr 1fr 1fr i did it and now this is the result all on the left and the logo up and search bar down
ἔρως
ἔρως4w ago
no, replace the middle one for auto you will have 1fr auto 1fr
Gabriele
GabrieleOP4w ago
it worked, the search bar is super thick but i guess its a value inside of it that i need to change
Chris Bolson
Chris Bolson4w ago
The reason 1fr didn't work was because you have defined a fixed width on the search element. That is fine, however it does demonstrate why it helps to have the complete (relevant) code.
ἔρως
ἔρως4w ago
it's because you have the input as a child of the nav
Gabriele
GabrieleOP4w ago
it shouldnt be?
ἔρως
ἔρως4w ago
move the input inside a <form>
Gabriele
GabrieleOP4w ago
inputs should always be inside forms?
ἔρως
ἔρως4w ago
no, you will need a form with a submit button to handle the search ... it's complicated it's not a must, but you will need it for this case you will, for sure, also add a submit button for the search and for that, you need the form
Gabriele
GabrieleOP4w ago
i was kind re-making a project i saw and the guy uses nav bar and inside the search component with just the input im using react not vanilla js
ἔρως
ἔρως4w ago
also, since the form is the parent, the input wont be controlled by the grid
Gabriele
GabrieleOP4w ago
No description
Gabriele
GabrieleOP4w ago
original one
ἔρως
ἔρως4w ago
EW ew ew ee ew sorry, but EEEEEWWWWWWWW
Gabriele
GabrieleOP4w ago
Explain no?
ἔρως
ἔρως4w ago
yes not only it isnt accessible, it's terribly ineficient as it will trigger multiple searches without debouncing
Jochem
Jochem4w ago
That's a very React way of doing things
Gabriele
GabrieleOP4w ago
As I said im using React and not Vanilla JS
ἔρως
ἔρως4w ago
and since the type is text, you arent properly informing the assistive technologies of what the field is im talking about the bad html, not about using react that is fine for a toy, but if you want to transform it into something you will use in a public-facing project, you will have to fix it
Chris Bolson
Chris Bolson4w ago
Regarding the reason as to why it has the same height as the grid is because grid cell contents automatically stretch to be 100% the height of their parent. Unless otherwise specified, this height is defined by the "tallest" child (in this case your logo). There are several ways to change this, the simplest probably being to define this in the parent element align-items: center; (as again Epic mentioned earlier). However, this will affect all of the grid children. If you just want to define it on a specific child (eg your search) you can define this align-self: center; on that specific element.
ἔρως
ἔρως4w ago
or just move the input into a form and the form will have the space it has
Gabriele
GabrieleOP4w ago
thanks for this Epic idk how to explain you this because im not really an expert but in React in this case the application the form or input doesn't do anything React holds the input value and changes
ἔρως
ἔρως4w ago
with grid or flex, you can set the submit button on the right side, and makr it amazing it was a comment about the original react code, not about your skills
Gabriele
GabrieleOP4w ago
no im telling you that i think the guy doesnt put the input in the form because we arent following any js logic here. React is in charge of taking the value from the input not the submit like this thing you said
ἔρως
ἔρως4w ago
you can set a change event on the form, as it bubbles up and with the search type, chrome implements debouncing for you
Gabriele
GabrieleOP4w ago
i will look into what you say. In the end how can I understand when I need a flexbox or grid? because I thought grids was only from grid looking stuff
ἔρως
ἔρως4w ago
that is a grid looking thing
Gabriele
GabrieleOP4w ago
how exactly?
ἔρως
ἔρως4w ago
you now have space to add a button on the right side that triggers a menu to shoe/hide show
Gabriele
GabrieleOP4w ago
i dont need a button. The user types and you have a window with the results under it and he just needs to click on them and an API display the results
ἔρως
ἔρως4w ago
i was talking about a possible navigation menu
Gabriele
GabrieleOP4w ago
so why is this a grid looking thing?
ἔρως
ἔρως4w ago
i saw something rigid the logo had it's specific size and the input has it's specific size and with grid, you can have much easier control of what each child does, from the parent with flex, you can do this you set the image to have flex-grow: 0; flex-shrink: 1 and the input has flex: 1 1 max-content or something it's a lot more work disclaimer: THIS IS NOT ADVICE AND IS A VERY VERY SUBJECTIVE AND NARROW VIEW OF HOW I INTERPRETED THE PROBLEM when i want to decide if it is grid or flex, i think about how i want it to behave and look like and how i want to control the layout it's mostly based on feelins, maintaibability and intuition
Gabriele
GabrieleOP4w ago
Ok thanks for the help the guy also used a grid for his navbar but the layout was different from mine
ἔρως
ἔρως4w ago
how was the layout?
Gabriele
GabrieleOP4w ago
just 3 elements instead of two but in the case you can also use a flexbox i guess and pick space btw and then with margins you move stuff around
ἔρως
ἔρως4w ago
yes you can do all that or you can use 3 lines of css, with grid and stuff is positioned as specified by the parent
Gabriele
GabrieleOP4w ago
ok thanks again, have a nice day
ἔρως
ἔρως4w ago
you too, and you're welcome

Did you find this page helpful?