Simple layout via Flex Box - css

I am trying to make a common html structure for my website using bootstrap 4, the common elements are: sidebar, h1 and content.
The main task is arranging them properly based on the screen size:
In mobile view they should go in the following order: h1, sidebar and content, like this:
In desktop view they should go like this:
Here is my codepin: https://codepen.io/anon/pen/ejLroV

As said earlier in above comments, you can achieve that using - bootstrap order-classes https://getbootstrap.com/docs/4.1/layout/grid/#order-classes, pleae have a look at the below working snippet, hope it helps :)
aside {
background: grey;
height: 300px;
}
h1 {
background: green;
}
section {
background: #c1c1c1;
height: 300px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row d-lg-block">
<h1 class="col-12 order-1 col-lg-9 float-lg-right order-lg-2">H1 Title</h1>
<aside class="col-12 order-2 col-lg-3 float-lg-left order-lg-1">Sidebar</aside>
<section class="col-12 order-3 col-lg-9 float-lg-right order-lg-3">Content</section>
</div>
</div>

Related

Bootstrap 4 How to Overlap Two Divs that Stack Responsively

I'm a backend guy and trying to figure out a few details for a project we have that's using Bootstrap 4.
Simply put, we want to create the layout that's executed here:
https://codepen.io/mediumandmessage/pen/xVeXop
(this example and the code below is from the original Bootstrap 3 example I found, not Bootstrap 4)
HTML:
.somesection {margin-top:50px!important;}
body {
font-size:17px;
font-family:Helvetica Neue;
}
img {max-width:100%;}
.overlay-text {
font-size:3.5vw;
float:right;
width:65%; /*important*/
bottom:21vw; /*important*/
padding: 25px;
background:#f7f7f7;
}
<div class="container somesection">
<div class="row col-xs-6">
<img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&auto=compress&dpr=1&crop=entropy&fit=crop&w=1087&h=725&q=80">
</div>
<div class="col-xs-3 col-sm-offset-4 overlay-text">
This is some text that should partially overlay.
</div>
</div>
However, that example uses Bootstrap 3 and breaks in Bootstrap 4 (the text displays horizontally below the image) and also does not stack the divs responsively.
I've tried screwing around with absolute and relative positioning, etc. it became a lot of code to execute cleanly and make responsive and I was hoping someone out there may have some insight into implementing in pure Bootstrap4...
If anyone out there can share any expertise here, I'd greatly appreciate it!
You could add a transform to your overlay column (you may need to cancel this with a media query for your smaller screens).
Please note in the html below, I have fixed your code to work with boostrap 4 - columns have to be inside a row (they cannot be on a row) and I don't think there is a -xs class any more
.overlay-text {
/* these two are needed - the align self makes the column not stretch the whole height of the image column and the translate moves the column over the image */
align-self: flex-start;
transform: translateX(-20%);
/* the following are for example only */
background-color: #ffffff;
padding:20px;
}
<div class="container somesection">
<div class="row">
<div class="col col-sm-6">
<img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&auto=compress&dpr=1&crop=entropy&fit=crop&w=1087&h=725&q=80" class="w-100">
</div>
<div class="col col-sm-3 col-sm-offset-4 overlay-text">
This is some text that should partially overlay.
</div>
</div>
</div>
Example bootply
Just add position:relative; to the .overlay-text
You can also adjust the value of bottom
.somesection {margin-top:50px!important;}
body {
font-size:17px;
font-family:Helvetica Neue;
}
img {max-width:100%;}
.overlay-text {
font-size:3.5vw;
float:right;
width:65%; /*important*/
bottom:21vw; /*important*/
padding: 25px;
background:#f7f7f7;
position:relative;
}
<div class="container somesection">
<div class="row col-xs-6">
<img src="https://images.unsplash.com/photo-1459664018906-085c36f472af?format=auto&auto=compress&dpr=1&crop=entropy&fit=crop&w=1087&h=725&q=80">
</div>
<div class="col-xs-3 col-sm-offset-4 overlay-text">
This is some text that should partially overlay.
</div>
</div>

Background-color is visible inside div with bootstrap col class

I'm pretty sure that the problem has some simple solution but I am not able to find one yet other than overriding the bootstrap's default behavior which doesn't seem like a good idea to me.
The issue is simple. When I have this:
#main {
margin-top: 100px;
width: 100px;
background-color: black;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div id="main" class="col-lg-12 col-md-12 col-sm-12">
</div>
</div>
</div>
You can see a black stripe on the screen even though there is not content.
After some inspection/investigation I understood that bootstrap has this default style:
// Prevent columns from collapsing when empty
min-height: 1px;
I've read this Bootstrap min height question and several other posts on the topic so it seems that it is intended to have this style.
However, and I guess this is not something uncommon, I have a page with a search functionality and when the user perform a search and select any of the results, a report should be displayed below the search but until this happens I have several stripes, where the content should be displayed at some point and I would like them to not be visible.
I can think of some JS workarounds, but wonder if it's possible to do this with pure CSS? I can always override the default value of min-height to 0 but I guess the bootstrap guys had a good reason to add this, and maybe there's a known way to avoid displaying stripes with the background color when no content is available.
If you do not feel like overriding bootstrap style, then the :empty selector can be used to remove background
#main {
margin-top: 100px;
width: 100px;
background-color: black;
}
#main:empty {
background: none;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div id="main" class="col-lg-12 col-md-12 col-sm-12"></div>
</div>
</div>
And idea is to hide it with a small inset box-shadow but you need to pay attention to transparency:
.main {
margin-top: 100px;
width: 100px;
box-shadow:0 1px 0 inset #fff;
background-color: black;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="main col-lg-12 col-md-12 col-sm-12">
</div>
</div>
</div>
Another idea is to rely on gradient for the background and you can adjust slightly the position:
.main {
margin-top: 100px;
width: 100px;
background: linear-gradient(black,black) 0 1px no-repeat;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="main col-lg-12 col-md-12 col-sm-12">
</div>
</div>
</div>
you can also add a border-top transparent and adjust the background-clip
.main {
margin-top: 100px;
width: 100px;
border-top:1px solid transparent;
background:black;
background-clip:padding-box;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="main col-lg-12 col-md-12 col-sm-12">
</div>
</div>
</div>
Bootstrap by default set min-height:1px; on his cols, so you have to set min-height:0px; to avoid this.

Bootstrap not possible to put a well in a col?

I want to put a .well div (or a button whatever), in the green area of this picture:
here
Here is my code for now:
<div class="container-fluid">
<div class="row row1" style="height: 400px;">
<div class="col-lg-4 col-lg-offset-1">
aaaa <!-- BUTTON OR WELL HERE -->
</div>
</div>
css:
.row1 {
background: url("appart.jpg");
background-size: 100% 400px;
background-repeat: no-repeat;
}
Problem, if I try this code:
<div class="container-fluid">
<div class="row row1" style="height: 400px;">
<div class="col-lg-4 col-lg-offset-1">
<div class="well well-sm">aaa
</div>
</div>
I obtain this result:
here
How can I put the .well div inside the green area (and have its height and width inferior than the green area ones)?
I am not exactly sure of what you are asking but... keep in mind the bootstrap col sizes. If you use lg it will get confused if the screen gets smaller than the lg size. Better then to use xs if you don't want to declare the other sizes.
Also, don't do inline css.
Is this what you are looking for?
Fiddle here
HTML:
<div class="container-fluid">
<div class="row backgroundPicHere">
<div class="col-xs-4 col-xs-offset-1 green">
<div class="well">Basic Well</div>
</div>
</div>
</div>
CSS:
.backgroundPicHere {
top: 20px;
height: 200px;
background: red;
}
.green{
background: green;
height: 200px !important;
}
.green .well{
position: relative;
top: 50%;
transform: translateY(-50%);
}
PS. Use any way you deem fitting for the vertical centering of the well. This is just one of many... some might disapprove. DS
EDIT: Correcting code indentation

How to create two rows in a bootstrap without scrolling using one with pixel and another with percentage?

How to create two rows in a bootstrap without scrolling using one with pixel and another with percentage ?
<div class="container-fluid">
<div class="row main_header">
sadf
</div>
<div class="row main_second">
<div class="col-lg-2 main_left">
test
</div>
<div class="col-lg-10 main_right">
test
</div>
</div>
</div>
How to create two rows in a bootstrap without scrolling using one with pixel and another with percentage ?
.main_header{
height: 80px;
background-color: #606060;
color: #fff
}
.main_left{
height: 100%;
background-color: #D8D8D8;
} .main_right{
height: 100%;
}
.main_second{
height: 100%;
}
I think you don't need to use any additional CSS to achieve what you want as Bootstrap already have a very established Grid System. Please see my following example in the following fiddle!
All what I have used is the following lines with Twitter Bootstrap V3.3.4:
<div class="row">
<div class="row">
<div class="col-sm-4" style="background-color:red;">.col-sm-4</div>
</div>
<div class="row">
<div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div>
</div>
</div>
And you can add any additional divs within those two divs.
Learn about the Grid System very well to use this via these kind of tutorials.
And it's better to look at the following StackOverflow answer, I guess.

Creating a fixed footer with Bootstrap

I am building an app that uses Bootstrap. I want this app to have a footer. The footer needs to "stick" to the bottom. In other words, if the content is larger than the height of the screen, the footer should still be visible, the content goes under it. If the content takes less than the height of the screen, I still need the footer to stick tothe bottom. I tried using the sticky footer. However, that doesn't work. Currently, I am trying the following:
Here's My Plunker
My HTML looks like this:
<div class="footer">
<div class="container text-center">
<button class="btn btn-warning"><span class="glyphicon glyphicon-filter"></span></button>
<button class="btn btn-warning"><span class="glyphicon glyphicon-th"></span></button>
</div>
</div>
How do I build a footer that permanently sticks to the bottom? I'm basically trying to build an "action bar" that is visible only when the site runs on a phone.
Thank you for your help.
use the following code
.footer {
background-color: #f5f5f5;
bottom: 0;
height: 60px;
position: fixed;
width: 100%;
}
you should change the footer position :
.footer {
background-color: #f5f5f5;
bottom: 0;
height: 60px;
position: fixed; /*change it*/
width: 100%;
}
Bootstrap comes with its nav elements ready to roll as a footer.
Simply create your element and add these classed navbar navbar-default navbar-fixed-bottom.
<footer>
<div class="navbar navbar-default navbar-fixed-bottom" id="footer">
<div class="container">
<p>this is your footer that sticks to the bottom</p>
</div>
</div>
</footer>
You can then expand on this by splitting the containing div into blocks with something like
<div class="row">
<div class="row">
<div class="col-xs-8 col-sm-6">
Level 2: .col-xs-8 .col-sm-6
</div>
<div class="col-xs-4 col-sm-6">
Level 2: .col-xs-4 .col-sm-6
</div>
</div>
</div>
the above would go inside the container div
as shown here http://jsfiddle.net/showcaseimagery/5y14pqgv/

Resources