Bootstrap 4: Difference between text-center and justify-content-center - css

Both text-center and justify-content-center seem to be doing much the same thing - centering text. But what's the difference?

Bootstrap 4 doesn't support for justify-text-center.
Source: Text
Maybe you're looking for something like this:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<p class="text-justify">Ambitioni dedisse scripsisse iudicaretur. Cras mattis iudicium purus sit amet fermentum. Donec sed odio operae, eu vulputate felis rhoncus. Praeterea iter est quasdam res quas ex communi. At nos hinc posthac, sitientis piros Afros. Petierunt uti sibi concilium totius Galliae in diem certam indicere. Cras mattis iudicium purus sit amet fermentum.</p>
<p class="text-center">Center aligned text on all viewport sizes.</p>
I meant, justify-content-center
The difference is: justify-content-* is used to change the alignment of flex items.
If the div doesn't have property display: flex. It doesn't seem to work. While text-center doesn't require property display: flex.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<div class="d-flex justify-content-center">
<div style="background-color: red;">Item 1</div>
<div style="background-color: yellow;">Item 2</div>
<div style="background-color: blue;">Item 3</div>
</div>
<hr />
<div class="justify-content-center">
<div style="background-color: red;">Item 1</div>
<div style="background-color: yellow;">Item 2</div>
<div style="background-color: blue;">Item 3</div>
</div>

text-center
The text-align CSS property specifies the horizontal alignment of an inline or table-cell box.This means it works like vertical-align but in the horizontal direction. - MDN
.text-center {
text-align: center !important;
}
text-center aligns inline and table-cell element in the center of the element you use it for. It does not change the alignment of block elements.
justify-content-center
The items are packed flush to each other toward the center of the alignment container along the main axis.
.justify-content-center {
-ms-flex-pack: center !important;
justify-content: center !important;
}
In order for this to work, you need to use d-flex also. It aligns all the content of the element(inline, block,and flex) you use it for in the center of the element. By default, the content of the element is centered horizontally should you have not changed the flex-direction of the element.
You may find these links useful.
Inline Element
Block Element
Flexible Box Layout

Related

Bulma navbar: how to style the menu on dropdown?

I'm using the Bulma navbar, and the function of it is really great! My question is: on smaller screens, when the navbar-menu is shown as a dropdown via clicking the burger menu - how can I style that?
It shows up as full-width, but I'd really like to be able to have it narrower, or maybe adjust to the width of the contents.
Here's how it looks currently:
Closed:
Open:
For the burger menu dropdown, all of the examples on the Bulma page seem to reach the full width of the Navbar. (Though not so for dropdowns within the navbar).
Does anyone know how I can make it not full-width? I mean, I can easily add max-width: 50%; etc on the .navbar-menu, but I don't know how to then make the menu div 'stick' to the right-hand side of the navbar, since it doesn't look very nice aligned to the left:
I feel like I'm missing the obvious here, if anyone can put me on the right path I'd appreciate it so much!
code stuff
My HTML looks basically exactly like the Bulma docs, but I'll add it here just in case:
<nav class="navbar" role="navigation">
<div class="navbar-brand">
<div class="navbar-item">
<p class="title"><span>title here</span></p>
</div>
<div class="button navbar-burger is-active">
<span></span>
<span></span>
<span></span>
</div>
</div>
<div class="navbar-menu is-active">
<div class="navbar-end">
<div class="navbar-item">
<div>menu item 1</div>
</div>
<div class="navbar-item">
<div>menu item 2</div>
</div>
</div>
</div>
</nav>
And just to be clear, because it's a common question, I don't have any problems with the functionality of the navbar - just wanting styling advice.
Update
Below, #sol wrote about adding max-width: 50%; and float: right; as a solution. Visually though, it isn't quite right:
Closed (looking normal):
Open (uh oh alignment):
You can create this layout by applying some flexbox properties to .navbar-menu and its container.
You'll need to wrap these rules in a media query to ensure it doesn't affect the menu at larger screen sizes.
fiddle
#media screen and (max-width: 1023px) {
.navbar {
display: flex;
flex-wrap: wrap;
}
.navbar-brand {
min-width: 100%;
}
.navbar-menu {
margin-left: auto;
min-width: 50%;
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.2/css/bulma.min.css" rel="stylesheet" />
<nav class="navbar" role="navigation">
<div class="navbar-brand">
<div class="navbar-item">
<p class="title"><span>title here</span></p>
</div>
<div class="button navbar-burger is-active">
<span></span>
<span></span>
<span></span>
</div>
</div>
<div class="navbar-menu is-active">
<div class="navbar-end">
<div class="navbar-item">
<div>menu item 1</div>
</div>
<div class="navbar-item">
<div>menu item 2</div>
</div>
</div>
</div>
</nav>
<div class="section content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque, provident quasi nulla fugiat libero nemo tempora adipisci quisquam voluptatibus blanditiis suscipit cupiditate obcaecati numquam, odio eligendi repellendus! Commodi, mollitia, modi!</p>
</div>
You can try the styling below:
.navbar {
position:relative;
}
.navbar-end {
position:absolute;
left:50%;
}
.navbar-menu {
max-width:50%;
}
This way ".navbar" will be positioned relative to its current position which will allow ".navbar-end" to be positioned in relation to ".navbar" as one of its descending children elements ("position:absolute" positions the element in relation to its nearest positioned ancestor). You may have to tweak the left % if it doesn't fit exactly and also specify either top or bottom offsets if the menu items get displaced vertically.

Horizontally Aligning Bootstrap 3.3.5 Columns inside a row in Wordpress

Re: Bootstrap 3.3.5 in Wordpress
I have not seen this issue before. All I want to do is align 3 x .col-md-4 .wells inside a row, but they just won't align. The first div is always a bit higher; see screenshot.
This is the code:
<div class="row">
<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-4 col-md-offset-0 col-lg-4 col-lg-offset-0 text-center">
<a class="page-scroll" href="#how-to"><div class="home-plans well">
<br/>
<h2>1.</h2>
<p class="lead text-left">Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo.p>
</div> <!-- /well --></a>
</div> <!-- /col-md-4 -->
<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-4 col-md-offset-0 col-lg-4 col-lg-offset-0 text-center">
<div class="home-plans well">
<br />
<h2>2.</h2>
<p class="lead text-left">Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo.p>
</div> <!-- /well -->
</div> <!-- /col-md-4 -->
<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-4 col-md-offset-0 col-lg-4 col-lg-offset-0 text-center">
<div class="home-plans well">
<br />
<h2>3.</h2>
<p class="lead text-left">Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo.</p>
</div> <!-- /well -->
</div> <!-- /col-md-4 -->
</div>
Using f12 tools in explorer, the issue seems to be the below CSS code styling the row:
/*media all*/
.btn-group-vertical > .btn-group::after, .btn-group-vertical > .btn-group::before, .btn-toolbar::after, .btn-toolbar::before, .clearfix::after, .clearfix::before, .container-fluid::after, .container-fluid::before, .container::after, .container::before, .dl-horizontal dd::after, .dl-horizontal dd::before, .form-horizontal .form-group::after, .form-horizontal .form-group::before, .modal-footer::after, .modal-footer::before, .nav::after, .nav::before, .navbar-collapse::after, .navbar-collapse::before, .navbar-header::after, .navbar-header::before, .navbar::after, .navbar::before, .pager::after, .pager::before, .panel-body::after, .panel-body::before, .row::after, .row::before {
display: table;
content: " ";
}
This page is in Wordpress. I have tried the same code in a normal HTML page and the .well .col-md-4s align OK.
To get the cols to align I can just remove the row, but I want to use rows.
This has been killing me for nearly one day now. I have done loads of research and tried using clearfix and clear: both all over without joy. Can anyone shed some light on this please?
Edit: I also note that the two rows in the footer section are causing the page footer to have a scroll bar? So, I have had to remove those rows too. It seems that I can't use rows in Wordpress with Bootstrap anymore for some reason.
Hmm, it's really tricky to say without seeing more of the CSS but have you tried reformatting your first div as follows:
<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-4 col-md-offset-0 col-lg-4 col-lg-offset-0 text-center">
<div class="home-plans well">
<a class="page-scroll" href="#how-to">
<br/>
<h2>1.</h2>
<p class="lead text-left">Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo.p>
</a>
</div> <!-- /well -->
</div> <!-- /col-md-4 -->
Then styling the inner anchor element as follows:
CSS:
.well > a {
display: block;
}
I'd guess that it's something to do with the anchor element wrapping around the .well div?

Bootstrap - messed up #media and overflow issue

https://jsfiddle.net/537wen91/
I am using Bootstrap, and in the example, if you make html view wide, scrollbars disappear, when the view is narrow scrollbars show up. That is what I want. The problem starts when I am in the "narrow view": scroll down to the gray box, now expand html view, see how scrollbars are gone (good), but I also lost my text at the top (not good). Why is my text at the top gone?
Edited to clarify
This way it works: On page load - don't scroll anywhere and stretch the screen so that you see all colored columns on one row. You see some text at top, columns at the bottom, no scrollbar. This is how it should be.
This way it doesn't: Refresh page. Scroll down to the pink column. Now stretch it so that all colored columns appear on one row. See that my text at the top is gone? Why?
If this is still not clear, I would have to make a screen recording...
HTML
<div class="container-fluid">
<div class="row">
<div class="col-md-2">
<h2>title</h2>
</div>
<div class="col-md-4">
<h2>title</h2>
<p>2 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus et ultrices neque, vel vestibulum turpis. In ex nunc, vulputate at quam vitae, ultrices vestibulum velit. Phasellus lorem orci, maximus vitae tristique a, sollicitudin sed mauris. Donec ipsum nibh, pulvinar quis nulla at, cursus congue odio. Cras accumsan sem erat, volutpat elementum ante accumsan sed.</p>
</div>
<div class="col-md-4">
<h2>title</h2>
<p>bbb</p>
</div>
<div class="col-md-2">
<h2>title</h2>
<p>bbb</p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h2>title</h2>
</div>
</div>
<div id="see" class="row">
<div class="col-md-2" style="background-color: #FFC;">...</div>
<div class="col-md-2" style="background-color: #CCC;">...</div>
<div class="col-md-2" style="background-color: #CCC;">...</div>
<div class="col-md-2" style="background-color: #FC9;">...</div>
<div class="col-md-2" style="background-color: #CCF;">...</div>
<div class="col-md-2" style="background-color: #CCF;">...</div>
</div>
</div>
<nav class="navbar navbar-fixed-bottom">
<p>Place sticky footer content here.</p>
</nav>
CSS
html {
/*position: relative;
min-height: 100%;*/
height: 100%;
}
body {
overflow: hidden;
}
#see > div {
height: 1200px;
}
.navbar {
margin-bottom: 0;
min-height: inherit;
}
nav {
background-color: #222;
color: #666;
}
#media (max-width: 992px) {
body {
overflow: auto;
}
#see > div {
height: 500px;
}
}
JS
$(window).bind('resize load', function () {
if ($(this).width() <= 992) {
$('nav').removeClass('navbar-fixed-bottom');
} else {
$('nav').addClass('navbar-fixed-bottom');
}
});
As mentioned above, the issue is that #see div changes width but not height, and as the page was scrolled, the scrolling remains, leaving the text out of the viewport. Something like this (excuse my poor MSPaint skills):
One possible solution for that would be to scroll to the top of the page right before that change is made, so the text is always visible. You can achieve that just by adding a line of code:
$(window).scrollTop(0);
You can see it working here: https://jsfiddle.net/537wen91/12/
One possible CSS-only solution would be to, if the text height is constant, for the #see div add a height of calc(100% - HEIGHT_OF_TEXT). But I haven't tried this.
Try replacing:
body { overflow: hidden; }
with
body { overflow: auto; }
More info on Overflow values: http://www.w3schools.com/cssref/pr_pos_overflow.asp
You may have to define what happens in different view sizes, using Bootstrap's grid layout: http://getbootstrap.com/css/#grid
In your class, define grid sizes for these:
.col-xs- .col-sm- .col-md- .col-lg-
Add hidden-xs to your class to hide in extra-small, or hidden-md in medium views, and so on (in the Fiddle below, if you make the width of the result window narrow enough, you will see this happen,
When you go to a smaller screen size, let's say you want to display two "title" elements instead of four, you would change your HTML to this:
<div class="row">
<div class="hidden-xs col-md-2">
<div class="col-xs-6 col-md-4">
<div class="col-xs-6 col-md-4">
<div class="hidden-xs col-md-2">
</div>
This makes it so that on smaller screens, the only middle two elements will display as they will take up all 12 columns of the grid. Or you could make it so that on smaller views. Here is a Fiddle: https://jsfiddle.net/1dtnd59s/1/
Basically the column options for different display sizes have to be tweaked, and if you're ok with hiding certain elements on small displays, that will make it easier.

Can't seem to ever understand margin: 0 auto;

To my understanding all you need for margin: 0 auto; to center the content is:
display: block;
No floats
No absolute positioning
And a set width
Any ideas on why this isn't working?
I can apply text-align: center, and this will center is, however I just want to center with the margins. Any ideas.
Here is the pen: http://codepen.io/anon/pen/JovwoJ
It is working, you just don't have another element wrapped around it to see that it is centered and its width is set to 100% so you can't see that it is centered because it takes up the full width.
HTML & CSS:
.wrapper
{
width: 80%;
margin: 0 auto;
display: block;
}
<div style="width:100%">
<div class="wrapper">
<div class="nav-bg">
<div class="nav">
Solar Panel Kits
Solar Water & Pool
Portable Solar
Solar Panels
Solar System Parts
Emergency Solar
Sale
</div>
</div>
<div class="info1-bg">
<div class="info">
<div class="inner2">
<b>Free Delivery Wordwide</b>
<b>At vero eos et accusamus et iusto odio dignis</b>
</div>
</div>
<div class="info2">
<div class="inner2">
<b>Free Return For 90 Days</b>
<b>At vero eos et accusamus et iusto odio dignis</b>
</div>
</div>
<div class="info3">
<div class="inner2">
<b>Discount On Order Gift</b>
<b>At vero eos et accusamus et iusto odio dignis</b>
</div>
</div>
<div class="slider">
<img src="">
</div>
<div class="learnSolar">
<div class="learn1">
<b>Get Insight On</b>
<b>Solar Basics</b>
</div>
<div class="learn1">
<b>Get Insight On</b>
<b>Solar Rebates</b>
</div>
<div class="learn1">
<b>Schedule A Free</b>
<b>Solar Analysis</b>
</div>
</div>
<div class="footer-bg">
<div class="footer">
Customer Service
Contact Us
Blog
Links
Learn More
FAQ
About Us
</div>
</div>
</div>
</div>
</div>
text-align is just centering the display:inline elements that are inside of your wrapper div.
You have the wrapper set to 100% width. With that, Margin: 0 auto; will automatically center the wrapper by giving it left and right margins of 0, since it's already taking up the entire page. You could set the wrapper to width: 90%;, or anything besides 100% to have it center correctly.
Further, the divs that are inside of your wrapper are all block elements, meaning they will take up the entire width of your wrapper. Which is why, even when it's centered correctly with Alex W's answer, it isn't centered by our standards.
Edit: I posted this because the previous answer didn't mention the wrapper width problem before it was edited, and I'm not able to make comments yet.

How to achieve equal height of inner divs and float:left at the same time?

I had four containers with four inner divs, floated left with auto height.
<div class="box">
<div class="head">
Heading text
</div>
<div class="image">
Image
</div>
<div class="text">
Integer posuere erat a ante venenatis dapibus posuere velit aliquet.
</div>
<div class="link">Link text</div>
.box{
width: 150px;
float: left;
}
Complete jsFiddle:
http://jsfiddle.net/H8w32/
To make the inner divs height the same througout all of the four containers, I changed the floated layout, and used display:table instead.
<div class="table">
<div class="row head">
<div class="cell">Heading text</div>
<div class="cell">Here is a lot longer heading text to examplify</div>
<div class="cell">Heading text</div>
<div class="cell">Heading text</div>
</div>
<div class="row image">
<div class="cell">Image</div>
<div class="cell">Image</div>
<div class="cell">Image</div>
<div class="cell">Image</div>
</div>
<div class="row text">
<div class="cell">Integer posuere erat a ante venenatis dapibus posuere velit aliquet.</div>
<div class="cell">Integer posuere erat a ante venenatis dapibus posuere velit aliquet.</div>
<div class="cell">Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Integer posuere erat a ante. Dapibus posuere velit aliquet.</div>
<div class="cell">Integer posuere erat a ante venenatis dapibus posuere velit aliquet.</div>
</div>
<div class="row link">
<div class="cell">Link text</div>
<div class="cell">Link text</div>
<div class="cell">Link text</div>
<div class="cell">Link text</div>
</div>
</div>
-
.table{
display: table;
}
.row{
display: table-row;
}
.cell{
display: table-cell;
width: 150px;
}
http://jsfiddle.net/Wh7Pm/1/
This looks exactly like I wanted it to. But now I lost the perks from using float, specifically the ability to add more containers and that they automatically got pushed in to the next "row", or that they get pushed to a new row if the viewport is too small to fit them. The containers are added to the same "row" if I add more containers to my display:table layout.
Is this possible to solve? I want the behaviour from both display:table (equal height of heading divs and text divs) and float:left.
Not possible currently without using javascript to set heights. You may want to look into the spec for CSS flexible boxes, which is the real CSS-only solution to this problem.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes
Adoption of flexbox is still in the early stages, but if you're building a cutting edge app that targets mostly modern browsers, you may be able to get away with it.

Resources