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.
Related
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.
This question already has answers here:
How can I horizontally center an element?
(133 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How can I center an absolutely positioned element in a div?
(37 answers)
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
Closed 4 years ago.
I've been trying my best to center a div that contains a form .form-group inside another div #myCarousel, responsively both vertically and horizontally. (works across different device widths)
I've tried absolute positioning with pixels, percentage - but that didn't work. I tried making the form a child element inside the #myCarousel, but I wasn't able to make it work.
My goal is to make the form in the exact center of the caraousel (over the image slider) and have the images sliding in background, while the form stays fixed in the center.
Here is what my code looks like. Codepen Link
<div id="myCarousel" class="carousel slide" data-ride="carousel">
...
</div>
<div class="form-group">
<form action="thank-you.php" method="post">
<input class="form-control" type="text" name="name" placeholder="Enter your name">
<input class="form-control" type="text" name="mobile" placeholder="Enter mobile number">
<input class="form-control" type="text" name="email" placeholder="Enter your email address">
<input class="form-control" type="text" name="address" placeholder="Enter your area and landmark">
<select id="multi-select" name="products[]" multiple="multiple">
<option value="product1">Product 1</option>
<option value="product2">Product 2</option>
<option value="product3">Product 3</option>
</select>
<input type="submit">
</form>
</div>
I'd be glad if you'll could guide me here.
Well first you will need to place the .form-group div inside of your #myCarouse div and then using position: absolute and transform trick you can align it vertically and horizontally center
HTML
<div id="myCarousel" class="carousel slide" data-ride="carousel">
...
<a class="right carousel-control" href="#myCarousel" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span><span class="sr-only">Next</span></a>
<div class="form-group">
...
</div>
</div>
CSS
#myCarousel .form-group {
width: 300px;
width: 300px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 10px;
}
Updated Codepen
Add this to your form-group class.
.form-group{
max-width: 300px;
margin: auto;
}
#myCarousel {
display: flex;
justify-content: center;
align-items: center;
}
Sticking <div class="form-group"> inside <div id="myCarousel"> should make .form-group vertically and horizontally centered inside #myCarousel.
CSS Tricks has a good overview
margin:0px auto or position :absolute;left:50%;top:50%; to center
You need to put .form-group inside #myCarousel.
.form-group {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Try this in your CSS:
.slider-container {
position: relative;
}
.form-group { # you should add and define css for custom class instead of overide Bootstrap class.
margin: auto;
position: absolute;
top: 50%;
left: 50%;
margin-left: -150px;
}
and HTML:
<div class="slider">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
.....
</div>
<div class="form-group">
<form></form>
</div>
</div>
Currently I'm creating a product box with two columns near each other, left one have the product image and right one product description.
The main problem is, I'm trying to make the product description the same height as the image, but after few tries I didn't get close to solve it, because it needs to be responsive. I tried a lot of solutions but still stuck, the closest one was using media queries and apply height to the .product-det but seems like it's not the good way.
I have create a Bootply as a demo.
The blue border must reach the bottom of the wrapper.
HTML
<div class="container">
<div class="col-xs-6">
<div class="col-xs-12 padding-15 margin-b-15 border-default padding-t-0 padding-b-0">
<div class="row">
<div class="col-xs-12">
<div class="row">
<div class="col-xs-4 col-sm-3 nopadding">
<img class="img-responsive" src="http://placehold.it/250x250">
</div>
<div class="col-xs-8 col-sm-9 border-l-default">
<div class="row"> <h4 class="col-xs-10 margin-t-15 text-ellipsis">Article pokeball superpotion lighting boltubertext pokemon pikachu</h4>
<div
class="col-xs-2 padding-t-15"> <span class="pointer pull-right">
<i class="glyphicon glyphicon-remove"></i>
</span>
</div>
<div class="product-det col-xs-12 line-h-35">
<div class="row">
<div class="col-xs-4">ITEMID</div>
<div class="col-xs-4">
<div class="input-group">
<input class="form-control" placeholder="qnt" type="text"> <span class="input-group-btn">
<button class="btn btn-secondary" type="button">
<span class="glyphicon glyphicon-refresh"></span>
</button>
</span>
</div>
</div>
<div class="col-xs-4"><span class="pull-right">3.203 €</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="">Right content</div>
</div>
CSS
.text-ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.padding-15 {
padding: 15px;
}
.padding-t-15 {
padding-top: 15px;
}
.margin-b-15 {
margin-bottom: 15px;
}
.border-default{
border: 1px solid #e0e0e0;
}
.padding-t-0 {
padding-top: 0px;
}
.padding-b-0 {
padding-bottom: 0px;
}
.nopadding {
padding: 0;
}
.line-h-35 {
line-height: 35px;
}
.border-l-default {
border-left: 1px solid blue;
}
Try the following:
Html:
<div class="row row-eq-height">
<!--The div columns you want them to be same height should be here-->
</div>
CSS:
/*
Row with equal height columns
*/
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
you can try javascript to do this. because bootstrap col is changing width overtime when you change your devices. I like it, it typeless and you can put the class in your image wrap and your product descript wrap like so:
var st = $('.squarethis').width();
$('.squarethis').css({'height':st+'px'});
$('.sameheight').css({'height':st+'px'});
.wrapper{
background:#ccc;
width:100%;
height:100%;
display:flex;
flex-wrap:wrap;
flex-direction:row;}
.prodImage{
width:30%;
background:red;}
.prodDesc{
width:70%;
background:green;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="prodImage squarethis"></div>
<div class="prodDesc sameheight"></div>
</div>
it will apply on your bootstrap. try to input this.
let me explain what happened:
$('.squarethis').css({'height':st+'px}); it will make your image always square, and it don't care what devices you using.
$('.sameheight').css({'height':st+'px'}); and this thing will make your next bootstrap col following the height of your previous col.
don't forget to input a class in your image wrapper and description wrapper
I'd suggest you to look at bootstrap v4 which comes with awesome features, like flexbox grid.
It does just what you need - example:
#foo{
background-color: aqua;
height: 300px;
}
#bar{
background-color: yellow;
}
<link href="https://cask.scotch.io/bootstrap-4.0-flex.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-sm-6" id="foo">foo</div>
<div class="col-sm-6" id="bar">bar</div>
</div>
</div>
JSFiddle
You can tune this cell (product description) using plain css. Just a little modify class .border-l-default:
.border-l-default {
border-left: 1px solid blue;
height: 100%;
position: absolute;
right: 0;
}
bootply
Try to change the text ellipsis like that, it is a bit rude because i didn't use bootstrap classes, but it works fine to me:
.text-ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin-bottom: 71px;
}
Try to set min-height in different mediaquery
.border-l-default {
border-left: 1px solid blue;
min-height: 135px;
This page has questions on the left, and radio buttons for ranking/scale answers on the right. It's designed so that if the question text is too long too fit in the left pane it just keeps going and pushes the radio buttons down a line beneath the question text. I emphasized if because I want to keep it that way, so it only moves if the text is long, I don't want to set something fixed so that every question does the same thing.
The problem is, the N/A radio button doesn't adjust like the other radio buttons. Is there a way to force it to move down with the others? I've tried surrounding the two sections with a div with vertical align bottom but that didn't work. Maybe I am doing something wrong but whatever I've read with regards to vertically aligning inner divs doesn't seem to accomplish this. Is it possible to fix this?
https://jsfiddle.net/x5y49d0n/
.qg {
width: 700px; /* this is only to demonstrate a smaller screen */
padding-top: 10px;
margin-top: 0;
}
.qg-row {
margin-left: 10px;
margin-bottom: 10px;
overflow: auto;
}
.qg-head {
padding-top: 5px;
padding-bottom: 5px;
background-color: #fcfcfc;
}
[class*="qg-guide"] {
font-size: 11px;
min-height: 10px;
text-align: center;
float: left;
}
[class*="qg-right-section"] {
float: right;
width: 500px;
}
.qg-right-section-1 {
width: 60px;
}
.qg-right-section-5 {
width: 300px;
}
[class*="qg-guide-"] {
word-wrap: break-word;
}
.qg-guide-1 {
empty-cells: hide;
width: 100%;
min-width: 100%;
}
.qg-guide-5 {
/* replicate in mobile */
empty-cells: show;
width: 20%;
min-width: 20%;
}
.qg-guide-7 {
empty-cells: show;
width: 14.28%;
min-width: 14.28%;
}
.qg-label {
font-size: 13px;
float: left;
margin-bottom: 10px;
}
[class*="qg-input"] {
font-size: 11px;
width: 60px;
text-align: center;
float: left;
}
<div class="qg">
<div class="qg-table">
<div class="qg-row qg-head">
<div class="qg-guide-label"></div>
<div class="qg-guide qg-right-section-1">
<div class="qg-guide-1"> </div>
</div>
<div class="qg-guide qg-right-section-5">
<div class="qg-guide-7">High7</div>
<div class="qg-guide-7"></div>
<div class="qg-guide-7"></div>
<div class="qg-guide-7">Neutral4</div>
<div class="qg-guide-7"></div>
<div class="qg-guide-7"></div>
<div class="qg-guide-7">Low1</div>
</div>
</div>
<div class="qg-row">
<div class="qg-label">Far far away, behind the word mountains, far from the countries Vokalia and Consonantia.</div>
<div class="qg-guide qg-right-section-1">
<div class="qg-input-1">
<input type="radio" name="0LoMVa" value=""><br>
N/A<br>
</div>
</div>
<div class="qg-right-section-5">
<div class="qg-input-5"><input type="radio" name="0LoMVa" value="5"><br>5<br></div>
<div class="qg-input-5"><input type="radio" name="0LoMVa" value="4"><br>4<br></div>
<div class="qg-input-5"><input type="radio" name="0LoMVa" value="3"><br>3<br></div>
<div class="qg-input-5"><input type="radio" name="0LoMVa" value="2"><br>2<br></div>
<div class="qg-input-5"><input type="radio" name="0LoMVa" value="1"><br>1<br></div>
</div>
</div>
</div>
</div>
<div class="qg">
<div class="qg-table">
<div class="qg-row qg-head">
<div class="qg-guide-label"></div>
<div class="qg-guide qg-right-section-1">
<div class="qg-guide-1"> </div>
</div>
<div class="qg-guide qg-right-section-5">
<div class="qg-guide-5 qg-guide-item-0">High0</div>
<div class="qg-guide-5 qg-guide-item-1"></div>
<div class="qg-guide-5 qg-guide-item-2">Neutral2</div>
<div class="qg-guide-5 qg-guide-item-3"></div>
<div class="qg-guide-5 qg-guide-item-4">Low4</div>
</div>
</div>
<div class="qg-row">
<div class="qg-label">Far far away, behind the word mountains.</div>
<div class="qg-guide qg-right-section-1">
<div class="qg-input-1">
<input type="radio" name="W1MkXk" value=""><br>
N/A<br>
</div>
</div>
<div class="qg-right-section-5">
<div class="qg-input-5"><input type="radio" name="W1MkXk" value="5"><br>5<br></div>
<div class="qg-input-5"><input type="radio" name="W1MkXk" value="4"><br>4<br></div>
<div class="qg-input-5"><input type="radio" name="W1MkXk" value="3"><br>3<br></div>
<div class="qg-input-5"><input type="radio" name="W1MkXk" value="2"><br>2<br></div>
<div class="qg-input-5"><input type="radio" name="W1MkXk" value="1"><br>1<br></div>
</div>
</div>
</div>
</div>
This is what I want it to do:
Also, these questions are created programmatically using a template, so the divs used have to be the same, IOW I can't add a div (with an additional class) to the 1st question that fixes the issue without the 2nd question having the same div. This is the critical requirement which is making it difficult for me to find a solution. Can this be solved?
Is it possible to change DOM structure?? If yes then just try pushing
<div class="qg-guide qg-right-section-1"></div> inside
<div class="qg-guide qg-right-section-5">
code
<div class="qg-row qg-head">
<div class="qg-guide-label"></div>
<div class="qg-guide qg-right-section-5">
<div class="qg-guide-5 qg-guide-item-0">High0</div>
<div class="qg-guide-5 qg-guide-item-1"></div>
<div class="qg-guide-5 qg-guide-item-2">Neutral2</div>
<div class="qg-guide-5 qg-guide-item-3"></div>
<div class="qg-guide-5 qg-guide-item-4">Low4</div>
<div class="qg-guide qg-right-section-1">
<div class="qg-guide-1"> </div>
</div>
</div>
</div>
and change css
.qg-right-section-5 {
width: 360px;
}
.qg-right-section-1 {
width: 60px;
float: left;
}
You can achieve the effect you want by adding a clear-fix after the .qg-label class. I did this in the fiddle by manually inserting an empty <div> with the style clear:both;. You could instead use the clear-fix class of your favorite library or create you own such as:
(This is a classic clearfix example from this answer)
.clearfix:after {
content: " "; /* Older browser do not support empty content */
visibility: hidden;
display: block;
height: 0;
clear: both;
}
Another solution is to include all radio buttons in the same parent element, give it a fixed width, and add float:right; to it and the radio element children. This should be a simple fix. I showed this in comparison to your original fiddle here - fiddle.
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;
}