Div not being centered when using margin: auto; (Bootstrap) - html

I have a working example website from the bootstrap template website and was analysing the code to understand how they created it. Namely how they centered the text on the first page located here. The code for that snipplet is,
<header class="masthead text-center text-white d-flex"> <!-- Background image set here -->
<div class="container m-auto">
<div class="row">
<div class="col-lg-10 mx-auto">
...
</div>
<div class="col-lg-8 mx-auto">
...
</div>
</div>
</div>
</header>
So then I noticed that the m-auto was doing the centering. However, when I attempt to create it from scratch for myself and create this basic code,
<style>
#frontpage {
height: 100vh;
width: 100vw;
}
</style>
<section id="frontpage">
<div class="container m-auto">
<div class="row">
<div class="col-8">
<h1> YOU CAN DO IT </h1>
</div>
<div class="col-8">
<h1> I should be centered </h1>
</div>
</div>
</div>
</section>
It doesn't center at all.
What am I forgetting here?
I would suspect that the container gets centered inside it's parent, which takes up the page.

you have copied the code but styles not included for them if guess correctly. anyway, I have corrected please try this one.
<style>
#frontpage {
height: 100vh;
width: 100%;
}
.center-me{
margin: auto;
width: 60%;
text-align: center;
}
</style>
<section id="frontpage">
<div class="center-me">
<div >
<h1> YOU CAN DO IT </h1>
</div>
<div >
<h1> I should be centered </h1>
</div>
</div>
</section>

Is this what you're after?
Version 1
https://codepen.io/panchroma/pen/YdEWZJ
HTML
<section id="frontpage">
<div class="container m-auto">
<div class="row">
<div class="col-8">
<h1> YOU CAN DO IT </h1>
</div>
<div class="col-8">
<h1> I should be centered </h1>
</div>
</div>
</div>
</section>
CSS
#frontpage{
background-color:pink;
height: 100vh;
width: 100vw;
}
.col-8{
border:1px solid grey;
margin-left:auto !important;
margin-right:auto !important;
text-align:center;
}
Version 2
This also centers the text vertically
https://codepen.io/panchroma/pen/KbygzX
HTML
<section id="frontpage">
<div class="container m-auto">
<div class="row vert-align">
<div class="col-8">
<h1>YOU CAN DO IT</h1>
</div>
<div class="col-8">
<h1> I should be centered </h1>
</div>
</div>
</div>
</section>
CSS
#frontpage{
background-color:pink;
height: 100vh;
width: 100vw;
}
.col-8{
margin-left:auto !important;
margin-right:auto !important;
text-align:center;
}
.row.vert-align{
position: absolute;
top: 50%;
left:50%;
-ms-transform: translateY(-50%);
transform: translate(-50%, -50%);
}

Related

i can't get the div out of normal flow, it stays below

i want to add a "contact" card to the website's right-top and sticky, but i have a div there so it's blocking the contact card. how can i fix that?
https://imgur.com/a/cnLDL69
<div class="card card-contact">
<div class="card-header"><h2>Social Media</h2>
</div>
<div class="card-body"><p class="body-card">
Facebook, Instagram, Twitter
</div>
</div>
---CSS---
.card-contact{
width: 20rem;
height: auto;
}
.body-card {
font-size: 1rem;
}
Assuming you are using bootstrap 4 or above you can edit your code like this so as to make your card sticky to top right
.card-contact{
width: 20rem;
height: auto;
}
.body-card {
font-size: 1rem;
}
.main-card{
position:fixed;
top:0;
right:0;
}
<section class="container-fluid">
<div class="row">
<div class="col-lg-4 main-card ">
<div class="card card-contact">
<div class="card-header">
<h2>Social Media</h2>
</div>
<div class="card-body">
<p class="body-card">
Facebook, Instagram, Twitter
</p>
</div>
</div>
</div>
</div>
</section>

Bootstrap 4 image half page width

How can I make responsive layout like this?
I need only few tips how to do it. I dont need full source code. Sorry for my english.
My code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-6">
<img src='https://via.placeholder.com/350x150' />
</div>
<div class="col-md-6">
CONTENT
</div>
</div>
<div class="row">
<div class="col-md-6">
CONTENT
</div>
<div class="col-md-6">
<img src='https://via.placeholder.com/350x150' />
</div>
</div>
</div>
I want img to be half page size.
Check fiddle and snippet below. It is fully responsive and it is working in any device.
body {
padding:15px 0;
}
.content-box {
position:relative;
width:100%;
min-height:350px;
background:#ccc;
margin:15px 0;
}
.img-box.left {
position:absolute;
top:-15px;
right:-15px;
bottom:-15px;
width:calc( 50vw - 15px );
background:red;
}
.img-box.right {
position:absolute;
top:-15px;
left:-15px;
bottom:-15px;
width:calc( 50vw - 15px );
background:red;
}
#media only screen and (max-width: 768px) {
.img-box.left, .img-box.right {
position:absolute;
top:0px;
left:0px;
right:0px;
bottom:0px;
width:100%;
background:red;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class=container>
<div class="row">
<div class="col-md-6">
<div class="content-box">
<div class="img-box left">
image
</div>
</div>
</div>
<div class="col-md-6">
<div class="content-box">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="content-box">
</div>
</div>
<div class="col-md-6">
<div class="content-box">
<div class="img-box right">
image
</div>
</div>
</div>
</div>
</div>
</body>
https://jsfiddle.net/Sampath_Madhuranga/tfz87bqe/18/
This works for you...Check and let me know if need any help.
Thanks.
here's your layout with bootstrap 4. put image into box--wide but keep in mind pink divs are absolutely positioned so they will not resize the parent - they will take full parent's height which depends on content in gray box
to make this responsive - my suggestion is to wrap images into .col-12and for desktop set it's to position static while images are absolute so they will align inside .container, not .col-12 and on mobile images should be just static
.container {
background: #5f408c;
padding: 20px;
}
.container h1 {
text-align: center;
color: white;
}
.box {
min-height: 200px;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-weight: bold;
text-transform: uppercase;
font-size: 40px;
}
.box--wide {
position: absolute;
left: 15px;
right: calc(50% + 15px);
}
.box--wide:last-child {
right: 15px;
left: calc(50% + 15px);
}
.box--pink {
background: #ec008c;
}
.box--gray {
background: #272827;
}
<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">
<div class="col-12">
<h1>TITLE</h1>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="box box--pink box--wide">image</div>
<div class="col-12 col-md-6 offset-md-6 ">
<div class="box box--gray">content</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-12 col-md-6">
<div class="box box--gray">content</div>
</div>
<div class="box box--pink box--wide">image</div>
</div>
</div>

Bootstrap row appearing on top of jumbotron, should be below jumbotron

This is the markup for my index page. I have the jumbotron and below it is a rowthat should be appearing below the jumbotron. But it is not, it is appearing on top of the jumbotron for some reason I can't figure out. ( I have close to none experience in bootstrap)
<div class="jumbotron jumbotron-billboard">
<div class="img"></div>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1>Jumbotron</h1>
<p>
Lorem ipsum is the best
</p>
Sign Up
</div>
</div>
</div>
</div>
<div class="row main-row">
<div class="col-md-3">
text1
</div>
<div class="col-md-3">
text2
</div>
<div class="col-md-3">
text3
</div>
<div class="col-md-3">
text4
</div>
</div>
And here is the .cssfor the jumbotron
.jumbotron-billboard .img {
margin-bottom: 0px;
opacity: 0.8;
color: #fff;
background: #000 url("gogn1.jpg") center center;
width: 100%;
height: 50vh;
background-size: cover;
overflow: hidden;
position:absolute;
top:0;left:0;
z-index:1;
}
.jumbotron {position:relative;padding:50px;}
.jumbotron .container {z-index:2;
position:relative;
z-index:2;
}
It would be very nice if some-one could inform me on why the row isn't just appearing below the jumbotron.
Here is a link to the problem here is a bootply for the problem bootply.com/9vxeaMQ6Xf

Height of rows to fill column - Bootstrap layout

I know this may be easy for most of you but I'm stuck with this issue.
I need to implement this design:
Layout Design
... and for now I've got this Current layout.The general structure is a row with col-3 and col-9, this col-9 has two rows, one for name and job title, and the other one for statistics in the page (col-3 for each of them). I need them to fill the height of his parent, but height property doesn't work. Which could be a clean solution? Thanks a lot.
Here is the structure, it's pretty simple tho.
<div class="row profile-header">
<div class="col-md-3 user-image text-center">
<img ...>
<span>..</span>
</div>
<div class="col-md-9">
<div class="row">
<div class="col-md-12">
<h2 class="text-white">...</h2>
<h4 class="text-muted">...</h4>
</div>
</div>
<div class="row text-center">
<div class="col-md-3 stat">
<h3>...</h3>
<small>...</small>
</div>
...
</div>
</div>
Use position: relative on the parent and then position:absolute; bottom: 0; on the children.
Nice explanation there.
Flexbox would be my recommendation although CSS tables might be an option too.
Codepen Demo
Snippet Demo (view Fullscreen)
.user-image {
background: pink;
}
.user-image img {
display: block;
margin: auto;
}
.profile-header {
display: flex;
}
.right {
background: #c0ffee;
display: flex;
flex-direction: column;
}
.stats {
margin-top: auto;
}
.stat {
background: lightgrey;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="row profile-header">
<div class="col-md-3 user-image text-center">
<img src="http://www.fillmurray.com/300/200" alt="" />
<span>User Ranking</span>
</div>
<div class="col-md-9 right">
<div class="row">
<div class="col-md-12">
<h2 class="text-white">User Name</h2>
<h4 class="text-muted">reference</h4>
</div>
</div>
<div class="row text-center stats">
<div class="col-md-3 stat">
<h3>Some Stat</h3>
<small>Some Stat</small>
</div>
<div class="col-md-3 stat">
<h3>Some Stat</h3>
<small>Some Stat</small>
</div>
<div class="col-md-3 stat">
<h3>Some Stat</h3>
<small>Some Stat</small>
</div>
<div class="col-md-3 stat">
<h3>Some Stat</h3>
<small>Some Stat</small>
</div>
</div>
</div>

Center text in div with background not 100% width

I'm using Bootstrap 3.
I need to center a text (h2) in a div with a background. I need that his background not 100% width.
I cannot center them in their column(s) (see screenshot of wrong behavoiur).
This is HTML:
<div class="col-sm-6">
<div class="category-title">
<h2>{{ #value->category }}</h2>
</div>
</div>
And this is the CSS:
div.category-title{display:inline-block;
background-color:#E1001A;
padding:10px;
text-align:center;
margin:0 auto}
div.category-title > h2{color:#FFF;
text-align:center;
text-transform:uppercase;
font-size:200%;}
Thank you
Add class text-center to the parent element of category-title
div.category-title {
display: inline-block;
background-color: #E1001A;
padding: 10px;
margin: 0 auto
}
div.category-title > h2 {
color: #FFF;
text-transform: uppercase;
font-size: 200%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="col-sm-6 text-center">
<div class="category-title">
<h2>Offerte Pala</h2>
</div>
</div>
<div class="col-sm-6 text-center">
<div class="category-title">
<h2>Offerte Pala</h2>
</div>
</div>
</div>
Try class="text-center" from official Bootstrap site. http://getbootstrap.com/css/
<p class="text-left">Left aligned text.</p>
<p class="text-center">Center aligned text.</p>
<p class="text-right">Right aligned text.</p>
Try text-center in div class. Something like following.
<div class="col-sm-6 text-center">
<div class="category-title">
<h2>{{ #value->category }}</h2>
</div>