input boxes shape restricted by parent div - html

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>

Related

centering items even with or without labels

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>

Always position button at the bottom of a form using flexbox

I've created a Fiddle here: http://jsfiddle.net/gonw4udf/1/
I have a form that we want to be of a minimum height. Sometimes, the form only has very few fields which means that the "Submit" button immediately follows the last field. However, we want the "Submit" button to always be positioned at the bottom of its container.
I'm wondering if there is a way to do so without having to rely on position: absolute.
To clarify: If the form is taller than the minimum height, it's okay to put the "Submit" button immediately after its last field. However, for forms that are shorter than the minimum height, we always want the "Submit" button at the bottom of the form. There may be multiple valid heights for these form so a solution that doesn't rely on hard coding a pixel value would be best.
.form-wrapper {
max-width: 300px;
width: 300px;
background-color: #D2B4DE;
padding: 20px 30px;
min-height: 300px;
}
.form {
display: flex;
flex-direction: column;
flex-wrap: wrap;
margin: 0 auto;
}
.form-title {
font-weight: bold;
font-size: 28px;
margin-bottom: 8px;
}
.button {
margin-top: 10px;
justify-content: flex-end;
align-items: baseline;
}
<div class="form-wrapper">
<form class="form">
<div class="form-title">
This is a form title.
</div>
<label for="field">Field</label>
<input type="text" id="field" placeholder="Enter a field" />
<button class="button">Submit</button>
</form>
</div>
Your form does not fill its container, you can use either min-height: 100% or set the container as a flex box too (easier way, no need to mind about margins then).
Once this done, the button should go down to the bottom of the form with an auto margin.
Possible example:
.form-wrapper {
max-width: 300px;
width: 300px;
background-color: #D2B4DE;
padding: 20px 30px;
min-height: 300px;
display:flex;/* NEW*/
}
.form {
display: flex;
flex-direction: column;
gap:10px;/*NEW*/
flex-wrap: wrap;
margin: 0 auto;
}
.form-title {
font-weight: bold;
font-size: 28px;
margin-bottom: 8px;
}
.button {
margin-top:auto;/* MODIFIED*/
align-items: baseline;
}
<div class="form-wrapper">
<form class="form">
<div class="form-title">
This is a form title.
</div>
<label for="field">Field</label>
<input type="text" id="field" placeholder="Enter a field" />
<button class="button">Submit</button>
</form>
</div>
Here is the solution with justify-content: space-between. To make this work I've added 1 div wrapping the form content and another div wrapping the button.
.form-wrapper {
max-width: 300px;
width: 300px;
background-color: #D2B4DE;
padding: 20px 30px;
min-height: 300px;
display: flex;
}
.form {
display: flex;
flex-direction: column;
flex-wrap: wrap;
margin: 0 auto;
flex: 1;
justify-content: space-between;
}
.form-content {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.form-action {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.form-title {
font-weight: bold;
font-size: 28px;
margin-bottom: 8px;
}
.button {
margin-top: 10px;
justify-content: flex-end;
align-items: baseline;
}
<div class="form-wrapper">
<form class="form">
<div class="form-content">
<div class="form-title">
This is a form title.
</div>
<label for="field">Field</label>
<input type="text" id="field" placeholder="Enter a field" />
</div>
<div class="form-action">
<button class="button">Submit</button>
</div>
</form>
</div>
I have solved your issue and that is, you need to specify some height to the .form-wrapper and then give height: 100%; to form and then give margin-top: auto to the .button. AND YOU ARE DONE... This is happening because your form is not filling the content-space of the .form-wrapper. Moreover, margin-top: auto; takes the available space of the wrapper. There is another way using position: absolute; but that is very error prone, I don't suggest you doing that. However, If you want I can tell you that too. The solution by Nik is also a good way to deal with this situation
.form-wrapper {
max-width: 300px;
width: 300px;
background-color: #D2B4DE;
padding: 20px 30px;
min-height: 300px;
height: 10vh;
margin:0 auto;
}
.form {
display: flex;
flex-direction: column;
flex-wrap: wrap;
margin: 0 auto;
height: 100%;
}
.form-title {
font-weight: bold;
font-size: 28px;
margin-bottom: 8px;
}
.button {
margin-top: auto;
justify-content: flex-end;
align-items: baseline;
}
<div class="form-wrapper">
<form class="form">
<div class="form-title">
This is a form title.
</div>
<label for="field">Field</label>
<input type="text" id="field" placeholder="Enter a field" />
<button class="button">Submit</button>
</form>
</div>

image auto stretches in flexbox

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>

Pin a button to the bottom corner of a container

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>

Place four elements inside div next to each other, with different widths

I am attempting to put four elements next to each other inside a container div; which all are a different width.
This is my desired outcome:
Unfortunately, this is what it looks like at the moment:
.container {
width: 100%;
background: blue;
padding: 10px;
}
p {
margin: 0px;
color: #fff;
font-size: 30px;
width: 11%;
float: left;
}
button {
float: right;
}
<div class="container">
<p>Title</p>
<form>
<input type="text" class="input">
</form>
<button>Apple</button>
<button>Orange</button>
<div style="clear: both;"></div>
</div>
JsFiddle: https://jsfiddle.net/uLL708jg/
Would Flex work for this? If so, could someone show me in an answer?
Yes, flexbox could solve this. This is just a basic setup. On .container, put a display: flex; and remove all the floats. Then wrap the buttons in a div (.right here) and give it margin-left: auto; to park it on the right of the screen.
Edit
justify-content: space-between; will align the three items so that there's space between the items and so the search will be in the middle of the div .container. No need anymore of margin-left: auto;, though the div wrapper around it is needed. Read more about flexbox and how to use it at MDN.
.container {
display: flex;
width: 100%;
background: blue;
padding: 10px;
flex-flow: row wrap;
align-items: center;
justify-content: space-between;
}
p {
margin: 0px;
color: #fff;
font-size: 30px;
width: 11%;
}
<div class="container">
<p>Title</p>
<form>
<input type="text" class="input">
</form>
<div class="right">
<button>Apple</button>
<button>Orange</button>
</div>
</div>