Form alignment collapses into single row - html

I have the parent element set to newTableForm with labelColumn and mainFormColumn as children - but with the current CSS everything collapses into one row:
.newTableForm {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-rows: 30px 30px 30px 40px;
grid-gap: 10px;
grid-template-areas: "... main" "labels main" "labels main" "... main";
border: 2px dashed deeppink;
}
.labelColumn {
grid-area: labels;
}
.mainFormColumn {
grid-area: main;
}
<form method='post' action="/admin-post.php" class="newTableForm">
<h4 class="mainFormColumn">
Add a new price compare table
</h4>
<label for="store" class="labelColumn">Store</label>
<input type="text" name="store" placeholder="" class="mainFormColumn"/>
<label for="product-page" class="labelColumn">Product Page Link</label>
<input type="text" name="product-page" placeholder="Copy address here" class="mainFormColumn" />
<button class="mainFormColumn">Save new price compare table</button>
</form>
Is there a way to undo the single column stacking behavior here? I would remove the template areas altogether if not for the blank sections with ... (above and below the label section)

Again you can drop grid-template-areas as multiple grid-areas overlap - and you can use pseudo elements for this:
place labelColumn into the first column using grid-column: 1 and mainFormColumn into the second column using grid-column: 2.
after element will be the first column in the last row using grid-row: -2 and grid-column: 1
before will be first column in the first row using grid-column: 1
See demo below:
.newTableForm {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-rows: 30px 30px 30px 40px;
grid-gap: 10px;
border: 2px dashed deeppink;
}
.labelColumn {
grid-column: 1;
}
.mainFormColumn {
grid-column: 2;
}
.newTableForm:after, .newTableForm:before {
content: '';
display: block;
grid-column: 1;
}
.newTableForm:after {
grid-row: -2;
}
<form method='post' action="/admin-post.php" class="newTableForm">
<h4 class="mainFormColumn">
Add a new price compare table
</h4>
<label for="store" class="labelColumn">Store</label>
<input type="text" name="store" placeholder="" class="mainFormColumn"/>
<label for="product-page" class="labelColumn">Product Page Link</label>
<input type="text" name="product-page" placeholder="Copy address here" class="mainFormColumn" />
<button class="mainFormColumn">Save new price compare table</button>
</form>

I just update your .newTableForm css with some updates. I hope it'll help you out. Thanks
.newTableForm {
display: flex;
flex-direction: column;
border: 2px dashed deeppink;
}

Related

How to style child component html elements to parent component grid?

I have a component with a grid defined, I want to style a child component input elements so that they stay in a specific grid row and column; I tried doing this;
parent component html;
<div class="patient-details-grid">
<app-child-component></app-child-component>
</div>
.patient-details-grid {
gap: 15px 40px;
grid-template-columns: repeat(4, 1fr);
display: grid;
}
app-child-component {
.form {
display: block;
.firstName {
grid-area: 2 / 1 / 2 / 2;
}
.lastName,
.year,
.city,
.zipCode,
.phone,
.email {
grid-column: 1 / 3;
}
}
The form elements do not get placed into the correct grid tracks as I expected. How do I style these child elements into the parent div grid? the child component has multiple input elements like this;
<input type="text" class="firstName">
<input type="text" class="lastName">
<input type="text" class="year">
<input type="text" class="city">
<input type="text" class="zipCode">
<input type="text" class="phone">
<input type="text" class="email">

Conditionally place item within grid layout

I have a few scenarios in my grid. I have 4 possible grid elements, but depending on a user selection from the previous screen, I might only display three of those.
I'm running into an issue where in a particular circumstance, I want to display only 3 elements (2 per row), with the third element on the bottom row placed to the far right, essentially leaving an empty space where the first element of the second row normally would be.
const MyGrid = styled(OtherGrid)`
#media (max-width: ${BP.SMALL - 1}px) {
div:nth-child(2) {
grid-row-start: 3;
}
div:nth-child(3) {
grid-row-start: 2;
}
}
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: 1fr;
`;
My grid is styled from another grid, but all in all, acts like a basic grid.
<MyGrid>
<FormGroup
type={"number"}
name={"initial_moisture"}
label={"Initial Moisture (%)"}
step={0.1}
required
/>
<FormGroup
type={"number"}
name={"initial_temp"}
label={`Initial Temperature (${isMetric ? "C" : "F"})`}
step={1}
required
/>
{operatingMode != "auto_cool" && (
<>
<FormGroup
type={"number"}
name={"final_moisture"}
label={"Target Moisture (%)"}
step={0.1}
required
/>
</>
)}
{operatingMode != "auto_hydrate" && (
<>
<FormGroup
class={"auto-cool"}
type={"number"}
name={"final_temp"}
label={`Target Temperature (${isMetric ? "C" : "F"})`}
step={1}
required
/>
</>
)}
</MyGrid>
In the last possible state here (operatingMode != "auto-hydrate"), I want to place that element on the second row, but in the second column as well, and leave an empty space on the 2nd row, first column. Is there something within my parent container GoalsFieldGrid that I can define that will help me do this?
grid-column: -1; is what I'm looking for, but I'm not sure how to apply it in that specific scenario.
If I understand correctly you only require this when there are three elements only.
That being the case you can use :nth-child(3):last-child to select the element.
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: 1fr;
margin-bottom: 1em;
gap: .25em;
}
.item {
display: flex;
justify-content: center;
align-items: center;
padding: 1em;
border: 1px solid grey;
}
.item:nth-child(3):last-child {
grid-column: 2;
}
<div class="grid">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>

Beginner element problem with single form element and responsive width

I'm doing an assignment in Html and CSS using grid layout and I know my code is sloppy since I'm just starting to learn. I have two issues that are preventing me from passing this assignment and they are:
have a form element that is as wide as the window when the window is narrower than 600 px
I thought I had that in the code with the width and also my media screen changes the form size when the window is less than 600 px.
Also, I only have one form element, but for some reason, I keep getting the two above mentioned errors when being graded, can someone assist me?
my html and css:
form {
display: grid;
grid-template-columns: 0.7fr 1.3fr;
grid-template-rows: 0.2fr 0.2fr 0.2fr 0.2fr 0.2fr;
gap: 1px 1px;
grid-template-areas: ". ." ". ." ". ." ". ." ". .";
width: 600px;
}
select {
width: 200px;
}
.name {
grid-column: 1 / 1;
}
.type {
grid-column: 2 / 2;
}
.bio {
grid-column: 1 / 1;
}
.email {
grid-column: 2 / 2;
}
.submit {
grid-column: 1 / 2;
}
.reset {
grid-column: 3 / 2;
}
#media screen and (max-width: 600px) {
button,
input,
textarea,
select,
label {
display: block;
margin: 0;
text-align: left;
width: 100%;
}
}
/* card */
.card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.3);
transition: 0.3s;
width: 344px;
}
.card:hover {
box-shadow: 0 2px 4px 0 rgba(0,0,0,0.3);
}
.container {
padding: 16px 16px;
}
div.title {
font-size: 22px;
color: #000;
}
div.secondary {
font-size: 11px;
color: #232F34;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Assessment</title>
<link rel="stylesheet" href="site.css">
</head>
<body>
<!-- Your markup goes here. -->
<form action="/pets" method="post">
<fieldset>
<legend>Add Pet</legend>
<div class="formgrid">
<div class="name">
<label for="name">Name</label>
<input type="text" id="name" name="pet_name">
</div>
<div class="type">
<label for="type">Type</label>
<select id="type" name="pet_type">
<option value="cat">Cat</option>
<option value="dog">Dog</option>
<option value="hamster">Hamster</option>
<option value="other">Other</option>
<option value="zebra">Zebra</option>
</select>
</div>
<div class="bio">
<label for="bio">Biography</label>
<textarea id="bio" name="pet_bio"></textarea>
</div>
<div class="email">
<label for="owner-email">Owner's Email</label>
<input type="email" id="owner-email" name="pet_owner_email">
</div>
<div class="submit">
<button type="submit" id="new-pet-submit-button">Create new pet</button>
<button type="reset">Reset</button>
</div>
</div>
</fieldset>
</form>
<!----card---->
<div class="card">
<img src="images/desert.jpg" alt="Avatar" height="height:194px" width="344px">
<div class="container">
<div class="title">Title goes here</div>
<div class="secondary">
<img src="images/person-avatar.jpg" alt="Avatar" class="avatar" style="width:40px">
Secondary text
<p>
Greyhound divisively hello coldly wonderfully marginally far upon excluding.
</p><br>
</div>
</div>
</body>
</html>
Change your width:600px; in the form section to max-width: 600px;
form {
display: grid;
grid-template-columns: 0.7fr 1.3fr;
grid-template-rows: 0.2fr 0.2fr 0.2fr 0.2fr 0.2fr;
gap: 1px 1px;
grid-template-areas: ". ." ". ." ". ." ". ." ". .";
max-width: 600px;
}
This will make it responsive while under 600px

Trying to use css-grid but not all of the elements seem to be going in the right spot

I have the grid area template with correct formating, but for some reason, when I add an element and assign it's grid-area, it goes into the next available space in the column. eg: supposed to be middle column bottom row, but instead showed up on the middle column a space away from the privious element. (example shown in code) Also, the spacing is not working eg: 3fr is the same size as a 1fr. (This is in code pen)
fixed this problem in comments
New problem: the numbers are surrounding everything, why?
body {
background-color: rgb(90, 250, 190);
text-align: center;
font-family: 'Alegreya', serif;
min-width: 320px;
}
.container {
min-height: 500px;
width: 98%;
margin: 10px;
display: grid;
background: rgb(255, 255, 255);
grid-template-columns: 30px 3fr 2fr;
grid-template-rows: 0.5fr 1fr 1fr 1fr 3fr 3fr 1fr 3fr 2fr;
grid-template-areas: "description description description" "1 name nameinput"
"2 email emailinput"
"3 age ageinput"
"4 redblueoryellow redblueoryellowinput"
"5 mixedwith mixedwithinput"
"6 lighterordarkerdropdown lighterordarkerdropdowninput"
"7 radiobuttons radiobuttonsinput"
"8 questionsorcomments questionsorcommentsinput"
}
.description {
grid-area: description;
font-size: 30px;
}
.\31 {
grid-area: \31;
}
.\32 {
grid-area: \32;
}
.\33 {
grid-area: \33;
}
.\34 {
grid-area: \34;
}
.\35 {
grid-area: \35;
}
.\36 {
grid-area: \36;
}
.\37 {
grid-area: \37;
}
.\38 {
grid-area: \38;
}
.name {
grid-area: name;
}
.email {
grid-area: email;
}
.questionsorcomments {
grid-area: questionsorcomments;
}
<div class="container">
<p id="description" class="description">
What is your favorite color?
</p>
<p class="1">1.</p>
<p class="2">2.</p>
<p class="3">3.</p>
<p class="4">4.</p>
<p class="5">5.</p>
<p class="6">6.</p>
<p class="7">7.</p>
<p class="8">8.</p>
<p class="name">What is your name?</p>
<p class="email">What is your email?</p>
<p class="questionsorcomments"> Any questions or comments?</p>
</div>
The numbers are supposed to be running down the side. If you want a link to the full codepen then here it is: https://codepen.io/Homburg_Molly/pen/arbzeN

List in CSS Grid Not Aligning as Expected [duplicate]

This question already has answers here:
How do I remove the first empty column in a CSS grid?
(3 answers)
Closed 4 years ago.
Problem / Misunderstanding:
I've created an input type="radio" for radio choices. The radio choices should align to the left at the beginning of column 3, but instead they are in the middle, between column 3 and 4.
Expected behavior:
Radio input options should be at the beginning of column 3, under aligning with the text input bar of the Name label.
Minimal, complete and verifiable code below.
MCV.html code:
<link rel="stylesheet" href="MCP.css">
<div class="grid-container">
<form id="survey-form">
<label for="name" id="name-label">Name:</label>
<input type="text" id="name">
<div class="radio-title">
<p>Gender:</p>
</div>
<div class="radio-options">
<ul>
<li>
<input type="radio" id="choice-1">
<label for="choice-1">Option A</label>
</li>
<li>
<input type="radio" id="choice-1">
<label for="choice-1">Option B</label>
</li>
</ul>
</div>
</form>
</div>
MCV.css code:
.grid-container {
display: grid;
grid-template-areas:
". . . ."
". c c ."
". . . .";
grid-template-columns: 0.7fr 1.5fr 1.5fr 0.7fr;
grid-template-rows: 0.7fr 1.5fr 0.7fr;
}
#survey-form {
display: grid;
grid-area: c;
grid-template-columns: repeat(4, 1fr);
grid-gap: 5px;
}
label {
grid-column: 2 / 3;
text-align: right;
}
input {
text-align: left;
}
ul {
list-style: none;
}
.radio-title {
grid-column: 2;
text-align: right;
}
.radio-options {
grid-column: 3 / 4;
text-align: left;
}
Here's an image with line numbers and form grid:
All feedback is appreciated, thank you!
NOTE: This issue was already answered in How do I remove the first empty column in a css grid?. Take a look at second answer.
I believe this is more of a List issue. Most browsers attach some sort of padding / margin to an element so most people clear these out before starting on a new page.
Going to your codepen use:
//css
.gender ul {
padding: 0;
}
and you'll see the buttons move over to your desired location.