How do I align elements horizontally? - html

I have three elements (DIVs) and I need to align them horizontally. I'm following Bootstrap's guides and it won't work.
This is what I need it to look like:
This is what it looks now, it's aligned to the left:
Here's my code:
.price-selection {
border: 1px solid #8ABE57;
display: inline-block;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<div class="row m-0">
<div class="col-12 product-info-subtitle cuanto-pagar">
<p>¿Cuánto quieres pagar?</p>
</div>
<div class="col-12">
<div class="row justify-content-center">
<div class="col-3 price-selection text-center">
<p class="price">one</p>
<p class="period">one</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">two</p>
<p class="period">two</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">three</p>
<p class="period">three</p>
</div>
</div>
</div>
</div>

Here's the correct markup for what you're trying to achieve:
.price-selection {
border: 1px solid #8ABE57;
display: inline-block;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-12 product-info-subtitle cuanto-pagar">
<p>¿Cuánto quieres pagar?</p>
</div>
<div class="col-12">
<div class="row justify-content-around">
<div class="col-3 price-selection text-center">
<p class="price">one</p>
<p class="period">one</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">two</p>
<p class="period">two</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">three</p>
<p class="period">three</p>
</div>
</div>
</div>
</div>
</div>
The important part is: i wrapped the col's in a row which has the class of justify-content-around.
The row part fixes the margin/paddings of Bootstrap grid on various screen sizes.
The justify-content-around gives it justify-content: space-evenly. Currently Bootstrap only has justify-content-around and justify-content-between. However, since it was released, flexbox was added a new value for justify-content (which will probably get added to Bootstrap), which is space-evenly. However, Bootstrap doesn't yet have a class for it.
If you want to space your elements evenly, you'll need to add it in CSS yourself:
.price-selection {
border: 1px solid #8ABE57;
display: inline-block;
}
.justify-content-evenly {
display: flex;
justify-content: space-evenly;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-12 product-info-subtitle cuanto-pagar">
<p>¿Cuánto quieres pagar?</p>
</div>
<div class="col-12">
<div class="row justify-content-evenly">
<div class="col-3 price-selection text-center">
<p class="price">one</p>
<p class="period">one</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">two</p>
<p class="period">two</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">three</p>
<p class="period">three</p>
</div>
</div>
</div>
</div>
</div>
To better understand the difference between various types of spacing in flexbox, here's a graphical explanation: https://css-tricks.com/almanac/properties/j/justify-content/#article-header-id-1

Use m-0 for remove horizontal bar.
Columns should not be direct children of other columns. If you want to divide a column use a .row wrapper.
Solution 1
I removed justify-content-center because it cant give gap between cols, so use justify-content-around.
.price-selection {
border: 1px solid #8ABE57;
display: inline-block;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<div class="row m-0 justify-content-around element">
<div class="col-3 price-selection text-center">
<p class="price">one</p>
<p class="period">one</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">two</p>
<p class="period">two</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">three</p>
<p class="period">three</p>
</div>
</div>
Solution 2
you can also use CSS for equally gap between cols ( no bootstrap class for space-evenly ).
.element {
justify-content: space-evenly;
}
.price-selection {
border: 1px solid #8ABE57;
display: inline-block;
}
.element {
justify-content: space-evenly;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<div class="row m-0 element">
<div class="col-3 price-selection text-center">
<p class="price">one</p>
<p class="period">one</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">two</p>
<p class="period">two</p>
</div>
<div class="col-3 price-selection text-center">
<p class="price">three</p>
<p class="period">three</p>
</div>
</div>

Related

How can I create a Bootstrap grid layout with a responsive but minimum width left column and scrollable right column?

I am close to achieving this look and behavior but I am having issues with the scrolling of the right column. The scroll bar appears but does not scroll the content on the right column. Should look like this:
.
Currently looks like this:
There are probably multiple approaches or changes I can take or make but any suggestions are welcome! Code is provided below.
.scroller {
overflow-x: scroll;
white-space: nowrap;
}
.row {
display: flex;
flex-wrap: nowrap;
}
.col-md-4 {
min-width: 240px;
}
.col-md-8 {
flex-grow: 1;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<div class="row">
<div class="col-4" style="min-width: 240px;">
<div class="row pb-1">
<div class="col-3 offset-3">
<b>NAME</b>
</div>
</div>
<div class="row row-striped align-items-center pb-1 pt-1">
<div class="col-3">
<img src="https://media.api-sports.io/football/players/730.png" alt="player photo" width="40px" height="40px">
</div>
<div class="col-9 player-table-text">
<div>
Thibaut Courtois
<span class="ps-1 player-number-text">1</span>
</div>
</div>
</div>
</div>
<div class="col scroller" style="min-width: 500px;">
<div class="row pb-1">
<div class="col-2 offset-1">
<b>POS</b>
</div>
<div class="col-2">
<b>AGE</b>
</div>
<div class="col-2">
<b>HT</b>
</div>
<div class="col-2">
<b>WT</b>
</div>
<div class="col-2">
<b>NAT</b>
</div>
</div>
<div class="row row-striped align-items-center pb-1 pt-1 player-table-text" style="height:48px;">
<div class="col-2 offset-1">
GK
</div>
<div class="col-2">
30
</div>
<div class="col-2">
6' 7"
</div>
<div class="col-2">
212 lbs
</div>
<div class="col-2">Belgium</div>
</div>
</div>
</div>

do not cut my ngFor generated div when I send it to print. (break-inside didnt work)

What I want to achieve is that my DIV element is not cut off by the Print option. I have used the code BREAK-INSIDE: AVOID code and it doesn't work, in the end my DIV ends up sliced in half.
HTML
<block-ui></block-ui>
<div class="content-wrapper container-xxl p-0">
<div class="content-body">
<div class="card">
<div class="row">
<div class="col-xl-9 col-md-8 col-12">
<div class="row m-1">
<div class="m-1 dont-break" *ngFor="let QRID of printData.results">
<div class="d-flex align-items-center flex-column"
style="width: 4.5cm; height: 8cm; max-width: 4.5cm; border: 1px dashed #000000; padding: .25cm;">
<h6 class="mb-0" style="font-size: 10px;">Name:</h6>
<h6 class="mb-30">{{ QRID.proyecto }}</h6>
<img [src]="QRID.foto" style="height: 3.5cm; width: 3.5cm; margin-bottom: 7px;">
<ngx-qrcode-styling [config]="config" [type]="'canvas'" [shape]="'square'" [width]="110" [height]="110"
[margin]="0" [data]="QRID.qr">
</ngx-qrcode-styling>
</div>
</div>
</div>
</div>
<div class="col-xl-3 col-md-4 col-12 invoice-actions mt-md-0 mt-2">
<div class="card">
<div class="card-body">
<a class="btn btn-outline-secondary btn-block mb-75" href="javascript:window.print();" rippleEffect>
Print
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
CSS
#media print {
div.dont-break {
break-inside: avoid !important;
page-break-inside: avoid !important;
-webkit-break-inside: avoid !important;
}
}

Vertical Text text alignment in bootstraps column

<div class="container">
<div class="row">
<div class="col-sm-1">
<p style=" writing-mode: vertical-rl; text-align: center; transform:scale(-1);">Total Number of Correct and Wrong Number</p>
</div>
<div class="col-sm-11">
<div class="row" style="height: 400px">
<canvas id="{{ answer.id }}_barChart" class="bar_chart"></canvas>
</div>
</div>
</div>
</div>
Above is the code for my work, i tried to make the text align to middle by put text-align:center or <center> xxx </center> , but it's still not in the middle
https://prnt.sc/m8hPkJ3N5x4v here is the image
Just add the following 3 more bootstrap classes d-flex align-items-center justify-content-center, in your heading div.
so your heading div will be <div class="col-sm-1 d-flex align-items-center justify-content-center">
check below.
.container{
background-color:aqua;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-sm-1 d-flex align-items-center justify-content-center">
<p style=" writing-mode: vertical-rl; transform:scale(-1);" class="align-middle">
Total Number of Correct and Wrong Number
</p>
</div>
<div class="col-sm-11">
<div class="row" style="height: 400px">
Your Chart.
</div>
</div>
</div>
</div>

Bootstrap 4 div height not expanding to fit content

I'm using bootstrap 4 in an angular app to try and create a grid with several nested boxes, the second column has 2 rows, the bottom one needs to always vertically align to the bottom, ideally it should look like this:
(I achieved this by adding a bunch of br tags)
However, inreality the contect overlaps it it is small and looks like this:
Here is the css I use to align the bottom div:
.bottom {
position: absolute;
bottom: 0;
left: 0;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-12 mb-3 border border-secondary rounded">
<div class="row">
<div class="col-2 p-2">
ABC123<br> Text
<br> More Text
</div>
<div class="col-5 border-right border-left border-secondary">
<div class="col-12 p-2">
<a [routerLink]="['/tickets', ticket.id]">Lorem Ipsum</a><br> This is a fake text<br>This is a fake text<br>This is a fake text<br>This is a fake text<br>
</div>
<div class="col-12 border-top border-secondary bottom">
<div class="row">
<div class="col-4 p-2">bottom-align</div>
<div class="col-4 p-2 border-left border-right border-secondary">bottom-align</div>
<div class="col-4 p-2">bottom-align</div>
</div>
</div>
</div>
<div class="col-3 p-2 border-right border-secondary">Test</div>
<div class="col-2 p-2 text-center">Test</div>
</div>
</div>
</div>
So far everything I have tried has failed, please advise.
Thanks
No extra CSS is needed. Use the Bootstrap utilility classes to make the inner row 'h-100' and then 'mt-auto' to push the content to the bottom...
<div class="col-5 border-left border-right border-secondary">
<div class="row h-100">
<div class="col-12 p-2">
<a [routerlink]="['/tickets', ticket.id]">Lorem Ipsum</a>
<br> This is a fake text
<br>This is a fake text
<br>This is a fake text
<br>This is a fake text
</div>
<div class="col-12 border-top border-secondary mt-auto">
<div class="row">
<div class="col-4 p-2">bottom-align</div>
<div class="col-4 p-2 border-left border-right border-secondary">bottom-align</div>
<div class="col-4 p-2">bottom-align</div>
</div>
</div>
</div>
</div>
https://www.codeply.com/go/rxzugvCk80
I tried to replicate it, using flex instead of <br> tag. Is this what you are looking to achieve ?
.text-container {
display: flex;
flex-direction: column;
}
<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"
/>
<div class="row">
<div class="col-12 mb-3 border border-secondary rounded">
<div class="row">
<div class="col-2 p-2">
ABC123<br />
Text<br />
More Text
</div>
<div class="col-5 border-right border-left border-secondary p-0">
<div class="col-12 p-2 text-container">
<a [routerLink]="['/tickets', ticket.id]">Lorem Ipsum</a><br />
<span>This is a fake text</span>
</div>
<div class="col-12 border-top border-secondary bottom">
<div class="row">
<div class="col-4 p-2">bottom-align</div>
<div
class="col-4 p-2 border-left border-right border-secondary"
>
bottom-align
</div>
<div class="col-4 p-2">bottom-align</div>
</div>
</div>
</div>
<div class="col-3 p-2 border-right border-secondary">Test</div>
<div class="col-2 p-2 text-center">Test</div>
</div>
</div>
</div>
you remove css :
.bottom {
position:absolute;
bottom:0;
left:0;
}
and replace with shown in example: add the display class. and also add the container-fluid class for look as result.
.display{
display:flex;
flex-direction: column;
}
<head>
<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">
</head>
<div class="container-fluid">
<div class="row">
<div class="col-12 mb-3 border border-secondary rounded">
<div class="row">
<div class="col-2 p-2 ">
ABC123<br>
Text<br>
More Text
</div>
<div class="col-5 border-right border-left border-secondary bootom">
<div class="row">
<div class="col-12 p-2 display">
<a [routerLink]="['/tickets', ticket.id]">Lorem Ipsum</a>
<span>This is a fake text</span>
<span>This is a fake text</span>
<span>This is a fake text</span>
</div>
<div class="col-12 border-top border-secondary bottom">
<div class="row">
<div class="col-4 p-2">bottom-align</div>
<div class="col-4 p-2 border-left border-right border-secondary">bottom-align</div>
<div class="col-4 p-2">bottom-align</div>
</div>
</div>
</div>
</div>
<div class="col-3 p-2 border-right border-secondary">Test</div>
<div class="col-2 p-2 text-center">Test</div>
</div>
</div>
</div>
</div>

Unable to centre bootstrap media card on small mobile device screen

The following code using flexbox/centre bootstrap media card works correctly, apart from when viewed on a mobile sized screen, where the boxes are aligned to the left. I'd like them centred in the screen.
I looked at changing align-self-left to centre or text-align: center but it does not appear to work. I tried applying this to the article and row below. Not sure why it will not centre correctly?
CSS
.card .card-body {
padding: 15px;
text-align: center;
}
.boxFixDw {
display:inline-block; width: 285px;
color: #1576b9;
}
HTML
<div class="row flexbox-wrap">
<div class="col-lg-4 flexbox-equalise">
<article class="media card card-1">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-12">
<div class="card-body align-self-left">
<h4 class="list-group-item-headline boxFixDw">LATEST RESOURCES</h4>
<p class="list-group-item-text">More added each week.</p>
VIEW NOW
</div>
</div>
</div>
</article>
</div>
<div class="col-lg-4 flexbox-equalise">
<article class="media card card-1">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-12">
<div class="card-body card-body-left">
<h4 class="list-group-item-headline boxFixDw">JOIN NOW</h4>
<p class="list-group-item-text">For instant access.</p>
SIGN UP
</div>
</div>
</div>
</article>
</div>
<div class="col-lg-4 flexbox-equalise">
<article class="media card card-1">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-12">
<div class="card-body card-body-left">
<h4 class="list-group-item-headline boxFixDw">SAMPLE RESOURCES</h4>
<p class="list-group-item-text">Check the quality.</p>
VIEW NOW
</div>
</div>
</div>
</article>
</div>
</div>
apart from when viewed on a mobile sized screen, where the boxes are
aligned to the left.
To center the cards on mobile you need to change align-items: flex-start; to align-items: center; for the .media through media query
#media only screen and (max-width: 767px) {
.media {
align-items: center !important;
}
}
.card .card-body {
padding: 15px;
text-align: center;
}
.boxFixDw {
display:inline-block; width: 285px;
color: #1576b9;
}
#media only screen and (max-width: 990px) {
.media {
align-items: center !important;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row flexbox-wrap">
<div class="col-lg-4 flexbox-equalise">
<article class="media card card-1">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-12">
<div class="card-body align-self-left">
<h4 class="list-group-item-headline boxFixDw">LATEST RESOURCES</h4>
<p class="list-group-item-text">More added each week.</p>
VIEW NOW
</div>
</div>
</div>
</article>
</div>
<div class="col-lg-4 flexbox-equalise">
<article class="media card card-1">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-12">
<div class="card-body card-body-left">
<h4 class="list-group-item-headline boxFixDw">JOIN NOW</h4>
<p class="list-group-item-text">For instant access.</p>
SIGN UP
</div>
</div>
</div>
</article>
</div>
<div class="col-lg-4 flexbox-equalise">
<article class="media card card-1">
<div class="row">
<div class="col-xs-12 col-sm-12 col-lg-12">
<div class="card-body card-body-left">
<h4 class="list-group-item-headline boxFixDw">SAMPLE RESOURCES</h4>
<p class="list-group-item-text">Check the quality.</p>
VIEW NOW
</div>
</div>
</div>
</article>
</div>
</div>
</div>