Can CSS over-rule HTML input size declarations - html

I have inherited a website that has many inputs on various pages, such as:
<input name="name" type="text" id="name" size="40" maxlength="64">
and
<textarea name="descr" cols="40" rows="2" id="descr">
I have been improving the CSS of the site to make it flexible layout for mobile devices, etc. But the size/cols rules of the HTML persists in setting the fixed size, regardless of outside factors.
I have tried using CSS such as:
CSS:
input, textarea, select {
max-width:100%;
}
(And with also appending !important) but this doesn't effect the elements.
It's been converted into an HTML5 template, and the inputs are in a table (but the table is flexible and is not the issue).
Is there a way that CSS can overwrite the HTML size/cols declaration in the inputs?
The large number of inputs over multiple pages wanted me to find a CSS simple way of overwriting them all in one fell swoop. As far as I can see this doesn't seem directly possible and I will have to go through and edit the size values for each input elements :-/.
EDIT
Full Code:
HTML:
<table id='centralTable'>
<tr>
<td colspan="2">Update Category</td>
</tr>
<tr>
<td width="28%"><strong><label for='name'>Category Name</label></strong></td>
<td width="70%"><input name="name" type="text" id="name" value="catname" size="40" maxlength="40" required></td>
</tr>
<tr>
<td><input name="id" type="hidden" id="id" value="12" >
</td>
<td><input type="submit" value="Update" ></td>
</tr>
</table>
CSS:
#centralTable {
width:90%;
max-width:780px;
min-width:300px;
margin:1rem auto;
}
input, textarea, select {
max-width:100%;
}
If I adjust the sizing of the size value, the other elements on the page fit the screen as intended, but the size value offsets this. Firebug shows that max-width is applied to the element but the element size does not accord to this.
EDIT TWO:
Setting the td element max-width to a px value rather than a percentage works, but obviously doesn't adapt to viewport size.
td {
max-width:200px; /* This works in containing the input size */
}

CSS can override the size attribute using width. There's a good explanation about it here.
Here, we have a typical input, size 10:
<input type="text" size="10">
And here is that same input, adjusted with CSS
input {
width: 20px;
}
<input type="text" size="10">
max-width is also a viable option, depending on the circumstance
div {
width: 20px;
}
input {
max-width: 100%;
}
<div>
<input type="text" size="10">
</div>

OK, I think I've got it:
The table elements are set to take a percentage size but the nature of tables is that they expand to fit their contents, and the contents is set to take a maximum of 100% of the table size, so:
Size sets input elements size:
Table cell expands to encase input element
CSS input sets the input to fill table cell
So; Using a Viewport Width as a value gives a more absolute container for the size to sit into.
#centralTable input, #centralTable textarea, #centralTable select {
max-width:65vw;
}
This limiter, rather than a percentage limiter, then correctly resizes the child input size value.
Viewport width units should be used in preference to percentage sizes.

Related

ResposiveNess of div with multiple content

I am using Bootstrap 4. My HTML code is like below.
<div class="font-weight-bold col-12">
<span>Filter :</span>
<input type="text" class="form-control" />
</div>
Before decrease the browser size my output is like below
After decrease the browser size my output is like below
How can I keep both elements in the same line after I decrease the browser size?
Try like that:
<div class="font-weight-bold col-lg-12 col-md-12 col-sm-12"> // use fullwidth for large, medium and small devices
<span>Filter :</span>
<input type="text" class="form-control" />
</div>
You can read more about bootstrap grid system here.
You can do a few things here:
1) get rid of the "form-control", add a class of your own and set its width using CSS
HTML
<input type="text" class="my-input" />
CSS
.my-input {
width: 100px; // or the width you desire
}
2) Overwrite the CSS of the element inside the media query.
Somewhere in bootstrap CSS you have a selector (of "form-control") inside a media query which gives the input a smaller width. You can create your own media query and selector and overwrite it.
3) Sort of a combination of both. You can just add a class to the input
<input type="text" class="form-control my-input" />
and then add CSS according to the screen width.
It might not be that elegant - but you can also use !important on the width and then it won't be overwritten when the screen size changes.
Like:
.my-input {
width: 100px !important; //again - not elegant, but will do the work
}
Write the CSS for input as:
input {
width: calc(100% - 55px)
}
HTML
<div class="font-weight-bold">
<span>Filter :</span>
<input type="text" class="form-control" />
</div>
55px is the width allotted for the label. The input will adjust itself as per the screen and the label width without using any bootstrap also.

Decrease width of input in HTML

So I have an input tag in HTML, and I need to decrease its width to (let's just say) 80% of its normal width. I've tried setting width:80% but that didn't work. I would prefer solutions using only HTML/CSS, but I'm open to anything. My Google searches came up with results to change the width to a fixed amount like 50 pixels, not relative to its normal width. Thanks!
<input id="myInput" type="text">
First, get the current input width (myInputWidth). Multiply it by some factor (0.8). Then modify the width.
var myInputWidth = (document.getElementById("myInput").offsetWidth);
document.getElementById("myInput").style.width = myInputWidth * 0.8 + "px";
You can check out an example of this here.
Pretty simple with CSS. Check this out!
.input-box{
width:100px
}
<input id="myInput" class="input-box" type="text">
This is what you want hope this helps you.
Regards.
You can place your input element in a container, set a width of said container and then modify the width of the input in percentages.
#input {
width: calc(0.8 * 500px); /* 80% = 0.8 && 500px is the input element's initial width. Just for example I took 500px */
}
<input type="text" id="input">
If you are going to do this for one input box only then you can add Inline Css
<input type="text" id="input" style="width:100px">
You need a block element around.
For example:
<div class="myBox">
<input class="myInput" type="text">
</div>
.myBox {
display: block;
width: 100px;
}
.myInput {
width: 80%;
}
This method your input take 80% width of your parent item (div, myBox).

How do I set textarea size to x cols, y rows using CSS

My textarea I was using
<textarea cols="50" rows="20">
and that worked fine. But I realized I needed to adjust the size for smaller displays (such as mobile) so I then used width and height attributes with CSS
i.e
.genre_grey_list {
width: 50ch;
height: 20em;
}
<textarea cols="50" rows="20"></textarea>
<textarea class="genre_grey_list"></textarea>
However a width of 50ch is less than cols="50", and a height of 20em is less than rows="20". How can I accurately set size of textarea in terms of rows and columns using CSS ?

increase the text size but keep the position of input field constant

Let's say I have an input field and I make its height:25px. If I increase the font-size inside that text field, although the box size is constant, it appears as it I added a whole lot of padding. When the font size is normal, it looks something like this:
BEFORE
But now when I increase the size of the font, it looks something like extra padding added something this:
AFTER
However, the padding is unchanged when I debug. I tried adding the box-sizing:border-box, but still it is unchanged. I would really appreciate if someone can help me. Thanks.
A simple demonstration can be achieved by just changing the size of the font-size.
<body>
<div>
<input type="text" value="HI"/>
<input type="text" value="HELLO" style="font-size: 900px; height: 30px; width: 100%;"/>
<input type="text" value="HI"/>
</div>
</body>
You can reset the vertical-align propertie .
Defaut is baseline , line- height is equal to font-size if not reset.
example with vertical-align: (added text and reduced some value from your funny example ;) )
input {
vertical-align:middle;
}
/* see div middle center. Notice: the tallest input gives the line-height on the line it stands */
div {
background:linear-gradient(to top,rgba(0,0,0,0.2) 50%,rgba(0,0,0,0.1) 50%) ,linear-gradient(to left,rgba(0,0,0,0.2) 50%,rgba(0,0,0,0.1) 50%);
<div>
<input type="text" value="HI"/> text
<input type="text" value="HELLO" style="font-size: 90px; height: 30px; width:50px;"/> text
<input type="text" value="HI"/>
</div>

Aligning form fields for responsive layout

Im currently trying to fix a form that i have built to work on a responsive layout
Ive attached a jpeg of what the form should look like a on full version of the site with the comments field aligned to the right of the rest of fields but when viewed on a mobile i want the comments field to drop below the rest of the fields.
Because i was advised to wrap the comments field in a DIV and place it before the rest of the form fields then float it right, when i view the mobile version the comments field sits at the top of the form instead of the bottom
any suggestions to how i can fix this issue?
see the CSS & HTML below
<style type="text/css">
body {
background-color: #FFF;
}
#form {
width:960px;
background-color:#edf8ff;
height:650px;
}
.gezza-form {
width:894px;
margin:0 auto;
margin-top:20px;
}
.gezza-field {
width:437px;
height:75px;
margin-bottom:10px;
border: 1px solid #d9e7f1;
}
.gezza-comments{
width:437px;
height:300px;
}
-->
</style></head>
<body>
<div id="form">
<form action="" class="gezza-form" method="post" >
<div style="float:right;">Comments<br /><textarea name="comments" cols="" rows="" class="gezza-comments" ></textarea></div>
First Name<br />
<input name="firstname" type="text" class="gezza-field" /><br/>
Last Name<br />
<input name="lastname" type="text" class="gezza-field" /><br/>
Email Address<br />
<input name="email" type="text" class="gezza-field" />
</form>
Should all fields have a fixed width? Wrap each column in a div, then float both divs left and give them an explicit width.
Put the div with the text area below the other fields in the HTML.
That should get you a lot closer.
Then, you will need to wrap each field / label pair in a div too.
On the mobile view it should all go into place more or less (bit of padding / margin needed maybe).
Then, on the wider view float the div for the text area right and the other fields' divs left. You'll need to set width for them .. say 49% each.
You'll need to use a clear fix on the element after this lot to clear the floats.