I try to setup a multi column contact form by wrapping the texarea into a "second column" using a wrapped flexbox. I expected the textarea to be the same width as the text input. Actually, the text input (first column) is wider than the textarea (second column). In the sample I created on codepen, the difference is e.g. 12px.
In the below example, the textarea is wider than the fieldset. In this example the texarea (and the submit button) is 10px wider than the text inputs, respective the fieldset. Why is one element wider than another when it's wrapped within a flexbox?
In the code a single element should not be higher than the max-height of the entire flexbox.
* {
box-sizing: border-box;
}
form {
display: flex;
flex-flow: column wrap;
max-height: 116px;
}
input[type=text] {
min-width: 0;
padding: 0;
margin: 0;
min-width: 0;
margin-bottom: 1em;
display: block;
width: 100%
}
fieldset {
padding: 0;
margin: 0;
}
textarea {
min-height: 50px;
max-height: 84px;
min-width: 0;
resize: vertical;
}
textarea, input[type="submit"] {
padding: 0;
margin: 0;
}
<form action="/doit" method="post">
<fieldset>
<input type="text" value="Text">
<input type="text" value="Text">
<input type="text" value="Text">
<input type="text" value="Text">
</fieldset>
<textarea>I am wider than the fieldset.</textarea>
<input type="submit" />
</form>
I tried it in Safari (Mojave) and Chrome 85.
Could anyone tell me what causes this behaviour?
The problem still exists in your code. You have set the max-height of your form to 116px (this time). The sum of the heights of input elements and textarea will definitely grow more than 116px and since your flex-wrap is wrap it will turn to a row. So you should not expect them to be of equal width.
If you want your text inputs to be of equal size and be next to each other you must use flex-direction: row. Your code can be something like:
* {
box-sizing: border-box;
}
form {
display: flex;
flex-flow: row wrap;
}
input[type=text] {
min-width: 0;
padding: 0;
margin: 0;
min-width: 0;
margin-bottom: 1em;
display: block;
width: 100%
}
fieldset {
flex:1;
padding: 0;
margin: 0;
}
.textarea {
min-height: 50px;
max-height: 84px;
min-width: 0;
resize: vertical;
}
.submit{
align-self:center;
}
textarea, input[type="submit"] {
padding: 0;
margin: 0;
}
div{
flex:1;
display:flex;
flex-direction:column;
}
<form action="/doit" method="post">
<fieldset>
<input type="text" value="Text">
<input type="text" value="Text">
<input type="text" value="Text">
<input type="text" value="Text">
</fieldset>
<div >
<textarea class="textarea">I am wider than the fieldset.</textarea>
<input class="submit" type="submit" />
</div>
</form>
Note that I have wrapped your textarea and button in a <div> to be child of form element and gave it a flex:1. Then made that <div> a vertical flexbox itself.
Related
<div className="formContainer">
<InputBox types={"Questions"} setText={setQuestion} submit={submit} />
<InputBox types={"Solutions"} setText={setAnswer} submit={submit} />
</div>
Above are my html code and InputBox is a component of react which has a textArea nested between fieldset
.formContainer {
display: flex;
}
.inputBox {
flex: 1;
resize: none;
line-height: 30px;
border-radius: 0px;
border-style: none;
width: 100%;
max-width: 100%;
}
The desired pattern is two textarea in the fieldset aligns side by side with 50% width. I don't understand why my code shrinked two textarea and float to the left, please refer to the attached screencap , of the textarea and how could i fix that . Please kindly advise.
and your code do something?
I don't know react but I see that you put <div className="formContainer">. In html corect is <div class="formContainer">. Also on the InputBox you need to set a class:
as I said, I don't know react, but if you want to arrange 2 objects in the same row with css you need to put to the main container
display: flex;
flex-direction:row;
Also you need to set a smaller width to the .inputBox, if you put 100% is impossible for them to be aligned next to each other
I'm not familiar with React InputBox per se, but it looks to me like you simply haven't assigned you .inputBox class, to the inputBox component.
Maybe it should be something like this:
<div className="formContainer">
<InputBox types={"Questions"} setText={setQuestion} submit={submit} className="inputBox" />
<InputBox types={"Solutions"} setText={setAnswer} submit={submit} className="inputBox" />
</div>
Outside of that I put together a quick plain HTML mockup of what you (I think) are trying to achieve:
.formContainer {
display: flex;
gap: 10px;
}
fieldset {
position: relative;
padding: 0;
margin: 0;
height: auto;
border: none;
}
fieldset label {
position: absolute;
top: -15px;
left: 7px;
background: white;
padding: 6px;
}
.inputBox {
flex: 1;
resize: none;
line-height: 30px;
border-radius: 10px;
padding: 5px 12px;
}
<div class="formContainer">
<fieldset>
<label for="questions">Questions</label>
<textarea name="questions" rows="3" cols="20" class="inputBox" placeholder="Questions"></textarea>
</fieldset>
<fieldset>
<label for="solutions">Solutions</label>
<textarea name="solutions" rows="3" cols="20" class="inputBox" placeholder="Solutions"></textarea>
</fieldset>
</div>
Hopefully that will be enough to help you out a bit there?
Also, here is a codepen to see the mockup working:
I want to put a checkbox with a label to its right in my simple webpage, but when I select the label and checkbox to change their display, they both behave differently.
When checkbox is set to inline, it moves up to the right of textarea who's display is set to block. So I figured I can just set checkbox display to block and label display to inline and the label will move to the right of block. Instead, the label stays below the checkbox and eliminates any margin between it and the send button.
How can I fix this?
The code is in JS Bin
The form is a child of .main-body
HTML
<form>
<label for="name-input">Name</label>
<input type="text" id="name-input">
<label for="message-input">Message</label>
<textarea id="message-input"></textarea>
<input type="checkbox" id="current-user">
<label for="current-user">I currently use SuiteLyfe</label>
<input type="submit" value="Send">
</form>
CSS
.main-body {
display: box;
width: 600px;
margin: 0 auto 0 auto;
}
textarea[id=message-input] {
display: box;
box-sizing: border-box;
width: 400px;
height: 200px;
margin-bottom: 15px;
font-size: 14px;
}
input[type=checkbox] {
display: block;
}
label[for=current-user] {
display: inline;
}
P.S. I'm aware of bootstrap and other technologies but right now I am learning barebones html and css and wish to understand it even though I realize it may not matter in my career.
Change your textarea display type to block and your checkbox display type to inline
textarea[id=message-input] {
display: block;
box-sizing: border-box;
width: 400px;
height: 200px;
margin-bottom: 15px;
font-size: 14px;
}
input[type=checkbox] {
display: inline;
}
I have created a dynamic height input as follow which scales when browser resize. The second step that I would like to achieve is to vertically center the input text. I use vertical-align: middle but it seems that nothing changes.
What could I do to vertically align the input text
input{
background: lightgray;
vertical-align: middle;
width: 100%;
height: 30%;
padding-top: 30%;
}
<input type="text">
Since your input has a padding-top: 30%; you can just say padding: 15% 0; instead so your text will be dynamically centered within your <input>.
input{
background: lightgray;
width: 100%;
padding: 15% 0;
}
<input type="text">
You need to remove padding-top and by default text in input will be centered.
body, html {
height: 100%;
}
input {
background: lightgray;
width: 100%;
height: 30%;
}
<input type="text" placeholder="Text">
How does one set a margin to siblings but not to the parent?
For example, I've got input fields, buttons and a text area. I do want margins between these elements but I do not want an (extra) margin to the parent element.
I know I can set separate values for top right bottom left but that's not applicable IMO.
body {
background-color: #717074;
font-family: sans-serif;
margin: 1em;
}
.d1 {
background-color: white;
margin: 0 auto;
padding: 1px 1em;
width: 960px;
}
.fw {
width: 100%
}
input {
margin: 5px
}
textarea {
width: 100%
}
<div class=d1>
<form method=post>
<input name=name type=text required placeholder="Naam">
<br>
<input name=email type=email required placeholder="Emailadres">
<textarea name=body rows=5></textarea>
<input type=submit>
</form>
</div>
So there should be margin between the name and email input elements but not between these two and the parent div (for example).
I can't cheat by setting left and right margin to 0 as that'd break things when the name and email elements would be on the same line.
Updated to work with inputs on a single line (no line break between).
Note that I've put all the inputs on to a single line in the HTML because the whitespace between the elements was being rendered! (See here for an explanation)
body {
background-color: #717074;
font-family: sans-serif;
margin: 1em;
}
.d1 {
background-color: white;
margin: 0 auto;
padding-top: 5px;
padding-left: 5px;
padding-right: 5px;
padding-bottom: 0px;
width: 960px;
}
.fw {
width: 100%
}
input {
margin-top: 0px;
margin-left: 0px;
margin-right: 5px;
margin-bottom: 5px;
}
textarea {
width: 100%;
}
<div class=d1>
<form method=post>
<input name=name type=text required placeholder="Naam"><input name=email type=email required placeholder="Emailadres"><textarea name=body rows=5></textarea><input type=submit>
</form>
</div>
Howsat?
I've got approximately this layout code:
<form class="form">
<input type="text" placeholder="Enter something!" class="form-control" />
<input type="submit" class="btn btn-md btn-default" />
</div>
.form {
max-width: 500px;
margin: 0 auto;
}
.form input[type="text"] {
display: inline;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.form input[type="submit"] {
display: inline;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
margin-top: -4px;
margin-left: -5px;
}
body {
padding: 20px;
}
.form {
max-width: 500px;
margin: 0 auto;
}
.form input[type="text"] {
display: inline;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.form input[type="submit"] {
display: inline;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
margin-top: -4px;
margin-left: -5px;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<form class="form">
<input type="text" placeholder="Enter something!" class="form-control" />
<input type="submit" class="btn btn-md btn-default" />
</div>
I would like the button to be on the same line as the text box. But the default width of the text box is 100%, and that pushes the button to the next line. How can I get the text box to expand to be as big as possible without doing that?
Check out flexbox, you can specify the width of the button and let the text input grow and fill the space. The downside is it's a CSS3 feature and therefore not supported in older browsers.
There's no easy way to do this. Best is to give the text box and the button percentage widths. First get rid of the negative margin on the submit button. Then add these styles:
.form input[type="text"] {
width:80%;
}
.form input[type="submit"] {
width:17%;
}
The reason for the missing 3% is that buttons in particular in forms have a lot of extra style rules applied that add things like border and padding, and they differ depending on your browser.
jsFiddle
You can use max-width for text input
max-width: 430px; and button width width: 70px; to adjust with container form
You can also use percentage width;
jsfiddle link