This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 1 year ago.
I'm trying to center my form inside a div but the methods I'm using are not working. I tried using the justify-content-center attribute as stated by the bootstrap documentation but it is not centering it. When I try to center it but applying display: flex and justify-content: center in my CSS on the box it's wrapped in the form losses its orientation and messes up. I can't figure out how to center it, where did I go wrong. Any help is appreciated. Thanks in advance.
.onlineRefilFormCon {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 50%;
width: 100%;
height: 90%;
}
.onlineRefillTitleCon {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 10%;
}
.onlineRefillForm {
position: absolute;
top: 20%;
width: 40%;
height: 80%;
background-color: white;
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}
.form-group {
padding-top: 5%;
}
.col-form-label {
font-weight: 600;
}
.form-check-input {
border: 2px solid;
}
.form-control {
width: 60%;
border: 2px solid;
}
.textarea {
margin-top: -10%;
margin-left: 17%;
border: 2px solid;
}
.radio1 {
margin-top: 3%;
margin-left: 20%;
}
.form-check-inline {
padding: 0%;
}
.OnlineSubmitBtnCon {
position: absolute;
bottom: 7%;
width: 100%;
height: 10%;
}
.OnlineSubmitBtnCon button {
margin-left: 35%;
}
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<div class="onlineRefilFormCon">
<div class="onlineRefillTitleCon">
<h2 class="onlineRefillTitle">Online Refill</h2>
</div>
<form class="onlineRefillForm" id="onlineRefillForm">
<div class="form-group row justify-content-center">
<label for="text-input" class="col-2 col-form-label">First Name</label>
<div class="col-10">
<input class="form-control" type="text" value="" id="text-input">
</div>
</div>
<div class="form-group row justify-content-center">
<label for="text-input" class="col-2 col-form-label">Last Name</label>
<div class="col-10">
<input class="form-control" type="text" value="" id="text-input">
</div>
</div>
<div class="form-group row justify-content-center">
<label for="date-input" class="col-2 col-form-label">Date</label>
<div class="col-10">
<input class="form-control" type="date" value="yyyy-mm-dd" id="date-input">
</div>
</div>
<div class="form-group row justify-content-center">
<label for="email-input" class="col-2 col-form-label">Email</label>
<div class="col-10">
<input class="form-control" type="email" value="" id="email-input">
</div>
</div>
<div class="form-check-inline radio1">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios1"
value="option1" checked>
Pickup
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios2"
value="option2">
Delivery
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="optionsRadios" id="optionsRadios2"
value="option2">
Mail
</label>
</div>
<div class="form-group justify-content-center">
<label for="textarea" class="col-2 col-form-label">Refill Details</label>
<textarea class="form-control textarea" rows="7" placeholder="Example: Lisinopril"></textarea>
</div>
<div class="OnlineSubmitBtnCon">
<button type="button" class="btn btn-primary contactUsSubmitBtn" onclick="proccessContactMessage()">Submit</button>
</div>
</form>
</div>
If you want to align the form elements center, you should use text-align:center style. So you should add text-center bootstrap class into the elements which you want to align center.
Does it work for you?
Any chance it's because the link to the bootstrap styles is broken? When I tried the example code the url failed so I uploaded a new cdn link and the form appears centered.
.onlineRefilFormCon {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
font-weight: 600;
}
.onlineRefillForm {
display: flex;
flex-direction: column;
width: 40%;
padding: 48px;
background-color: white;
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
}
.form-group {
padding-top: 5%;
}
.form-check-input {
border: 2px solid;
}
.form-control {
width: 60%;
border: 2px solid;
}
.textarea {
margin-top: -10%;
margin-left: 17%;
border: 2px solid;
}
.radio1 {
margin-top: 3%;
margin-left: 20%;
}
.form-check-inline {
padding: 0%;
}
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.1/dist/js/bootstrap.min.js"></script>
<div class="onlineRefilFormCon">
<form class="onlineRefillForm" id="onlineRefillForm">
<label for="text-input">First Name </label>
<input type="text" value="" id="text-input" />
<br>
<label for="text-input">Last Name</label>
<input type="text" value="" id="text-input" />
<br>
<label for="date-input">Date</label>
<input type="date" value="yyyy-mm-dd" id="date-input" />
<br>
<label for="email-input">Email</label>
<input type="email" value="" id="email-input" />
<br>
<label>Pickup
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked /></label>
<br>
<label>Delivery
<input type="radio" name="optionsRadios" id="optionsRadios2" value="option2" /></label>
<br>
<label>Mail
<input type="radio" name="optionsRadios" id="optionsRadios2" value="option2" /></label>
<br>
<label for="textarea" class="col-2 col-form-label">Refill Details</label>
<textarea rows="7" placeholder="Example: Lisinopril"></textarea>
<br>
<button type="button" class="btn btn-primary contactUsSubmitBtn" onclick="proccessContactMessage()">
Submit
</button>
</form>
</div>
Applying the style
margin-left:auto;margin-right:auto;
Will also center anything for future reference
Related
I have created both a select-box and a searchbar in html. Now I want to place both elements next to each other. I thought this would be possible by placing both elements into another div-container with display set to inline. But this doesn't really solve the issue.
.select-search-container {
display: inline;
}
.select-box {
display: flex;
flex-direction: column;
width: 400px;
}
.search-bar {
max-width: 400px;
border: none;
position: relative;
box-sizing: border-box;
left: calc(98% - 400px);
top: 20px;
}
#searchBarInput {
background: transparent;
padding: 12px 24px;
border-radius: 8px;
border: 2px solid black;
width: 100%;
box-sizing: border-box;
}
.search-bar__icon {
position: absolute;
height: 100%;
left: 90%;
top: 25%;
}
<div class="select-search-container">
<div class="select-box">
<div class="options-container">
<div class="option">
<input type="radio" class="radio" name="category" id="apples">
<label for="apples">apples</label>
</div>
<div class="option">
<input type="radio" class="radio" name="category" id="bananas">
<label for="bananas">bananas</label>
</div>
<div class="option">
<input type="radio" class="radio" name="category" id="lemons">
<label for="lemons">lemons</label>
</div>
</div>
<div class="selected">
Pick a fruit
</div>
</div>
<div class="search-bar" id="searchBar">
<input type="text" placeholder="Search Fruit" id="searchBarInput">
<i class="material-icons search-bar__icon">search</i>
</div>
</div>
As you can see, I have created the select-box using radio-buttons. I like the styling of this better than a normal select-box. Basically, I am showing the selected-div all the time and upon clicking on this div, I show the options-container. The display of the select-box is set to flex, with flex-direction column, as I want the options-container to be placed below the selected-div:
I am trying to place the searchbar next to the select-box. For this purpose I am also playing around with the left-, top- etc. specifiers. It doesn't really work though. When changing the display-type of the wrapper from inline to inline-flex, it kind of works, but the size of my searchbar shrinks and the icon isn't placed where I want it to be. I know this is a very specific question. I am very surprised that it doesn't work though and would really appreciate an explanation.
.select-search-container {
display: flex;
justify-content: space-between;
}
.select-box {
}
.search-bar {
max-width: 400px;
border: none;
position: relative;
box-sizing: border-box;
top: 20px;
}
#searchBarInput {
background: transparent;
padding: 12px 24px;
border-radius: 8px;
border: 2px solid black;
width: 100%;
box-sizing: border-box;
}
.search-bar__icon {
position: absolute;
height: 100%;
left: 90%;
top: 25%;
}
<div class="select-search-container">
<div class="select-box">
<div class="options-container">
<div class="option">
<input type="radio" class="radio" name="category" id="apples">
<label for="apples">apples</label>
</div>
<div class="option">
<input type="radio" class="radio" name="category" id="bananas">
<label for="bananas">bananas</label>
</div>
<div class="option">
<input type="radio" class="radio" name="category" id="lemons">
<label for="lemons">lemons</label>
</div>
</div>
<div class="selected">
Pick a fruit
</div>
</div>
<div class="search-bar" id="searchBar">
<input type="text" placeholder="Search Fruit" id="searchBarInput">
<i class="material-icons search-bar__icon">search</i>
</div>
</div>
I have a small issue here with aligning three input fields in the same row with equal space between each other.
I tried to do it with display: flex and space-between but then the label is placed left to the input and not on-top.
What I want to achieve is as followed:
Then if there is more inputs I want it to "break a row" and start a new 3 input row.
How can I achieve it ?
Code snippet:
/*Stage 3 input group*/
.dInput{
margin-top: 15px;
}
.dInput label {
display: block;
padding: 6px;
color: #f8f9fa;
}
.dInput input {
padding: 5px;
margin-left: 5px;
width: 33%;
background-color: #495057;
border-radius: 4px;
}
<div class="dInput" v-if="activeStage == 3">
<label for="name">Username*</label>
<input type="text" id="name">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
I think this is what you want. I hope my code is helpful to you.
/*Stage 3 input group*/
.dInput{
display:flex;
justify-content:space-between;
align-items:center;
width:100%;
margin-top: 15px;
}
.dInput label {
text-align:center;
display: block;
padding: 6px;
color: #000;
}
.dInput input {
padding: 5px;
margin-left: 5px;
background-color: #495057;
border-radius: 4px;
}
<div class="dInput" v-if="activeStage == 3">
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
</div>
<div class="dInput" v-if="activeStage == 3">
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
<div><label for="name">Username*</label>
<input type="text" id="name"></div>
</div>
display: flex implementation
.dInput {
margin-top: 15px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.input-wrapper {
display: flex;
flex-direction: column;
max-width: 33%;
flex: 33%;
}
.dInput label {
display: block;
padding: 6px;
/* color: #f8f9fa; */
}
.dInput input {
padding: 5px;
margin-left: 5px;
width: 33%;
background-color: #495057;
border-radius: 4px;
}
<div class="dInput">
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="input-wrapper">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
</div>
With css grid :
/*Stage 3 input group*/
.dInput{
margin-top: 15px;
display: grid;
grid-template-columns: repeat(3, 1fr);
align-items: center;
justify-content: space-between;
justify-items: center;
background-color: violet;
gap: 1em;
padding: 1em;
}
.dInput label {
display: block;
color: #f8f9fa;
}
.dInput input {
padding: 5px;
background-color: #495057;
border-radius: 4px;
max-width: 8em;
margin-top: 1em;
}
.dInput-item {
justify-self: center;
text-align: center;
}
<div class="dInput" v-if="activeStage == 3">
<div class="dInput-item">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="dInput-item">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="dInput-item">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div class="dInput-item">
<label for="name">Username*</label>
<input type="text" id="name">
</div>
</div>
Wrap each label and input in a div
<div class="dInput">
<div>
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div>
<label for="name">Username*</label>
<input type="text" id="name">
</div>
<div>
<label for="name">Username*</label>
<input type="text" id="name">
</div>
</div>
css:
.dInput{
margin-top: 15px;
display:flex;
justify-content: space-between;
}
I have a form with a checkbox section. The last option is a text field for a custom entry, but I can't get the field onto the same line as the checkbox.
Picture for reference
HTML (I included the option before it as well for reference):
<li>
<label class="container">Legal
<input id="legal" name="legal" type="checkbox" value="1">
<span class="checkmark" class="square"></span>
</label>
</li>
<li>
<label class="container">
<input id="other" name="other" type="checkbox" value="1">
<span class="checkmark" class="square"></span>
</label>
<input id="otherText" maxlength="40" name="otherText" size="10" type="text" placeholder="Other...">
<span class="bar" style="width: 150px;"></span>
</li>
CSS:
#otherText{
font-size:.9em;
width: 150px;
display:block;
border:none;
color: #fff;
background-color: transparent;
border-bottom:1px solid #757575;
}
#otherText:focus { outline:none; }
<li>
<input id="legal" name="legal" type="checkbox" value="1">
<label class="container">Legal
<span class="checkmark" class="square"></span>
</label>
</li>
<li>
<label class="container">
<input id="other" name="other" type="checkbox" value="1">
<span class="checkmark" class="square"></span>
</label>
<input id="otherText" maxlength="40" name="otherText" size="10" type="text" placeholder="Other...">
<span class="bar" style="width: 150px;"></span>
</li>
CSS -
li {
display: flex;
}
Does this help?
Try display:inline, rather than display:block for your #otherText input. Check out this page for info about display options.
To me it looks as if inline-block will make a difference, and so did removing your color:#fff line:
#otherText{
font-size:.9em;
width: 150px;
display:inline-block;
border:none;
background-color: transparent;
border-bottom:1px solid #757575;
}
#otherText:focus { outline:none; }
<ul>
<li>
<label class="container">Legal
<input id="legal" name="legal" type="checkbox" value="1">
<!--span class="checkmark" class="square"></span-->
</label>
</li>
<li>
<label class="container">
<input id="other" name="other" type="checkbox" value="1">
<!--span class="checkmark" class="square"></span-->
</label>
<input id="otherText" maxlength="40" name="otherText" size="10" type="text" placeholder="Other...">
<span class="bar" style="width: 150px;"></span>
</li>
</ul>
As I did not see any merrit in your <span class="checkmark"> I commented them out out .
The problem was with the "container" class. It was set to display: block.
To solve, I removed the "container" class from the "other" option, and assigned it a unique id with all of the same styling except for display: block.
OP eventually found the solution on their own, but I would like to add something for future reference. I often in time see Flexbox being overlooked.
Flexbox is a great tool for exactly these kind of problems, which rely on grid. I would encourage everyone to read up on Flexbox, especially these properties:
flex-direction
flex-wrap
flex-grow
flex-shrink
flex-basis
justify-content
order
If you master these 7 properties, then grid troubles will be something of the past.
Pair it with max-width properties, padding, margin and what else you'd need to make the grid fit your design.
Often at times, CSS frameworks, such as Bootstrap, can be quite overkill if the project is of a small nature. CSS frameworks should only be justified if you can utilize multiple aspects of it over a larger project. Using Bootstrap for grid alone is not ideal. With that said, Bootstrap does offer a very powerful grid layout with the use of the classes row and col. It's worth reading up on.
Example of using flex with the current problem at hand:
HTML:
<div class="row container">
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="1" />
<span class="cb-text">Dev Team</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="2" />
<span class="cb-text">Product Manager</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="3" />
<span class="cb-text">Security / SecOps</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="4" />
<span class="cb-text">DevOps / CI/CD</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="5" />
<span class="cb-text">Legal</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="6" />
<input type="text" class="cb-text other-text" placeholder="Other..." />
</div>
</div>
CSS:
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
flex: 0 0 100%;
max-width: 100%;
}
.container {
width: 650px;
height: 180px;
background-color: #4F4F4F;
padding: 15px;
justify-content: space-between;
margin-left: auto;
margin-right: auto;
font-size: 25px;
color: #FFFFFF;
}
.cb-container {
flex: 0 0 45%;
max-width: 45%;
padding-left: 15px;
padding-right: 15px;
justify-content: flex-start;
align-items: center;
gap: 8px;
}
.cb-element {
display: flex;
flex-direction: column;
flex: 0 0 25px;
max-width: 25px;
height: 25px;
}
input.other-text {
display: flex;
flex-direction: column;
flex: 0 0 65%;
max-width: 65%;
color: #FFFFFF;
font-size: 25px;
border-top: none;
border-left: none;
border-right: none;
border-bottom: 3px solid #B3B3B3;
background-color: transparent;
outline: none;
}
Snippet Example:
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
flex: 0 0 100%;
max-width: 100%;
}
.container {
width: 650px;
height: 180px;
background-color: #4F4F4F;
padding: 15px;
justify-content: space-between;
margin-left: auto;
margin-right: auto;
font-size: 25px;
color: #FFFFFF;
}
.cb-container {
flex: 0 0 45%;
max-width: 45%;
padding-left: 15px;
padding-right: 15px;
justify-content: flex-start;
align-items: center;
gap: 8px;
}
.cb-element {
display: flex;
flex-direction: column;
flex: 0 0 25px;
max-width: 25px;
height: 25px;
}
input.other-text {
display: flex;
flex-direction: column;
flex: 0 0 65%;
max-width: 65%;
color: #FFFFFF;
font-size: 25px;
border-top: none;
border-left: none;
border-right: none;
border-bottom: 3px solid #B3B3B3;
background-color: transparent;
outline: none;
}
<div class="row container">
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="1" />
<span class="cb-text">Dev Team</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="2" />
<span class="cb-text">Product Manager</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="3" />
<span class="cb-text">Security / SecOps</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="4" />
<span class="cb-text">DevOps / CI/CD</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="5" />
<span class="cb-text">Legal</span>
</div>
<div class="row cb-container">
<input type="checkbox" class="cb-element" value="6" />
<input type="text" class="cb-text other-text" placeholder="Other..." />
</div>
</div>
Codepen example here.
Useful links:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://getbootstrap.com/docs/4.0/layout/grid/
Hey, so I'm trying to build a movie review site which uses a database (I'm using PouchDB) and I want to style my admin/add a review page so that the inputs and labels are all aligned in a certain way. I keep adjusting widths, padding and margins but I just cant seem to get them to align. Can someone teach me how to align them the way i want them to be please?
This is how I want them to look:
body {
margin: 0;
font-family: georgia, arial;
background-color: #e0e0e0;
text-align: center;
}
.newFilm {
text-align: left;
display: inline-block;
background-color: green;
width: 80%;
padding: 20px;
}
label {
padding: 6px;
text-align: center;
background: red;
}
.form {
display: flex;
text-align: center;
flex-direction: column;
}
input {
margin: 10px;
width: 40%;
background-color: yellow;
padding: 8px;
border: 2px;
}
<div class="newFilm">
<h2 id='formTitle'>Create new review</h2>
<div class="form">
<form>
<label for="title">Title:</label><input type="text" id="title">
<label for="genre">Genre:</label><input type="text" id="genre">
<label for="image">Image:</label><input type="text" id="image">
<label for="rating">Rating:</label><input type="number" id="rating">
<label for="synopsis">Synopsis:</label><input type="text" id="synopsis">
<label for="trailer">Trailer:</label><input type="text" id="trailer">
<input type="hidden" id="id">
<button id="modifyFilmButton">Post review!</button>
</form>
</div>
</div>
you can use either flex or grid or both. I've used both just as an example. you could also use only grid and nest a grid within a grid.
body {
margin: 0;
font-family: georgia, arial;
background-color: #e0e0e0;
text-align: center;
}
.newFilm {
background-color: green;
max-width: 100%;
padding: 10%;
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 30px;
}
#formTitle {
grid-column: 1/3;
}
#modifyFilmButton {
grid-column: 2/3;
width: 50%;
justify-self: end;
}
.left {
display: flex;
justify-content: center;
flex-direction: column;
text-align: left;
}
.right {
display: flex;
flex-direction: column;
text-align: left;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>temp</title>
<link rel="stylesheet" type="text/css" href="temp.css" />
</head>
<body>
<div class="newFilm">
<h2 id="formTitle">Create new review</h2>
<div class="left">
<label for="title">Title:</label><input type="text" id="title" />
<label for="image">Image:</label><input type="text" id="image" />
<label for="trailer">Trailer:</label><input type="text" id="trailer" /> <input type="hidden" id="id" />
<label for="rating">Rating:</label><input type="number" id="rating" />
</div>
<div class="right">
<label for="genre">Genre:</label><input type="text" id="genre" />
<label for="synopsis">Synopsis:</label><input type="text" id="synopsis" />
</div>
<button id="modifyFilmButton">Post review!</button>
</div>
<script src="temp.js"></script>
</body>
</html>
Modified your html a little to add some structure.
I would not recommend using this code, look into bootstrap tables, or something of that nature. It will look better, and be a more scaleable solution.
.newFilm {
background-color: red;
padding: 20px;
border-radius: 20px;
}
textarea,
input,
label {
display: block
}
textarea,
input {
width: 200px;
}
.form {
width: 100%;
}
.left,
.right {
width: 49%;
display: inline-block;
vertical-align: top;
}
<div class="newFilm">
<h2 id='formTitle'>Create new review</h2>
<div class="form">
<form>
<div class="left">
<label for="title">Title:</label>
<input type="text" id="title">
<label for="genre">Genre:</label>
<input type="text" id="genre">
<label for="image">Image:</label>
<input type="text" id="image">
<label for="rating">Rating:</label>
<input type="number" id="rating">
</div>
<div class="right">
<label for="synopsis">Synopsis:</label>
<input type="text" id="synopsis">
<label for="trailer">Trailer:</label>
<textarea id="trailer" rows="5"></textarea>
<input type="hidden" id="id">
<button id="modifyFilmButton">Post review!</button>
</div>
</form>
</div>
</div>
Responsive solution:
.myForm {
background-color: red;
padding: 20px;
border-radius: 20px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid myForm">
<div class="row">
<div class="col-lg"><h1>Create New Review</h1></div>
</div>
<form>
<div class="row">
<div class="col-md"> <!-- left column -->
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label>Genre</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label>Image</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label>Rating</label>
<input type="text" class="form-control">
</div>
</div>
<div class="col-md"> <!-- right column -->
<div class="form-group">
<label>Synopsis</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label>Trailer</label>
<textarea class="form-control"></textarea>
</div>
<input class="btn btn-primary" type="submit" value="Submit" disabled>
</div>
</div>
</form>
</div>
Adjust the left/right columns to have your desired effect.
See: https://getbootstrap.com/docs/4.1/layout/grid/
Well there are couple ways to do that.
My best option would be to have one big div that would hold two smaller divs.
Then give those two divs width of 50% each - i have used grid for that.
After that you want to make sure that label and input are taking full width of their container.
Like that you will achieve most of what you want. Also for Synopsis you should use textarea not input.
Here is my example on codepen.
Here is what I did
body {
margin: 0;
font-family: georgia, arial;
background-color: #e0e0e0;
text-align: center;
}
.newFilm {
text-align: left;
display: inline-block;
background-color: green;
width: 80%;
padding: 20px;
}
label {
padding: 6px;
color: #fff;
display: block;
text-align: left;
}
.form {
display: flex;
text-align: center;
flex-direction: column;
}
.row {
display: grid;
grid-template-columns: 1fr 1fr;
}
.col {
padding: 20px;
}
input,
textarea {
width: 100%;
display: block;
background-color: yellow;
padding: 8px;
border: 2px;
}
#modifyFilmButton {
float: right;
}
<div class="newFilm">
<h2 id='formTitle'>Create new review</h2>
<div class="form">
<form>
<div class="row">
<div class="col">
<label for="title">Title:</label><input type="text" id="title">
<label for="image">Image:</label><input type="text" id="image">
<label for="trailer">Trailer:</label><input type="text" id="trailer">
<label for="rating">Rating:</label><input type="number" id="rating">
</div>
<div class="col">
<label for="genre">Genre:</label><input type="text" id="genre">
<label for="synopsis">Synopsis:</label>
<textarea rows="9" id="synopsis"></textarea>
</div>
</div>
<input type="hidden" id="id">
<button id="modifyFilmButton">Post review!</button>
</form>
</div>
</div>
I want to create this form in css
HTML
<div class="contact__right">
<form action="" class="contact__form">
<div>
<label for="name" class="contact__form-label">Name</label>
<input type="text" id="name" class="contact__form-input">
</div>
<div>
<label for="email" class="contact__form-label">Email</label>
<input type="text" id="email" class="contact__form-input">
</div>
<div>
<label for="phone" class="contact__form-label">Phone</label>
<input type="text" id="phone" class="contact__form-input">
</div>
<div>
<label for="message" class="contact__form-label">Message</label>
<textarea name="message" id="message" cols="30" rows="10" class="contact__form-textarea"></textarea>
</div>
</form>
</div>
CSS
.contact {
&__form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 105px 95px;
color: $white;
}
&__form-input,
&__form-textarea {
width: 300px;
height: 40px;
background: transparent;
border: 1px solid white;
border-radius: 2px;
margin-bottom: 25px;
}
&__form-textarea {
height: 150px;
}
&__form-label {
padding-right: 70px;
}
}
What I'm struggling with is alignment of inputs with labels and of textarea at the same line vertically.
textarea always sticks out at the side, because of its label which has longer text than the rest.
All the help will be appreciated.
I would not use a flexbox for the form, but for each <div> inside the form. The labels are vertically centered for each input field, except for the textarea. There the label is vertically aligned at the top. You can play with top padding there when desired.
* {
box-sizing: border-box;
}
.contact__right {
background-color: gray;
}
.contact__form {
padding: 105px 95px;
width: 100%;
color: white;
}
.contact__form>div {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 25px;
}
.contact__form-input,
.contact__form-textarea {
width: 300px;
height: 40px;
background: transparent;
border: 1px solid white;
border-radius: 2px;
}
.contact__form-textarea {
height: 150px;
}
label[for="message"].contact__form-label {
align-self: flex-start;
}
<div class="contact__right">
<form action="" class="contact__form">
<div>
<label for="name" class="contact__form-label">Name</label>
<input type="text" id="name" class="contact__form-input">
</div>
<div>
<label for="email" class="contact__form-label">Email</label>
<input type="text" id="email" class="contact__form-input">
</div>
<div>
<label for="phone" class="contact__form-label">Phone</label>
<input type="text" id="phone" class="contact__form-input">
</div>
<div>
<label for="message" class="contact__form-label">Message</label>
<textarea name="message" id="message" cols="30" rows="10" class="contact__form-textarea"></textarea>
</div>
</form>
</div>
Add following css code and your problem will be solved.
.contact__form-label {
vertical-align: top;}
Add this CSS rule for label:
label {
width: 100px;
display: inline-block;
vertical-align: top;
}
(otherwise label will be treated as an inline-element. This way you give it a fixed width and align it to the top)
If you don't like the alignment at the very top, you can add a padding-top ike in the following snippet:
form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 105px 95px;
color: $white;
}
input,
textarea {
width: 300px;
height: 40px;
background: transparent;
border: 1px solid white;
border-radius: 2px;
margin-bottom: 25px;
}
textarea {
height: 150px;
}
label {
width: 100px;
display: inline-block;
vertical-align: top;
padding-top: 14px;
}
.contact__right {
background: #ccc;
}
<div class="contact__right">
<form action="" class="contact__form">
<div>
<label for="name" class="contact__form-label">Name</label>
<input type="text" id="name" class="contact__form-input">
</div>
<div>
<label for="email" class="contact__form-label">Email</label>
<input type="text" id="email" class="contact__form-input">
</div>
<div>
<label for="phone" class="contact__form-label">Phone</label>
<input type="text" id="phone" class="contact__form-input">
</div>
<div>
<label for="message" class="contact__form-label">Message</label>
<textarea name="message" id="message" cols="30" rows="10" class="contact__form-textarea"></textarea>
</div>
</form>
</div>
ยด
You are gonna have to do that manually I guess. Normally websites tend to break line after the labels and bring the input fields to the next line.