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 */
Related
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.
I am using CSS grid to display some tabular data as shown in example
https://codepen.io/shyamforflex/pen/qBrLQeq
On row selection , the selected row should have a background color.
In current example the background red color is disappearing on mouse click release
&-row:active {
background-color: red;
}
You could use a radio button hack. Add a hidden radio button to each row. Change your "cells" to label associated with the radio button then use type=[radio]:checked ~ label to apply the styling. While the HTML is perfectly valid, the use of input here is semantically questionable at best.
From your pen you would want something like
PUG
each episode in episodes
.grid-table-row
input(type="radio" name="tbl" id=episode.series +"|"+episode.no)
label.grid-table-cell(data-title="Title" for=episode.series +"|"+episode.no)= episode.title
label.grid-table-cell(data-title="Number" for=episode.series +"|"+episode.no)= episode.no
label.grid-table-cell(data-title="Series" for=episode.series +"|"+episode.no)=episode.series
label.grid-table-cell(data-title="Air Date" for=episode.series +"|"+episode.no)= episode.airdate
SCSS
.grid-table {
display: grid;
/*Additional code removed for brevity*/
input[type=radio] {display:none}
input:checked ~ label {
background-color: red;
}
}
Compiled Demo:
h1 {
text-align: center;
}
input[type=radio] {
display: none;
}
.grid-table {
display: grid;
}
.grid-table-row {
display: grid;
grid-template-columns: repeat(2, 2fr) repeat(2, 1fr);
border-bottom: 1px solid #ddd;
}
.grid-table-row:first-child {
background: rgba(0, 0, 0, 0.5);
font-weight: bold;
}
.grid-table-row:hover {
background-color: yellow;
cursor: pointer;
}
.grid-table input:checked~label {
background-color: red;
}
.grid-table-row:nth-child(even) {
background-color: #e5e4e2;
}
.grid-table-cell {
padding: 1rem;
}
.grid-table-cell:not(:last-child) {
border-right: 1px solid #ddd;
}
<h1>Random Dr Who Tabular Data With CSS Grid</h1>
<div class="grid-table">
<div class="grid-table-row">
<div class="grid-table-cell">Title</div>
<div class="grid-table-cell">Episode Number</div>
<div class="grid-table-cell">Series</div>
<div class="grid-table-cell">Air Date</div>
</div>
<div class="grid-table-row">
<input type="radio" name="tbl" id="7|239" />
<label class="grid-table-cell" data-title="Title" for="7|239">The Name of the Doctor</label>
<label class="grid-table-cell" data-title="Number" for="7|239">239</label>
<label class="grid-table-cell" data-title="Series" for="7|239">7</label>
<label class="grid-table-cell" data-title="Air Date" for="7|239">05/18/2013</label>
</div>
<div class="grid-table-row">
<input type="radio" name="tbl" id="Special|1" />
<label class="grid-table-cell" data-title="Title" for="Special|1">The Day of the Doctor</label>
<label class="grid-table-cell" data-title="Number" for="Special|1">1</label>
<label class="grid-table-cell" data-title="Series" for="Special|1">Special</label>
<label class="grid-table-cell" data-title="Air Date" for="Special|1">11/23/2013</label>
</div>
<div class="grid-table-row">
<input type="radio" name="tbl" id="Special|2" />
<label class="grid-table-cell" data-title="Title" for="Special|2">The Time of the Doctor</label>
<label class="grid-table-cell" data-title="Number" for="Special|2">2</label>
<label class="grid-table-cell" data-title="Series" for="Special|2">Special</label>
<label class="grid-table-cell" data-title="Air Date" for="Special|2">11/25/2013</label>
</div>
<div class="grid-table-row">
<input type="radio" name="tbl" id="Special|3" />
<label class="grid-table-cell" data-title="Title" for="Special|3">The Salary of the Doctor</label>
<label class="grid-table-cell" data-title="Number" for="Special|3">3</label>
<label class="grid-table-cell" data-title="Series" for="Special|3">Special</label>
<label class="grid-table-cell" data-title="Air Date" for="Special|3">6/25/2015</label>
</div>
</div>
:focus can work for what you want to achieve only if the element is an focusable element such as anchor link or inputs. Please refer to the MDN for more information.
Solution
There is a way to add focusability to normal elements. Set tabindex to each row so that the elements become focusable.
Set tabindex to .grid-table-row
each episode in episodes
.grid-table-row(tabindex="0")
Replace &-row:active with below
&-row:focus {
background-color: red;
}
However, this method should only be used for web accessbility, which means only the interactive elements should be focusable.
This question already has an answer here:
Why aren't my grid-template-areas with ASCII art not working?
(1 answer)
Closed 2 years ago.
I have the following code in HTML
.gridDiv {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: 1fr 2fr;
grid-template-areas:
"topLeft topMiddle topRight"
"blank form blank";
}
.leftButton {
background-color: yellow;
grid-area: topLeft;
}
.middleButton {
background-color: red;
grid-area: topMiddle;
}
.rightButton {
background-color: green;
grid-area: topRight;
}
.bottomForm {
grid-area: form;
}
<div class="gridDiv">
<div class="leftButton"><button>This should be in top-left</button></div>
<div class="middleButton"><button>This should be in top middle</button></div>
<div class="rightButton"><button>This should be in top right</button></div>
<div class="bottomForm">
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
</div>
</div>
:
I want the 3 divs containing the buttons to be on the top row and positioned in the top left, top middle, and top right, while I want the div containing the form to be in the bottom row in the middle, as indicated by the grid-template-areas in the gridDiv style. However, this is clearly not working, with everything being moved to the top right for some reason - does anyone know what the problem is?
blank seems to be a reserved word, use a single dot instead will do.
I have not found any official specification yet about it. aside https://drafts.csswg.org/css-grid/#grid-area-concept
.gridDiv {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: 1fr 2fr;
grid-template-areas: "topLeft topMiddle topRight"
". form .";
}
.leftButton {
background-color: yellow;
grid-area: topLeft;
}
.middleButton {
background-color: red;
grid-area: topMiddle;
}
.rightButton {
background-color: green;
grid-area: topRight;
}
.bottomForm {
grid-area: form;
}
<div class="gridDiv">
<div class="leftButton"><button>This should be in top-left</button></div>
<div class="middleButton"><button>This should be in top middle</button></div>
<div class="rightButton"><button>This should be in top right</button></div>
<div class="bottomForm">
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
</div>
</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
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