Bootstrap element to bottom with flexbox - html

I have a snippet with my code of section height height: 450px;. Please see snippet:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
.get-tour .form-block {
display: inline-block;
}
.get-tour .form-block label {
display: block;
}
</style>
<section style="min-height: 450px">
<div class="container">
<div class="row">
<div class="col-lg-8">
Use the .flex-fill class on a series of sibling elements to force them into widths .... align-items , flex-direction: column , and margin-top: auto
</div>
<div class="col-lg-3">
Use the .flex-fill class on a series of sibling elements to force them into widths .... align-items , flex-direction: column , and margin-top: auto
</div>
</div>
<div class="element">
<div class="get-tour d-flex align-items-end">
<form action="">
<div class="form-block">
<label>When</label>
<input type="text">
</div>
<div class="form-block">
<label>Date </label>
<input type="text">
</div>
<div class="form-block">
<label>Qty ночей</label>
<input type="text">
</div>
<div class="form-block">
<label>Who</label>
<input type="text">
</div>
<div class="form-block">
<label>Type</label>
<input type="text">
</div>
<div class="form-block">
<button type="submit">Search</button>
</div>
</form>
</div>
</div>
</div>
</section>
How I can put to bottom my form get-tour with flexbox? I tried add class d-flex align-items-end but my code is not work. Why? If I set height: 100% to container or on my section also not work. Please help fix this issue. Thanks.

If I set height: 100% to container or on my section also not work.
You need to set body and html's height to 100% as well. Because they wrap all of your content.
Then, you can use flexbox layout model to put your get-tour at the bottom of your page.
body,
html {
height: 100%;
}
section {
height: 100%;
}
section .container {
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
.get-tour .form-block {
display: inline-block;
}
.get-tour .form-block label {
display: block;
}
</style>
<section style="min-height: 450px">
<div class="container">
<div class="row">
<div class="col-lg-8">
Use the .flex-fill class on a series of sibling elements to force them into widths .... align-items , flex-direction: column , and margin-top: auto
</div>
<div class="col-lg-3">
Use the .flex-fill class on a series of sibling elements to force them into widths .... align-items , flex-direction: column , and margin-top: auto
</div>
</div>
<div class="element">
<div class="get-tour ">
<form action="">
<div class="form-block">
<label>When</label>
<input type="text">
</div>
<div class="form-block">
<label>Date </label>
<input type="text">
</div>
<div class="form-block">
<label>Qty ночей</label>
<input type="text">
</div>
<div class="form-block">
<label>Who</label>
<input type="text">
</div>
<div class="form-block">
<label>Type</label>
<input type="text">
</div>
<div class="form-block">
<button type="submit">Search</button>
</div>
</form>
</div>
</div>
</div>
</section>

Related

Bootstrap button padding makes it appear like the button is going past the container border

Why does the "Subscribe now!" button go outside the container in the picture below and how can I make it so that the right edge lines up with the rest of the page? (column 12 in the bootstrap grid)
I have tried box-sizing: border-box but it had no effect.
My HTML code:
.container-fluid {
padding: 0;
}
.carousel-inner .carousel-item h1 {
font-weight: bold;
font-size: 300%;
/*-webkit-text-stroke:1px black;*/
font-family: 'Open Sans', sans-serif;
/*text-align:right;*/
}
.carousel-inner .carousel-item img {
filter: brightness(50%);
}
#paddingrow {
padding: 25px;
}
#paddingrowLarge {
padding: 100px;
}
#accordionRightalign {
float: right;
}
#mycard {
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">
<div class="container">
<div class="row" id="paddingrow"></div>
<div class="row">
<div class="col-sm-12">
<h1 style="text-align:center">SUBSCRIBE TO OUR NEWSLETTER</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<form>
<div class="row">
<div class="col">
<input type="text" class="form-control" placeholder="Enter your email">
</div>
<button class="btn btn-primary" style="box-sizing: border-box" type="submit">Subscribe now!</button>
</div>
</form>
</div>
</div>
</div>
</div>
Bootstrap rows have margin-right: -15px; margin-left: -15px; that's why the right edge doesn't lined up. Try to add mx-0 = margin-left:0 and margin-right: 0, a bootstrap class. In your nested row form.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
<div class="row" id="paddingrow"></div>
<div class="row">
<div class="col-sm-12">
<h1 style="text-align:center">SUBSCRIBE TO OUR NEWSLETTER</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<form>
<div class="row mx-0">
<div class="col pl-0 pr-3">
<input type="text" class="form-control" placeholder="Enter your email">
</div>
<button class="btn btn-primary" style="box-sizing: border-box" type="submit">Subscribe now!</button>
</div>
</form>
</div>
</div>
</div>
</div>
Make sure col in the container with margin and padding to zero.
Put your image into container with the width not greather than 100%.
I noticed my button wasn't in the column div, so I made two column div's, one size 10 and one size 2, put input in the first, put the button in the second, and it seems to work now.
Here is the fixed code:
<div class="container">
<div class="row" id="paddingrow"></div>
<div class="row">
<div class="col-sm-12">
<h1 style="text-align:center">SUBSCRIBE TO OUR NEWSLETTER</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<form class="form">
<div class="row">
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="Enter your email">
</div>
<div class="col-sm-2">
<button class="btn btn-primary w-100" type="submit">Subscribe now!</button>
</div>
</div>
</form>
</div>
</div>
</div>

How to align form elements next to image with CSS?

I want to align a label, a text-input and an X sign next to each other using Bootstrap 4.
I managed to align the label and text-input but can't seem to do that with the X sign.
JSFiddle: https://jsfiddle.net/jptyuv5n/
How can we align that X sign next to the input field? What am I missing here?
.collection {
display: flex;
}
.collection img {
float: left;
width: 150px;
}
.collection .form {
display: inline-block;
width: 100%;
margin: 0px 15px;
}
.collection .form input {
display: inline-block;
width: 100%;
}
<div class="row">
<div class="col-6 collection">
<img src="https://via.placeholder.com/150/771796">
<div class="form">
<label>Text</label>
<input type="text" name="text1">
<span>X</span>
</div>
</div>
<div class="col-6 collection">
<img src="https://via.placeholder.com/150/771796">
<div class="form">
<label>Text</label>
<input type="text" name="text2">
<span>X</span>
</div>
</div>
</div>
Edit: I just saw in your question that you mentioned Bootstrap 4. Same result can be achieved with using bootstrap styles and 0 css lines from you.
The classes used in the snippet below are documented here: https://getbootstrap.com/docs/4.0/components/forms/#overview
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="row">
<div class="col-12 collection">
<div class="row">
<div class="col-3">
<img src="https://via.placeholder.com/150/771796">
</div>
<div class="col-9">
<div class="form-row align-items-center">
<div class="col-sm-3 my-1">
<label>text</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-append">
<div class="input-group-text">X</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Old answer:
You can wrap the <input> and the <span> holding x in a div and use flex on it.
.collection {
display: flex;
}
.collection img {
float: left;
width: 150px;
}
.collection .form {
display: inline-block;
width: 100%;
margin: 0px 15px;
flex-direction: column; /* <- changed flex direction of form */
}
.collection .form input {
display: inline-block;
width: 100%;
}
.input-with-x { /* <- new classes added */
display: flex;
}
<div class="row">
<div class="col-6 collection">
<img src="https://via.placeholder.com/150/771796">
<div class="form">
<label>Text</label>
<div class="input-with-x"> <!-- <- new class used -->
<input type="text" name="text2">
<span>X</span>
</div>
</div>
</div>
</div>

How to make image expand/shrink to available space while keeping aspect ratio

How do we make an image "fit" inside the second column below? This is Bootstrap 4 and I want the right column to take the same height as the left column and then the image to fit inside it while keeping the aspect ratio. I also want it to be centered horizontally and vertically.
The HTML currently looks like this:
<div class="form-row">
<div class="col">
<div class="form-group">
<!-- Upload Image option -->
</div>
<div class="form-group">
<!-- Image URL option -->
</div>
</div>
<div class="col-md-4">
<img src="leg.jpg" height="70px" />
</div>
</div>
I simply don't want to specify that height="70px" attribute. The image should expand/shrink to cover the available height (dictated by the left column). Width of the image should then adjust to keep aspect ratio intact. The image should finally be centered horizontally or vertically.
By the look of the screenshot there's CSS from other classes and more HTML layout than what's provided. Here are the following styles relevant to the img with a <figure> wrapped around it.
<figure class='figure'> <img class='figure-img fluid-img'...> </figure>
img {
object-fit:contain;
max-width:100%;
height:auto
}
.col {
display:flex;
flex-flow:column nowrap;
justify-content: center;
align-items:center
}
.form-row {
max-width: 540px;
}
img {
object-fit: contain;
max-width: 100%;
height: auto
}
.column {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<main class='container'>
<section class="form-row">
<section class="column col-8">
<div class="form-group">
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="radio">
</div>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input form-control">
<label class="custom-file-label">Choose file</label>
</div>
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="radio">
</div>
</div>
<input type="url" class="custom-url-input form-control">
</div>
</section>
<section class="column col-4">
<figure class='figure'>
<img src="https://oehawkseye.org/wp-content/uploads/2017/01/chicken-leg.png" class='figure-img img-fluid'>
</figure>
</section>
</section>
</main>

CSS Flex basis grandchild

I'm having a hard time trying to get this to work.
div {
min-height: 100px;
}
.container {
width: 100%;
display: flex;
flex-wrap: wrap;
padding: 10px;
}
.sub-container {
width: 50%;
display: flex;
}
.child, .grand-child {
flex-basis: 50%;
}
.yellow {
background-color: yellow;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
<div class="desired-result container yellow">
<div class="child red">
1
</div>
<div class="child red">
2
</div>
</div>
<div class="issue container yellow">
<div class="sub-container green">
<div class="grand-child red">
3
</div>
</div>
<div class="sub-container green">
<div class="grand-child red">
4
</div>
</div>
</div>
https://jsfiddle.net/hugojg/dvasufot/21/
I want to make the elements width respect flex-basis while being a grand-child from my container (display: flex).
How can I change the code to get 3 and 4 to be like 1 and 2?
Just for reference, I'm having this problem because of bootstrap 4.
I have an element between .row and the .col-sm-6(*) element which is the exact same scenario described above.
EDIT:
<form>
<div class="row">
<div class="wrapper">
<div class="form-group col-md-6">
<label for="inputEmail4">Email</label>
<input type="email" class="form-control" id="inputEmail4" placeholder="Email">
</div>
</div>
<div class="wrapper">
<div class="form-group col-md-6">
<label for="inputPassword4">Password</label>
<input type="password" class="form-control" id="inputPassword4" placeholder="Password">
</div>
</div>
</div>
</form>
That would be a realistic scenario.
Taken from the Bootstrap 4 docs and added a wrapper element around the inputs.
Everything I want is that this wrapper do not change the elements positioning.
I am not sure if I get this, but you want 3 & 4 be exactly like 1 & 2? If so, here you are:
div {
min-height: 100px;
}
.container {
width: 100%;
display: flex;
flex-wrap: wrap;
padding: 10px;
}
.sub-container {
width: 50%;
display: flex;
}
.child, .grand-child {
flex: 1;
background-color: red;
}
.yellow {
background-color: yellow;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
<div class="desired-result container yellow">
<div class="child red">
1
</div>
<div class="child red">
2
</div>
</div>
<div class="issue container yellow">
<div class="sub-container green">
<div class="grand-child red">
3
</div>
</div>
<div class="sub-container green">
<div class="grand-child red">
4
</div>
</div>
</div>
You have to change flex-basis parameter, 50% for child and 100% for grandchild, as they are subs of them.
Everything I want is that this wrapper do not change the elements
positioning.
When you add a .wrapper around the col-* elements, you break Bootstraps structure, where row/col-* has a parent/child relation ship, which btw also Flexbox has.
That means, you can't tell a flex parent's grand child to pick up from its width.
If you instead add the .wrapper around your label/input, the layout will be the same as w/o the .wrapper
Stack snippet
.wrapper {
background: red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" />
<form>
<div class="row">
<div class="form-group col-md-6">
<div class="wrapper">
<label for="inputEmail4">Email</label>
<input type="email" class="form-control" id="inputEmail4" placeholder="Email">
</div>
</div>
<div class="form-group col-md-6">
<div class="wrapper">
<label for="inputPassword4">Password</label>
<input type="password" class="form-control" id="inputPassword4" placeholder="Password">
</div>
</div>
</div>
</form>
If you want to add your .wrapper around the col-* elements, and keep Bootstrap's structure, you need to add/update its classes to something like below sample.
Note, when double up those classes, also their extra padding is added, and if that is unwanted, they need to reset.
.wrapper {
background: red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" />
<form>
<div class="row">
<div class="wrapper row col-md-6">
<div class="form-group col-md-12">
<label for="inputEmail4">Email</label>
<input type="email" class="form-control" id="inputEmail4" placeholder="Email">
</div>
</div>
<div class="wrapper row col-md-6">
<div class="form-group col-md-12">
<label for="inputPassword4">Password</label>
<input type="password" class="form-control" id="inputPassword4" placeholder="Password">
</div>
</div>
</div>
</form>

Row elements won't align

I'm using bootstrap and I'm trying to align the stuff inside my row. I realize that I haven't used up all the column space, and that's fine. I just want the stuff centered.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<style>
#headings {
text-align: center;
}
#row-container {
text-align: center;
}
#take-from-break, #take-from-session {
float: right;
}
</style>
<html>
<div class="container-fluid">
<div class="container">
<div id="headings">
<h1>Pomodoro Clock</h1>
<h3>A Pomodoro Clock helps you break down work into intervals, separated by short breaks.</h3>
</div>
<div id="setup">
<div id="row-container">
Enter time in minutes
<div class="row">
<div class="col-md-1"><button id="take-from-break">-</button></div>
<div class="col-md-2"><input type="number" placeholder="Break Length"></div>
<div class="col-md-1"><button id="add-to-break">+</button></div>
<div class="col-md-2"></div> <!-- blank space -->
<div class="col-md-1"><button id="take-from-session">-</button></div>
<div class="col-md-2"><input type="number" placeholder="Session Length"></div>
<div class="col-md-1"><button id="add-to-session">+</button></div>
</div>
</div>
</div>
<div id="clock-holder">
<button id="clock"></button>
</div>
</div>
</div>
</html>
Here is the JSFiddle: https://jsfiddle.net/c8wLxkob/
And here is the image of the page that I get:
Since it doesn't look like you're using Bootstrap to size your columns in particular, one way to center-align your boxes is to use display: flex along with justify-content: center. I've retained the Bootstrap sizing in the spacer, so the result is still responsive.
Screenshot of result:
Here is a demo to show what I mean:
#headings {
text-align: center;
}
#row-container {
text-align: center;
}
.wrap {
display: flex;
justify-content: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="container">
<div id="headings">
<h1>Pomodoro Clock</h1>
<h3>A Pomodoro Clock helps you break down work into intervals, separated by short breaks.</h3>
</div>
<div id="setup">
<div id="row-container">
Enter time in minutes
<div class="wrap">
<div class="center-wrap">
<button id="take-from-break">-</button>
<input type="number" placeholder="Break Length">
<button id="add-to-break">+</button>
</div>
<div class="col-md-2"></div>
<div class="center-wrap">
<button id="take-from-session">-</button>
<input type="number" placeholder="Session Length">
<button id="add-to-session">+</button>
</div>
</div>
</div>
</div>
<div id="clock-holder">
<button id="clock"></button>
</div>
</div>
</div>
JSFiddle Version: https://jsfiddle.net/re8jzxer/