I am working on design of the website that's using Bootstrap 4. I wanted to add to this sticky sidebar on the right. My general vision was to have two boxes - 1 (content) and 2 (sidebar).
I managed to place boxes correctly for devices except mobile. Then for mobile I wanted to have sidebar on top of content, so 2 on top of 1.
This part is a bit of a struggle. There is no problem in stacking them correctly. Problem is the fact, that box 2 is overlapping 1. And I am not sure how to fix this.
I have following code
<style>
.content-section {
min-height: 2000px;
}
.sidebar-item {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.make-me-sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
}
</style>
<div class="container mt-4">
<div class="row">
<div class="col-md-8">
<div class="content-section card card-body">
<div>1</div>
</div>
</div>
<div class="col-md-4 order-first order-md-last">
<div class="sidebar-item">
<div class="make-me-sticky">
<div class="card card-body">2</div>
</div>
</div>
</div>
</div>
</div>
You can see demo here:
https://www.codeply.com/go/IiDkjH70EY
<style>
.content-section {
min-height: 2000px;
}
.sidebar-item {
position: relative; /* Change From absolute TO relative */
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.make-me-sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
}
</style>
<div class="container mt-4">
<div class="row">
<div class="col-md-8">
<div class="content-section card card-body">
<div>1</div>
</div>
</div>
<div class="col-md-4 order-first order-md-last">
<div class="sidebar-item">
<div class="make-me-sticky">
<div class="card card-body">2</div>
</div>
</div>
</div>
</div>
</div>
Related
I want to position a div according to the picture:
I'm successful so far by using Bootstrap's row class and using z-index in my CSS. But when I resize the browser, it's not responsive, it just floats off the right side of the page. By the way, I'm using position: absolute (I read online that I have to use this in order to make use of z-index). Is there any other more elegant way to do this? I want it to be responsive but can't seem to find any other workaround than the wonky one I implemented.
Code:
#div2 {
float: inherit;
position: absolute;
top: inherit;
left: 60%;
width: 320px;
height: 1290px;
z-index: 5;
}
<div class="container">
<div class="div-container">
<div class="row">
<div id="div1">
<p>Div 1</p>
</div>
<div id="div2" align='center'>
<p>Div 2</p>
</div>
</div>
<div class="row">
<div id="div3">
<p>Div 3</p>
</div>
</div>
</div>
</div>
You need to make use of the nested rows inside a column. See here - Bootstrap Nesting. Ignore the CSS here as it is for snippet styling and height is used for ignoring the content.
.B {
min-height: 130px;
background: #393276;
margin-bottom: 20px;
}
.A {
min-height: 100px;
margin-bottom: 20px;
background: #393276;
}
.C {
min-height: 250px;
background: #393276;
}
div {
text-align: center;
color: #fff;
font-size: 32px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="container mt-4">
<div class="row">
<!-- First Column -->
<div class="col-sm-6">
<!--Rows nested inside a column-->
<div class="row">
<div class="col-12">
<div class="A">A</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="B">B</div>
</div>
</div>
</div>
<!-- Second Column -->
<div class="col-sm-6">
<div class="C">C</div>
</div>
</div>
</div>
I have used flexbox to keep responsive design and some margin positioning to keep the formation together.
.container{
display: flex;
flex-wrap: wrap;
width: 150px;
}
.div1, .div3{
margin-right: 5px;
width: 50px;
height: 50px;
}
.div2{
margin-right: 5px;
width: 50px;
height: 110px;
}
<div class="container">
<div class="div1"> div1 </div>
<div class="div2"> div2 </div>
<br/>
<div class="div3" style="margin-top: -55px;"> div 3 </div>
</div>
What does my css for fixed-left, fixed-right and content have to be such that the left and right divs are fixed, the content div is max width less the width of the two fixed width div and that the divs don't roll one under the other even if the width of the screen is less than (fixed-left.width + fixed-right.width)?
<div class="row" id="row01">
<div class="fixed-left">
<div class="fixed-left-a">001</div>
<div class="fixed-left-b">Item 1</div>
</div>
<div class="content">
<div class="row">
<div class="col-xs-4 col-md-3">1,234</div>
<div class="col-xs-4 col-md-3">1,234</div>
<div class="col-xs-4 col-md-3">1,234</div>
<div class="col-xs-4 col-md-3">1,234</div>
</div>
</div>
<div class="fixed-right">
<span>A</span>
</div>
</div>
<div class="row" id="row02">
...
</div>
I want to use bootstrap 3 grid within the main content, but have fixed attributes of every row that don't require the left and right pieces to be variable sized.
Edit:
I want the output to look something like the attached image.
You can use calc to set the width of the content
.fixed-left, .fixed-right {
position: fixed;
width: 100px;
top: 0;
bottom: 0;
}
.fixed-left {
left: 0;
background: yellow;
}
.fixed-right {
right: 0;
background: purple;
}
.content {
background: pink;
width: calc(100% - 200px);
margin: 0 auto;
height: 100vh;
}
<div class="row" id="row01">
<div class="fixed-left">
<div class="fixed-left-a">001</div>
<div class="fixed-left-b">Item 1</div>
</div>
<div class="content">
<div class="row">
<div class="col-xs-4 col-md-3">1,234</div>
<div class="col-xs-4 col-md-3">1,234</div>
<div class="col-xs-4 col-md-3">1,234</div>
</div>
</div>
<div class="fixed-right">
<span>A</span>
</div>
</div>
see the snippet in full-page and try to resize the browser window
you set margin to content margin: 0 100px; like this.
Here 100px is your fixed element width.
* {
margin: 0;
padding:0;
box-sizing:border-box;
}
.fixed-left, .fixed-right {
position: fixed;
width: 100px;
top: 0;
bottom: 0;
}
.fixed-left {
left: 0;
background: yellow;
}
.fixed-right {
right: 0;
background: purple;
}
.content {
background: pink;
width: 100%;
margin: 0 100px;
height: 100vh;
}
<div class="row" id="row01">
<div class="fixed-left">
<div class="fixed-left-a">001</div>
<div class="fixed-left-b">Item 1</div>
</div>
<div class="content">
<div class="row">
<div class="col-xs-4 col-md-3">1,234</div>
<div class="col-xs-4 col-md-3">1,234</div>
<div class="col-xs-4 col-md-3">1,234</div>
</div>
</div>
<div class="fixed-right">
<span>A</span>
</div>
</div>
https://stackoverflow.com/a/29532261/2193381 offers a very simple solution that appears to work. It is not the approach I had in mind, and may have unintended effects, but is interesting nonetheless.
In short, pull-left and pull-right the fixed width divs before the central floating width div is set.
How can I align div element at the bottom of parent element while using bootstrap col?
.wrapper {
background-color: green;
height: 200px;
}
.bottom {
position: relative;
}
.bottom-div {
height: 200px;
position: absolute;
bottom: 0;
}
<div class="wrapper">
<div class="col-md-3 bottom">
<div class="bottom-div"> TEST1 </div>
</div>
<div class="col-md-6">
TEST2
</div>
<div class="col-md-3">
TEST3
</div>
</div>
bottom div element does not align at bottom. What is correct way of doing this? Thanks.
UPDATE: Div element runs out of wrapper (it basically moves up)
Not sure exactly what you're trying to do, as #CBroe said flexbox would be the best way but try this:-
/* CSS used here will be applied after bootstrap.css */
.wrapper{
background-color:green;
height: 200px;
position: relative;
}
.bottom-div{
height: 50px;
width: 50px;
position: absolute;
bottom:0;
left: 0;
background-color: red;
}
.testclass {
height: 100%;
}
<div class="wrapper">
<div class="col-md-3 testclass">
<div class="bottom-div"> TEST1 </div>
</div>
<div class="col-md-6">
TEST2
</div>
<div class="col-md-3">
TEST3
</div>
</div>
Please check this two photos
I don't know how to get ".myDivInTheGrid" in boxed bootstrap div. Any suggestions?
I have something like this...
<div class="fluid-container">
<div class="col-md-6"></div><!-- Div with image -->
<div class="col-md-6">
<div class="myDivInTheGrid"></div>
</div><!-- div with content -->
</div>
I have created a working example with a picture in the div you showed in the picture. I made it for col-md-* but you can do the same for larger grid system. If your screen is small, stretch the browser. Check it out HERE
The code is like this:
HTML:
body
<div class="container-fluid">
.fluid-container
<div class="row">
<div class="col-md-6">
.col-md-6
</div>
<div class="col-md-6">
<div class="row">
.col-md-6
</div>
<img class="row" src="http://s22.postimg.org/8z6hs0mch/Chrysanthemum.jpg"/>
</div>
</div>
</div>
CSS:
body{
background:#8EC34D;
color: white;
}
.container-fluid {
background: #81AD4B;
position: relative;
top: 40px;
padding-bottom: 40px;
}
.col-md-6 {
background:#769C47;
height: 300px;
}
img {
width: 256px;
height: 192px;
}
I'm solving HTML layout problem similar to drawing responsive Tic Tac Toe board in HTML + CSS and without any JS. Here is how I define the board's layout:
<div class="board">
<div class="lines">
<div class="line">
<div class="cell">
<div class="cell-content"></div>
</div>
<div class="cell">
<div class="cell-content"></div>
</div>
<div class="cell">
<div class="cell-content"></div>
</div>
</div>
<div class="line">
<div class="cell">
<div class="cell-content"></div>
</div>
<div class="cell">
<div class="cell-content"></div>
</div>
<div class="cell">
<div class="cell-content"></div>
</div>
</div>
<div class="line">
<div class="cell">
<div class="cell-content"></div>
</div>
<div class="cell">
<div class="cell-content"></div>
</div>
<div class="cell">
<div class="cell-content"></div>
</div>
</div>
</div>
</div>
and here is the corresponding CSS:
.board {
position: relative;
height: 100%;
width: 100%;
border:1px solid black;
box-sizing:border-box;
}
.board:before {
content:"";
display: block;
padding-top: 100%;
}
.lines {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.line {
width: 100%;
}
.cell {
float: left;
width: 33.3333%;
border:1px solid black;
box-sizing:border-box;
}
.cell:before {
content:"";
display: block;
padding-top: 100%;
}
.cell-content {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
Here I do not set any sizes besides width: 33.3333%. The height of the board and fields is set using the Height equals width with pure CSS approach.
And I would note that almost everything is great. The only problem here: sometimes the sum of widths/heights of board cells are less than the width/height of the board. It means that I can see the gap between last field border and the board border. I can reproduce it with Chrome or FF, but it never happens in IE. Is there a way to fix this?
The demo is available on the jsfiddle (the red line is what I'm trying to get rid of)
UPDATE: It happens in IE also, not sure why I did not saw it before.
Simple Fix is to give the .line Elements overflow: auto;
.line {
overflow: auto;
}
and your good to go :D