I have an HTML defined as follows:
<div id="container" className='row'>
<div className='col-sm-9 col-md-6 col-lg-8'>
</div>
<div className='col-sm-3 col-md-6 col-lg-4'>
<button id="bot" />
</div>
</div>
So, I want the button with the id of bot (or its container div) to be placed at the bottom relative to the container div with the class row.
I tried to do this:
#container
{
position: relative;
}
#bot
{
position: absolute;
bottom: 0;
}
But, this changes the design considerably, as it will push the bot to the left and on top of its sibling div. As you can see I'm using Bootstrap for dividing it into grids. How can I push this button to the bottom with CSS in my case?
Use d-flex at div and align-self-end for button
Example : https://jsfiddle.net/guiljs/rupoc45j/
<div class='col-sm-3 col-md-6 col-lg-4 bg-success d-flex'>
Lorem ipsum
<br />
<button id="bot" class=" align-self-end">
Button at Bottom!
</button>
</div>
If you're using React, just change class to className as your sample code.
Using display:flex; you can make the contents of the second column align to the end of the column.
.full-height {
min-height: 100vh;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="container py-3 full-height">
<div class='row'>
<div class='col-sm-9 col-md-6 col-lg-8'>
This column has content. This column has content. This column has content. This column has content. This column has content. This column has content. This column has content. This column has content. This column has content. This column has content. This
column has content. This column has content. This column has content.
</div>
<div class='col-sm-3 col-md-6 col-lg-4 d-flex flex-column justify-content-end'>
<button>Button</button>
</div>
</div>
</div>
You're looking for Bootstrap's align-items-end property. Here is a runnable example:
#container {
position: relative;
height: 100px;
width: 300px;
background: gray;
}
#bot {
background: black;
width: 50px;
height: 50px;
}
#first {
height: 100%;
background: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div id="container" class='row align-items-end'>
<div id="first" class='col-sm-9 col-md-6 col-lg-8'>
</div>
<div class='col-sm-3 col-md-6 col-lg-4'>
<button id="bot" />
</div>
</div>
If you only want just that one item aligned to the bottom, see Guilherme de Jesus Santos's answer for an example using align-self-end.
Try #bot's container css
as
'{
height:100%;
}`
Related
I am trying to create a layout that would be responsive using Bootstrap 4. The problem I am having is when two divs wrap vertically for a smaller device the top div seems to push the lower div down exceeding the parent container (please see the images below). I want the lower div to fit with in the container, how can I achieve this using Bootstrap 4 or minimal css?
I added a yellow border on the bottom of the second div so we can see the push.
Large device view
Small device view
html,
body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
.bottom-border {
border-bottom: 50px solid yellow;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<body>
<div class="container-fluid p-0 h-100">
<div class="row h-100">
<div class="col-md-8 bg-primary h-md-100 ">
<div class="d-md-none text-center bg-primary">
<h5>Left Section</h5>
</div>
<div class="d-none d-md-block m-3">
<h1>Left Section</h1>
</div>
</div>
<div class="col-md-4 h-100 text-center bg-danger bottom-border">
<h4>Right Section</h4>
</div>
</div>
</div>
</body>
After you explained the significance of the border height, now I see what you mean, as mentioned previously, your h-100 classes are the issue.
That class tells the element to use 100% of its parents height, since you put that class on the main container and on the row elements, those two are using the full screen's height since the container's parent is the browser window and the row parent is the container.
When you use that same class on the children div, in this case, the red div, it also tries to use the full height of its parent (the row), but it does not take into consideration the other item inside (the blue div), if you remove the h-100 class from the red div, then each of them will use half the available space.
html,
body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
.bottom-border {
border-bottom: 50px solid yellow;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="container-fluid h-100">
<div class="row h-100 flex-column flex-md-row">
<div class="col-md-8 bg-primary">
<div class="d-md-none text-center bg-primary">
<h5>Left Section</h5>
</div>
<div class="d-none d-md-block m-3">
<h1>Left Section</h1>
</div>
</div>
<div class="col-md-4 flex-grow-1 text-center bg-danger bottom-border">
<h4>Right Section</h4>
</div>
</div>
</div>
</body>
If I understand correctly, you'd wish to keep the left and right div keep doesn't go on top of eachother?
col-md means everything that the window size is above 768px, keep that number of columns. if you use col-sm, it would be everything above 576px. If you just say col-number, you'll keep it responsive reguardeless of the container width.
html,
body {
height: 100%;
margin: 0;
padding: 0;
width: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
.bottom-border {
background-color: yellow;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<body>
<div class="container-fluid p-0 ">
<div class="d-flex flex-row flex-wrap bd-highlight text-center ">
<div class="col-md-8 ">
<div class=" h-100 bg-primary">left</div>
</div>
<div class="flex-column col-md-4">
<div class=" bg-danger ">Right</div>
<div class=" bottom-border ">Bottom</div>
</div>
</div>
</div>
</body>
Also, since you're using Bootstrap 4, I'd also recommand checking Flex, which you basically put d-flex class on the parent and all the child will become responsive. You can make them even more precices with columns like you've did, but Flexbox is pretty powerful and really braindead to apply when you know how.
I am a HTML / CSS beginner and I can't fix a problem. My problem is that when I zoom in my footer follows me.
By the way, I am using bootstrap 4
html{
position: relative;
min-height: 100%;
}
.Footer {
position: absolute;
bottom: 0;
width: 100%;
height: 60px; /* Set the fixed height of the footer here */
line-height: 60px;
background-color: #f5f5f5;
}
<footer class="Footer">
<div class="container-fluid">
<div class="col">
<!-- Les lanngues (Footer) -->
<div class="col-lg-3 col-xs-2">
EN
GER
FR
</div>
</div>
<div class="col">
<!-- L'adresse E-mail (Footer) -->
<div class="col-lg-5 col-xs-5" id="EmailFooter">
<span class="">E-mail : asdpawd#bluewin.ch</span>
</div>
<div class="col">
<!-- Telephone (Footer) -->
<div class="col-lg-3 col-xs-6">
<span id="Tel-Footer" >Tél : xxxxx</span>
</div>
</div>
</div>
</footer>
Not zoomed in : https://prntscr.com/pi19m7
Zoomed in : https://prntscr.com/pi19v3
When I zoom in, it just stop into the button
Welcome to our community. It is better to use a felx-box than absolute.
html,
body {
height: 100%;
}
#page-content {
flex: 1 0 auto;
}
#sticky-footer {
flex-shrink: none;
}
/* Other Classes for Page Styling */
body {
background: #000;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<body class="d-flex flex-column">
<div id="page-content">
<div class="container text-center">
<div class="row justify-content-center">
<div class="col-md-7">
<h1 class="font-weight-light mt-4 text-black">Sticky Footer using Flexbox</h1>
</div>
</div>
</div>
</div>
<footer id="sticky-footer" class="py-4 bg-dark text-white-50">
<div class="container text-center">
<small>Copyright ©</small>
</div>
</footer>
</body>
Setting the position to absolute may be the issue. This property will not be responsive to zoom ins and will remain in the same position regardless of the movement of other elements. Attempt removing the "position: absolute;" and instead giving the footer a margin-top if you want it to be spaced away from the content above.
I think that position: absolute is the issue here. Try to remove it and then run it. Sometimes working with position can be little tricky as well so try to get over with it and then apply your footer and see the results.
My homepage consists of multiple blocks(top part/mid part/bottom part). I've created a row for each block. I want to add some space between my blocks in Bootstrap. Can I simply give my rows id's and add some margin, or is this wrong?
Structure of my code:
<div class="container" id="ho_main_content">
<div class="row">
<div class="col-md-12 text-center"></div>
</div>
<div class="row">
<div class="col-md-12"></div>
</div>
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
</div>
This "answer" of mine should really be a comment; however, I don't have enough rep.
For an answer, yes, give the divs with the row class another class, probably something like this, spacing the top and bottom of each 10px:
.part {
margin: 10px 0;
}
An important thing to think about when using frameworks like bootstrap is that it isn't the end of the world if you modify the components or spacing or something. Some things won't look like you want them to; just give them extra classes, or if you are desperate, use the !important flag. It was built on the same technology, after all.
In bootstrap 5 I add g-0 to g-5 class with row class to add space around each col.
EX.
<div class="row g-3">
<div class="col">...</div>
<div class="col">...</div>
</div>
https://getbootstrap.com/docs/5.0/layout/gutters/
/*you can create your own custom css for use here is some example*/
.border {
border: 1px solid red; /* just to make sure space between blocks*/
}
.margin-top {
margin-top: 5px;
}
.nopad{
padding:0 ;
}
div[class*='spacer-'] { display: block; }
.spacer-mini { height: 20px; }
.spacer-small { height: 40px; }
.spacer-medium { height: 60px; }
.spacer-big { height: 100px; }
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container" id="main_content">
<div class="row border margin-top">
<div class="col-md-12 text-center">text1</div>
</div>
<div class="row border margin-top">
<div class="col-md-12">text2</div>
</div>
<div class="spacer-mini"></div> <!-- Using Spacer-Mini and avoiding the margin top -->
<div class="row">
<div class="col-md-6 col-xs-6 border">part1</div>
<div class="col-md-6 col-xs-6 border">part2</div>
</div>
</div>
</body>
I am trying to implement a design from my graphic designer, which whilst looks cool is giving me some headaches as i don't know how to implement in bootstrap.
We have a call to action section, which aligns with the 12 column grid system on its left and right extremes.
It also stretches to the view-port edges:
On the left we have red background stretching all the way to the view-port edge.
On the right we have a grey background image stretching all the way to the view-port edge.
I haven't been able to find a search term for what I am looking to achieve let alone where to start (other than have the cta use the background for the entire width, then overlay a left element over the top).
Any idea on how to code the below graphical layout in bootstrap please?
<section class="cta" style="background: grey; position: relative">
<div class="red" style="position: absolute; left: 0; width: 10%; background: red"></div>
<div class="text-outer">
<div class="container">
<div class="row">
<div class="col-xs-6">left</div>
<div class="col-xs-6">right</div>
</div>
</div>
</div>
</section>
Using <div class="container-fluid"> as a starting point; I am guessing at your page's layout. Let's try this:
See below:
.cntn {
border: 1px red solid; /* you can remove this (not needed) */
}
.red {
background-color: red;
text-align: right;
margin: 0; /* optional */
width: 100px; /* adjust to suit your needs */
float: left;
}
.cta {
margin: 0; /* optional */
float: right;
border: 1px solid green; /* you can remove this (not needed) */
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- make container fluid -->
<div class="container-fluid">
<div class="row">
<!-- heading area: hexagon -->
<div class="red">
<img src="http://placehold.it/100/100" />
</div>
<!-- heading area: call-to-action -->
<section class="cta">
Action
</section>
</div>
<div class="row cntn">
<div class="col-xs-6">left</div>
<div class="col-xs-6">right</div>
</div>
</div>
Simply change 'div class="container"' to 'div class="container-fluid"'
Something like this? Where black should be the grey gradient and max-width:400px could be anything.
.cta {
overflow-x: hidden;
position: relative
}
.text-outer .container {
width: 100%;
max-width: 400px;
background: grey;
z-index: 2;
position: relative;
}
.text-outer:before,
.text-outer:after {
content: "";
position: absolute;
width: 50%;
height: 100%;
top: 0;
}
.text-outer:before {
background-color: red;
left: 0;
}
.text-outer:after {
background-color: black;
right: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<section class="cta">
<div class="text-outer">
<div class="container">
<div class="row">
<div class="col-xs-6">left</div>
<div class="col-xs-6">right</div>
</div>
</div>
</div>
</section>
jsFiddleLink
I created with 3 divs as Left Center and Right but if you want to use Left and center then create your own class. Probably following will work
.custom {
width:calc(100% - (50% - 768px/2));
}
.custom {
width:calc(100% - leftCellWidth);
}
You can set height of left as per height of hex image.
Use jumbotron class outside the class container for full-width, as explained here.
HTML:
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="red col-xs-4">
</div>
<div class="grey col-xs-8">
</div>
</div
</div>
</div>
CSS:
.red {
background: url('awesomeredimage.png');
background-size: cover;
}
.grey {
background: url('awesomegreyimage.png');
background-size: cover;
}
All your divs should be wrapped in the container div. And as some others have also suggested: container-fluid helps.
Within container fluid you can add a regular container for the rest of your content. My code below explains this.
You could take the easy route and just use the entire cta image you've posted as a clickable image with .img-responsive in a col-xs-12. In that case my fix takes you about 2 minutes:
<section style="background: grey; position: relative">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<img src="/img/cta.jpg" class="img-responsive">
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="container">
<!-- All you other content here-->
</div>
</div>
</div>
</div>
</section>
But you could also hack the design into cols, as I try to show in the code snippet below. Of course you need to tweak and decide on the exact sizes yourself.
<section style="background: grey; position: relative">
<div class="container-fluid">
<div class="row">
<div class="col-xs-3 red">
<img src="/img/hexagon.png" class="img-responsive pull-right">
<!--and give this img a negative margin to flow over to the grey area-->
</div>
<div class="col-xs-1 grey-image"></div>
<div class="col-xs-3 grey-image">
<h3 class="text-center">Call to action</h3>
<p class="text-center">Discount etcetera</p>
</div>
<div class="col-xs-5 grey-image">
<button class="btn center-block">Request quote</button>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="container">
<!-- All you other content here-->
</div>
</div>
</div>
</div>
</section>
Use class="container-fluid" instead of class="container" and than do this style:
.container-fluid {
margin-right: auto;
margin-left: auto;
padding-left: 0px;
padding-right: 0px;
}
I'm having a tough time wrapping my mind around how to achieve vertical spacing with the bootstrap grid system. I want one box to be 1/3 from the top and left and another to take up 1/3 from the bottom, but be full-width. I have three rows right now spanning 12, 6, and 12 columns respectively:
An example layout using draw.io:
Within the body tag of index.html:
<body>
<br>
<br>
<br>
<!-- Content -->
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-0 col-sm-8 col-sm-offset-0 col-xs-12 col-xs-offset-0">
<div class="col-md-12 col-sm-12 col-xs-12 text-left">
<h1><strong>I am strong<strong><span class="lead">i am less strong</span></h1>
</div>
</div>
</div>
</div>
<!-- JavaScript -->
<script src="assets/third-party/js/jquery.min.js"></script>
<script src="assets/third-party/js/bootstrap.min.js"></script>
</body>
One trick is to force the height of your container to fill the whole page with a line of jQuery. You don't need jQuery for this, but I noticed that you already include it on your page.
The markup:
<div class="container full-page-height">
<div class="row offset-top-third">
<div class="col-md-6 col-md-offset-3 white">
<h1>Hey you!</h1>
</div>
</div>
<div class="row offset-bottom-third">
<div class="bgimg">Background image here</div>
</div>
</div>
The CSS:
.bgimg {
background: #333;
color: #eee;
}
.white {
background: #fff;
}
.offset-top-third {
position:relative;
top: 33.333%;
height: 33.333%;
background: #888;
}
.offset-bottom-third {
position:relative;
top: 66.667%;
height: 33.333%;
background: #888;
}
The Javascript:
$(function() {
var h = window.innerHeight;
$(".full-page-height").css({height: h});
});
The bootply.
If your vertical columns are div's, then give each one a margin value in pixels to space them away from each other. Then set the width of each column to a percent value so they fit the whole window.