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>
Related
This is a "newbie" project that i'm studying with, and im trying to work mobile first so i go to desktop flow
When i try to implement the media query for desktop flow, elements don't fit the screen (screen is scrolling even if the elements could fit)??
I tried to change the height of ".container" using percentage but it didn't work as well. The screen is always scrolling while desktop flow, i want that elements fit the entire screen (no scroll page)..
`
<body>
<div class="container">
<div class="cont1">
<header>
<h1>Learn to code by watching others</h1>
</header>
<p>
See how experienced developers solve problems in real-time. Watching
scripted tutorials is great, but understanding how developers think is
invaluable.
</p>
</div>
<div class=" cont2">
<p class="container-blue-p">
<strong>Try it free 7 days</strong> then $20/mo. thereafter
</p>
<form action="#" id="form" class="form">
<div class="form-control">
<input
type="text"
name="firstname"
id="firstname"
placeholder="First Name"
/>
<i class="fas fa-exclamation-circle"></i>
<i class="fas fa-check-circle"></i>
<small>Error Message</small>
</div>
[...]
</div>
</body>
#media(min-width:1200px) {
body {
background-image: url(images/bg-intro-desktop.png);
}
.container {
display: grid;
grid-template-areas: "container1 container2";
grid-template-columns: 50% 50%;
max-width: 1200px;
}
.cont1 {
display: flex;
align-items: flex-start;
justify-content: center;
grid-area: container1;
}
.cont1 header {
max-width: 50%;
}
header h1 {
text-align: start;
}
.cont1 p {
max-width: 90%;
text-align: start;
}
.cont2 {
margin-top: 0;
grid-area: container2;
}
.cont1,
.cont2 {
display: flex;
flex-direction: column;
justify-content: center;
}
.container-blue-p {
display: flex;
flex-direction: row;
height: 3em;
}
}
`
I'm trying to come out with following card for profile details. But the grid system is not giving me the expected result. Any help will be greatly appreciated.
Output:
Expected result:
Code:
Here is how to do it using Grid:
It is very similar the main difference is grid-template-columns, I recommend using Flexbox since this is a 1-dimensional issue which is what Flex was built to solve. Flex and Grid are best used together :D
.grid-container {
display: grid;
width: 300px;
grid-template-columns: 1fr 3fr;
column-gap: 25px;
align-items: center;
border: 1px solid #f4f4f4;
}
.icon img {
height: 100px;
}
.info label {
display: block;
}
<div class="grid-container">
<div class="icon">
<img src="https://www.pngall.com/wp-content/uploads/5/Profile-PNG-Clipart.png" />
</div>
<div class="info">
<label>First Name</label>
<label>Last Name</label>
<label>Position</label>
<label>Identification No.</label>
<label>Email</label>
</div>
</div>
I would recommend using Display Flex in the parent container also setting the labels to block elements so they go on separate lines.
.flex-container {
display: flex;
gap: 30px;
align-items: center;
border: 1px solid #f4f4f4;
width: 300px;
}
.icon img {
height: 100px;
}
.info label {
display: block;
}
<div class="flex-container">
<div class="icon">
<img src="https://www.pngall.com/wp-content/uploads/5/Profile-PNG-Clipart.png" />
</div>
<div class="info">
<label>First Name</label>
<label>Last Name</label>
<label>Position</label>
<label>Identification No.</label>
<label>Email</label>
</div>
</div>
I'm fairly new to HTML and CSS and I am trying to align the Heading and the search on the same line but I got stuck. Here is the HTML:
HTML:
<header>
<h1 id="Heading">Welcome Home</h1>
<form id="search">
<label for="keywordBox">Search: </label>
<input id="keywordBox" type="text" name="keywordsrch">
</form>
</header>
How do I align the Heading with the form? Thanks for your help!
The old way would be to float the header elements like so:
<style type="text/css">
header > h1,
header > form {
float: left;
}
header {
clear: both;
}
</style>
Alternatively, the more modern and arguably better way would be to use CSS grids to do this, like so:
<style type="text/css">
header {
display: grid;
grid-template-columns: 1fr 1fr;
}
</style>
The value 1fr means "one flexible unit" and is proportional to other flexible units. Thus, in this case, it would be equivalent to 50% 50%.
header {
padding: 10px;
}
#heading,
#search {
display: inline-block;
margin: 0;
}
#search {
float: right;
}
<header>
<h1 id="heading">Welcome Home</h1>
<form id="search">
<label for="keywordBox">Search: </label>
<input id="keywordBox" type="text" name="keywordsrch">
</form>
</header>
body{
margin: 0px;
}
header{
padding: 0px 15px;
display: flex;
display: -webkit-flex;
align-items: center;
justify-content: space-between;
}
#search {
margin: 0px;
}
<header>
<h1 id="Heading">Welcome Home</h1>
<form id="search">
<label for="keywordBox">Search: </label>
<input id="keywordBox" type="text" name="keywordsrch">
</form>
</header>
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>
I'm trying to build out a form module so that regardless of the various inputs a form may have, the following conditions are always met:
The form is centered within its containing element. (I think this is just inline-block combined with text-align: center.)
The inputs are all the same width and line up along their left edge.
The labels can be any length (within reason), and their right edges all line up.
The form is only as wide as its longest label/input combination (in other words, the width of the form is dynamic and determined by its longest label). (The red lines in the image below represent the bounding edges of the form as per the given labels and inputs.)
Basically, I want something like the following:
This type of layout is really easy to do with tables, but I have read that tables are not advisable to use for forms, so my question is, how can I do all of this without using a table?
Thank you.
Edit: The layout must work in IE8.
You can accomplish the required behavior using flex.
HTML structure (it's just an approach):
<div id="container">
<div class="form-container">
<form>
<div class="column">
<label for="text1">This is a label</label>
<label for="text2">Label</label>
<label for="text3" style="flex-grow:2;">Another larger label :D</label>
<label for="text4">A short one!</label>
</div>
<div class="column">
<input type="text" name="text1" />
<input type="text" name="text2" />
<textarea name="text4" style="flex-grow:2;"></textarea>
<input type="text" name="text3" />
</div>
</form>
</div>
</div>
CSS:
#container {
text-align: center;
}
#container .form-container {
display: inline-block;
}
#container form {
background: #eee;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
padding: 20px;
}
#container form .column {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
}
#container form .column label {
padding: 2px;
text-align: right;
}
#container form .column input,
#container form .column textarea {
align-self: flex-start;
width: 200px;
}
Here is a working demo: http://jsfiddle.net/gopafm4g/2/