This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 4 years ago.
How do I get the input box to be vertically aligned to the middle? I tried adding vertical-align: "middle" to a few places but I am not getting any success. I feel like flexbox is a part of the problem here?
.container {
/* Flex Properties */
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
}
.item {
background-color: orange;
}
label {
width: 100px;
display: inline-block;
}
<div class="container">
<div class="item">
<label>Gee my iefwaf fwats: </label>
<input>
</div>
</div>
Give the .item div display: flex and align-items: center, since it's the parent of the input element:
.container {
/* Flex Properties */
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
}
.item {
display: flex; /* added */
align-items: center; /* added */
background-color: orange;
}
label {
width: 100px;
/*display: inline-block; not necessary*/
}
<div class="container">
<div class="item">
<label>Gee my iefwaf fwats: </label>
<input type="text">
</div>
</div>
Try this css code with your html.
.container {
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
}
.item {
background-color: orange;
display: flex;
align-items: center;
}
label {
width: 100px;
display: inline-block;
}
input {
display: inline-block;
}
Please check with the added CSS::
.container {
/* Flex Properties */
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-items: center;
}
.item {
background-color: orange;
}
label {
width: 100px;
display: inline-block;
vertical-align: middle;
}
input {
width: 100px;
display: inline-block;
vertical-align: middle;
}
<div class="container">
<div class="item">
<label>Gee my iefwaf fwats: </label>
<input>
</div>
</div>
Related
I have divs that contain each individual arrow (one for the greater than text and another for the less than text). However, when I hover over the less than arrow, it pushes all the other elements to the right a bit. How can I have the hover effect not push any other elements away?
This is my HTML:
<div className="datepicker-wrapper">
<div className="dates">
<div className="arrows prev-month"><</div>
<div className="months">
<div className="start-month">February</div>
<div className="end-month">March</div>
</div>
<div className="days"></div>
<div className="arrows next-month">></div>
</div>
<div className="selected-dates">
<div className="check-in-date">02/13/2020</div>
</div>
</div>
And this is my CSS:
#import 'variables.css';
.datepicker-wrapper {
position: relative;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: flex-start;
width: 100%;
height: 50px;
background: transparent;
}
.datepicker-wrapper .dates {
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
width: 60%;
height: 100%;
}
.datepicker-wrapper .selected-dates {
display: flex;
flex-direction: column;
cursor: default;
width: 40%;
height: 100%;
}
.datepicker-wrapper .dates .arrows .previous-month {
}
.datepicker-wrapper .dates .arrows:hover {
width: 15px;
height: 15px;
background: var(--light-gray);
border-radius: 50%;
cursor: pointer;
}
.datepicker-wrapper .dates .months {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
NOTE
I've tried giving the arrows prev-month div a set width, but that doesn't seem to have any effect.
You give the width and height only on hover.. try to give the styles to the arrows anyway, and on hover only change the background-color
.datepicker-wrapper {
position: relative;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: flex-start;
width: 100%;
height: 50px;
background: transparent;
}
.datepicker-wrapper .dates {
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
width: 60%;
height: 100%;
}
.datepicker-wrapper .selected-dates {
display: flex;
flex-direction: column;
cursor: default;
width: 40%;
height: 100%;
}
.datepicker-wrapper .dates .arrows {
width: 15px;
height: 15px;
border-radius: 50%;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
}
.datepicker-wrapper .dates .arrows:hover {
background: #c1c1c1;
}
.datepicker-wrapper .dates .months {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
<div class="datepicker-wrapper">
<div class="dates">
<div class="arrows prev-month"><</div>
<div class="months">
<div class="start-month">February</div>
<div class="end-month">March</div>
</div>
<div class="days"></div>
<div class="arrows next-month">></div>
</div>
<div class="selected-dates">
<div class="check-in-date">02/13/2020</div>
</div>
</div>
Setting the styles on :hover impacts the CSS box model for the .arrows element:
All four of those impact the overall space an element takes.
This question already has answers here:
Center and bottom-align flex items
(3 answers)
Closed 4 years ago.
I have a problem with flexbox. I attempted to align two elements; the one to the top of the container, and another one to the center. Most of the flexbox examples were using three elements, not two elements. So I tried my own solution.
#main {
display: flex;
flex: 1;
flex-direction: column;
justify-content: space-between;
height: 100px;
background-color: #dfdfdf;
}
#box1 {
display: flex;
justify-content: flex-end;
background-color: #ff0000;
}
#box2 {
display: flex;
background-color: #00ff00;
}
#dummy {
display: flex;
opacity: 0;
}
<div id="main">
<div id="box1">box1</div>
<div id="box2">box2</div>
<div id="dummy">dummy</div>
</div>
...and I also applied it to horizontal case.
#main {
display: flex;
flex: 1;
flex-direction: row;
justify-content: space-between;
height: 100px;
background-color: #dfdfdf;
}
#box1 {
display: flex;
justify-content: flex-end;
background-color: #ff0000;
width: 200px;
}
#box2 {
display: flex;
background-color: #00ff00;
width: 100px;
}
#dummy {
display: flex;
opacity: 0;
width: 200px;
}
<div id="main">
<div id="box1">box1</div>
<div id="box2">box2</div>
<div id="dummy">dummy</div>
</div>
However, it needs a useless dummy element. I think it is not a good idea :(
Is there any better way to solve this?
you don't have to add this 'dummy' div. According to me you should keep display flex in you container, but change justify-content: space-between to justify-content: center.
Then simply add position absolute to you first, child element and display it on the top of the container. Also remember to add relative position to your container.
Here is working code:
#main {
display: flex;
flex: 1;
flex-direction: column;
justify-content: center;
height: 100px;
background-color: #dfdfdf;
position: relative;
}
#box1 {
display: flex;
justify-content: flex-end;
background-color: #ff0000;
position:absolute;
top:0;
width:100%;
}
#box2 {
display: flex;
background-color: #00ff00;
}
Fiddle: https://jsfiddle.net/95Lwcbuk/1/
If you want to use flex-box only, you could wrap another .container element around each of your boxes, set these to use flex also, but set the first one to justify-content: flex-start, the last one to justify-content: flex-end.
See example
#main {
display: flex;
flex: 1;
flex-direction: column;
justify-content: center;
height: 100px;
background-color: #dfdfdf;
}
.container {
flex-basis:50%;
display:flex;
flex-direction:column;
}
.container:first-child {
justify-content: flex-start;
}
.container:last-child {
justify-content: flex-end;
}
#box1 {
background-color: #ff0000;
}
#box2 {
background-color: #00ff00;
}
<div id="main">
<div class="container">
<div id="box1">box1</div>
</div>
<div class="container">
<div id="box2">box2</div>
</div>
</div>
I have the following in my HTML file:
<div class="container-box">
<div class="profile-box">
<div class="image-container">
<div class="profile-picture"></div>
</div>
</div>
</div>
In my CSS file, I have the following:
.container-box {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.profile-box {
height: 350px;
width: 500px;
background-color: white;
padding: 20px;
display: flex;
flex-direction: column;
flex: 1;
}
This results in my profile-box being center-aligned, but I also want it vertically aligned. I have tried changing the flex-direction to row, but that only stretches it to take over all the horizontal space.
Here is a codepen: https://codepen.io/Humad/pen/rKLMeo
It is already centered, but your body and .container-box do not have a height set for it go in the center.
JSFiddle demo
body, html {
height: 100%; /* added */
width: 100%; /* added */
margin: 0; /* added */
}
.container-box {
height: 100%; /* added */
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
background-color: black;
}
This question already has answers here:
White space under image [duplicate]
(5 answers)
Remove white space from image
(3 answers)
Closed 5 years ago.
So I have two divs in a full width container that I want to give variable sizing with flexbox, but no matter what I do, there is an annoying offset at the bottom. Using margins I can come close to fixing the problem, but it's never perfect.
If you run the code snippet below and scroll to the bottom you can see it, the image and the black content container are not aligned at the bottom.
What's going on?
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
margin-bottom:7px;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg"/>
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</container>
There is some space below the image since the image is an inline-element and as such there is some space reserved below the (invisble) baseline that the image is aligned to vertically. To avoid that, there are two possible solutions:
1.) Apply display: block; to the image (see first snippet)
or
2.) Apply font-size: 0 to the image container (see second snippet)
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
img {
display: block;
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg" />
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</div>
SECOND SOLUTION:
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
font-size: 0;
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg" />
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</div>
#container {
width: 100%;
display: inline-flex;
flex-direction: row;
}
#image-wrapper {
flex-grow: 3;
max-width: 1000px;
position: relative;
/*background-color: black;*/
}
#menu {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 50px;
background-color: #101010;
color: #fefefe;
align-items: stretch;
display: flex;
margin-bottom:4px;
}
#form {
width: 100px;
}
#image {
width: 100%;
}
<div id="container">
<div id="image-wrapper">
<img id="image" src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg"/>
</div>
<div id="menu">
<div id="form">
CONTENT<br>CONTENT<br>
</div>
</div>
</container>
Looks like the margin is just a bit off
How can I push .right all the way to the right while keeping .centred in the center? (It doesn't need to be with flex, but I would need to keep .centred above .right on mobile view)
https://codepen.io/anon/pen/rjZLOv
<div class="flex">
<div class="centred">
<div class="a">asdfasdf</div>
<div class="b">asdfasdf</div>
</div>
<div class="right">asdfasfad</div>
</div>
.flex {
display: flex;
align-items: center;
justify-content: center;
}
.centred {
justify-content: center;
}
.right {
justify-content: flex-end;
}
You can make .centred inline-block and use text-align: center; on the parent to center it, then float .right to the right.
.container {
width: 1024px;
margin: auto;
text-align: center;
}
.centred {
display: inline-block;
}
.right {
float: right;
}
<div class="container">
<div class="centred">
<div class="a">asdfasdf</div>
<div class="b">asdfasdf</div>
</div>
<div class="right">asdfasfad</div>
</div>
You can do it like this: https://codepen.io/anon/pen/dNqXjo
The code:
.flex {
display: flex;
align-items: center;
justify-content: space-between;
}
.flex > * {
flex: 1;
}
.flex::before {
content: "";
flex: 1;
background: red;
}
This makes the items inside the flex container (.flex) use flex.
Then we add another item to use up the rest of the space on the left site so that the rest of the content is pushed to the right.
Configure width with flex-basis.
You almost have it you just need margin-left and direction base:
.flex {
display: flex;
align-items: center;
flex-direction: column;
}
.centred {
}
.right {
margin-left: auto;
}