Alright, so I am trying to use Flexbox to align my form on the left hand side of the screen and my image on the right hand of the screen using justify-content: space-between but when I put that on my .container it doesn't work. Here is what I have so far:
<form>
<div class="heading">
<h1 class="callout">Send Us A Message!</h1>
</div>
<div class="container">
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" name="firstname">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" name="lastname">
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" class="form-control" name="phonenumber">
</div>
<div class="form-group">
<label>Email Address</label>
<input type="email" class="form-control" name="email">
</div>
<div class="form-group">
<label>Message</label>
<textarea name="message" cols="30" rows="10">
</textarea>
<button class="btn-1">Send</button>
</div>
<div class="image">
<img src="img/city.jpg" alt="">
</div>
</div>
</form>
My style:
.container {
display: flex;
justify-content: space-between;
flex-direction: column;
}
img {
width: 20%;
}
Put the two sections in their own containers, then it can work.
.container {
display: flex;
justify-content: space-between;
}
section:first-child {
background-color: orange;
}
section:last-child {
background-color: lightgreen;
}
.image {
border: 2px dashed black;
height: 100px;
width: 100px;
}
<form>
<div class="heading">
<h1 class="callout">Send Us A Message!</h1>
</div>
<div class="container">
<section><!-- container one -->
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" name="firstname">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" name="lastname">
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" class="form-control" name="phonenumber">
</div>
<div class="form-group">
<label>Email Address</label>
<input type="email" class="form-control" name="email">
</div>
<div class="form-group">
<label>Message</label>
<textarea name="message" cols="30" rows="10">
</textarea>
<button class="btn-1">Send</button>
</div>
</section>
<section><!-- container two -->
<div class="image"> image
<img src="img/city.jpg" alt="">
</div>
</section>
</div>
</form>
Related
I'm a newbie so please forgive the probably very silly question. I need to have the contact elements in my right grid column to be aligned with the form elements in the left column. So far the contact details just sit at the bottom and I can't figure out why.
Any help is greatly appreciated.
.form-list,
.form-contact {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 2rem;
padding-top: 1rem;
}
.form-group {
grid-column: 1 / 2;
}
.form-details {
grid-column: 2 / 3;
}
<form action="" method="post">
<div class="form-list">
<div class="form-group">
<label for="name" class="form-label">Name</label>
<input id="name" name="name" type="text" placeholder="Your name" class="form-control" required />
</div>
<div class="form-group">
<label for="email" class="form-label">Email</label>
<input id="email" name="email" type="text" placeholder="youremail#email.com" class="form-control" required />
</div>
<div class="form-group">
<label for="message" class="form-label">Message</label>
<textarea id="message" name="message" rows="4" class="message-control" required></textarea>
</div>
<div class="form-group">
<button type="submit" class="button">Contact us</button>
</div>
</div>
</form>
</main>
<div class="form-contact">
<div class="form-details">
<h2>Contact</h2>
<p><span>456 Random Street,</span><span>Sydney,</span>
<span>Australia</span></p>
<p><span>Phone: 024 123 45678</span><span>Email: sales#website.co.nz</span></p>
</div>
</div>
I think you misunderstood how grid works, you need a wrapper or container that will display its content in a grid, what you did was put the display: grid on the elements themselves
.grid-wrapper {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 2rem;
padding-top: 1rem;
align-items : flex-start;
}
.form-group {
grid-column: 1;
}
.form-details {
grid-column: 2;
}
.form-details h2 {
margin-top : 0;
}
<div class="grid-wrapper">
<form action="" method="post">
<div class="form-list">
<div class="form-group">
<label for="name" class="form-label">Name</label>
<input id="name" name="name" type="text" placeholder="Your name" class="form-control" required />
</div>
<div class="form-group">
<label for="email" class="form-label">Email</label>
<input id="email" name="email" type="text" placeholder="youremail#email.com" class="form-control" required />
</div>
<div class="form-group">
<label for="message" class="form-label">Message</label>
<textarea id="message" name="message" rows="4" class="message-control" required></textarea>
</div>
<div class="form-group">
<button type="submit" class="button">Contact us</button>
</div>
</div>
</form>
<div class="form-contact">
<div class="form-details">
<h2>Contact</h2>
<p><span>456 Random Street,</span><span>Sydney,</span>
<span>Australia</span></p>
<p><span>Phone: 024 123 45678</span><span>Email: sales#website.co.nz</span></p>
</div>
</div>
</div>
You can avoid all those css and just work with flexbox
.form-list,
.form-contact {
padding-top: 1rem;
}
main {
display: flex;
}
<main>
<form action="" method="post">
<div class="form-list">
<div class="form-group">
<label for="name" class="form-label">Name</label>
<input id="name" name="name" type="text" placeholder="Your name" class="form-control" required />
</div>
<div class="form-group">
<label for="email" class="form-label">Email</label>
<input id="email" name="email" type="text" placeholder="youremail#email.com" class="form-control" required />
</div>
<div class="form-group">
<label for="message" class="form-label">Message</label>
<textarea id="message" name="message" rows="4" class="message-control" required></textarea>
</div>
<div class="form-group">
<button type="submit" class="button">Contact us</button>
</div>
</div>
</form>
<div class="form-contact">
<div class="form-details">
<h2>Contact</h2>
<p><span>456 Random Street,</span><span>Sydney,</span>
<span>Australia</span></p>
<p><span>Phone: 024 123 45678</span><span>Email: sales#website.co.nz</span></p>
</div>
</div>
</main>
I'm working on a project with a Bootstrap form. Very simply, I would like the form to have a scroll bar when the window is resized. I tried adding the overflow-auto class to my div, but that doesn't work for it. So when I resize my window, no scroll appears. Here's my code.
.form-container {
border-radius: 25px;
border: 1px solid black;
padding: 15px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.register-container { max-width: 500px; }
.title-header {
font-size: 25px;
font-weight: bold;
}
<div class="overflow-auto container form-container register-container">
<h1 class="title-header">REGISTER</h1>
<form id="register" method="post" action="register">
<?php writeMessageBoxIfMessage($message); ?>
<div class="form-group row">
<div class="col-sm-6">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">FIRST NAME</label> <br>
<input id="firstname" name="firstname" maxlength="80" required>
</div>
<div class="col-sm-6">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">LAST NAME</label> <br>
<input id="lastname" name="lastname" maxlength="80" required>
</div>
<div class="col-sm-12">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">EMAIL</label> <br>
<input type="email" id="email" name="email" maxlength="255" required>
</div>
<div class="col-sm-12">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">PASSWORD</label> <br>
<input type="password" id="password" name="password" maxlength="80" required>
</div>
<div class="col-sm-12">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">CONFIRM PASSWORD</label> <br>
<input type="password" id="confirm-password" name="confirm-password" maxlength="80" required>
</div>
<div class="col-sm-6">
Already have an account? <br> Click here</span> to login.
</div>
<div class="col-sm-6">
<input id="register" type="submit" name="submit"
value="REGISTER" />
</div>
</div>
</form>
</div>
Can anyone provide some suggestions so I can get this to scroll?
Can’t replicate this at the moment but try using
position: absolute;
and give a container
position: relative;
Rather than using the ‘fixed’ position.
Let me know if this doesn’t work.
I hope you want internal scroll if the form is more in height.
Use the following CSS,
Try it.
#register {
max-height: 200px;
overflow: scroll;
}
Please add media
#media (max-height: 420px) {
#register {
height: 150px;
overflow-x: hidden;
overflow-y: scroll;
}
}
for better solution and check your device height should be (max-height: yourDeviceHeight) in media
.form-container {
border-radius: 25px;
border: 1px solid black;
padding: 15px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.register-container { max-width: 500px; }
.title-header {
font-size: 25px;
font-weight: bold;
}
#media (max-height: 420px) {
#register {
height: 150px;
overflow-x: hidden;
overflow-y: scroll;
}
}
<div class="overflow-auto container form-container register-container">
<h1 class="title-header">REGISTER</h1>
<form id="register" method="post" action="register">
<?php writeMessageBoxIfMessage($message); ?>
<div class="form-group row">
<div class="col-sm-6">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">FIRST NAME</label> <br>
<input id="firstname" name="firstname" maxlength="80" required>
</div>
<div class="col-sm-6">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">LAST NAME</label> <br>
<input id="lastname" name="lastname" maxlength="80" required>
</div>
<div class="col-sm-12">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">EMAIL</label> <br>
<input type="email" id="email" name="email" maxlength="255" required>
</div>
<div class="col-sm-12">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">PASSWORD</label> <br>
<input type="password" id="password" name="password" maxlength="80" required>
</div>
<div class="col-sm-12">
<label for="colFormLabelSm" class="col-sm-12 col-form-label col-form-label-sm">CONFIRM PASSWORD</label> <br>
<input type="password" id="confirm-password" name="confirm-password" maxlength="80" required>
</div>
<div class="col-sm-6">
Already have an account? <br> Click here</span> to login.
</div>
<div class="col-sm-6">
<input id="register" type="submit" name="submit"
value="REGISTER" />
</div>
</div>
</form>
</div>
I am new to coding and I am trying to make a contact form with the Flex property so that I can place my "name" and "telephone number" horizontally next to each other and my "message" below. They must all be the same width.
I can´t figure it out with CSS.
below is my HTML code:
<div class="contactcontainer">
<form action="">
<div class="input">
<p>First name</p>
<input type="text" name="firstname" placeholder="Your first name...">
<p>Phone number</p>
<input type="text" name="pnumber" placeholder="your phone number">
</div>
<div class="message">
<p>Your message</p>
<input type="text" name="message" placeholder="type your message">
</div>
</form>
</div>
You can achieve this by using flexbox.
And remember to use semantic markup for your forms, e.g. use label for labels instead of p tag.
.contactcontainer label {
display: block;
}
.input {
display: flex;
}
.input > div {
margin-right: 8px;
}
<div class="contactcontainer">
<form action="">
<div class="input">
<div>
<label for="firstname">First name</label>
<input id="firstname" type="text" name="firstname" placeholder="Your first name...">
</div>
<div>
<label for="pnumber">Phone number</label>
<input id="pnumber" type="text" name="pnumber" placeholder="your phone number">
</div>
</div>
<div class="message">
<label for="message">Your message</label>
<textarea id="message" type="text" name="message" placeholder="type your message"></textarea>
</div>
</form>
</div>
Here is my solution, Its not perfect, but it will give u starting point
* {
box-sizing: border-box;
}
.contactcontainer {
width: 500px;
}
.input {
display: flex;
}
.message input {
display: block;
width: 100%;
}
.l,
.r {
flex: 1;
}
.l input,
.r input {
width: 100%;
}
<div class="contactcontainer">
<form action="">
<div class="input">
<div class="l">
<p>First name</p>
<input type="text" name="firstname" placeholder="Your first name...">
</div>
<div class="r">
<p>Phone number</p>
<input type="text" name="pnumber" placeholder="your phone number">
</div>
</div>
<div class="message">
<p>Your message</p>
<input type="text" name="message" placeholder="type your message">
</div>
</form>
</div>
If you want to achieve it using flex then
.input { display: flex; flex-direction: row; flex-wrap: wrap; }
<div class="contactcontainer">
<form action="">
<div class="input">
<div class="inline">
<p>First name</p>
<input type="text" name="firstname" placeholder="Your first name...">
</div>
<div class="inline">
<p>Phone number</p>
<input type="text" name="pnumber" placeholder="your phone number">
</div>
</div>
<div class="message">
<p>Your message</p>
<input type="text" name="message" placeholder="type your message">
</div>
</form>
</div>
Using FlexBox
.input {
display: flex;
}
input {
width: 100%;
}
.hor{
flex: 1;
}
<div class="contactcontainer">
<form action="">
<div class="input">
<div class="hor">
<p>First name</p>
<input type="text" name="firstname" placeholder="Your first name...">
</div>
<div class="hor">
<p>Phone number</p>
<input type="text" name="pnumber" placeholder="your phone number">
</div>
</div>
<div class="message">
<p>Your message</p>
<input type="text" name="message" placeholder="type your message">
</div>
</form>
</div>
EDIT:
Use type=Number for number validation
.input {
display: flex;
}
input {
width: 100%;
}
.hor{
flex: 1;
}
<div class="contactcontainer">
<form action="">
<div class="input">
<div class="hor">
<p>First name</p>
<input type="text" name="firstname" placeholder="Your first name...">
</div>
<div class="hor">
<p>Phone number</p>
<input type="number" name="pnumber" placeholder="your phone number" min=1>
</div>
</div>
<div class="message">
<p>Your message</p>
<input type="text" name="message" placeholder="type your message">
</div>
</form>
</div>
In other words, how to get from this:
To something like this:
In other words, from labels on top of the input to labels on the left side of the input while making the input fit the parent's width.
.row {
display: flex
}
.form-group {
display: flex
flex: 1 0 0
flex-direction: column
}
.col-12 {
//
}
.col-6 {
//
}
.col-4 {
//
}
<div class="row">
<div class="form-group">
<label class="col-6">First name</label>
<input class="col-6" type="text" value=""></input>
</div>
<div class="form-group">
<label class="col-6">Last name</label>
<input class="col-6" type="text" value=""></input>
</div>
</div>
<div class="row">
<div class="form-group">
<label class="col-4">Message</label>
<input class="col-8" type="text" value=""></input>
</div>
</div>
<div class="row">
<div class="form-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
<div class="form-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
<div class="form-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value=""></input>
</div>
</div>
You don't need display:flex on your row- declaring it on the form-group is sufficient. Then you add the according flex properties on the child elements (input and label).
Also very important: Always insert ; after each declaration, or you will break the entire rule block.
/* essential rules */
.form-group {
display: flex;
}
label {
flex: 0 0 100px;
}
input {
flex: 1;
}
/* only for style */
.form-group {
margin-bottom:1em;
}
input{
padding:.4em;
border-radius:2px;
border:1px solid #ccc;
}
label {
padding:.4em;
font-family:sans-serif;
}
<div class="row">
<div class="form-group">
<label class="col-6">First name</label>
<input class="col-6" type="text" value="">
</div>
<div class="form-group">
<label class="col-6">Last name</label>
<input class="col-6" type="text" value="">
</div>
</div>
<div class="row">
<div class="form-group">
<label class="col-4">Message</label>
<input class="col-8" type="text" value="">
</div>
</div>
<div class="row">
<div class="form-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value="">
</div>
<div class="form-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value="">
</div>
<div class="form-group">
<label class="col-4">Message</label>
<input class="col-12" type="text" value="">
</div>
</div>
Note: inputs are void elements. They don't take closing tags.
With the below code, the summary and it's text box are aligned inline.But the text box for description is appearing below the label 'description'. How can I make it align inline? Please help me in resolving this.
.left {
width: 30%;
float: left;
text-align: right;
}
.right {
width: 65%;
margin-left: 10px;
float: left;
}
<body>
<div class="container">
<div class="row">
<div class="col-sm-5 col-sm-push-3 col-xs-4 col-xs-push-2">
<h1 class="text-center" style="font-family:Colonna MT;font-size:50px;color:ForestGreen ;">Post Your Ad</h1>
<form action="post" class="login-form">
<div>
<label class="left">Summary:</label>
<input class="right" type="text" name="Summary" size="50">
</div>
<div>
<label class="left">Description:</label>
<input class="right" type="text" name="Description" style="height:100px;width:400px">
</div>
</form>
</div>
</div>
</div>
Try like this: Demo
If you need multi line text box, use textarea instead of input type text
<div class="form-group row">
<label for="inputEmail3" class="col-md-6 col-sm-12 form-control-label text-right">Summary</label>
<div class="col-md-6 col-sm-12">
<input type="text" class="form-control" name="Summary" size="50">
</div>
</div>
<div class="form-group row">
<label for="inputEmail3" class="col-md-6 col-sm-12 form-control-label text-right">Description</label>
<div class="col-md-6 col-sm-12">
<textarea class="form-control" name="Description"></textarea>
</div>
</div>
Edit:
As you mentioned, its not form-control class, its form-control-label.. You can get more info here
Updated Demo: To change the height, try chnaging the value of rows="8" for <textarea>
If you're using Bootstrap (as the classes on your code suggest). I would recommend using their form layout options. Sounds like you want a Horizontal Form:
Docs
For Example:
<form class="form-horizontal">
<div class="form-group">
<label for="summary" class="col-sm-2 control-label">Summary</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="summary" />
</div>
</div>
<div class="form-group">
<label for="description" class="col-sm-2 control-label">Description</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="description" style="height:100px;width:400px" />
</div>
</div>
</form>
<input class="right" type="text" name="Description" style="height:100px;width:400px;">
Remove width from input of description, also please change to textarea, instead of input.
You have already used bootstrap framework.so that there is no need to write extra css for inline forms. follow the following code
<div class="form-group">
<label class="col-lg-3 control-label ">City</label>
<div class="col-lg-9">
<div class="form-inline">
<div class="form-group ">
<div class="col-lg-12">
<label class="sr-only" for="city">City</label>
<input type="text" id="city" name="city" class="form-control " placeholder="E.g. Thrissur" >
</div>
</div>
<div class="form-group ">
<div class="col-lg-12">
<label class="sr-only" for="county">County</label>
<input type="text" id="county" name="county" class="form-control " placeholder="E.g. India" >
</div>
</div>
</div>
</div>
</div>
Like this?
https://jsfiddle.net/d6tscd18/
.left {
width: 30%;
display: inline-block;
text-align: right;
}
.right {
width: 65%;
margin-left: 10px;
display: inline-block;
}
try this:
<html>
<head>
<title>sample</title>
<style type="text/css">
.left {
width: 30%;
display: inline-block;
text-align: right
}
.right {
margin-left: 10px;
display: inline-block;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-5 col-sm-push-3 col-xs-4 col-xs-push-2">
<h1 class="text-center" style="font-family:Colonna MT;font- size:50px;color:ForestGreen ; text-align: center">Post Your Ad</h1>
<form action="post" class="login-form">
<div>
<label class="left">Summary:</label>
<input class="right" type="text" name="Summary" size="50" style="width:400px">
</div>
<div>
<label class="left">Description:</label>
<input class="right" type="text" name="Description" style="height:100px;width:400px">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
If you have trouble getting it in place you can also force a table layout without using tables:
.login-form {
display: table;
}
.login-form>div {
display: table-row;
}
.cell {
display:table-cell;
vertical-align: top;
}
...
Adding a div around the inputs and a "cell" class (and shouldn't it be a textarea?):
...
<form action="post" class="login-form">
<div>
<label class="left cell">Summary:</label>
<div class="cell">
<input class="right" type="text" name="Summary" size="50">
</div>
</div>
<div>
<label class="left cell">Description:</label>
<div class="cell">
<textarea class="right" name="Description" style="height:100px;width:400px"></textarea>
</div>
</div>
</form>
...
Check it here
Remove the style attribute from the description input type, it will then appear as the summary input box.
<form action="post" class="login-form">
<div>
<label class="left">Summary:</label>
<input class="right" type="text" name="Summary" size="50" style="width:400px">
</div>
<div>
<label class="left">Description:</label>
<input class="right" type="text" name="Description">