How to align input boxes center? - html

I have difficulty trying to put the input boxes for a form in the middle of the form, they don't seem to get affected by justify-content and align-items
(This question has been asked before, but I don't use labels, so the answers don't seem to work for me)
.form_exterior{
display: block;
width: 80vw;
height: fit-content;
width: 50vw;
background-color: #d3d3d3;
border: solid 1px;
border-radius: 60px;
margin: 50px auto;
}
.grid_form{
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-gap: 20px;
}
.form_title{
display: flex;
justify-content: center;
}
.section_button_form{
text-align: center;
}
.form_boxes{
display: flex;
text-align: center;
width: 50%;
}
<section>
<article class="form_exterior">
<form class="form_items">
<section class="grid_form">
<h1 class="form_title">Personal Information</h1>
<input class="form_boxes" type="text" name="name" value="Name">
<input class="form_boxes" type="text" name="last name" value="Last name">
<input class="form_boxes" type="text" name="email" value="Email">
<input class="form_boxes" type="text" name="phone number" value="Phone number">
<h1 class="form_title">Shipping information</h1>
<select class="form_boxes" id="country" name="Select country">
<option>select country</option>
<option value="US">United States</option>
<option value="UK">United Kingdom</option>
<input class="form_boxes" type="text" name="city" value="City">
<input class="form_boxes" type="text" name="postal code" value="Postal code">
<input class="form_boxes" type="text" name="street" value="Street address">
</select>
<section class="section_button_form">
<button onclick="alertForm()" class="button_form" type="button" name="order_button">order</button>
<button class="button_form" type="button" name="go_back_button">go back</button>
</section>
</form>
</article>
</section>

You can try this, it might save your issue:
.grid_form{
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-gap: 20px;
justify-items : center;
}

I believe your input boxes are block level elements and therefore are referenced to the width of their container.
You can set the width in your input element.
: The Input (Form Input) element
You could also try changing the display attribute on your input box to display: inline-block.
You might need to set a min-width on those elements also.

Related

Is there a way to correct content "expanding" behavior caused by fr sizing when zooming out in browser?

I created the following form layout with CSS grids, and as the gif shows, zooming out in the browser causes the content seem to expand while shrinking, instead of just shrinking the content towards the center as a whole just like with zoomed out stackoverflow.
I've tried to use pixels instead of fr to size my containers and it solves my issue, but using fr for sizing grid columns gives much more precision and convenience.
Is there a solution for this or is it to just stop using fr for css grids?
*{
box-sizing: border-box;
margin: 0;
}
label {
display: block;
}
body {
display: grid;
grid-template-rows: 50px auto 50px;
align-content: center;
align-items: center;
}
#checkout {
margin: 20px 100px;
display: grid;
grid-template:
"shipping payment" auto/
3fr 4fr
;
grid-column-gap: 50px;
}
.form1 {
display: grid;
padding: 15px;
grid-row-gap: 10px;
}
.form1 input, select {
width: 100% ;
height: 35px;
border-radius: 5px;
border: 1px solid gray;
padding: 5px;
margin-top: 2px;
}
#shipping {
grid-area: shipping;
border-right: 1px solid lightgray;
}
#payMethod {
grid-area: payment;
display: grid;
grid-template-rows: auto;
grid-row-gap: 30px;
}
input[type="submit"] {
justify-self: end;
width: 250px;
height: 50px;
background-color: #013CA6;
color: white;
font-size: 18px;
}
.payment-method {
display: grid;
grid-template-columns: 20px 1fr;
grid-column-gap: 15px;
justify-content: center;
align-items: start;
}
.pay-label {
display: grid;
grid-template:
"img label"
"img description" /
50px 1fr
;
grid-column-gap: 15px;
}
.pay-description {
grid-area: description;
}
.payImg {
grid-area: img;
}
#header {
}
.two-cols {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 5px;
width: 100%;
}
img {
max-width: 100%;
}
.required {
color: red;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="orderingStyle.css">
<title>Checkout Form</title>
</head>
<body>
<header id="header">
</header>
<main>
<form id="checkout">
<section id="shipping" class="form1">
<h2>Shipping Address</h2>
<div class="two-cols">
<div>
<label for="fName">First Name <span class="required">*</span></label>
<input type="text" name="fName" id="fName" placeholder="First Name" required/>
</div>
<div>
<label for="lName">Last Name <span class="required">*</span></label>
<input type="text" name="lName" id="lName" placeholder="Last Name" required/>
</div>
</div>
<div>
<label for="phoneNum">Phone Number <span class="required">*</span></label>
<input type="text" name="phoneNum" id="phoneNum" placeholder="Phone Number" required/>
</div>
<div>
<label for="stAddress">Street Address <span class="required">*</span></label>
<input type="text" name="stAddress" id="stAddress" placeholder="Street Address: Line 1" required/>
</div>
<div>
<label for="province">Province <span class="required">*</span></label>
<select name="province" id="province" required>
<option value="Abra">Abra</option>
<option value="Agusan del Norte">Agusan del Norte</option>
<option value="Agusan del Sur">Agusan del Sur</option>
<option value="Aklan">Aklan</option>
<option value="Albay">Albay</option>
<option value="Antique">Antique</option>
<option value="Apayao">Apayao</option>
<option value="Aurora">Aurora</option>
<option value="Basilan">Basilan</option>
<option value="Bataan">Bataan</option>
<option value="Batanes">Batanes</option>
<option value="Batangas">Batangas</option>
<option value="Biliran">Biliran</option>
<option value="Benguet">Benguet</option>
</select>
</div>
<div>
<label for="city">City <span class="required">*</span></label>
<select name="city" id="city" placeholder="Please select a city" required>
<option value="Alaminos">Alaminos</option>
<option value="Angeles">Angeles</option>
<option value="Antipolo">Antipolo</option>
<option value="Bacolod">Bacolod</option>
<option value="Bacoor">Bacoor</option>
<option value="Bago">Bago</option>
<option value="Baguio">Baguio</option>
<option value="Bais">Bais</option>
<option value="Balanga">Balanga</option>
</select>
</div>
</section>
<section id="payMethod">
<h2 style="border-bottom: 1px solid lightgray; padding: 10px;">Select Payment Method</h2>
<div class="payment-method">
<input type="radio" name="paymentOpt" value="BDO" id="bdo" required/>
<label class="pay-label" for="bdo"> <img class="payImg" src="https://www.firstbenefits.org/wp-content/uploads/2017/10/placeholder.png" alt="BDO icon"/>
<p>BDO Online Installment</p>
<p class="pay-description">Up to 12 months installment term based on installment price. Promo price may not be applicable.</p>
</label>
</div>
<div class="payment-method">
<input type="radio" name="paymentOpt" value="Metrobank" id="metrobank"/>
<label class="pay-label" for="metrobank"> <img class="payImg" src="https://www.firstbenefits.org/wp-content/uploads/2017/10/placeholder.png" alt="Metrobank icon"/>
<p>Metrobank Online Installment</p>
<p class="pay-description">Up to 12 months installment term based on installment price. Promo price may not be applicable.</p>
</label>
</div>
<div class="payment-method">
<input type="radio" name="paymentOpt" value="COD" id="cod"/>
<label class="pay-label" for="cod"> <img class="payImg" src="https://www.firstbenefits.org/wp-content/uploads/2017/10/placeholder.png" alt="Cash on Delivery icon"/>
<p>Cash on Delivery</p>
<p class="pay-description">Within P30,000 and Metro Manila, Cavite, Laguna only.</p>
</label>
</div>
<div class="payment-method">
<input type="radio" name="paymentOpt" value="GCash" id="gcash"/>
<label class="pay-label" for="gcash"> <img class="payImg" src="https://www.firstbenefits.org/wp-content/uploads/2017/10/placeholder.png" alt="GCash icon"/>
<p>GCash</p>
<!--<p class="pay-description">Within P30,000 and Metro Manila, Cavite, Laguna only.</p>-->
</label>
</div>
<input type="submit" value="PLACE ORDER">
</section>
</form>
</main>
</body>
</html>
Wrap the the form elements in an additional div use that as your grid container and set a max width to the form, centering the grid container.
#checkout {
/*Adjust max width as required*/
max-width:2000px;
/*Center the contents*/
margin: 0 auto;
}
#checkout .wrap {
margin: 20px 100px;
display: grid;
grid-template: "shipping payment" auto/ 3fr 4fr;
grid-column-gap: 50px;
}
* {
box-sizing: border-box;
margin: 0;
}
label {
display: block;
}
body {
display: grid;
grid-template-rows: 50px auto 50px;
align-content: center;
align-items: center;
}
.form1 {
display: grid;
padding: 15px;
grid-row-gap: 10px;
}
.form1 input,
select {
width: 100%;
height: 35px;
border-radius: 5px;
border: 1px solid gray;
padding: 5px;
margin-top: 2px;
}
#shipping {
grid-area: shipping;
border-right: 1px solid lightgray;
}
#payMethod {
grid-area: payment;
display: grid;
grid-template-rows: auto;
grid-row-gap: 30px;
}
input[type="submit"] {
justify-self: end;
width: 250px;
height: 50px;
background-color: #013CA6;
color: white;
font-size: 18px;
}
.payment-method {
display: grid;
grid-template-columns: 20px 1fr;
grid-column-gap: 15px;
justify-content: center;
align-items: start;
}
.pay-label {
display: grid;
grid-template: "img label" "img description" / 50px 1fr;
grid-column-gap: 15px;
}
.pay-description {
grid-area: description;
}
.payImg {
grid-area: img;
}
#header {}
.two-cols {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 5px;
width: 100%;
}
img {
max-width: 100%;
}
.required {
color: red;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="orderingStyle.css">
<title>Checkout Form</title>
</head>
<body>
<header id="header">
</header>
<main>
<form id="checkout">
<div class="wrap">
<section id="shipping" class="form1">
<h2>Shipping Address</h2>
<div class="two-cols">
<div>
<label for="fName">First Name <span class="required">*</span></label>
<input type="text" name="fName" id="fName" placeholder="First Name" required/>
</div>
<div>
<label for="lName">Last Name <span class="required">*</span></label>
<input type="text" name="lName" id="lName" placeholder="Last Name" required/>
</div>
</div>
<div>
<label for="phoneNum">Phone Number <span class="required">*</span></label>
<input type="text" name="phoneNum" id="phoneNum" placeholder="Phone Number" required/>
</div>
<div>
<label for="stAddress">Street Address <span class="required">*</span></label>
<input type="text" name="stAddress" id="stAddress" placeholder="Street Address: Line 1" required/>
</div>
<div>
<label for="province">Province <span class="required">*</span></label>
<select name="province" id="province" required>
<option value="Abra">Abra</option>
<option value="Agusan del Norte">Agusan del Norte</option>
<option value="Agusan del Sur">Agusan del Sur</option>
<option value="Aklan">Aklan</option>
<option value="Albay">Albay</option>
<option value="Antique">Antique</option>
<option value="Apayao">Apayao</option>
<option value="Aurora">Aurora</option>
<option value="Basilan">Basilan</option>
<option value="Bataan">Bataan</option>
<option value="Batanes">Batanes</option>
<option value="Batangas">Batangas</option>
<option value="Biliran">Biliran</option>
<option value="Benguet">Benguet</option>
</select>
</div>
<div>
<label for="city">City <span class="required">*</span></label>
<select name="city" id="city" placeholder="Please select a city" required>
<option value="Alaminos">Alaminos</option>
<option value="Angeles">Angeles</option>
<option value="Antipolo">Antipolo</option>
<option value="Bacolod">Bacolod</option>
<option value="Bacoor">Bacoor</option>
<option value="Bago">Bago</option>
<option value="Baguio">Baguio</option>
<option value="Bais">Bais</option>
<option value="Balanga">Balanga</option>
</select>
</div>
</section>
<section id="payMethod">
<h2 style="border-bottom: 1px solid lightgray; padding: 10px;">Select Payment Method</h2>
<div class="payment-method">
<input type="radio" name="paymentOpt" value="BDO" id="bdo" required/>
<label class="pay-label" for="bdo"> <img class="payImg" src="https://www.firstbenefits.org/wp-content/uploads/2017/10/placeholder.png" alt="BDO icon"/>
<p>BDO Online Installment</p>
<p class="pay-description">Up to 12 months installment term based on installment price. Promo price may not be applicable.</p>
</label>
</div>
<div class="payment-method">
<input type="radio" name="paymentOpt" value="Metrobank" id="metrobank" />
<label class="pay-label" for="metrobank"> <img class="payImg" src="https://www.firstbenefits.org/wp-content/uploads/2017/10/placeholder.png" alt="Metrobank icon"/>
<p>Metrobank Online Installment</p>
<p class="pay-description">Up to 12 months installment term based on installment price. Promo price may not be applicable.</p>
</label>
</div>
<div class="payment-method">
<input type="radio" name="paymentOpt" value="COD" id="cod" />
<label class="pay-label" for="cod"> <img class="payImg" src="https://www.firstbenefits.org/wp-content/uploads/2017/10/placeholder.png" alt="Cash on Delivery icon"/>
<p>Cash on Delivery</p>
<p class="pay-description">Within P30,000 and Metro Manila, Cavite, Laguna only.</p>
</label>
</div>
<div class="payment-method">
<input type="radio" name="paymentOpt" value="GCash" id="gcash" />
<label class="pay-label" for="gcash"> <img class="payImg" src="https://www.firstbenefits.org/wp-content/uploads/2017/10/placeholder.png" alt="GCash icon"/>
<p>GCash</p>
<!--<p class="pay-description">Within P30,000 and Metro Manila, Cavite, Laguna only.</p>-->
</label>
</div>
<input type="submit" value="PLACE ORDER">
</section>
</div>
</form>
</main>
</body>
</html>

Aligning div to another Div

I want to align my two divs. When I change screen resolution divs look weird. Here is picture of 1440 x 900 resoluton:
1440x900
And here is 1920 x 1080 screen:
1920x1080
I want to align 2nd div to first in order for screen to look like in the second picture, no matter the resolution.
Here is my Code :
.totalPriceDiv {
float: right;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
margin-top: 1rem;
margin-right: 22%;
}
.totalPrice {
background: none;
outline: none;
color: black;
border: none;
width: 100px;
font-size: 1rem;
padding: 0.115rem;
}
.totalPriceItem {
background: none;
outline: none;
color: black;
border: none;
width: 200px;
font-size: 1rem;
padding: 0.115rem;
}
<h1>My project</h1>
<div class="input_div">
<input class="inputMovieName" type="text" placeholder="Input 1">
<input class="inputMoviePrice" type="text" placeholder="Input 2">
<button class="addButton">Button</button>
</div>
<div class="container">
<div class="itemFirstRow">
<input type="text" class="item_input" disabled value="#">
<input type="text" class="item_input" disabled value="Input 1">
<input type="text" class="item_input" disabled value="Input 2">
</div>
<ul id="the-ul">
</ul>
</div>
<div class="totalPriceDiv">
<div>
<input type="text" class="totalPrice" disabled value="First: ">
<input id="totalAmount" type="text" class="totalPriceItem" value="0" disabled>
</div>
<div>
<input type="text" class="totalPrice" disabled value="Second: ">
<input id="discount" type="text" class="totalPriceItem" value="0" disabled>
</div>
<div id="border">
<input type="text" class="totalPrice" disabled value="Third: ">
<input id="afterDiscount" type="text" class="totalPriceItem" value="0" disabled>
</div>
</div>
<script type="text/javascript" src="main.js"></script>
Simple solution. You just follow basic bootstrap framework structure.
`
<div class="container">
<h1>xxxx</h1>
<div class="row">
<div class="col-offset-3 col-6">
Bootstrap form-group code comes here
</div>
</div> <div class="row"> block1</div>
</div>
`
Then it automatically behaves in responsive way.
Also, arrange using grid columns. like display: grid; and grid-gap: 10px;,
.input_div {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
.itemFirstRow {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
.totalPriceDiv {
display: flex;
align-items: flex-end;
flex-direction: column;
justify-content: center;
text-align: center;
margin-top: 1rem;
}
.totalPriceDiv > div {
border-bottom: 1px solid #ddd;
}
.totalPrice {
background: none;
outline: none;
color: black;
border: none;
width: 100px;
font-size: 1rem;
padding: 0.115rem;
align-items: flex-end;
}
.totalPriceItem {
background: none;
outline: none;
color: black;
border: none;
width: 200px;
font-size: 1rem;
padding: 0.115rem;
align-items: flex-end;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>My project</h1>
<div class="input_div">
<input class="inputMovieName" type="text" placeholder="Input 1">
<input class="inputMoviePrice" type="text" placeholder="Input 2">
<button class="addButton">Button</button>
</div>
<br/>
<div class="container">
<div class="itemFirstRow">
<input type="text" class="item_input" disabled value="#">
<input type="text" class="item_input" disabled value="Input 1">
<input type="text" class="item_input" disabled value="Input 2">
</div>
<ul id="the-ul">
</ul>
</div>
<div class="totalPriceDiv">
<div>
<input type="text" class="totalPrice" disabled value="First: ">
<input id="totalAmount" type="text" class="totalPriceItem" value="0" disabled>
</div>
<div>
<input type="text" class="totalPrice" disabled value="Second: ">
<input id="discount" type="text" class="totalPriceItem" value="0" disabled>
</div>
<div id="border">
<input type="text" class="totalPrice" disabled value="Third: ">
<input id="afterDiscount" type="text" class="totalPriceItem" value="0" disabled>
</div>
</div>
</body>
</html>
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));.
I think it will work.
check below the given example.
wrap your both div with a "div container" which will a css like this :
display: flex;
flex-direction: column;
And for your last div (i think is "totalPriceDiv") set your css with :
align-self: flex-end
and replace your margin and padding values ('rem', '%') by a fixed value ("px") or set this margin to the div container.
practice with this tool : flexy-boxes

Css Grid Can't Affect Input Items

I'm learning css grid and i'm trying to arrange a page with inputs and other stuff.
Nothing works for some reason and i can't figure out what the problem is.
The form doesn't show any reaction, doesn't move at all.
Here's the html code:
<div className="shipment-page">
<div className="shipment-form">
<form onSubmit={this.handleSubmit}>
<div className="first-input">
<label className="sp-input-label" htmlFor="first_name">First Name</label>
<input
type="text"
name='first_name'
className="sp-input first"
value={this.state.first_name}
onChange={this.handleChange}
required
/>
</div>
<div className="second-input">
<label className="sp-input-label" htmlFor="last_name">Last Name</label>
<input
type="text"
name='last_name'
className="sp-input second"
value={this.state.last_name}
onChange={this.handleChange}
required
/>
</div>
<div className="third-input">
<label className="sp-input-label" htmlFor="address">Address</label>
<input
type="text"
name='address'
className="sp-input"
value={this.state.address}
onChange={this.handleChange}
required
/>
</div>
<div className="fourth-input">
<label className="sp-input-label" htmlFor="Zip Code">Zip Code</label>
<input
type="number"
name='zip_code'
className="sp-input"
value={this.state.zip_code}
onChange={this.handleChange}
required
/>
</div>
<div className="fifth-input">
<label className="sp-input-label" htmlFor="phone_number">Phone Number</label>
<input
type="number"
name='phone_number'
className="sp-input"
value={this.state.phone_number}
onChange={this.handleChange}
required
/>
</div>
<div className="check-current-address">
<label htmlFor="current_address">Is this your current address? </label>
<input
type="radio"
name="current_address"
value={true}
onChange={this.handleChange}
required
/><span>Yes</span>
<input
type="radio"
name="current_address"
value={false}
onChange={this.handleChange}
required
/><span>No</span>
</div>
<CustomButton type='submit'>Add Address</CustomButton>
</form>
</div>
<span className="progress-indicator">
<StepProgress/>
</span>
</div>
and here's the sass code:
.shipment-page{
margin: 100px;
display: grid;
grid-template-columns: repeat(2, minmax(375px, 1fr)) minmax(300px, 1fr);
grid-template-rows: repeat(6, 50px) 1fr;
grid-template-areas:
"first second info-box"
"third third info-box"
"fourth fifth info-box"
"check . ."
"buttons . ."
"progres progres progres";
.shipment-form{
grid-row: 1/6;
grid-column: 1/3;
.first-inputs{
grid-area: first;
}
.second-inputs{
grid-area: second;
}
.third-inputs{
grid-area: third;
}
.fourth-inputs{
grid-area: fourth;
}
.fifth-inputs{
grid-area: fifth;
}
.sp-input{
background: none;
background-color: white;
color: grey;
font-size: 18px;
padding: 10px 10px 10px 5px;
width: 100%;
border: none;
border-radius: 0;
border-bottom: 1px solid grey;
&:focus{
outline: none;
color: black;
border-bottom: 1px solid black;
}
}
.check-current-address{
margin-bottom: 25px;
}
}
.progress-indicator{
grid-area: progres;
}
}
how the page looks like now
how it should look like
Help.
The form isn't responding the way you expect because only direct grid children matter (just like with flexbox). .shipment-form is a valid grid child because it's one level deep from the grid parent, .shipment-page.
<div className="shipment-page"> <!-- grid parent -->
<div className="shipment-form"> <!-- grid child -->
<form onSubmit={this.handleSubmit}> <!-- too deep—not a grid child -->
Try to restructure your code so that the grid parent is one level above your form input containers. Also, you have a potential bug in your code. Your CSS is referencing the plural inputs but your HTML class is using the singular input
<div className="first-input">
And then:
.first-inputs{ ... } /* Oops, that class does not exist in your HTML */

Centring a form

I am trying to center my form to the middle of the page. Currently, I am using a div and placing the form inside it. The div got centered but not the form inside the div. Here is the portion of my HTML and CSS.
form {
margin: auto;
}
.wrapper {
margin: auto;
width: 50%;
padding: 0px;
display: table;
}
fieldset {
margin: 1em;
padding: 1em;
border-color: crimson;
border-radius: 20px;
border-style: double;
border-width: 10px;
width: 70%;
}
<div class="wrapper">
<form id="form1">
<fieldset>
<legend>Your contact details</legend>
<p class="formtext">Name <em>(required)</em></p>
<input type="text" id="name" required placeholder="Forname & Surname" />
<p class="formtext">Email Address <em>(required)</em></p>
<input type="text" id="email" required placeholder="example#example.com" />
<p class="formtext">Website Address</p>
<input type="text" id="url" placeholder="https:www.example.com" />
<p class="formtext">Message</p>
<textarea id="message" required></textarea>
<p class="formtext">Would you like to recieve regular email updates?</p>
<select name="cars">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</fieldset>
<fieldset>
<legend>Would you like more information?</legend>
<label class="button" for="information-yes">
<input type="checkbox" id="information-yes" name="information" value="yes" />Yes please</label>
<label class="button" for="information-no">
<input type="checkbox" id="information-no" name="information" value="no" checked />No thanks</label>
</fieldset>
<input type="submit" value="Click to send" />
</form>
</div>
add text-align: center; to the form tag
form {
margin: auto;
text-align: center;
}
.wrapper {
margin: auto;
width: 50%;
padding: 0px;
display: table;
}
fieldset {
margin: 1em;
padding: 1em;
border-color: crimson;
border-radius: 20px;
border-style: double;
border-width: 10px;
width: 70%;
}
<div class="wrapper">
<form id="form1">
<fieldset>
<legend>Your contact details</legend>
<p class="formtext">Name <em>(required)</em></p>
<input type="text" id="name" required placeholder="Forname & Surname">
<p class="formtext">Email Address <em>(required)</em></p>
<input type="text" id="email" required placeholder="example#example.com">
<p class="formtext">Website Address</p>
<input type="text" id="url" placeholder="https:www.example.com">
<p class="formtext">Message</p>
<textarea id="message" required></textarea>
<p class="formtext">Would you like to recieve regular email updates?</p>
<select name="cars">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</fieldset>
<fieldset>
<legend>Would you like more information?</legend>
<label class="button" for="information-yes">
<input type="checkbox" id="information-yes" name="information" value="yes">Yes please</label>
<label class="button" for="information-no">
<input type="checkbox" id="information-no" name="information" value="no" checked />No thanks</label>
</fieldset>
<input type="submit" value="Click to send" />
</form>
</div>
<form> is taking width and height of parent div, so as div got centered, the <form> inside it also got centered. Now the challenge comes in to center the content inside the <form>. For it check my solution.
HTML
<div class="wrapper">
<form id="form1">
<fieldset>
<legend>Your contact details</legend>
<div class="form-center">
<p class="formtext">Name <em>(required)</em></p>
<input type="text" id="name" required placeholder="Forname & Surname" />
<p class="formtext">Email Address <em>(required)</em></p>
<input type="text" id="email" required placeholder="example#example.com" />
<p class="formtext">Website Address</p>
<input type="text" id="url" placeholder="https:www.example.com" />
<p class="formtext">Message</p>
<textarea id="message" required></textarea>
<p class="formtext">Would you like to recieve regular email updates?</p>
<select name="cars">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</fieldset>
<fieldset>
<legend>Would you like more information?</legend>
<div class="form-center">
<label class="button" for="information-yes">
<input type="checkbox" id="information-yes" name="information" value="yes" />Yes please</label>
<label class="button" for="information-no">
<input type="checkbox" id="information-no" name="information" value="no" checked />No thanks</label>
</div>
</fieldset>
<input type="submit" value="Click to send" />
</form>
</div>
CSS
form {
margin:auto;
}
div.form-center {
width: 60%;
margin-left: auto;
margin-right: auto;
}
.wrapper {
margin: auto;
width: 50%;
padding: 0px;
display: table;
}
fieldset {
margin: 1em;
padding: 1em;
border-color: crimson;
border-radius: 20px;
border-style: double;
border-width: 10px;
width: 70%;
}
form {
/*margin: auto; Not required as long as you didn't specify the width of the form*/
text-align: center;
}
fieldset {
/*centering the fieldset horizontally*/
margin: 1em auto;
...
}
You may want to use Flexbox.
On the fieldset selector add the following 3 lines of code:
display: flex;
flex-direction: column;
align-items: center;
Also, on the fieldset selector, instead margin: 1em, use margin: 1em auto. This will make the margin 1em on top and bottom of the fieldset element, but will position the element centrally inside its <form> parent.
Your example would then remain the same, only for the fieldset selector you have the following css properties:
fieldset {
display: flex;
flex-direction: column;
align-items: center;
margin: 1em auto;
padding: 1em;
border-color: crimson;
border-radius: 20px;
border-style: double;
border-width: 10px;
width: 70%;
}
EDIT: Flexbox on <fieldset> element is supported only on Firefox 64+, not yet supported on Chrome. I found this after posting and testing to see if it works in Chrome as I tested it only on Firefox before posting. More information about flexbox and <fieldset> elements can be found in this thread.

Width of html input is not inherited from the parent

I am trying to understand deeply the HTML/CSS behavior, in the following example the birth date input width is shorter than the other inputs.
When I am setting its width to 100%, it gets out the boundaries of the parent div.
What am I doing wrong?
HTML:
<body>
<div class="form">
<input type="text" placeholder="Last Name">
<input type="text" placeholder="First Name">
<input type="text" placeholder="Country">
<div>
<input type="text" id="birthDate" placeholder="Birth Date">
<span class="dateFormat">YYYY-MM-DD</span>
</div>
</div>
</body>
CSS:
.form input {
margin:5px;
height: 30px;
padding: 5px;
}
.form {
display: grid;
margin: auto;
width: 50%;
grid-template-columns: 1fr 1fr;
}
JSFIDDLE:
https://jsfiddle.net/AlexLavriv/oxmq71sn/
You will have to add styling rule for your input as well. Please use the below code as reference.
.form input {
margin:5px;
height: 30px;
padding: 5px;
}
.form {
display: grid;
margin: auto;
width: 50%;
grid-template-columns: 1fr 1fr;
}
input[type=text]{
width: 100%;
}
<body>
<div class="form">
<input type="text" placeholder="Last Name">
<input type="text" placeholder="First Name">
<input type="text" placeholder="Country">
<div>
<input type="text" id="birthDate" placeholder="Birth Date">
<span class="dateFormat">YYYY-MM-DD</span>
</div>
</div>
</body>
Hope this help