I want to style my table as shown in picture - html

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.

Related

table column width by content but not more the 50%

Good day.
I have table with 2 columns: the first one for labels and the second one for control elements. Labels could be short and very long.
table{
padding-bottom: 20px;
}
td{ border: 1px solid}
<table style="width:100%;">
<tr>
<td class="first-column">very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very label</td>
<td class="second-column"><input style="width:100%"/></td>
</tr>
<tr>
<td class="first-column">label</td>
<td class="second-column"><input style="width:100%"/></td>
</tr>
</table>
<table style="width:100%;">
<tr>
<td class="first-column">medium label</td>
<td class="second-column"><input style="width:100%"/></td>
</tr>
<tr>
<td class="first-column">label</td>
<td class="second-column"><input style="width:100%"/></td>
</tr>
</table>
<table style="width:100%;">
<tr>
<td class="first-column">label</td>
<td class="second-column"><input style="width:100%"/></td>
</tr>
<tr>
<td class="first-column">label</td>
<td class="second-column"><input style="width:100%"/></td>
</tr>
</table>
I need the first column to be sized by it's content (width of the longest label), but in case of very long labels it should not fill more then 50% of table.
Something like this
Could you please help me to write correct css for this case? I can't use Javascript, it should be css-only solution.
My own suggestion for this would be to firstly – unless this is impractical for other reasons – consolidate all your label and <input> elements into the same parent-element, which simplifies the alignment of all columns.
To this end I've grouped the elements into a single <fieldset> element, itself within a <form>:
// This JavaScript has no part to play in the resizing of elements,
// but merely logs the size of each of the two columns in the
// relevant elements (the last <label> and the last <input>:
const D = document,
logLengths = () => {
let label = D.querySelector('label:last-of-type'),
input = D.querySelector('input:last-of-type');
[label, input].forEach(
(el) =>{
el[
el.tagName.toLowerCase() === 'input' ? 'value' : 'textContent'
] = Math.round(el.getBoundingClientRect().width*10)/10 + 'px';
});
};
logLengths();
D.querySelectorAll('label').forEach(
(label) => label.addEventListener('input', logLengths)
);
/* Generic CSS reset: */
*,
::before,
::after {
box-sizing: border-box;
font-size: 16px;
line-height: 1.5;
margin: 0;
padding: 0;
}
form {
border: 1px solid currentColor;
/* using CSS logical properties to place a 1em margin
on the block axis (top/bottom) in left-to-right, top-
to-bottom languages) */
margin-block: 1em;
/* ...and a margin of auto (to centre the element) on
the inline-axis (left and right in ltr languages): */
margin-inline: auto;
padding: 0.2em;
width: 90vw;
}
fieldset {
/* in order to use CSS Grid: */
display: grid;
/* setting gaps/'gutters' between rows of 0.5em,
and 0.25em gaps between adjacent columns: */
gap: 0.5em 0.25em;
/* defining two columns, the first of which is sized
'auto', allowing the layout to be sized appropriately
to the content within the limits of other columns,
the second column is sized using the 'minmax()'
function between a minimum size of 50% (because the
maximum permitted size of the first column is 50%),
and 1fr, which is the fractional unit of the remaining
space available: */
grid-template-columns: auto minmax(50%, 1fr);
}
/* this styles the 'information' <div> that provides
guidance about the interactivity of the editable
<label> elements: */
fieldset div {
grid-column: 1 / -1;
text-align: center;
padding: 0 2rem;
}
code, kbd {
font-family: consolas, ubuntu mono, monospace;
}
kbd {
border: 1px solid #aaa;
border-radius: 0.4em;
padding: 0.1em 0.3em;
}
label {
/* to indicate interactivity: */
cursor: pointer;
/* for positioning the pseudo element: */
position: relative;
}
/* entirely irrelevant to the demo, but just to
make it look a little prettier (adjust to
taste or remove as you like): */
label::before {
content: '';
background: linear-gradient(90deg, lime, #ffff);
height: 100%;
position: absolute;
transform: scaleX(0);
transition: transform 0.3s ease-in-out;
transform-origin: 0 100%;
width: 100%;
z-index: -1;
}
label:hover::before {
transform: scaleX(1);
}
.length {
background: revert;
background-color: #fff;
border-color: transparent;
border-top: 1px solid currentColor;
cursor: not-allowed;
font-style: italic;
text-align: center;
}
<form action="#">
<fieldset>
<!-- this <div> is here simply to provide some guidance as to the
interactivity of the <label> elements for the purpose of this
demo; otherwise it can be removed entirely: -->
<div>Most labels are <code>contentEditable</code>, though they will focus the <input> element when clicked; <kbd>shift</kbd>&plus;<kbd>tab</kbd> to focus the <label></div>
<!-- because we're using a grid (and the majority of browsers don't yet
support display: subgrid) to create the aligned columns, the <input>
elements are the following-siblings of the <label> elements; in order
to associate the <label> with the correct <input> I've added an 'id'
attribute to each <input>, and the same id-value for the associated
<label> in their 'for' attribute; this means clicking/tapping on a
<label> will focus the appropriate <input>: -->
<label for="inputElement_0" contentEditable>longer label</label>
<input id="inputElement_0">
<label for="inputElement_1" contentEditable>label</label>
<input id="inputElement_1">
<label for="inputElement_2" contentEditable>medium label</label>
<input id="inputElement_2">
<label for="inputElement_3" contentEditable>label</label>
<input id="inputElement_3">
<label for="inputElement_4" contentEditable>label</label>
<input id="inputElement_4">
<label for="inputElement_5" contentEditable>label</label>
<input id="inputElement_5">
<!-- these elements are here purely to report the length of the
grid-columns in which they appear: -->
<label for="lengthInput" class="length"></label>
<input id="lengthInput" type="text" class="length" readonly>
</fieldset>
</form>
JS Fiddle demo.
If subgrid is available in your browser (currently it's only available in Firefox), then the following is possible:
// Again, this JavaScript has nothing to do with the resizing, but only shows
// the widths of the grid-tracks the elements are in:
const D = document,
logLengths = () => {
let label = D.querySelector('label:last-of-type'),
input = label.querySelector('input:last-of-type');
[label, input].forEach(
(el) => {
let width = Math.round(el.getBoundingClientRect().width / 10) * 10,
val = `${width}px`;
if (el.matches('label')) {
label.firstChild.nodeValue = val;
} else if (el.matches('input')) {
el.value = val;
}
});
};
logLengths();
D.querySelectorAll('label').forEach(
(label) => label.addEventListener('input', logLengths)
);
*,
::before,
::after {
box-sizing: border-box;
font-size: 16px;
line-height: 1.5;
margin: 0;
padding: 0;
}
form {
border: 1px solid currentColor;
margin-block: 1em;
margin-inline: auto;
padding: 0.2em;
width: 90vw;
}
fieldset {
display: grid;
gap: 0.5em 0.25em;
grid-template-columns: auto minmax(50%, 1fr);
}
fieldset div {
grid-column: 1 / -1;
text-align: center;
padding: 0 2rem;
}
code,
kbd {
font-family: consolas, ubuntu mono, monospace;
}
kbd {
border: 1px solid #aaa;
border-radius: 0.4em;
padding: 0.1em 0.3em;
}
label {
cursor: pointer;
position: relative;
/* Using display: grid to allow us to make use
of subgrid: */
display: grid;
/* instructing the layout engine to use the
grid-columns/grid-tracks of the parent
element: */
grid-template-columns: subgrid;
/* positioning the grid-start in the first
grid-column and the grid-end in the last
grid-column: */
grid-column: 1 / -1;
}
label::before {
content: '';
background: linear-gradient(90deg, lime, #ffff);
height: 100%;
position: absolute;
transform: scaleX(0);
transition: transform 0.3s ease-in-out;
transform-origin: 0 100%;
width: 100%;
z-index: -1;
}
label:hover::before {
transform: scaleX(1);
}
.length {
background: revert;
background-color: #fff;
border-color: transparent;
border-top: 1px solid currentColor;
cursor: not-allowed;
font-style: italic;
text-align: center;
}
<form action="#">
<fieldset>
<div>All labels are <code>contentEditable</code>, though they will focus the <input> element when clicked; <kbd>shift</kbd>&plus;<kbd>tab</kbd> to focus the <label></div>
<!-- because we can use subgrid, we can place the <input> elements
inside of their associated <label> elements, which removes the
need to assign either a 'for' attribute to the <label> or an
'id' attribute to the <input>, since they're automatically
associated this way via nesting: -->
<label contentEditable>longer label
<input>
</label>
<label contentEditable>label
<input>
</label>
<label contentEditable>medium label
<input>
</label>
<label contentEditable>label
<input>
</label>
<label contentEditable>label
<input>
</label>
<label contentEditable>label
<input>
</label>
<label class="length">
<input type="text" class="length" readonly>
</label>
</fieldset>
</form>
JS Fiddle demo.
References:
HTML
contentEditable.
CSS:
CSS <length>.
grid-template-columns.
display.
:last-of-type().
minmax().
subgrid.
JavaScript:
Arrow functions.
Array.prototype.forEach().
Conditional (ternary) operator.
document.querySelector().
document.querySelectorAll().
Element.getBoundingClientRect().
Math.round().
NodeList.prototype.forEach().
td{
border: 1px solid black;
width: 100%;
}
td.first-column {
max-width: 50%;
min-width: 10px;
}
<table style="width:100%;">
<tr>
<td class="first-column">very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very label</td>
<td class="second-column"><input style="width:100%"/></td>
</tr>
</table>
<table style="width:100%;">
<tr>
<td class="first-column">short label</td>
<td class="second-column"><input style="width:100%"/></td>
</tr>
</table>

Horizontaly aligning placeholder in input field

What would be correct approach to aligning placeholder to the top of the field, while input text appearing normally in the middle?
Any way to do that with CSS on input/::placeholder only, or should i rather construct a wrapper with span that would disappear when active and input field below it?
Here's a fiddle of what i've got now: https://jsfiddle.net/ejsLfvdn/1/
And that's what it should look like up to customers will:
The input masks are not the case here, i'm only struggling with the placeholder being aligned to the top, while input should appear normally in the middle. The placeholder MUST disappear after filling input.
I don't think that you will be able to do this by directly targeting the placeholder pseudo class (::placeholder).
Only a small subset of CSS properties can be applied to this element and position is not one of them:
https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder
I think you will need to take the approach of a wrapper with span and input and position appropriately.
You could use something like this with the only issue being the input must have the required attribute.
* {
box-sizing: border-box;
}
.input {
display: flex;
flex-flow: column-reverse nowrap;
border: 1px solid gray;
width: 220px;
}
.input input:valid + label {
opacity: 0;
}
.input input {
width: 100%;
padding: 10px;
border: none;
}
<div class="input">
<input required id="username" name="username" type="text" />
<label for="username">Username</label>
</div>
I hope I achieved what you need.
btw, I used jquery to hide the placeholder while typing and display it again if the field is empty.
$('.form-control').keyup(function(){
var val = $(this).val();
if(val == ""){
$('.placeholder').show();
}else{
$('.placeholder').hide();
}
});
.input-cont{
position: relative;
}
.form-control{
border: 1px solid #DDD;
border-radius: 5px;
height: 40px;
padding-left: 8px;
}
.placeholder{
position: absolute;
top: 5px;
left: 8px;
color: #3dc185;
font-size: 12px;
text-transform: uppercase;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<form>
<div class="input-cont">
<span class="placeholder">Imię</span>
<input class="form-control" type="text" name="name">
</div>
</form>
</body>
</html>
You can use translateY(-100%) on your placeholder to move the text upwards and then give your textbox some padding at the top to reveal the text:
.placeholder-offset {
font-size: 20px;
padding-top: 25px;
}
.placeholder-offset::placeholder {
color: red;
transform: translateY(-100%);
}
<input type="text" placeholder="Username" class="placeholder-offset" />

How to group an icon and an input element

I'm trying to achieve the following:
Create 3 input elements in a row
Each should have a logo to the left of it, centered perfectly.
Each should have a border-bottom that spans the logo as well.
Like the following image:
However with my current code the images can't be centered and the border doesn't span them. Here's my code:
input {
border: none;
width: 250px;
background-color: #393d49;
border-bottom: 1px solid #767D93;
padding: 10px;
}
form img {
width: 24px;
height: 24px;
}
<form>
<img src="assets/images/envelope.png" alt="Envelope icon indicating user's E-Mail.">
<input type="email" placeholder="E-Mail"><br>
<img src="assets/images/locked.png" alt="Lock icon indicating user's Password.">
<input type="password" placeholder="Password"><br>
<img src="assets/images/avatar.png" alt="Avatar icon indicating user's Name.">
<input type="text" placeholder="Username"><br>
</form>
As it was suggested, I would also use the font-awesome library. But if your not comfortable with that idea, here is how you can do without.
form, .form-row, input {
background-color: #051024;
}
.input-icon, label, input {
display: inline-block;
}
form {
padding: 0.8em 1.2em;
}
.form-row {
padding: 0.8em 0;
padding-bottom: 0.2em;
}
.form-row:not(:last-child) {
border-bottom: solid #18273a 1px; /* Only the last row has a border */
}
.input-icon {
width: 15px;
height: 15px;
margin: 0 10px;
}
label {
max-width:4em; /* Or the maximum width you want your lebel to be */
min-width:4em; /* Same */
color:white;
font-weight: 100;
}
input {
border:none;
padding: 0.8em 0.5em;
color: #6691c9;
font-size: 15px;
outline: none; /* No glowing borders on chrome */
}
<form>
<div class="form-row">
<!-- Put your image here, like so -->
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-email">Email</label>
<input id="form-email" type="email">
</div>
<div class="form-row">
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-password">Password</label>
<input id="form-password"type="password" placeholder="(8 characters min)">
</div>
<div class="form-row">
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-user">User</label>
<input id="form-user" type="text"><br>
</div>
</form>
If you're feeling adventurous
Try bootstrap, it has all you need to create cool web sites (it also includes the font-awesome library).

Having trouble keeping search elements on one horizontal line

I’m having trouble keeping my search fields with the search image on one line. I have created a container area that i want centered in the middle of the screen, and I gave it a max-width of “580px” …
#loginArea {
border-radius: 25px;
font-family: 'russo_oneregular';
font-size: 20px;
padding: 20px;
background-color: #ffffff;
color: #000000;
display: table;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
max-width: 580px;
}
Then I created my three search elements with the magnifying glass search icon …
<input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField" style="width:25%">
<input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField" style="width:25%">
<input type="text" name="event" id="event" placeholder="Event" class="searchField">
<input alt="Search" type="image" src="http://www.racertracks.com/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2ef2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button" height="40" align="middle">
But I can’t get everything to stay on one line even if there are 580 pixels available on the screen. On both Mac Chrome and Firefox this looks off — https://jsfiddle.net/4sjxum1k/1/ . Even if you expand the viewing area to have way more than 580 pixels, things are still wrapping. I’m fine if things wrap when the screen area is small (e.g. mobile browsers), but if there is enough room, I’d like everything to display on one line.
Any help is appreciated, - Dave
You need to set all all the input elements to "display:inline-block;" and also apply a set percentage width (width: 25%;) to each of the input elements.
.searchField {
display: inline-block;
line-height: 40px;
font-size: 22px;
margin: 0;
vertical-align: middle;
padding: 5px;
border: 1px solid #ccc;
box-sizing: border-box;
-webkit-appearance: textfield;
background-color: white;
-webkit-rtl-ordering: logical;
-webkit-user-select: text;
letter-spacing: normal;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
text-align: start;
width:25%;
}
See JSFiddle here:
https://jsfiddle.net/4sjxum1k/5/
Your items are too wide for your wrapping div.
You have multiple options, either increase the width of the containing box to something like 650px (for example) or reduce the width of the existing input fields.
Another idea would be to float all the form inputs to the left with a little margin-right value and float the submit item to the right. That would remove the default browser spacing on your elements. But adjustment to the widths are still necessary. Don't forget to add a clear fix after your <form>

CSS bubble error box for a form

I want to create an error box for a form like the one below.
I already themed the input box and am using jQuery validation to display errors. However I can't get that error box right. I think I'll need to put that together with three tags, but I don't know what tags to use (jQuery validation uses a label tag to display the error).
My current code for the error is:
<label for="email">
<span>Email:</span>
<input type="text" id="email" name="email" class="error">
<label for="email" class="error" style="">Az e-mail címet kötelező megadni</label>
</label>
I must make this IE7 compatible.
I made the following changes:
<div class="dataline">
<div class="label">Label:</div>
<div class="field"><input type="text" id="name" name="name" /></div>
<div class="arrow"></div>
<label class="error">Error text</label>
<div class="ender"></div>
</div>
I set the arrowhead as an image for the class arrow so now it looks perfect. Basicly I used 4 left floated block elements (label, input, arrowhead and bubble body). Now I only have two problems: the arrowhead is displayed even when there's no error. How can I hide it when the label is not after it? My other problem is that the container div is 800px wide and if the error text is long, it wraps around to the next line. How can I avoid it?
My css is:
div.dataline {
margin: 10px 0 0 0;
white-space: nowrap;
width: 3000px;
owerflow: visible;
height: 60px;
}
div.field {
float: left;
}
div.label {
float: left;
width: 120px;
margin: 20px 0 0 0;
}
div.arrow {
background-image: url('gfx/redarrow.png');
margin: 7px 0 0 10px;
background-repeat: no-repeat;
width: 20px;
height: 45px;
float: left;
}
div.ender {
background-image: url('gfx/bubbleend.png');
margin: 7px 0 0 0px;
background-repeat: no-repeat;
width: 3px;
height: 45px;
float: left;
}
label.error {
height: 27px;
background-image: url('gfx/bubblemiddle.png');
float: left;
padding: 9px;
margin: 7px 0 0 0;
}
the following fiddle is a good start for your implementation:
http://jsbin.com/aReQUgay/1/edit
#email.error
{
border-color: red;
}
#email.error + label
{
background-color: red;
}