I'm trying to align this box and the two buttons below it directly in the center of the page [horizontally and vertically]. Whenever, I get the textbox to align center, the buttons always seem stuck on the side. I think it's the way I'm trying use CSS for the body and then move the text and buttons as well. The buttons need to be side by side under the textbox [with everything aligned directly in the center of the page].
Here is what I have so far:
.calc {
display: inline-block;
/* align-items: center; */
padding-top: 200px;
padding-left: 20px;
/* display: flex; */
}
.search_btn {
align-items: center;
width: 200px;
margin: 0 auto;
/* display: flex; */
display: inline-block;
}
.search_txt {
width: 300px;
margin: 0 auto;
display: block;
/* display: flex; */
/* flex-direction: column; */
border-radius: 25px;
}
<form class="calc" action="https://calculator.com">
<input class="search_txt" type="text" name="q">
<input class="search_btn" type="submit" value="value">
<input class="search_btn" type="submit" value="area">
<!-- <input class="search_btn" type="submit" value="perimeter"> -->
</form>
#Katie Melosto. I think that the most important thing to deal with flex box is to know where to apply flex to align items, i.e parent element or child element itself.
You can check this url for detailed information of flex css.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I have changed some part of your code.
Check this url for my chage.
"https://codepen.io/devbluesky111/pen/oNZxBYW"
body {
display: flex;
justify-content: center;
}
.calc {
display: inline-block;
/* align-items: center; */
padding-top: 200px;
padding-left: 20px;
display: flex-column;
}
.search_btn {
align-items: center;
width: 150px;
margin: 0 auto;
/* display: flex; */
display: block;
}
.search_txt {
width: 300px;
margin: 0 auto;
display: block;
border-radius: 25px;
margin-bottom: 10px;
}
.buttons {
display: flex;
/* flex-direction: column; */
}
<form class="calc" action="https://calculator.com">
<input class="search_txt" type="text" name="q">
<div class="buttons">
<input class="search_btn" type="submit" value="value">
<input class="search_btn" type="submit" value="area">
</div>
</form>
I think you need to wrap both your buttons in a container div and let that container have display:flex. Also your main form need to be display:flex as well but its direction would be flex-direction:column. Then to center things , you can just use align-items:center; justify-content:center
.form-buttons {
display: flex;
gap: 10px;
}
.calc {
display: flex;
flex-direction: column;
gap: 10px;
align-items: center;
justify-content: center;
height: 100vh;
}
.search_btn {
width: 200px;
}
.search_txt {
width: 300px;
border-radius: 25px;
}
<form class="calc" action="https://calculator.com">
<input class="search_txt" type="text" name="q">
<div class="form-buttons">
<input class="search_btn" type="submit" value="value">
<input class="search_btn" type="submit" value="area">
</div>
<!-- <input class="search_btn" type="submit" value="perimeter"> -->
</form>
Flex align items based on how many there are within the assigned element. So to do what you want you should wrap you buttons in a div and then set the container to flex with column. That will place the input and buttons in a column. Now set the container for the button to flex row.
.calc {
display: inline-block;
/* align-items: center; */
padding-top: 200px;
padding-left: 20px;
display: flex;
flex-direction: column;
}
#btnContainer {
display: flex;
}
.search_btn {
align-items: center;
width: 200px;
margin: 0 auto;
}
.search_txt {
width: 300px;
margin: 0 auto;
border-radius: 25px;
}
<form class="calc" action="https://calculator.com">
<input class="search_txt" type="text" name="q">
<div id="btnContainer">
<input class="search_btn" type="submit" value="value">
<input class="search_btn" type="submit" value="area">
</div>
<!-- <input class="search_btn" type="submit" value="perimeter"> -->
</form>
You can use display:gird; and place-items:center;
To align content in center
body {
display:grid;
place-items:center;
}
.calc {
display: inline-block;
/* align-items: center; */
padding-top: 200px;
padding-left: 20px;
/* display: flex; */
}
.search_btn {
align-items: center;
width: 200px;
margin: 0 auto;
/* display: flex; */
display: inline-block;
}
.search_txt {
width: 300px;
margin: 0 auto;
display: block;
/* display: flex; */
/* flex-direction: column; */
border-radius: 25px;
}
<form class="calc" action="https://calculator.com">
<input class="search_txt" type="text" name="q">
<input class="search_btn" type="submit" value="value">
<input class="search_btn" type="submit" value="area">
<!-- <input class="search_btn" type="submit" value="perimeter"> -->
</form>
Related
In my project, I want to design a shipping address design with HTML and css(flexbox). And i want to design "city","zip code","state", box in one line like this
but in my code, I am unable to do like that design so help me to what changes should I have done to get that result?
And my code results shows is like this
So I want "city","zip code","state" box takes same size fit to the "ad
Here is my code
.shipping-containers {
height: 100vh;
width: 100%;
border: 2px solid black;
display: flex;
flex-direction: column;
flex-wrap: wrap;
background: red;
justify-content: center;
align-items: center;
}
.shipping-wrapper {
display: flex;
flex-direction: column;
width: 70%;
background: lightskyblue;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.shipping-wrapper>form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
border: 1px solid black;
}
.shipping-div {
display: flex;
flex-direction: row;
margin-right: 15rem;
justify-content: center;
align-items: center;
}
.shipping-div input {
text-align: center;
justify-content: center;
align-items: center;
}
.shipping-wrapper input {
width: 50%;
font-size: 10px;
padding: 15px;
background: #f1f1f1;
margin-bottom: 10px;
}
<div class="shipping-containers">
<div class="shipping-wrapper">
<form>
<input type="text" placeholder=" name" />
<input type="text" placeholder=" address" />
<div class="shipping-div">
<div>
<input type="text" placeholder=" city" class="shipping-input" />
</div>
<div>
<input type="text" placeholder=" zip code " class="shipping-input" />
</div>
<div>
<input type="text" placeholder=" state" class="shipping-input" />
</div>
</div>
<input type="text" placeholder=" Country" class="shipping-input" />
<button>Continue</button>
</form>
</div>
</div>
You have many borders included which are a mess to take into account. The easiest and cleanest solution is to use * { box-sizing: border-box; }. With that property, we don't have to take all the different borders into account.
You nested way too many Flexbox and used unnecessary div's. As such I removed all div's out of your form.
I used flex-wrap: wrap on your form. Then I raised the width from all the inputs to 100% and created the space left and right with padding on the form.
I changed justify-content: center to justify-content: space-between for your form.
I changed the flex-direction: column on the form to default row as otherwise, the flex-wrap will not work.
The width for the 3 small inputs must be calculated (that's why CSS-Grid would have been a better and easier solution). YOu have to take the 2x gaps a 10px each into account. So the width for the 3 elements in total is 100% - 20px. That again must then be divided by 3:
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.shipping-containers {
height: 100vh;
border: 2px solid black;
display: flex;
flex-direction: column;
flex-wrap: wrap;
background: red;
justify-content: center;
align-items: center;
}
.shipping-wrapper {
display: flex;
flex-direction: column;
width: 70%;
background: lightskyblue;
justify-content: center;
align-items: center;
}
.shipping-wrapper > form {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
border: 1px solid black;
padding: 0 15%;
}
.shipping-wrapper input {
width: 100%;
font-size: 10px;
padding: 15px;
background: #f1f1f1;
margin-bottom: 10px;
}
.shipping-wrapper input:nth-child(3),
.shipping-wrapper input:nth-child(4),
.shipping-wrapper input:nth-child(5) {
width: calc((100% - 20px) / 3);
}
.shipping-wrapper button {
margin: 0 auto;
}
<div class="shipping-containers">
<div class="shipping-wrapper">
<form>
<input type="text" placeholder="name">
<input type="text" placeholder="address">
<input type="text" placeholder="city">
<input type="text" placeholder="zip code">
<input type="text" placeholder="state">
<input type="text" placeholder="Country">
<button>Continue</button>
</form>
</div>
</div>
Here's my take. Points to note: box-sizing: border-box is a must for the inputs.
.shipping-div has negative margin so children with margin would align to width (this is bootstrap trick). And the rest is trivial.
.shipping-containers {
height: 100vh;
border: 2px solid black;
display: flex;
background: #ff00007f;
justify-content: center;
align-items: center;
}
.shipping-wrapper {
width: 70%;
background: lightskyblue;
}
.shipping-wrapper>form {
border: 1px solid black;
}
.shipping-div {
display: flex;
justify-content: space-between;
margin-left: -5px;
margin-right: -5px;
}
.shipping-div>div {
margin-left: 5px;
margin-right: 5px;
width: 100%;
}
.shipping-wrapper input {
width: 100%;
min-width: 100%;
font-size: 10px;
padding: 15px;
background: #f1f1f1;
margin-bottom: 10px;
position: relative;
display: block;
box-sizing: border-box;
}
<div class="shipping-containers">
<div class="shipping-wrapper">
<form>
<input type="text" placeholder=" name" />
<input type="text" placeholder=" address" />
<div class="shipping-div">
<div>
<input type="text" placeholder=" city" class="shipping-input" />
</div>
<div>
<input type="text" placeholder=" zip code " class="shipping-input" />
</div>
<div>
<input type="text" placeholder=" state" class="shipping-input" />
</div>
</div>
<input type="text" placeholder=" Country" class="shipping-input" />
<button>Continue</button>
</form>
</div>
</div>
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>
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I horizontally center an element?
(133 answers)
Closed 1 year ago.
I am currently doing an assignemnt for my diploma of website development.. I am struggling to get a form to be centred within a flexbox.
<div id="article_right">
<h2>Contact Us!</h2>
<form id="contact_form">
<div class="input-box">
<label for="contact_name">Full name:</label>
<input type="text" id="contact_name" name="contact_name" placeholder="Name">
</div>
<div class="input-box">
<label for="contact_phone">Mobile number:</label>
<input type="tel" id="contact_phone" name="contact_phone" placeholder="Mobile number">
</div>
<div class="input-box">
<label for="contact_email">Email address:</label>
<input type="email" id="contact_email" name="contact_email" placeholder="E-mail address">
</div>
<div class="input-box">
<label for="contact_message">Message:</label>
<textarea rows="4" cols="40" id="contact_message" name="contact+message" style="resize: none"></textarea>
</div>
<div class="button">
<button type="submit">Submit</button>
</div>
</form>
</div>
</article>
/* Article Styling Definitions */
article {
display: flex;
flex: 3;
background-color: transparent;
}
#article_left {
flex: 1;
align-content: center;
margin-right: auto;
background-color: aqua;
}
#article_right {
flex: 1;
align-content: center;
justify-content: center;
padding-bottom: 5px;
background-color: transparent;
margin-left: auto;
}
.cta {
flex: 30;
border: 1px solid black;
border-radius: 5px;
margin: 25px;
padding-left: 5px;
}
form {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
margin: 0 auto;
}
.input-box {
padding-right: 5px;
display: flex;
width: 100%;
margin-bottom: 10px;
}
label {
width: 45%;
padding-right: 5px;
text-align: right;
}
input, textarea {
width: 150%;
}
I have tried everything and I just can't seem to get it to fill the right flexbox and for the form elements to be centered. The button and h2 are but the form itself won't
The problem is that you are setting article to display flex but you are setting what is to happen with the flex to its child div.
I am not absolutely sure which you want (perhaps both) but this snippet adds display flex just to the child div to at least demonstrate that something changes:
<style>
body {
height: 100vh;
display: inline-block;
width: 100vh;
}
/* Article Styling Definitions */
article {
height: 100%;
position: relative;
display: flex;
flex: 3;
background-color: transparent;
}
#article_left {
flex: 1;
align-content: center;
margin-right: auto;
background-color: aqua;
}
#article_right {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
padding-bottom: 5px;
background-color: transparent;
margin-left: auto;
}
.cta {
flex: 30;
border: 1px solid black;
border-radius: 5px;
margin: 25px;
padding-left: 5px;
}
form {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
margin: 0 auto;
}
.input-box {
padding-right: 5px;
display: flex;
width: 100%;
margin-bottom: 10px;
}
label {
width: 45%;
padding-right: 5px;
text-align: right;
}
input,
textarea {
width: 150%;
}
</style>
<body>
<article>
<div id="article_right">
<h2>Contact Us!</h2>
<form id="contact_form">
<div class="input-box">
<label for="contact_name">Full name:</label>
<input type="text" id="contact_name" name="contact_name" placeholder="Name">
</div>
<div class="input-box">
<label for="contact_phone">Mobile number:</label>
<input type="tel" id="contact_phone" name="contact_phone" placeholder="Mobile number">
</div>
<div class="input-box">
<label for="contact_email">Email address:</label>
<input type="email" id="contact_email" name="contact_email" placeholder="E-mail address">
</div>
<div class="input-box">
<label for="contact_message">Message:</label>
<textarea rows="4" cols="40" id="contact_message" name="contact+message" style="resize: none"></textarea>
</div>
<div class="button">
<button type="submit">Submit</button>
</div>
</form>
</div>
</article>
</body>
You probably want to set flex-direction to get the contact us centered above the form.
It's true, just set width to <form> for example max-width:600px
What You are doing is ok.
Just try to add some width to the form, example:
https://jsfiddle.net/nk9owjyz/3/
form {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
margin: 0 auto;
width: 50%;
}
Just update your article css
article {
display: flex;
justify-content: center;
align-items: center;
width: 80vw;
background-color: transparent;
}
Try Full Code
/* Article Styling Definitions */
article {
display: flex;
justify-content: center;
align-items: center;
width: 80vw;
background-color: transparent;
}
#article_left {
flex: 1;
align-content: center;
margin-right: auto;
background-color: aqua;
}
#article_right {
flex: 1;
align-content: center;
justify-content: center;
padding-bottom: 5px;
background-color: transparent;
margin-left: auto;
}
.cta {
flex: 30;
border: 1px solid black;
border-radius: 5px;
margin: 25px;
padding-left: 5px;
}
form {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
margin: 0 auto;
}
.input-box {
padding-right: 5px;
display: flex;
width: 100%;
margin-bottom: 10px;
}
label {
width: 45%;
padding-right: 5px;
text-align: right;
}
input, textarea {
width: 150%;
}
<article id="article">
<div id="article_right">
<h2>Contact Us!</h2>
<form id="contact_form">
<div class="input-box">
<label for="contact_name">Full name:</label>
<input type="text" id="contact_name" name="contact_name" placeholder="Name" />
</div>
<div class="input-box">
<label for="contact_phone">Mobile number:</label>
<input type="tel" id="contact_phone" name="contact_phone" placeholder="Mobile number" />
</div>
<div class="input-box">
<label for="contact_email">Email address:</label>
<input type="email" id="contact_email" name="contact_email" placeholder="E-mail address" />
</div>
<div class="input-box">
<label for="contact_message">Message:</label>
<textarea rows="4" cols="40" id="contact_message" name="contact+message"
style="resize: none"></textarea>
</div>
<div class="button">
<button type="submit">Submit</button>
</div>
</form>
</div>
</article>
This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 3 years ago.
So I have a container with label text and checkboxes, now this is what I currently have
body {
display: flex;
justify-content: center;
}
.container {
background-color: yellow;
display: flex;
width: 200px;
height: fit-content;
margin: 0 10px;
justify-content: space-around;
}
.container.firstCheck{
flex-grow: 1;
}
.container.secondCheck{
flex-grow: 1;
}
<div class="container">
<div class="checkboxes">
<div class="firstCheck">
<label for="check1">this is 1</label>
<input name="check1" type="checkbox"/>
</div>
<div class="SecondCheck">
<label for="check2">this is 2</label>
<input name="check2" type="checkbox"/>
</div>
</div>
</div>
now I want the label to be at the far left of the container and the checkbox to be at the far right of the container using flexbox. This illustrates my goal
.
PS: I also want to be able to reduce html tags if possible.
Use justify-content: space-between to place the items on the left and right of the container.
Also, you need to correct the SecondCheck name in CSS. In the HTML structure, it has capital 'S' while in CSS it has small 's'.
I have corrected it in my answer.
Hope this helps, Thanks!!
body {
display: flex;
justify-content: center;
}
.container {
background-color: #fff;
display: flex;
width: 200px;
height: fit-content;
margin: 0 10px;
justify-content: space-around;
border:2px solid #000;
}
.container.firstCheck{
flex-grow: 1;
}
.container.SecondCheck{
flex-grow: 1;
}
.firstCheck{
margin-bottom: 20px;
}
.checkboxes{
width: 100%;
display: inline-block;
padding: 20px;
}
.firstCheck, .SecondCheck{
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
<div class="container">
<div class="checkboxes">
<div class="firstCheck">
<label for="check1">this is 1</label>
<input name="check1" type="checkbox"/>
</div>
<div class="SecondCheck">
<label for="check2">this is 2</label>
<input name="check2" type="checkbox"/>
</div>
</div>
</div>
Use flex on .firstCheck, and .SecondCheck then set margin-left: auto on input. I removed some useless codes, check it out below:
body {
display: flex;
justify-content: center;
}
.container {
background-color: yellow;
width: 200px;
height: 150px;
margin: 0 10px;
align-items: center;
justify-content: center;
display: flex;
}
.checkboxes {
width: 100%;
padding: 10px;
}
.firstCheck input, .SecondCheck input {
margin-left: auto;
}
.firstCheck, .SecondCheck {
display: flex;
}
<div class="container">
<div class="checkboxes">
<div class="firstCheck">
<label for="check1">this is 1</label>
<input name="check1" type="checkbox" />
</div>
<div class="SecondCheck">
<label for="check2">this is 2</label>
<input name="check2" type="checkbox" />
</div>
</div>
</div>
You need to apply flex properties to the elements that you want to position, like this:
body {
display: flex;
justify-content: center;
width: 100%;
}
.container {
background-color: yellow;
display: flex;
width: 200px;
height: fit-content;
margin: 0 10px;
/* justify-content: space-around; */
}
.checkboxes {
width: 100%;
}
.firstCheck {
display: flex;
flex-direction: row;
flex-grow: 1;
justify-content: space-between;
width: 100%;
}
.secondCheck{
display: flex;
flex-direction: row;
flex-grow: 1;
justify-content: space-between;
width: 100%;
}
.firstLabel {
display: flex;
flex-direction: row;
}
.secondLabel {
display: flex;
flex-direction: row;
}
<div class="container">
<div class="checkboxes">
<div class="firstCheck">
<label class="firstLabel" for="check1">this is 1</label>
<input class="firstInput" name="check1" type="checkbox"/>
</div>
<div class="secondCheck">
<label class="secondLabel" for="check2">this is 2</label>
<input class="secondInput" name="check2" type="checkbox"/>
</div>
</div>
</div>
This can be done as follows:
body {
display: flex;
justify-content: center;
}
.container {
display: flex;
flex-direction: column;
justify-content: space-evenly;
width: 200px;
height: 140px;
border: solid 2px #ccc;
}
.divcheck {
display: flex;
justify-content: space-between;
margin: 0px 20px 0px 20px;
}
<div class="container">
<div class="divcheck">
<label for="check1">this is 1</label>
<input name="check1" type="checkbox"/>
</div>
<div class="divcheck">
<label for="check2">this is 2</label>
<input name="check2" type="checkbox"/>
</div>
</div>
minimal solution
body {
display: flex;
justify-content: center;
}
.container {
background-color: yellow;
width: 200px;
border: 2px solid black;
padding: 10px;
}
.checkbox {
display: flex;
justify-content: space-around;
padding: 5px;
}
<div class="container">
<div class="checkbox">
<label for="check1">this is 1</label>
<input name="check1" type="checkbox" />
</div>
<div class="checkbox">
<label for="check2">this is 2</label>
<input name="check2" type="checkbox" />
</div>
</div>
You can use grid if you want less divs
.parent {
display: grid;
width: 200px;
grid-template-columns: 50% 50%;
grid-auto-rows: 50px;
align-items: center;
justify-items: center;
}
.child {
margin: 0 10px;
}
<div class="parent">
<div class="child">label</div>
<div class="child"><input type="checkbox" /></div>
<div class="child">label</div>
<div class="child"><input type="checkbox" /></div>
</div>
Firstly, the container has some padding from your space-around applied css that you have to remove in order to create some space for the first&second check divs. You don't actually need the display flex for container div.
You should actually set both checks position: relative; and move your checkbox to right using position: absolute; and right: 0px;
You can reduce the number of classes by replacing the firstCheck and secondCheck with a simple .check class or whatever you want it to be. You can style them individually using the :nth-of-type(n) without having to assign a class or id to each of them.
HTML
<div class="container">
<div class="checkboxes">
<div class="check">
<label for="check1">this is the first and longer label spread on 2 rows</label>
<input name="check1" type="checkbox"/>
</div>
<div class="check">
<label for="check2">this is 2</label>
<input name="check2" type="checkbox"/>
</div>
</div>
</div>
CSS
body {
display: flex;
justify-content: center;
}
.container {
background-color: yellow;
width: 200px;
padding: 10px 60px 10px 30px;
margin: 0 10px;
}
.checkboxes {
width: 100%;
}
.checkboxes .check {
position: relative;
}
.checkboxes .check input {
position: absolute;
top: 0px; /* the checkbox is normally positioned at the end of the last row of a label, this positions it in line with the first one */
right: -30px; /* just to make sure the label wont be too close to the checkbox */
}
Here's a codePen for you to see how it works: https://codepen.io/ialexandru/pen/YzPvGPR
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>