How to center div within col-md-12 in bootstrap - html

I have problem with centering components within col-md-12. For example I have 12 columns with <h1> or <h2> inside. I want to <h1> to be centered.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="naglowek">
<h1>Something</h1>
</div>
</div>
</div>
</div>
I tried to access css class text-center or block-center to divs, but everytime <h1> was floated to the left. I need have this centered and set padding on my own. How can I solve this?

<div class="col-md-12 text-center"> works for bootstrap, no need to add any style unless you have already some style for h1. even if that's the case, you can use .text-center for h1
seems you are using width in that parent div. don't do that ever if you need content to be 100% width. and as per background, if you need 100% area occupy the background, you can simply use it in the div if not, you better use it with some pseudo classes :before or :after

In bootstrap 4
I know it's not the direct answer to this question but it may help someone
to center the childs horizontally, use bootstrap-4 class
justify-content-center
to center the childs vertically, use bootstrap-4 class
align-items-center
but remember don't forget to use d-flex class with these
it's a bootstrap-4 utility class, like so
<div class="d-flex justify-content-center align-items-center" style="height:100px;">
<span class="bg-primary">MIDDLE</span>
</div>
copy and paste this snippet to check how it works
Note: make sure to add bootstrap-4 utilities if this code does not work.
Hope this will help someone.

Add class="text-center" to div.
<div id="naglowek" class="text-center">
<h1>Something</h1>
</div>
text-center class is from bootstrap.

You can add the text-center class to the h1.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="naglowek">
<h1 class="text-center">Something</h1>
</div>
</div>
</div>
</div>

Related

Floating Images CSS

I'm new to CSS and Bootstrap .
Whenever I try to make a picture stick to left position so I can write text on the right side, I use this Bootstrap class rounded float-left
The problem is the image gets over other content in my webpage like the footer.
I know because it's floating .
How can I fix this ? Thank you.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="container-fluid">
<div class="row-fluid">
<div class="span2">
<img class="rounded float-left" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ3rcu77cYuBPWjgD_I1CLIQ0hoD6iArebYfA&usqp=CAU">
</div>
<div class="span10">
<h2> This is a Course Name </h2>
</div>
</div>
</div>
Try like this
Try like this please
<div class="container-fluid">
<div class="row-fluid">
<div class="span2 clearfix">
<img class="rounded float-left" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ3rcu77cYuBPWjgD_I1CLIQ0hoD6iArebYfA&usqp=CAU">
</div>
<div class="span10">
<h2> This is a Course Name </h2>
</div>
</div>
You are correct that you should use the "clearfix" class (under the condition that you use float).
You can achieve what you want by inserting a "clearfix" where appropriate.
I attach an image below. I sincerely hope this helps.
upload image
The easiest solution is to use the clearfix utility class, ideally adding it to the div that encloses your float (which would be the div with the class span-2 in your code above). This utility class ensures that the floats are cleared properly, and avoids problems like the one you encountered.
use flexbox or bootstrap grid, or even <table></table>
flexbox tutorial: https://www.tutorialspoint.com/flexbox/index.htm

Simple css container do not align elements as it should

I have a simple css div with some styling from bootstrap and this is the fiddle
https://jsfiddle.net/w9gyc0t5/
This is the code
<!--
Bootstrap docs: https://getbootstrap.com/docs
-->
<div class="container">
<div class="row">
<div class="col-md-6">
<div>
<h5 class="text-danger"><b>General</b></h5>
<h5 class="text-danger float-right"><b>General</b></h5>
</div>
</div>
</div>
I was expecting the H5 element to align perfectly with the element that has been floated right. I know i could move the element floated right to align with the one on the left, but i want to know what is causing this behavior and are there new css innovations such as flex box that solves this problem.
The simplest solution will be to remove the class float-right from second <h5> and put in on the first <h5> like this:
<div>
<h5 class="text-danger float-right"><b>General</b></h5>
<h5 class="text-danger"><b>General</b></h5>
</div>
Here is the fiddle for it: https://jsfiddle.net/6h20bwqc/3/
N.B. But FlexBox is recommended as it is a modern and clean solution.
Just add class="d-flex justify-content-between align-items-center" to the wrapper div and remove float-right for a flex-box solution:
Here is Fiddle for it: https://jsfiddle.net/6h20bwqc/4/

Bootstrap Grid does not fit divs in one line

I can't understand why this doesn't work. There are 12 columns but it's work only with 11
HTML
<section>
<div class="container">
<div class="row">
<div class="advantages col-12">
<div class="col-4">one</div>
<div class="col-4">two</div>
<div class="col-4">three</div>
</div>
</div>
</div>
</section>
SASS
.advantages
text-align: center
div
display: inline-block
You should remove col-12 and let your col-4 sit within .row. A col must be immediately preceeded by a row.
Since 4+4+4 = 12, you don't have to define your previous col-12, and you shouldn't have to set anything to inline-block.
It's what #Jay Kariesch said, but you can also keep advantages in the same div as row, like this:
<div class="advantages row">
This way you will keep advantages class working for all the divs inside.

div between container and row in bootstrap

I want to wrap a group of rows in bootstrap while still having everything in one container. Are there any rules/conventions about this regarding bootstrap?
I put a fiddle below of what i want to do, but i am not sure if this is the right method.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="extraDiv" id="topdiv">
<div class="row">
<div class="col-md-12">Bla</div>
</div>
<div class="row">
<div class="col-md-6">Bla</div>
<div class="col-md-6">Bla</div>
</div>
</div>
<div class="extraDiv" id="bottomdiv">
<div class="row">
<div class="col-md-12">Bla</div>
</div>
</div>
</div>
As far my understanding it is totally okay the way you use .row class in a .container class. For further understanding you can check this link Must Bootstrap container elements include row elements?
Seems fine - though it depends what you want to do with the gutters etc.
Generally you just want one .container wrapping the whole page (to give you the main central column), and within that you can nest as many rows/cols as you need to.

Display inline-flex but keep full width

I have place two div inside an inline-flex div one of the two divs width reduces. I'm using bootstrap:
<section>
<div class="container">
<div class="flexx">
<div class="foo">
....
</div>
<div class="col-md-10">
<div class="card">
<div class="card-block">
...
</div>
</div>
</div>
</div>
</div>
</section>
Basically, foo class should be inline with col-md-10 which it does but col-md-10 gets small instead it should still be at 100%. Am I doing it correct? I'm not strong with css/scss.
I'm not sure I entirely understand your issue. inline-flex items do not default to full width. You will need to add some css for that to happen since in the css for bootstrap the flex-grow property is set to 0;
I think adding one style and a class will fix your issue, again if I understand you right.
// to your html
<div class="col foo">
....
</div>
// to your css
[class^="col"] {
flex-grow: 1;
}
Check out this pen for help