I want to display the text (and if the image is smaller, then the image) in the middle by vertically. But i can't accomplish it. Any help?
Here is what i have right now: Jsfiddle
HTML:
<div class="container overview-sm">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
</div>
</div>
CSS:
.box2 {
margin: 5px 5px 5px 0;
text-align: center;
display: inline-block;
width: 100%;
}
.image{
width: 100%;
max-width: 450px;
}
.row{
padding-bottom: 30px;
}
Image of what i want it to be: Image
The easiest way to do this would be to use Flexbox. Note that older browsers may not have great/any Flexbox support: https://caniuse.com/#search=flexbox)
If you are using Bootstrap 4, its grid uses Flexbox, so the columns in the row should already match each other's height. You can then use Bootstrap 4's Flexbox utility classes to vertically center the column's content: class="d-flex align-items-center". Ex:
<div class="row">
<div class="col d-flex align-items-center">Centered Content</div>
<div class="col">
<ul>
<li>Longer</li>
<li>Multi</li>
<li>Line</li>
<li>Content</li>
</ul>
</div>
</div>
Codepen
If you are using Bootstrap 3, you're going to have to add the appropriate flexbox code to both get your columns to match in height and vertically center your content. Here is a little cheatsheet I find helpful for building out flexbox layouts: http://jonibologna.com/flexbox-cheatsheet/
Edit: Here is the same example with Bootstrap 3:
HTML:
<div class="row row-flex">
<div class="col-md-6 col-v-centered">Centered Content</div>
<div class="col-md-6">
<ul>
<li>Longer</li>
<li>Multi</li>
<li>Line</li>
<li>Content</li>
</ul>
</div>
</div>
CSS:
.row-flex {
display: flex;
}
.col-v-centered {
display: flex;
align-items: center;
}
Codepen
If you want the text to be displayed underneath the second row then you will need to create another image div and add a class so it only displays on mobiles.
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 visible-xs ">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 hidden-xs ">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
https://jsfiddle.net/7goLo3xy/2/
If you need more control than CSS provides you can use a simple jQuery or JavaScript implementable.
$(window).resize(function() {//on resize
setBoxSizes();
});
function setBoxSizes() {
$('.image').each(function() {
let $imageBox = $(this).closest('.box2');
let imageBoxTop = $imageBox.offset().top;
let $otherBoxes = $imageBox.closest('.row').find('.box2');
$otherBoxes.each(function(){
$box = $(this);
if(!$box.find('.image').length) {//doesn't contain .image
if(Math.abs($box.offset().top - imageBoxTop) < 3) {//on the same layout row
$box.height($imageBox.height());
}
else $box.css('height', 'auto');//restores single column layout
}
});
});
}
setBoxSizes();//initial configuration
Fiddle Example! - Handles multiple column layout, changing columns layouts, and ensures centering is performed against equal horizon boxes.
If you don't want to use Flexbox you could always just set a height of 350px and use table and table-cell with vertical-align set to middle.
However this method does restrict you to having a fixed height so, if the height of your image changes the box height won't be relative. If you want the box to relative you could always set a height of 0 and use padding-bottom. More on this here
<div class="box2">
<div class="center">
<div class="center-v">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
</div>
.center {
display: table;
height: 345px;
}
.center-v {
display: table-cell;
vertical-align: middle;
}
Related
This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 16 days ago.
I am trying to make a listing website. I am using the Jekyll Moonwalk theme as a base and heavily adapting it to my needs.
This is my first website project, so I am figuring it out as I go. Right now, I am trying to make my HTML button elements "pin" to the bottom of their container, instead of being at the bottom of the text, so they all are in line. Right now it looks like this:
Image
The red line is roughly where I want the bottom of all the buttons to me.
I have tried ChatGPT's suggestion, but it didn't work.
<div style="position: relative;">
<button style="position: absolute; bottom: 0;">Click Me</button>
</div>
You can use display flex for the boxes to arrange them like this, justify the content using space-between will always keep the buttons at the end.
here box is your card
.box{
display: flex;
flex-direction: column;
justify-content: space-between;
width: 280px;
height: 320px;
border: 1px solid #000000;
}
<div class="box">
<div class="head">
<!--header content-->
<h1> hello</h1>
</div>
<div class="body">
<!--bodycontent-->
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec facilisis libero nec nunc consectetur, non euismod ex vestibulum. Aliquam porttitor egestas sem.
</p>
</div>
<div class"bottom">
<!--button-->
<button> Tell me more</button>
</div>
</div>
style parent div of button position:relative
syle button position: absolute; bottom:0
.container{
display:flex;
}
.box{
position:relative;
outline: 1px solid black;
height: 300px;
margin:5px;
padding:5px;
display:flex;
flex-direction: column;
align-items: center;
}
button{
position:absolute;
bottom: 10px
}
<div class="container">
<div class="box">
<div class="head">
<!--header content-->
<h1> hello</h1>
</div>
<div class="body">
<!--bodycontent-->
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec facilisis
libero nec nunc consectetur, non euismod ex vestibulum. Aliquam porttitor
egestas sem.
</p>
</div>
<div class"bottom">
<!--button-->
<button> Tell me more</button>
</div>
</div>
<div class="box">
<div class="head">
<!--header content-->
<h1> hello</h1>
</div>
<div class="body">
<!--bodycontent-->
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec facilisis
libero nec nunc consectetur, non euismod ex vestibulum. Aliquam porttitor
egestas sem.
</p>
</div>
<!--button-->
<button> Tell me more</button>
</div>
</div>
I'm trying to put a Swiper JS carousel inside a div with the col class of Bootstrap 4, but my carousel doesn't stay inside the col div. To illustrate, this is the layout I'm trying to achieve:
But for some odd reason when I put the carousel code in the second col div, my page ends up like this:
Messing with the carousel properties with the Inspect Element tool, I found out that what causes this is the property display: flex that the swiper-wrapper has, and I still haven't been able to figure out how to make the carousel fit nicely within my second col div.
I've tried encapsulating the swiper-container in another div, setting max-width: 100%and min-width: 100% but nothing seems to work, it's like the display: flex property of the row class conflicts with the display: flex of the swiper-wrapper div.
Here's the code for this part of the layout:
HTML
<div id="myaboutdiv" class="row">
<div class="col topic">
<p>TEXT</p>
</div>
<div class="col mt-5">
<p class="text-left whoami"><i>This is some text</i></p>
<p class="text-justify">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent lorem purus, venenatis vel magna et, consectetur cursus libero. In augue est, iaculis sit amet faucibus ut, condimentum vitae ante. Donec et leo eu dolor suscipit viverra at et mauris. Quisque dapibus leo at ipsum elementum, sit amet interdum sapien ornare. Integer justo lorem, porttitor in gravida in, porttitor at tellus. Sed aliquet malesuada luctus. Duis ac nisl vitae nibh mattis luctus eget a ex.
</p>
<br>
<p class="text-left whoami"><i>Here's the Swiper Carousel</i></p>
<div class="swiper-container" id="academichistory">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</div>
</div>
CSS
.topic {
font-weight: bold;
font-size: 2.5rem;
}
.whoami {
color: grey;
font-weight: 500;
}
And finally, here are some screenshots:
swiper-wrapper with display: flex:
swiper-wrapper without the display: flex property:
I'm using:
Swiper v5.3.6 and Bootstrap 4.
And just in case, here's the JS code that initializes the Swiper carousel:
JavaScript
var swiper = new Swiper('#academichistory', {
loop: true,
pagination: {
el: '.swiper-pagination',
type: 'progressbar',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
You can solve it using absolute positioning.
Since .swiper-container already has position: relative, you can add position: absolute to .swiper-wrapper. In addition, position it using left: 0 and top: 0.
Finally, the swipper absolutely positioned needs an explicit height, so add the height you want in .swiper-container.
.swiper-container {
height: 270px;
}
.swiper-wrapper {
position: absolute;
left: 0;
top: 0;
}
I'm trying to align a movie poster image, description text & add to cart button on the same line. The text needs to be centered aligned and not wrap around button or image. It should look like this.
However, with my current code, it looks like this. Movie description text is not center-aligned and it wraps around the button.
This is my code. How can I modify it to fix it to look like the first image?
<div class="card-block">
<div class="pull-left"><img src="https://{{movie.image}}" alt="[Image Missing]" width="70px" /></div>
<br><span>{{movie.description}}</span>
<span>
<button class="float-right btn btn-sm btn-success" (click)="addToCart(movie)">
Add to Cart
</button></span>
</div>
There are many ways to solve your problem.
You can use flex
.container {
display: flex;
align-items: center;
}
.container .item:nth-child(2){ /*Only second item padding and center text*/
padding-left: 1em;
padding-right: 1em;
text-align: center;
}
<div class="container">
<div class="item">
<img src="https://via.placeholder.com/80x100">
</div>
<div class="item">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sagittis sagittis enim. Nulla viverra aliquam.</p>
</div>
<div class="item">
<button> Add to cart </button>
</div>
</div>
Or you can try with the new CSS-Grid, but it's not supported at all on some browsers yet
.container {
display: grid;
grid-template-columns: 1fr 4fr 1fr; /*Here you set how many columns will be and how much of the space they will take, for example there are 3 values, meaning 3 columns, the first and the last one will take 1 space of the row, and the second one will take 4 spaces*/
align-items: center;
grid-gap: 1em; /*The gap between items*/
}
.container .item:nth-child(2) {
text-align: center;
}
<div class="container">
<div class="item">
<img src="https://via.placeholder.com/80x100">
</div>
<div class="item">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sagittis sagittis enim. Nulla viverra aliquam.</p>
</div>
<div class="item">
<button> Add to cart </button>
</div>
</div>
Try to use flex instead, this is example:
.wrapper {
display: flex;
flex-direction: row;
align-items: center
}
p {
text-align:center
}
<div class="wrapper">
<img src="https://via.placeholder.com/150" />
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sit amet est vel erat rutrum tempus. Etiam auctor dapibus magna, ut auctor metus pretium sed.
</p>
<button>
Add
</button>
</div>
Hope this help.
I am trying to create a bootstrap site and what I need to happen is to have the content drop below a hero image I have if the screen gets to small. The hero image is just a DIV I've created that used the cover for the background. I've tried setting my columns to 12 for the smallest screen size, but it still just pushes it on top of my hero image. How do I get my content to push down below the image?
Example: http://www.bootply.com/aF7D9RDMqr
<div class="fill-screen container-fluid" style="height: 400px;">
<div class="row">
<div class="col-xs-12 col-sm-1 col-sm-offset-2" style="margin-top: 30px;"><img alt="Logo" src="http://placehold.it/184x18"></div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-4 col-sm-offset-2">
<h1>A new movement</h1><button class="btn btn-default" type="button">Default</button> <button class="btn btn-default" type="button">Default</button>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam vehicula condimentum purus. Nunc vehicula lorem lacinia efficitur commodo. Aliquam tempor elit eget dui faucibus, non euismod metus rhoncus. In sed ipsum id dolor tincidunt euismod. Ut sit amet ex at tortor molestie malesuada.</p>
</div>
</div>
</div>
How I'd like it to look:
I upgraded your code here : codepen
What I needed to do is put the background in a div with no children, and place the container in absolute position to put it on the background, and then relative position for little screens. If you are using Bootstrap alpha 4, you can do it even more easily using mixins.
.container-fluid {
position: absolute;
top: 0;
}
#media only screen and (max-width : 767px) {
.container-fluid {
position: relative;
}
}
I recently started using Bootstrap. I'm having a problem with centering Jumbotron. I can't post image as example now because I need 10 reputation. My code:
<div class="container">
<div class="col-md-8 jumbotron center">
<h1>Jumbotron heading</h1>
<p class="lead">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
<p><a class="btn btn-lg btn-success" href="#" role="button">Sign up today</a></p>
</div>
</div>
My css:
.jumbotron {
border-bottom: 1px solid #e5e5e5;
}
.jumbotron .btn {
padding: 14px 24px;
font-size: 21px;
}
.center{
text-align: center;
}
I'm sorry if solution is very easy but I'm beginner web developer.
You've set the size of your jumbotron to 8 columns on 'medium' and above (col-md-8) with no offsetting. This means as the screen gets bigger than what Bootstrap classes as 'Medium' it will change from 100% with to 8/12s width.
If you want this width then add an offset class of 2 (col-md-offset-2) or you can get rid of your col-md-8 to force it to always be 100% regardless of device width as in this example - http://jsfiddle.net/g8LUy/1/
Full width jumbtron
<div class="container">
<div class="jumbotron center">
...your jumbtron html
</div>
</div>
'8/12ths' width jumbotron
<div class="container">
<div class="jumbotron col-md-8 col-md-offset-2 center">
...your jumbtron html
</div>
</div>
You can read more on the Bootstrap grid system and the various offsetting classes on their website on their 'CSS' page under 'Grid system' - http://getbootstrap.com/css/#grid