I like the positioning of the two box class div's with justify-content: flex-end but I'd like to center the top img-container div vertically in the remaining space above but I'm not sure if this is possible, preferably without javascript.
The layout is for portrait orientation mobile devices. Maybe justifying the content isn't the best approach but I'd like a layout that places the form elements towards the bottom of the screen and spaces them well but responds to smaller devices by taking space from the logo area.
.flexcontainer {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: flex-end;
justify-content: flex-end;
/*iPhone 4*/
height: 480px;
width:320px;
/*iPhone 6*/
/*height: 667px;
width:375px;*/
background-color: blue;
border: 1px solid red;
}
.box {
text-align:center;
height: auto;
width: 100%;
background-color: green;
border: 1px solid pink;
margin: 3px;
padding-top:20px;
padding-bottom:20px;
margin-top:20px;
margin-bottom:20px;
}
.img-container{
text-align:center;
}
<div class="flexcontainer">
<div class="img-container">
<img src='https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png' width='80%'/>
</div>
<div class="box">
<input type="text" placeholder="username">
<br>
<input type="password" placeholder="password">
<br>
<button>submit</button>
</div>
<div class="box">
<button>password reset</button>
<br>
<button>register</button>
</div>
</div>
You could change just a little bit of code and achieve what you want.
Fiddle: https://jsfiddle.net/gczcorn0/
I just modified your image container to be like this:
<div class="box clear img-container">
<img src='https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png' width='80%'/>
</div>
So this takes the same properties as the boxes below. Then I assume you don't want the background-color and border on the image box so just clear the css attributes like this:
.box.clear {
background-color: transparent;
border: none;
}
I'm not sure what you meant by how you want it to behave in smaller devices since the width is set to 320px in the example.
EDIT based on comment:
This updated fiddle shows what you can do in a situation that you expressed in the comments: https://jsfiddle.net/gczcorn0/2/
You could use display: flex on the img-container as well.
.flexcontainer {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: flex-end;
justify-content: flex-end;
/*iPhone 4 */
height: 480px;
width:320px;
/*iPhone 6
height: 667px;
width:375px;*/
background-color: blue;
border: 1px solid red;
}
.box {
text-align:center;
height: auto;
width: 100%;
background-color: green;
border: 1px solid pink;
margin: 3px;
padding-top:20px;
padding-bottom:20px;
margin-top:20px;
margin-bottom:20px;
}
.img-container{
display: flex;
flex-flow: column nowrap;
justify-content: center;
flex: 1;
}
.img-container img {
margin: 0 auto;
}
<div class="flexcontainer">
<div class="img-container">
<img src='https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png' width='80%'/>
</div>
<div class="box">
<input type="text" placeholder="username">
<br>
<input type="password" placeholder="password">
<br>
<button>submit</button>
</div>
<div class="box">
<button>password reset</button>
<br>
<button>register</button>
</div>
</div>
Related
I have this on my codepen in which I was using flex to align items. What I want is to align items even with or without labels.
here is my html code:
<div class="parent-main">
<div class="child1">
<!--<label>checkbox</label> -->
<input type="text"/>
</div>
<div class="child2">
<label>checkbox</label>
<input type="checkbox"/>
</div>
<div class="child3">
<label>checkbox</label>
<input type="radio"/>
</div>
</div>
my css:
.parent-main {
display: flex;
justify-items: center;
border: solid 1px #ccc;
width: 55vh;
height: 25vh;
margin: 5px;
padding: 15px;
gap: 2px;
}
.child1 {
display: flex;
flex-direction: column;
/* position: relative;
top: 2px; */
}
.child2 {
display: flex;
flex-direction: column;
align-items: baseline;
}
.child3 {
display: flex;
flex-direction: column;
align-items: baseline;
}
Here is my codepen link: enter link description here
This is quite tricky on my side but I hope I can have your insights on this, been working this for 2 days now.
As you want to align the input of type "text" with the other div siblings but without it having any label element, we use a line break in order to mimic an empty blank space without any text element in it by using either <br> or
Tip:
Avoid duplication of codes in CSS properties, see the below snippet
.parent-main {
display: flex;
justify-items: center;
border: solid 1px #ccc;
width: 55vh;
height: 25vh;
margin: 5px;
padding: 15px;
gap: 2px;
}
.parent-main>div {
display: flex;
flex-direction: column;
}
.child2,
.child3 {
align-items: baseline;
}
<div class="parent-main">
<div class="child1">
<!--empty space -->
<br>
<!-- can also be used -->
<input type="text" />
</div>
<div class="child2">
<label>checkbox</label>
<input type="checkbox" />
</div>
<div class="child3">
<label>checkbox</label>
<input type="radio" />
</div>
</div>
I'd like to achieve the look for my login area similar to this photo.
My current HTML and CSS are as follows:
HTML
<div className="loginArea">
<p>Account Login</p>
<div className="inputBoxes">
<div>
<input type="text" placeholder="Username" />
<input type="password" placeholder="Password" />
</div>
<div>
<p>Sign In</p>
</div>
</div>
</div>
CSS
.loginArea {
margin: auto;
padding-top: 8%;
display: flex;
flex-direction: column;
height: 100%;
max-width: 400px;
justify-content: center;
}
.inputBoxes {
display: flex;
flex-direction: column;
justify-content: center;
border-radius: 15px;
background-color: blue;
}
.inputBoxes div {
display: flex;
justify-content: center;
}
input {
flex-grow: 1;
border: none;
}
My goal is to achieve the styling of this input area with respect to the shape of the inputs/sign in button as what look like one rectangle with rounded corners.
The problems I'm running into are two fold:
I can't seem to restrict the size of the input areas to the parent container. They run outside the parent div.
I can't seem to get the input areas to accept the restricted border-radius of the parent container.
Do I have to style each element individually for the rounded borders and how do I restrict the username and password areas to the width of the parent div?
If there are "better" approaches, I'm open to suggestions.
Since you're already using flexbox, the input element sizes seem to correctly fit inside the confines of the parents without any issue.
With regards to your second issue, adding overflow: hidden to the element .inputBoxes will work.
See proof-of-concept below:
body {
background-color: #ccc;
}
.loginArea {
margin: auto;
padding-top: 8%;
display: flex;
flex-direction: column;
height: 100%;
max-width: 400px;
justify-content: center;
}
.inputBoxes {
display: flex;
flex-direction: column;
justify-content: center;
border-radius: 15px;
background-color: blue;
overflow: hidden;
}
.inputBoxes div {
display: flex;
justify-content: center;
}
input {
flex-grow: 1;
border: none;
height: 35px; /* Just for demo */
}
<div class="loginArea">
<p>Account Login</p>
<div class="inputBoxes">
<div>
<input type="text" placeholder="Username" />
<input type="password" placeholder="Password" />
</div>
<div>
<p>Sign In</p>
</div>
</div>
</div>
I'm wondering why any image I put in this div container with display flex is automatically stretched out? And if I were to set a width for it, I can't center it with justify-content.
#container {
display: flex;
justify-content: center;
}
#container div {
padding: 25px;
background: blue;
color: white;
width: 500px;
display: flex;
flex-direction: column;
}
#container div h1 {
display: flex;
justify-content: center;
}
#container input,
#container button {
width: 75%;
}
#container img {
display: flex;
justify-content: center;
}
<div id="container">
<div>
<h1>a</h1>
<img src="https://placehold.it/350x150">
<input type="text" name="a">
<input type="text" name="b">
<button>a</button>
</div>
</div>
https://jsfiddle.net/nqt8bw4z/ auto stretch
https://jsfiddle.net/nqt8bw4z/2/ fixed width but doesn't center
An initial setting of a flex container is align-items: stretch. This means that flex items will expand the full length of the container's cross axis. That would be the container's height in flex-direction: row, and width in flex-direction: column.
Since you're working with a column-direction flex container, the image is stretching horizontally by default. You can override this setting with another value. Try align-items: flex-start or center.
#container {
display: flex;
justify-content: center;
}
#container div {
display: flex;
flex-direction: column;
align-items: center; /* NEW */
width: 500px;
padding: 25px;
background: blue;
color: white;
}
#container div h1 {
display: flex;
justify-content: center;
}
#container input,
#container button {
width: 75%;
}
#container img {
display: flex;
justify-content: center;
}
<div id="container">
<div>
<h1>
a
</h1>
<img src="http://placehold.it/350x150" />
<input type="text" name="a" />
<input type="text" name="b" />
<button>
a
</button>
</div>
</div>
In addition to the answer of #Michael_B your code can be simplifed like below as you don't need all these flexbox properties.
#container div {
padding: 25px;
background: blue;
color: white;
width: 500px;
margin:auto;
display: flex;
flex-direction: column;
align-items:center;
}
#container input,
#container button {
width: 75%;
}
<div id="container">
<div>
<h1>a</h1>
<img src="https://placehold.it/350x150">
<input type="text" name="a">
<input type="text" name="b">
<button>a</button>
</div>
</div>
I am using flexbox to center a form vertically and horizontally. Inside this form I'd like to pin a button to the bottom right of the flexbox container. I am not sure how to get the button pinned to the bottom right though.
html,
body {
height: 100%;
}
.container {
height: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
form {
border: 1px solid gray;
padding: 1em;
}
.form-button {
margin-top: 1em;
align-self: flex-end;
}
<div class="container">
<form>
<div class="form-input">
<label> Name <input type="text" /></label>
</div>
<div class="form-button">
<button type="submit">Submit</button>
</div>
</form>
</div>
You just need to make the form element a flex container, because flex properties only work between parent and child elements.
In other words, your align-self: flex-end on the .form-button is not working because the parent – form – does not have display: flex or display: inline-flex applied.
Here's a more complete explanation:
Proper use of flex properties when nesting flex containers
html, body {
height: 100%;
}
.container {
height: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
form {
border: 1px solid gray;
padding: 1em;
/* NEW */
display: flex;
flex-direction: column;
}
.form-button {
margin-top: 1em;
align-self: flex-end;
}
<div class="container">
<form>
<div class="form-input">
<label> Name <input type="text" /></label>
</div>
<div class="form-button">
<button type="submit">Submit</button>
</div>
</form>
</div>
.form-button {
margin-top: 1em;
align-self: flex-end;
display: flex;
justify-content: flex-end;
}
Just insert float: right;
like this:
.form-button {
float: right;<-----------added
//more code...
}
html,
body {
height: 100%;
}
.container {
height: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
form {
border: 1px solid gray;
padding: 1em;
}
.form-button {
margin-top: 1em;
float: right;
}
<div class="container">
<form>
<div class="form-input">
<label> Name <input type="text" /></label>
</div>
<div class="form-button">
<button type="submit">Submit</button>
</div>
</form>
</div>
I am trying to build a resposive form with CSS flexbox.
I want the title of each input and the input itself to be on the same line.
I am trying to align the form so the inputs will be on the same vertical line, and the titles will align always to the right (See the code below)
In-order to do this, I put the titles and inputs and separate containers.
My problem is that because the the inputs are higher than the titles, after a few rows the form is no longer straight.
Do you know how it can be fixed? See the code below.
Thx! (:
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<style>
.parent
{
border: 5px solid lightcoral;
display: flex;
}
.child
{
display: flex;
text-align: center;
flex-direction:column;
max-width:300px;
}
.item{
display: flex;
margin:5px;
text-align: center;
margin-left: auto;
}
.item-input{
margin:5px;
display: flex;
text-align: center;
box-sizing: border-box;
}
#media screen and (max-width: 1200px) {
.parent {
justify-content:left;
}
}
#media screen and (min-width: 1200px) {
.parent {
justify-content:center;
}
}
</style>
<div class="parent">
<div class="child">
<div class="item">Name</div>
<div class="item">Age</div>
<div class="item">Country</div>
<div class="item">Email</div>
</div>
<div class="child">
<div class="item-input"><input type=text></div>
<div class="item-input"><input type=text></div>
<div class="item-input"><input type=text></div>
<div class="item-input"><input type=text></div>
</div>
</div>
</div>
</htmL>
There you go :)
the span element has no effect, its just a way to give a text a class you want :p
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<style>
.parent
{
border: 5px solid lightcoral;
text-align:center;
display: flex;
justify-content:center;
}
.child
{
display: flex;
text-align: center;
flex-direction:column;
max-width:300px;
align-content: right;
}
.item{
display: flex;
margin:5px;
text-align: center;
margin-left: auto;
}
.item-input{
margin:5px;
display: flex;
text-align: center;
box-sizing: border-box;
}
#media screen and (max-width: 1200px) {
.parent {
justify-content:left;
}
}
</style>
<div class="parent">
<div class="child">
<div class="item-input"><span class="item">Name </span><input type=text></div>
<div class="item-input"><span class="item">Age </span><input type=text></div>
<div class="item-input"><span class="item">Country </span><input type=text></div>
<div class="item-input"><span class="item">Email </span><input type=text></div>
</div>
</div>
</htmL>
I'm not sure what you mean by "aligning to the right".
But if you want to have the textboxes in the left column and the "labels" on the right, without changing the html, you can use order.
.parent {
border: 5px solid lightcoral;
text-align: center;
display: flex;
justify-content: center;
}
.child {
display: flex;
text-align: center;
flex-direction: column;
max-width: 300px;
align-content: right;
}
.parent .child:first-child {
order: 1; /*added*/
}
.item {
display: flex;
margin: 5px;
text-align: center;
/*margin-left: auto;*/
}
.item-input {
margin: 5px;
display: flex;
text-align: center;
box-sizing: border-box;
}
#media screen and (max-width: 1200px) {
.parent {
justify-content: flex-start; /*"left" is not recognized by Chrome*/
}
}
<div class="parent">
<div class="child">
<div class="item">Name</div>
<div class="item">Age</div>
<div class="item">Country</div>
<div class="item">Email</div>
</div>
<div class="child">
<div class="item-input">
<input type=text>
</div>
<div class="item-input">
<input type=text>
</div>
<div class="item-input">
<input type=text>
</div>
<div class="item-input">
<input type=text>
</div>
</div>
</div>