This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Center a column using Twitter Bootstrap 3
(34 answers)
How can I horizontally center an element?
(133 answers)
How do I center floated elements?
(12 answers)
Closed 2 years ago.
I am trying to build a small ToDo App. I am trying to place input field and submit button in the centre, but alignment going to left side.
#todo {
align-items: center;
text-align: center;
}
#todo .form-group {
align-items: center;
margin: auto;
}
#id-button {
margin: 10px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js">
<div id="todo">
<p>
<h3>Add New ToDo</h3>
<div class="form-group">
<div class="col-xs-4">
<input type="text" class="form-control" placeholder="Add New ToDo Item">
<button id="id-button" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
JSFiddle
Any suggestions please?
you can do something like this:
When you looking for centering the contents, then CSS has a beautiful class called flex and one of the property called justify-content:center; which will take care of the alignment.
More on Justify-content
add this:
#todo .form-group {
display: flex;
margin: 0 auto;
justify-content: center;
}
#todo {
align-items: center;
text-align: center;
}
#todo .form-group {
display: flex;
margin: 0 auto;
justify-content: center;
}
#id-button {
margin: 10px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<div id="todo">
<p>
<h3>
Add New ToDo
</h3>
<div class="form-group">
<div class="col-xs-4">
<input type="text" class="form-control" placeholder="Add New ToDo Item">
<button id="id-button" type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
</div>
You could either modify the css of the .form-group to the below as correctly pointed by below user. Justify-content is the proper way to solve it.
#todo .form-group {
display: flex;
margin: 0 auto;
width: 100%;
justify-content: center;
}
But since you're using bootstrap as a css framework. You could also add <div class="col-xs-4"></div> in the start of <div class="form-group"> as the first child. Refer Bootstrap docs for grid structure. Bootstrap Docs
form {
margin-left: 25%;
margin-right:25%;
width: 50%;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js">
<div id="todo">
<p>
<h3>Add New ToDo</h3>
<div class="form-group">
<div class="col-xs-4">
<form>
<input type="text" class="form-control" placeholder="Add New ToDo Item">
<button id="id-button" type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
I have a few notices on your code:
your <p> tag has no closing.
try removing the "form-group" <div> and class="col-xs-4".
resize the input and out a <br> tag.
your code would be as you desired.
the col-xs-4 and (I think) form-group are Bootstrap classes that override whatever you are trying to do. So, either wrap your code in different <div> or override it in your CSS. Also if you want to use the col class you have to wrap that div in another <div> with "row" class.
Related
This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 1 year ago.
I'm trying to make a search box on left end and arrows on right end. Float is not working and the arrows are coming very close to search box. Also tried left:0px and right:0px. Could anyone help with this? Thank you.
.candidate {
display: flex;
width: 100%;
}
.candidate-search {
width: 50%;
float: left;
}
.pagearrow {
display: flex;
float: right;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="col-xl-7 col-lg-7 col-md-7">
<div class="conatiner-fluid">
<div class="row">
<div class="candidate">
<div class="candidate-search">
<input class="filter-searchbox py-2" type="search" name="search" placeholder="Search" />
</div>
<div class="pagearrow">
<span class="material-icons">arrow_back_ios</span>
<span class="material-icons">arrow_forward_ios</span>
</div>
</div>
</div>
</div>
</div>
Float property is not working on flex box property. You can try to use
justify-content to align elements
Example:
justify-content: space-between;
Exactly as the title says, except labels are not placed fully below them. i'm just making a login page over bootstrap and it turns out labels and inputs styled with bootstrap are the source of this problem. I checked if there could be any typo or misfollowed bootstrap doc on this, but it wasn't that as far as i'm aware. I think this info is enough to come up with an answer, if not so, please let me know.
By the by, i'm using django for the backend.
NAVIGATOR: Google Chrome
GOAL: Labels are fully displayed and correctly aligned beside their respectives inputs.
RENDERED TEMPLATE IMAGE
RENDERED TEMPLATE CODE
<html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/accounts/css/login.css">
<link rel="stylesheet" href="/static/css/bootstrap.css">
<script defer="" src="/static/js/jquery.min.js"></script>
<script defer="" src="/static/js/bootstrap.js"></script>
</head>
<body cz-shortcut-listen="true">
<div class="login-area my-5 border-dark border rounded">
<div class="image-area border border-top-0">
<img src="/static/accounts/images/user.svg" alt="">
</div>
<form class="" method="POST">
<input type="hidden" name="csrfmiddlewaretoken" value="FKM9D1moYiiOXKvq1gedhgYfa6A5I5ZYQTdSOiY1uBKaHLicQAN1PXZR4GvsgdAL">
<div class="form-group row mb-3 mt-5">
<label for="#username-field" class="col-form-label col-2">Username</label>
<div class="col-10">
<input type="text" class="form-control" id="#username-field" name="username">
</div>
</div>
<div class="form-group row my-3">
<label for="#password-field" class="col-form-label col-2">Password</label>
<div class="col-10">
<input type="password" id="#password-field" class="form-control" name="password">
</div>
</div>
<div class="d-flex justify-content-center my-5 ">
<button class="btn btn-primary font-weight-bold py-2 px-4">Submit</button>
</div>
</form>
<div class="links-area my-4">
Forgot your password?
New here?
</div>
</div>
</body></html>
CSS
html {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body, body * {
-webkit-box-sizing: inherit;
box-sizing: inherit;
}
body {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.login-area {
padding: 20px;
}
.login-area .image-area {
width: 300px;
height: 300px;
}
.login-area .image-area img {
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
-o-object-position: center;
object-position: center;
}
Your labels are being clipped because you fixed their width by using col-2. Use col-auto instead so the width is determined by its content. You've also fixed the width on the input div with col-10. Change this to col to span the remaining width of the row.
<div class="form-group row mb-3 mt-5">
<label for="#username-field" class="col-auto col-form-label">Username</label>
<div class="col">
<input type="text" class="form-control" id="#username-field" name="username">
</div>
</div>
Hope this helps.
How can I center 2 buttons vertically in my view?
For example:
<div class="container">
<div class="row">
<div class="col-md-5">
<h4>My List</h4>
<div style="height:400px; overflow-y:auto">
<ul>
// List of checkboxes
</ul>
</div>
</div>
<div class="col-md-2">
<input type="button" id="btnAddField" class="btn btn-primary" value="Add" />
<input type="button" id="btnRemoveField" class="btn btn-primary" value="Remove" />
</div>
<div class="col-md-5">
<h4>Selected Fields</h4>
// This will contain the list selected from the checkboxes.
</div>
</div>
</div>
I want the buttons in column 2 to be centered in relation to column 1 (which in this case I made height of 400px.
Try css Line Height. Hope it will help.
vertical-align: middle;
line-height: 400px;
If you can use flexbox, something like this would do what you want (using your existing classes):
.row {
display: flex;
[class^="col"] {
float: none;
flex-shrink: 0;
margin: auto 0;
}
}
You can add new class on row that will use Flexbox and vertically center items with align-items: center when width is > 992px which is breakpoint of md DEMO
#media(min-width: 992px) {
.flex-row {
display: flex;
align-items: center;
}
}
This question already has answers here:
Floating elements within a div, floats outside of div. Why?
(11 answers)
Closed 6 years ago.
I have a div which I'm trying to fill with a background color. It contains a form. However, the div, and therefore the color, doesn't wrap around the form. How do I fix this?
#signUpForEarlyAccess {
float: right;
background-color: #F8F8F8;
}
.fully-centered {
position: relative;
top: 50%;
transform: translateY(50%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-xs-6" style="background-color: grey">
Irrelevant div
</div>
<div class="col-xs-6 text-center" id="signUpForEarlyAccess">
<div class="fully-centered">
<h3 contenteditable="true">Sign Up for Early Access</h3>
<h5>Enter your email to be one of the first to try!</h5>
<div class="form-group input-group">
<input id="emailForm" placeholder="Please enter an email address" type="email" class="form-control">
<span class="input-group-btn">
<button type="submit" class="btn btn-success">Sign up</button>
</span>
</div>
</div>
JSFiddle
https://jsfiddle.net/02uLtz0w/2/
I think you want something like this
#signUpForEarlyAccess {
float: right;
background-color: #F8F8F8;
border:1px solid red;
display:flex;
align-items:center;
justify-content:center;
align-content:center;
height:250px;
}
this height is just to show you that it is vertically centered ok ? Modify it as you wish.
I am attempting to vertically center a search bar in my websites navigation. I've tried adding a div around the search element and adding text-center to it. I have also tried margin: 0px auto !important;. None of which seemed to have any effect on the element.
I have created a bootsnipp
http://bootsnipp.com/snippets/E7WgX
Try to this
.top-header {
background-color: #D9D9D9;
}
.media-body{
vertical-align: middle !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="row top-header">
<div class="container">
<div class="media">
<div class="media-left">
<img class="media-objects" src="https://placeholdit.imgix.net/~text?txtsize=30&txt=320%C3%97240&w=235&h=126" alt="logo image" />
</div>
<div class="media-body">
<div class="input-group">
<input type="text" class="form-control input-lg" placeholder="Search" />
<span class="input-group-btn">
<button class="btn btn-info btn-lg" type="button">
<i class="glyphicon glyphicon-search"></i>
</button>
</span>
</div>
</div>
</div>
</div>
</div>
Vertical align is a difficult topic and the correct solution is very dependent on the used layout. If the snippet layout is what you use I would suggest adding these styles to columns. To get vertical-align: middle css working, you need to disable floating of columns and to keep them inline add display: inline-block. So apply to both columns class float-disable, then add vcenter to column containing search. Css styles looks like this:
.float-disable{
float: none;
display: inline-block;
}
.vcenter{
vertical-align: middle;
}