Left aligning items in a column grid with each other - html

Have a section id="contact" where I am trying to put a heading and a brief one sentence description underneath in one column and an email submit form with a submit button under the input field in the right column. I want the email input element to reach across the entirety of the right column and have the button left aligned to it's edge underneath it. I have a sketch to show what I am looking for attached.
So far this section does everything I want it to do right now, however the top elements under the bottom elements are not left edge aligning although the class items .latest-from-here .input-email (nested inside the parent section #contact) are justify-self:start; align-self: center;.
https://codepen.io/holmedw/pen/KrvJEb
Sketch:
#contact {
display: grid;
grid-template-columns: 1fr 1fr;
justify-items: left;
grid-column-gap: 80px;
margin-left: 80px;
margin-right: 80px;
height: 480px;
align-items:center;
}
.latest-from-here .input-email {
position:relative;
z-index: -1;
justify-self:start;
align-self: center;
}

if you removed justify-items: left; in #contact and added width:100% to your input field that will solve the problem.
*{
box-sizing:border-box
}
#contact {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 80px;
margin-left: 80px;
margin-right: 80px;
height: 480px;
align-items:center;
}
.latest-from-here .input-email {
position:relative;
z-index: -1;
justify-self:start;
align-self: center;
}
.email{
width:100%
}
<section id="contact">
<div class="latest-from-here">
<h2>Get the latest from Aer</h2>
<p>Don't miss out on new arrivals, offers and events.</p>
</div>
<div class="input-email">
<form id="form" action="https://www.freecodecamp.com/email-submit">
<input name="email" id="email" type="email" placeholder="Enter your email" class="email" required/>
<div></div>
<input id="submit" type="submit" value="Submit" class="btn-submit"></input>
</form>
</div>
</section>

Related

CSS grid-column not spanning multiple columns

I am trying to use CSS grid to setup a form that has three columns: the first column is used to display the label for the input that will be listed in the second column. The third column is used to get the correct spacing of the form on the page and allow the form to scale to the page size.
I am able to separate the form labels and form inputs into columns one and two respectively, however when I cannot make a new section of the form that is centered between these two columns, it will either be in column one labels or column two data.
.formContainer {
display: grid;
grid-template-columns: [labels] auto [data] auto 1fr;
grid-auto-rows: auto;
grid-row-gap: 5px;
margin-top: 3%;
margin-left: 12%;
margin-right: 3%;
}
.formContainer>label {
grid-column: labels;
}
.formContainer>div {
grid-column: data;
}
.matches {
grid-column-start: labels !important;
grid-column-end: data !important;
}
<div class='formContainer'>
<label>
<span>Name</span>
</label>
<div>
<input type="text" />
</div>
<div class='matches'>
<div>No matches yet!</div>
</div>
</div>
I have also tried making the matches div a different HTML element such as article or span which did not work either. Any help with trying to figure out how to make the matches class span between both of these columns would be greatly appreciated.
As per my comments above, you can use grid-column: span 2 to the matches element to allow it to occupy two columns and then add text-align: center to center in that space.
Also note I've used .formContainer > .matches instead of .matches for specificity of styles (grid-column definition in .formContainer > div was overriding the grid-column in .matches as it is more specific) - see demo below:
.formContainer {
display: grid;
grid-template-columns: [labels] auto [data] auto 1fr;
grid-auto-rows: auto;
grid-row-gap: 5px;
grid-column-gap: 10px; /* added */
margin-top: 3%;
margin-left: 12%;
margin-right: 3%;
}
.formContainer > label {
grid-column: labels;
}
.formContainer > div {
grid-column: data;
}
/* changed below */
.formContainer > .matches{
grid-column: span 2;
text-align: center;
}
<div class='formContainer'>
<label>
<span>Name</span>
</label>
<div>
<input type="text" />
</div>
<div class='matches'>
<div>No matches yet!</div>
</div>
</div>

Why isn't my image aligning to the bottom of my grid column?

I'm trying to setup a grid system with one side a form and the other one an image. Now the problem is that I want everything to go to the bottom of the viewport, but for some reason the image doesn't go to the bottom of the screen.
I've already tried putting a height: 100vh on body, * and the image itself but it didn't change.
.login-section {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-areas: "form form form form form form form form map map map map";
align-items: center;
justify-items: center;
}
.login-section .login-form {
grid-area: form;
}
.login-section .login-map {
grid-area: map;
width: 100%;
overflow-x: hidden;
}
<div class="login-section">
<div class="login-map">
<img src="../img/map.png" alt="photo map">
</div>
<div class="login-form">
<div class="login-form-back">
Bla bla login
</div>
</div>
</div>
Give height: 100vh for login-section and use align-items: flex-end - see demo below:
body {
margin: 0; /* Reset body-margin */
}
img {
display: block; /* Remove whitespace below image*/
height: 100%; /* Image extending the full height of the grid item */
}
.login-section {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-areas: "form form form form form form form form map map map map";
/*align-items: center;*/
align-items: flex-end; /* ADDED */
height: 100vh; /* ADDED */
justify-items: center;
}
.login-section .login-form {
grid-area: form;
}
.login-section .login-map {
grid-area: map;
width: 100%;
overflow-x: hidden;
height: 100vh; /* image over full viewport*/
}
<div class="login-section">
<div class="login-map">
<img src="https://via.placeholder.com/100" alt="photo map">
</div>
<div class="login-form">
<div class="login-form-back">
Bla bla login
</div>
</div>
</div>

Aligning elements horizontally adjacent that are in different grid cells with CSS Grid and Flexbox

Currently I'm using a mixture between grid and flexbox to try and achieve a layout for a form.
I would like both input elements to horizontally lineup without setting a height on them. If I were to apply for padding on the text input, the range should continue to vertically center with the input adjacent to it.
Additionally I would like the label elements to also remain horizontally aligned too.
Please suggest solutions that DO NOT require changing the markup or using Javascript.
Currently what I have:
What I'm trying to achieve:
I know I could apply a fixed height or padding on the range to align it but I'm trying to make this as fluid as possible.
HTML:
<form class="grid">
<div class="group">
<label for="input-1">Label 1</label>
<input type="text" id="input-1" placeholder="placeholder" />
</div>
<div class="group">
<label for="input-2">Label 2</label>
<div class=range-wrapper>
<input type="range" id="input-2" placeholder="placeholder" />
<output>0</output>
</div>
</div>
</form>
SCSS:
html {
box-sizing: border-box;
}
*, *::before, *::after {
box-sizing: inherit;
}
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2rem;
}
.grid {
width: 80%;
max-width: 960px;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 40px 10px;
padding: 50px;
background: #ccc;
}
.group {
display: flex;
flex-direction: column;
margin-top: auto;
label {
margin-bottom: 10px;
}
}
input {
font-size: inherit;
&[type="text"] {
padding: 14px;
}
&[type="range"] {
width: 100%;
}
}
.range-wrapper {
display: flex;
align-items: center;
output {
padding: 10px;
}
You can also check out the codepen here.
Many thanks!
** EDIT **
Should support multiline labels, if the label wraps multiple lines, elements should still horizontally align.
Multiline label example: Codepen
try this for your "group" class.
(or use a new class for that element with this styling)
.group {
display: flex;
flex-direction: column;
justify-content: center;
label {
margin-bottom: 10px;
}
}

How to make elements stack in mobile view?

I have a small image, a form entry field, and a submit button. They are displaying as I want, side by side horizontally on a site on a normal desktop computer.
When the page is viewed on a small screen, the submit button is dropping down to the line below (good), but the image is staying where its at right next to the text entry field. Ideally, I'd like to force the three elements to stack on top of each other in mobile view. So, the top would be the image (centered), then the text entry field, then the submit button.
I have only a very basic understanding of responsive design. Any tips?
Here is the code that represents my situation:
.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
.row {
width: 100%;
}
.flex-item {
padding: 0;
margin: 0;
text-align: center;
}
<div class="flex-container">
<div class="row">
<span class="flex-item"><img src="https://images.unsplash.com/photo-1509718752889-8c69d2c6c11d?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ%3D%3D&s=1253b775db4261c2618d0b901b115b38" height="65" width="150"></span>
</div>
<div class="row">
<span class="flex-item">
<form action="find.php" method="post">
<input type="text" name="thingy_id" placeholder="Enter Thingy ID" size="13" maxlength="15">
<input type="submit" value="SUBMIT" class="thingyid" />
</form>
</span>
</div>
</div>
#media (max-width: 1024px) {
.flex-container {
display: block;
}
}
Here is a way of doing this with CSS Grid Layout.
It is pretty heavy on the CSS side, but it allows for incredibly minimal HTML.
You basically just set it up so that it will all display in one column, but if the screen is wide enough, you can make it side by side. You could do it the other way around, setting it up to display side by side, but if the screen is too narrow, you make it one column.
body {
display: grid;
grid-gap: 1em;
}
img {
justify-self: center;
}
form {
display: grid;
grid-template-columns: 1fr auto 1fr;
}
input {
margin: 5px;
}
input[type="text"] {
grid-column: 2;
align-self: center;
}
input[type="submit"] {
grid-column: 2;
align-self: center;
}
#media (min-width: 800px) {
body {
grid-template-columns: 1fr auto auto 1fr;
}
img {
grid-column: 2;
}
form {
grid-column: 3;
grid-template-columns: 1fr auto auto 1fr;
}
input[type="text"] {
grid-column: 2;
}
input[type="submit"] {
grid-column: 3;
}
}
<img src="https://images.unsplash.com/photo-1509718752889-8c69d2c6c11d?ixlib=rb-0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ%3D%3D&s=1253b775db4261c2618d0b901b115b38" height="65" width="150" />
<form action="find.php" method="post">
<input type="text" name="thingy_id" placeholder="Enter Thingy ID" size="13" maxlength="15">
<input type="submit" value="SUBMIT" class="thingyid" />
</form>
I just really wanted to do this in Grid. You don't have to do it, but it is a way, and I feel like more people should be aware of it.
You could use the extra Bootstrap class shown below to make an element's column take up a whole row on mobile devices. You could then use CSS to adjust the width and size of the elements.
<div class="flex-container">
<div class="row">
<div class="col-xs-12"> // setting the column to 12 makes the
element take up row entire row pushing the neighbouring
elements to move under it
<span class="flex-item"><img
src="https://images.unsplash.com/photo-1509718752889-
8c69d2c6c11d?ixlib=rb-
0.3.5&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJ
hcHBfaWQiOjE0NTg5fQ%3D%3D&s=1253b775db4261c2618d0
b901b115b38" height="65" width="150">
</span>
</div>
</div>
<div class="row">
<span class="flex-item">
<form action="find.php" method="post">
<div class="col-xs-12"> // Use here
<input type="text" name="thingy_id" placeh
older="Enter Thingy ID" size="13" maxlength="15">
</div>
<div class="col-xs-12"> // Use here
<input type="submit" value="SUBMIT" class="thingyid" />
</div>
</form>
</span>
</div>
</div>

horizontally align and vertically center three elements in a container

I need to align 3 items inside Div.
Every item must be placed vertically in the center.
First item 2% margin-left.
Second item in the center and must be a textbox.
Third item 2% margin-right.
Item First and Third must fit the div(square).
I used alignment but it didn't work. Here there is a graphic example:
HTML
<div id="container">
<div class="box" id="left"></div>
<input type="text" name="search" placeholder="SEARCH HERE" id="searchbox">
<div class="box" id="right"></div>
</div><!-- end #container-->
CSS
#container {
display: flex;
justify-content: space-between;
align-items: center;
width: 95%;
height: 90px;
background-color: orangered;
}
.box {
height: 75px;
width: 75px;
background-color: lightgreen;
}
#left {
margin-left: 2%;
}
#right {
margin-right: 2%;
}
input {
width: 250px;
padding: 18px;
font-size: 2em;
}
DEMO: http://jsfiddle.net/xptx9wxn/2/
For more information about CSS Flexbox visit:
A Complete Guide to Flexbox
Using CSS flexible boxes - Web developer guide | MDN