Since not everyone will be browsing the web on a computer, I need to use CSS to adapt to different screen sizes. I am working on the front-end part of the registration form, register.php. The issue is, on the computer the field name and field input position nicely. However, for the smaller screens (maximum width of 520px) I need the field name and field input to be on
top/bottom of each other, field input being on top of its field name.
HTML:
#formBody{
border-radius: 10px;
background-color: #3385ff;
padding-left: 5%;
padding-right: 5%;
padding-top: 0.5em;
padding-bottom: 0.5em;
width: 90%;
max-width: 50em;
margin: auto;
}
.field{
margin: auto;
width: 66.6%;
}
.fieldDescription{
width: 33%;
color: black;
font-size: 1.2em;
}
.fieldInput{
width: 33%;
margin: auto;
}
.input{
width: 100%;
background-color: #e6f0ff;
border-style: none;
}
CSS:
<div id="formBody">
<table>
<form>
<tr class="field">
<td class="fieldDescription">
<p>First Name:</p>
</td>
<td class="fieldInput">
<input class="input" name="firstname" type="text">
</td>
</tr>
</form>
</table>
</div>
I used an HTML table to give each field its own space. I used the <tr> element to make a row for each field, and used two <td>element to put the field name and field input in its own column. This works great for a computer screen, but it doesn't work on the smaller screen (like a smartphone). If I use something like <p class="fieldDescription">Fist Name:</p> for the field name, then <input class="fieldInput" name="firstName" type="text"> for the field input, then I'm hard coding only for small screens.
Is there a way to satisfy both screen sizes, without having to hard code it either way? I would prefer to not use Javascript. I would like to do it CSS.
Click here for the HTML
Click here for the CSS
(both are on gist.github.com)
I'll highly recommend you to use valid and semantic form elements like label and fieldset. But to address your question:
#media screen and (max-width: 520px) {
#mainContainer {
border-radius: 5px;
}
#formBody {
border-radius: 0px;
}
.fieldDescription,
.fieldInput {
width: 100%;
display: inline-block;
}
.fieldDescription p {
margin-bottom: 0;
}
}
A Few Things to tell:
What you are looking for is making responsive designs. You should take a look to Media Queries
I Highly recomend not to use tables in cases like this, just use a table when u REALLY need a table
Here is a LIVE EXAMPLE with the HTML and CSS code of what you want to do. (Resize the browser to see).
Try not to style your forms with <p> <tr> <td> and so on... Use <label> instead.
Hope this work.
#media + display:block; should be basicly what you need:
#media screen and (max-width: 520px) {
table,/* to spray whole space as block does */
tbody,/* because it is there even not in the code :) */
tr,/* this too must be a block */
td /* goal to reach to start from */{
display: block;
}
}
#media screen and (max-width: 520px) {
table,
tbody,
tr,
td {
display: block;
text-align: center;
}
}
/* copy to match snippet size */
#media screen and (max-width: 700px) {
table,
tbody,
tr,
td {
display: block;
text-align: center;
}
}
/* warning */
legend {
color: tomato;
}
<div id="formBody">
<form>
<fieldset>
<legend>Put that form around the table not in between table & tr tags !!</legend>
<table>
<tr class="field">
<td class="fieldDescription">
<p>First Name:</p>
</td>
<td class="fieldInput">
<input class="input" name="firstname" type="text">
</td>
</tr>
</table>
</fieldset>
</form>
</div>
And demo including your style
Related
I'm trying to help my mom out over break making a webform she can use in her office and I'm 100% self taught, so any help is incredibly appreciated. Basically, I'm trying to put an image fully left aligned on the page with a table directly under it, a larger table to the right of the image, and another table to the right of that. Right now, I can't get the first table to be directly under the image- it keeps going left aligned but under the second table (which is much larger than the first)
Here's the code I have so far:
<img src="{{logo}}" style="float:left; height: 80px; width: auto; cursor: pointer; padding: 5px;"><div>
<table style="float:right; background-color:#CCCCCC" border="1"><tbody><tr><td width="125px">#: <u>{{customerID}}</u><br>Sq Ft:<u><input type="text" style="width:50px"></u><br>Linear Ft:<u><input type="text" style="width:50px"></u><br><input type="checkbox">Live Infestation<br><input type="checkbox">Prevention<br><div style="padding-left:20px">Treatment</div><br><br><small><i></i></small><i></i><center><i><small>For Office Use Only</small></i></center></td></tr></tbody></table>
<table style="float:left; margin-left:100px" border="1"><tbody><tr><td style="text-align:center"><b>Termite Inspection Graph & Treatment Specifications</b></td></tr><tr><td style="line-height:1.4"><u>{{customerName}}</u><br><small>Name</small><br><u>{{customerAddress}} {{customerCity}} {{customerState}} {{customerZip}}</u><br><small>Address/Zip Code</small><br><u>{{customerPhone}}, {{customerEmail}}</u><br><small>Phone, Email</small><br><u>{{billingAddress}} {{billingCity}} {{billingState}} {{billingZip}}</u><br><small>Bill To Address</small><br><u><input type="text"></u><br><small>Agreement/Treatment is limited to the one structure listed above unless specified here</small></td></tr></tbody></table><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><table style="float:left; background-color:#D0F0C0" border="1"><tbody><tr><td><small><input type="checkbox"> Sentricon + Termidor <br><input type="checkbox"> Sentricon + Altriset<br><input type="checkbox"> Termidor Only<br><input type="checkbox"> Altriset Only<br><input type="checkbox"> Sentricon Only</small></td></tr></tbody></table></div>
For reference, it's the last chunk of code I'm having trouble with. Side note, the software she is using does not allow me to edit the CSS, so everything I do needs to be within the HTML code itself.
All help appreciated!!
If you have the ability to use css in the HTML page within tags this should work for you:
#media part of the CSS is to make the columns stack instead of sit next to each other on a narrow screen such as a phone; make sure to do testing and tweaking with the actual content to see if it behaves as intended.
<html>
<head>
<style>
body { /*display: inline-block;*/ }
#image { width: 25%; margin: 15px;}
#righttable { float: left; width: 20%; margin: 10px;}
#lefttable { float: left; width: 20%; margin: 10px;}
#midtable { float: left; width: 50%; margin: 10px;}
div.row { width: 100%; }
#media screen and (max-width: 600px) {
#image { width: 100%; }
#righttable { width: 100%; }
#lefttable { width: 100%; }
#midtable { width: 100%; }
}
</style>
</head>
<body>
<div class="row">
<img id="image" src="{{logo}}" style="height: 80px; width: auto; cursor: pointer; padding: 5px;"></div>
<div class="row">
<div id="lefttable">
<table style="background-color:#D0F0C0" border="1"><tbody><tr><td><small><input type="checkbox"> Sentricon + Termidor <br><input type="checkbox"> Sentricon + Altriset<br><input type="checkbox"> Termidor Only<br><input type="checkbox"> Altriset Only<br><input type="checkbox"> Sentricon Only</small></td></tr></tbody></table></div>
<div id="midtable"><table style=" /*margin-left:100px*/" border="1"><tbody><tr><td style="text-align:center"><b>Termite Inspection Graph & Treatment Specifications</b></td></tr><tr><td style="line-height:1.4"><u>{{customerName}}</u><br><small>Name</small><br><u>{{customerAddress}} {{customerCity}} {{customerState}} {{customerZip}}</u><br><small>Address/Zip Code</small><br><u>{{customerPhone}}, {{customerEmail}}</u><br><small>Phone, Email</small><br><u>{{billingAddress}} {{billingCity}} {{billingState}} {{billingZip}}</u><br><small>Bill To Address</small><br><u><input type="text"></u><br><small>Agreement/Treatment is limited to the one structure listed above unless specified here</small></td></tr></tbody></table><br></div>
</div>
<div id="righttable">
<table style=" background-color:#CCCCCC" border="1"><tbody><tr><td width="125px">#: <u>{{customerID}}</u><br>Sq Ft:<u><input type="text" style="width:50px"></u><br>Linear Ft:<u><input type="text" style="width:50px"></u><br><input type="checkbox">Live Infestation<br><input type="checkbox">Prevention<br><div style="padding-left:20px">Treatment</div><br><br><small><i></i></small><i></i><center><i><small>For Office Use Only</small></i></center></td></tr></tbody></table></div>
</body>
</html>
Update: this works by cheating the system and just putting the top image as the first row of the table that needs to be directly under it
I would like the font size for my form label and input fields to scale down from 18px to 10px when the browser width reaches 1460px or less.
I read that it is not possible to get fonts to automatically 'scale down' as such when the browser width decreases, and that I would need to use media queries instead.
Therefore I have put a media query at the top of my style tags asking the font size for my label and input to display at 10px when the screen size is 1460px, but it doesn't seem to work. The rest of my code is working fine however, so it must be something to do with the way I am coding my media query.
If someone could offer some help that would be much appreciated.. my code is pasted below.
#media only screen and (max-width: 1460px) {
label input {
font-size: 10px;
}
}
* {
box-sizing: border-box;
}
input[type=text],
select {
width: 95%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 3px;
resize: vertical;
transition: 0.3s;
outline: none;
font-family: Typ1451-Medium;
font-size: 18px;
margin: 7px;
}
input[type=text]:focus {
border: 1.25px solid #ea0088;
}
label {
padding: 21px 12px 12px 12px;
margin-left: 5px;
display: inline-block;
font-family: Typ1451-Medium;
font-size: 18px;
color: #999;
}
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
margin: 2.5% 20% 0 20%;
}
.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}
.col-75 {
float: left;
width: 75%;
margin-top: 6px;
}
.left,
.right {
width: 50%;
}
form {
display: flex;
}
<div class="container">
<form action="signin.php" method="post">
<div class="left">
<div class="row">
<div class="col-25">
<label for="fname">First Name</label>
</div>
<div class="col-75">
<input type="text" id="fname" name="firstname" placeholder="* Please complete">
</div>
</div>
</div>
<div class="right">
<div class="row">
<div class="col-25">
<label for="lname">Last Name</label>
</div>
<div class="col-75">
<input type="text" id="lname" name="lastname" placeholder="* Please complete">
</div>
</div>
</div>
</form>
</div>
Your selector — label input — doesn't match any elements in your HTML.
None of your input elements are descendants of your label elements.
Perhaps you meant label, input to select label elements and input elements. If so, then it still wouldn't work because you define the input font-size with a more specific selector later on (and the most specific selector wins the cascade) and the label in a similar way (it doesn't have a more specific selector, but when selectors are equal, the last one wins the cascade).
Actually, you CAN scale fonts up or down with the viewport size. There is a method with calc() and vw units:
Basically you do something like font-size: 3vw and then set max and min font sizes.
Here is a link to the calculation on Smashing Magazine. The rest of the article is pretty interesting, too.
You can extend this even further and optimize the font size with media queries.
Have fun! :)
I am building one of those contact forms that are conversational. Like:
The problem is that the placeholder text on one of them is very large and would either break into two lines or if I gave it a fixed width, it would break the layout of smaller mobile phones. Yes, I understand the the very simple solution would be to propose a change to the copy for any instance, but going on the fact that it cannot be done, how would I solve swapping the text based on the device width.
My proposed solution was to reduce the text on mobile. Since there they are more flexible with the mobile product, I can make these changes, but the desktop needs to comply with the PSD.
Demo
Demo on CodePen
HTML
<input type="text" id="org" name="" placeholder="ORGANIZATION OR COMPANY NAME">
CSS
#org{
display: block;
width: 96%;
margin: 0 auto;
}
#media only screen and (min-width: 768px) {
#org{
display: inline-block;
width: 360px;
}
}
It needs to be at least 360px to match the PSD for desktop.
One other option that I have is if I have entirely different inputs, would that work? like
<input type="text" id="org" class="orgm" name="" placeholder="ORG/COMPANY">
<input type="text" id="org" class="orgd" name="" placeholder="ORGANIZATION OR COMPANY NAME">
.orgm { //mobile
display: block;
}
.orgd {
display: none;
}
#media only screen and (min-width: 768px) {
.orgm{
display: none;
width: 96%;
margin: 0 auto;
}
.orgd {
display: inline-block;
width: 360px;
margin: 0;
}
}
I don't want to use jQuery or other javascript based solutions.
If you absolutely must keep that copy and are willing to do something a bit hacky, this is where it becomes annoying, but you could try...
<div class="input--container">
<p class="input--placeholder">
<span class="desktop">ORGANIZATION OR </span>
COMPANY NAME
</p>
<input type="text" id="org" class="input" name="">
</div>
// Hide the long part on mobile
.input--placeholder span { //mobile
display: none;
}
#media only screen and (min-width: 768px) {
// Show the full on desktop
.input--placeholder span {
display: inline;
}
}
And then you'll just have to position the placeholder text absolutely over the top of the input.
EDIT: You'll also have to hide the placeholder completely when the user is on the input which you can do with input:focus selector
This is driving me nuts! I'm hoping someone can help me...I'm pretty new to HTML and CSS.
I've got a sign-up form I'm trying to style on my website. Just first name, email and submit button. I want the two input fields to be on the same line (managed to achieve this), and have them take up 100% of the space. Then when the window shrinks down to mobile, they're on a line each. Mostly everything is working except the fields won't show past a certain width. I've tried a variety of combinations using width=100%, width=auto, width=..px...
Here's a screen shot of what it looks like at the moment:
http://www.marnielefevre.com/new/wp-content/uploads/2016/02/Screen-Shot-2016-02-22-at-4.10.20-pm.png
Here's a snippet of my relevant HTML...
<div class="fielddiv">
<div class="fieldrow"><label>First Name*</label>
<input name="firstname" required="" type="text" /></div>
<div class="fieldrow"><label>Email*</label>
<input name="email" required="" type="email" /></div>
<div style="padding-top: 40px; text-align: center;"><input type="submit" value="SIGN ME UP" /></div>
</div>
...and the CSS
.fielddiv {
text-align: center;
margin: auto;
width: auto;
}
.fieldrow {
display: inline-block;
margin: auto;
text-align: center;
padding-top: 20px;
min-width: 330px;
max-width: 800px;
}
I appreciate any help!!
Without knowing what the other code in your application is doing, I would suggest (blindly) the following;
.fielddiv {
display: flex;
}
.fieldrow {
flex: 1;
text-align: center;
}
.fieldrow:first-child {
margin-right: 20px;
}
Please use below code in your application, it will increase width of textbox for your page:
.fielddiv input[type="text"] {
width:100%;
}
.fielddiv input[type="email"] {
width:100%;
}
OR
.fielddiv input[type="text"] {
width:100% !important;
}
.fielddiv input[type="email"] {
width:100% !important;
}
Hope this will meet your requirement.
I would like my table to look like the one in image with CSS. Is there a way to do it because I tried by my side as much as I could but did not work. I would appreciate your help.
Here is my HTML code
<div id="login_fields">
<form id="login_form">
<table>
<tr>
<td>User</td>
<td><input type="text" name="user" id="user" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" id="password" /></td>
</tr>
</table>
</form>
</div>
May I suggest amending your HTML (the table is entirely unnecessary), to the following:
<form action="#" method="post">
<!-- using a label means that clicking the text automatically focuses
the relevant input, the value of the 'for' attribute must match the 'id'
of the relevant input though -->
<label for="uName">User</label>
<input id="uName" />
<label for="pass">Password</label>
<input type="password" id="pass" />
</form>
With the following CSS (amend colours and dimensions according to taste):
form {
/* aesthetics, just to move the label/input pairs from the edge of the screen */
padding: 1em;
}
label,
input {
float: left; /* to allow for width to be given, and for clearing */
border: 1px solid #999; /* amend the following as required */
line-height: 1.2em;
padding: 0.2em 0;
font-size: 1em;
height: 1.4em;
margin-bottom: 0.8em;
}
input + label {
clear: left; /* this styles a label element that immediately
follows an input, and forces a new-line */
}
label {
text-indent: 0.5em; /* moves the text away from the curved corners */
width: 30%;
border-radius: 0.5em 0 0 0.5em; /* handles the curved corners */
}
input {
width: 60%;
border-radius: 0 0.5em 0.5em 0;
outline: none;
}
input:focus,
input:active {
box-shadow: inset 0 0 5px #55f; /* compensates for the fact I removed the
default outline, and gives visual
feedback to show the input is focused/active */
}
JS Fiddle demo.
I'd start replacing the TD's for User and Password with TH's, since they're table headers. Then I'd produce two images, one with the curve in the left and one with the curve in the right, then I'd apply then in the background. The CSS would look like that:
table tr th { background: url(bg-left.png) no-repeat; width: 100px; height: 40px; }
table tr td { background: url(bg-right.png) no-repeat; width: 250px; height: 40px; }
I've removed the font styling to keep it easy to read.