Please see this pen for a quick example http://codepen.io/Irish1/pen/mymBje
html
<div class="container A">
<div class="col-xs-12 col-md-4 border1 height"></div>
<div class="col-xs-12 col-md-4 border2 height"></div>
<div class="col-xs-12 col-md-4 border3 height"></div>
</div>
<div class="container B border1">
<div class="col-xs-12 col-md-4 border1 height"></div>
<div class="col-xs-12 col-md-4 border2 height"></div>
<div class="col-xs-12 col-md-4 border3 height"></div>
</div>
css
.height {
height: 20px;
}
.B {
width: 325px;
height: 100px;
}
.border1 {
border: 1px black solid;
}
.border2 {
border: 1px blue solid;
}
.border3 {
border: 1px red solid;
}
container A has the width of the browser window and contains 3 columns that go from 33% width to 100% width when the window width is below 768px
container B is the same set up accept that its width is only 350px. As you can see in the pen the 3 columns are 33% width.
I am sure this is working as intended but is it possible to make the grid relative to its containing div instead of the browser window? ie so that the divs in container B have 100% width because B's width is less than 768px.
This is setting the width to 30%, along with display:inline-block to all div child's of class container. See below how this alters the appearance:
.height {
height: 20px;
}
.B {
width: 325px;
height: 100px;
}
.border1 {
border: 1px black solid;
}
.border2 {
border: 1px blue solid;
}
.border3 {
border: 1px red solid;
}
.container div {
width: 30%;
display: inline-block;
}
<div class="container A">
<div class="col-xs-12 col-md-4 border1 height"></div>
<div class="col-xs-12 col-md-4 border2 height"></div>
<div class="col-xs-12 col-md-4 border3 height"></div>
</div>
<div class="container B border1">
<div class="col-xs-12 col-md-4 border1 height"></div>
<div class="col-xs-12 col-md-4 border2 height"></div>
<div class="col-xs-12 col-md-4 border3 height"></div>
</div>
Media Query approach:
div{
display:inline-block;
width:30%;
height:50px;
background:blue;
border:1px solid black;
margin:1%;
font-weight:bold;
font-size:30px;
text-align:center;
transition: all 0.8s;
}
#media screen and (max-width:768px)
{
div{
background:red;
display:block;
width:100%;
}
}
<div>A</div><div>B</div><div>C</div>
At this point I would like to mention my absolute hatred for bootstrap, mainly due to its lack of functionality. Like, seriously, it would be more beneficial, (and actually less time consuming) to write the css yourself, especially when you want to do anything 'out of the box'. I found bootstrap to be far too restrictive for any sort of 'further functionality'
Looks like the only way is to do it programmatically.
Found a solution in another StackOverflow question.
Basically you have a class that gives 100% width to element and based on parent width, this class is toggled.
.m {
width: 100%;
}
$('.somecontainer').on('resize',function(){
if ($('.somecontainer').width() < 140) {
$('.somecontainer').addClass('m');
} else {
$('.somecontainer').removeClass('m');
}
});
Try this removing the padding of the bootstrap class or you can overright it by giving your own class
HTML:
<div class="container regular">
<div class="col-xs-12 col-md-4 border1 height"></div>
<div class="col-xs-12 col-md-4 border2 height"></div>
<div class="col-xs-12 col-md-4 border3 height"></div>
</div>
<div class="container regular">
<div class="col-xs-12 col-md-offset-4 col-md-4 border1 regular">
<div class="col-xs-12 col-md-4 border1 height"></div>
<div class="col-xs-12 col-md-4 border2 height"></div>
<div class="col-xs-12 col-md-4 border3 height"></div>
</div>
</div>
CSS:
.height {
height: 20px;
}
.target {
width: 325px;
height: 100px;
}
.col-md-4{
padding:0;
}
.border1 {
border: 1px black solid;
}
.border2 {
border: 1px blue solid;
}
.border3 {
border: 1px red solid;
}
Try with this css:
.abs {
position: relative;
display: inline-block;
}
Related
I have a list of items that I am presenting in a flexbox table. My actual display has more columns, but in this question, I'll use just 2 - the 'category' and 'item' names. The data is presented in category order. Where items repeat for a category, only the first item for the category should have the category listed, for subsequent ones I want the category cell empty. As this can scroll off the top of the screen, I'd like to make this cell sticky, as I do for the headings.
However, as the cell is a child of the row, not the container of the table, it scrolls up with the row. You can see this in the first table in the example below, the '.first_item' CSS class does not hold the caption in place.
I concluded that the only way to achieve this is to insert a div before the row to present the category and make this sticky - class cat_head in the 2nd table in the example below. This works, however, it knocks the first item down and does not align where I want it to. I therefore set its height to 0px, which brings the item row back into alignment, but then have to put the text within an absolute positioned div within this so that I can set a background colour to stop text for the categories from overlaying each other.
It works really well and is exactly how I'd like it to operate - except that because the height is set to 0px, the absolute div extends below the bottom of the table as it is scrolled off the top of the screen and overwrites what is below it - you can see in the example it overwrites "Text below".
I also have to manually add an 'alt' class name on alternate rows to get background colouring rather than relying on ':nth-child(even)' - but I can live with this.
Has anyone got any suggestions for stopping it falling out of the bottom of the container, or indeed a better way of doing this? Thanks.
.pad {
height: 50px;
background-color: yellow;
}
.fill {
height: 120vh;
background-color: yellow;
}
.container {
background-color:#ffffff;
}
.row {
display: flex;
flex-flow:row wrap;
}
.head {
position: sticky;
top: 0px;
background-color: #e0e0e0;
font-weight: bold;
border-bottom: 1px solid #000000;
height: 24px;
z-index: 3;
}
.cell {
flex: 0 0 100px;
padding: 5px;
}
.first_item {
position: sticky;
top: 25px;
z-index: 2;
}
.table_1 .row:nth-child(even),
.table_2 .alt {
background-color: #f0f0f0;
}
.cat_head {
position: sticky;
top: 25px;
height:0px;
overflow: visible;
z-index: 2;
}
.cat_text {
position: absolute;
width: 80px;
padding: 0;
background-color: rgba(8,156,213,1);
color: #ffffff;
border: 1px solid #000000;
border-radius:5px;
margin:2px 0 0 4px;
text-align:center;
}
<div class="pad"></div>
<div class="container table_1">
<div class="row head">
<div class="cell">Category</div>
<div class="cell">Item</div>
</div>
<div class="row cat">
<div class="cell first_item">Category 1</div>
<div class="cell">Item 1-1</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 1-2</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 1-3</div>
</div>
<div class="row cat">
<div class="cell first_item">Category 2</div>
<div class="cell">Item 2-1</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 2-2</div>
</div>
</div>
<div class="pad">Text below</div>
<div class="container table_2">
<div class="row head">
<div class="cell">Category</div>
<div class="cell">Item</div>
</div>
<div class="cat_head">
<div class="cat_text">Category 1</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 1-1</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 1-2</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 1-3</div>
</div>
<div class="cat_head">
<div class="cat_text">Category 2</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 2-1</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 2-2</div>
</div>
</div>
<div class="fill">Text below</div>
OK. So I went to sleep on it and realised that the sticky positioning when scrolling off the top of the viewport is based on the bottom of its div. Therefore, I need to shift the div below the row it is on - as its 0px high and that's where its 'bottom' should be.
I also need to move the contained absolute div above it rather than below so its bottom aligns to it. I initially put a -24px top margin on the absolute div, but this stopped the sticky positioning on its parent for some reason. I therefore put a -24px top margin on the sticky div, which worked. However, it also dragged the next row up, so I also added a 24px bottom margin which resolved that.
I also added a containing div around each category's rows so that the sticky heading scrolls off screen when the next category gets to the top.
All working fine.
.pad {
height: 50px;
background-color: yellow;
}
.fill {
height: 120vh;
background-color: yellow;
}
.container {
background-color:#ffffff;
}
.row {
display: flex;
flex-flow:row wrap;
}
.head {
position: sticky;
top: 0px;
background-color: #e0e0e0;
font-weight: bold;
border-bottom: 1px solid #000000;
height: 24px;
z-index: 3;
}
.cell {
flex: 0 0 100px;
padding: 5px;
}
.alt {
background-color: #f0f0f0;
}
.cat_head {
position: sticky;
top: 27px;
height:0px;
margin: -24px 0 24px 4px;
overflow: visible;
z-index: 2;
}
.cat_text {
position: absolute;
width: 80px;
padding: 0;
background-color: rgba(8,156,213,1);
color: #ffffff;
border: 1px solid #000000;
border-radius:5px;
text-align:center;
}
<div class="pad"></div>
<div class="container">
<div class="row head">
<div class="cell">Category</div>
<div class="cell">Item</div>
</div>
<div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 1-1</div>
</div>
<div class="cat_head">
<div class="cat_text">Category 1</div>
</div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 1-2</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 1-3</div>
</div>
</div>
<div>
<div class="row">
<div class="cell"></div>
<div class="cell">Item 2-1</div>
</div>
<div class="cat_head">
<div class="cat_text">Category 2</div>
</div>
<div class="row alt">
<div class="cell"></div>
<div class="cell">Item 2-2</div>
</div>
</div>
</div>
<div class="fill">Text below</div>
I am having a problem on displaying 3 Bootstrap columns in the ContentArea.
When I use Developer tools I can find them, they are at the bottom of the ContentArea, out of sight. I have tried adding the top attribute, and even the margin-top attribute but can't get those columns to move up into view in the ContentArea.
Can someone point out to me where I am going wrong?
$(document).ready(function () {
$('#menu-toggle').toggleClass('menu-hidden');
$('#menu-toggle').on('click', function () {
//alert();
$('#SidebarWrapper').toggleClass('menu-hidden');
$('#ContentArea').toggleClass('content-grew');
$('#SubCategories').toggleClass('slide-left');
});
});
body {
}
#TheLayoutContainer {
position:fixed;
z-index:1000;
width:100%;
height:100%;
border:1px solid black;
background-color:pink;
left:0px;
}
#menu-toggle {
}
#SidebarWrapper {
width:200px;
height:100%;
margin-top:0;
margin-left:0;
position:fixed;
z-index:2000;
background-color:black;
transition:1s;
}
#SidebarWrapper.menu-hidden {
width:50px;
transition:1s;
}
#ContentArea {
position: relative;
height: 100%;
background-color: purple;
z-index: 2000;
left: 325px;
margin-top:0px;
transition:1s;
padding-right:20px;
}
#ContentArea #SubCategories {
margin-left:-125px;
width:125px;
height:100%;
border:1px solid #72cad3;
background-color:#72cad3;
}
#SubCategories.slide-left {
margin-left:-75px;
width:125px;
height:100%;
border:1px solid #72cad3;
background-color:#72cad3;
}
#ContentArea.content-grew {
height: 100%;
left: 175px;
transition:1s;
}
#HeaderControlMenu {
height:45px;
width:100%;
background-color:yellow;
top:0px;
position:fixed;
z-index:2000;
left:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div id="HeaderControlMenu">
<button id="menu-toggle" class="btn btn-success">Click</button>
</div>
</div>
<div id="TheLayoutContainer">
<div id="SidebarWrapper">
</div>
<div id="ContentArea">
<div id="SubCategories">
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="col-md-4 col-lg-4 col-sm-4 col-xs-4" style="border:1px solid green">A</div>
<div class="col-md-4 col-lg-4 col-sm-4 col-xs-4" style="border:1px solid green">B</div>
<div class="col-md-4 col-lg-4 col-sm-4 col-xs-4" style="border:1px solid green">C</div>
</div>
</div>
</div>
In addition to what #Temani Afif said, you need to enclose the parent (element that contains the rows) in an element with class container.
<div class="container">
<div class="row">
<div id="HeaderControlMenu">
<button id="menu-toggle" class="btn btn-success">Click</button>
</div>
</div>
<div class="row">
<div id="TheLayoutContainer">
<div id="SidebarWrapper">
</div>
<div id="ContentArea">
<div id="SubCategories">
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="col-md-4 col-lg-4 col-sm-4 col-xs-4" style="border:1px solid green">A</div>
<div class="col-md-4 col-lg-4 col-sm-4 col-xs-4" style="border:1px solid green">B</div>
<div class="col-md-4 col-lg-4 col-sm-4 col-xs-4" style="border:1px solid green">C</div>
</div>
</div>
</div>
</div>
</div>
Here is a fiddle
https://getbootstrap.com/docs/4.0/layout/grid/
Because none of the other answers and comments have mentioned all of the mistakes in your code, I will do that in my answer:
Don't use "naked" rows in Bootstrap. Do this instead: If you need a full-width container, use a div (or another suitable element like section etc.) with the class .container-fluid. Otherwise, use a div with the class .container and put your row(s) inside that container.
A Bootstrap .row must always have at least one Bootstrap column (.col-*) inside and all of your content must reside inside columns. Don't put any content directly into a .row.
.col-xs-* classes don't exist in Bootstrap 4 (anymore). Use .col-* instead.
Replace col-lg-12 col-md-12 col-sm-12 col-xs-12 with col-12 because that will do the exact same job. (because Bootstrap 4 is "mobile-first")
Replace col-md-4 col-lg-4 col-sm-4 col-xs-4 with col-4 because that will do the exact same job.
Side note: You can put some content directly into a Bootstrap .container or .container-fluid i.e. without using Bootstrap rows and columns. However, that would make sense only in a few special cases because usually your content wouldn't be responsive if you'd do that. The exceptions to this rule are things like Bootstrap navbars and jumbotrons.
This question already has answers here:
How can I make Bootstrap columns all the same height?
(34 answers)
Closed 6 years ago.
I have a couple of widgets in line depending on screen size. Here is my layout:
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some description goes here
</div>
</div>
</div>
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some very long description goes here
</div>
</div>
</div>
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some description goes here
</div>
</div>
</div>
and styles are the following:
.widget {
width: 100%;
margin: 10px;
padding: 10px;
background-color: white;
//height: auto;
height: 80px;
border: 1px silver solid;
font-size: 18px;
}
.name {
font-weight: bold;
height: 40px;
color: #082654;
}
.description {
color: #4AB6E1;
}
Here is the jsfiddle
The problem is that I want to be them of certain height: e.g.80px, cause its the size that will look nice for almost all cases, but rarely I have some longer content inside widget description and then it goes out of the box. So in this case I'd like its height to adapt to content size and all other widgets to its height too. When I set widgets height to auto - only single widget stretches in height and they look uneven. Any ideas how to change the styles so that to achieve what I need?
I you can use jQuery look at this JSFiddle
https://jsfiddle.net/cnoof9hb/
Uses the following to set the height:-
var maxheight = 0;
$('div div.widget').each(function () {
maxheight = ($(this).height() > maxheight ? $(this).height() : maxheight);
});
$('div div.widget').height(maxheight);
This seems like a perfect time for flexbox, I've made a container div to surround all 3 elements and given it display: flex; I've also given display: flex; to each div bellow that (as these are the bits that need to change size).
Have a look
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.container {
display: flex;
flex-wrap: wrap;
}
.container > div {
display: flex;
width: 33.33%;
}
.widget {
width: 100%;
margin: 10px;
padding: 10px;
background-color: white;
border: 1px silver solid;
font-size: 18px;
}
.widget .name {
font-weight: bold;
height: 40px;
color: #082654;
}
.widget .description {
color: #4AB6E1;
}
<div class="container">
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some description goes here
</div>
</div>
</div>
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some very long description goes here
</div>
</div>
</div>
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some description goes here
</div>
</div>
</div>
</div>
I hope this helps.
you can try using diplay:table
.widget {
width: 100%;
margin: 10px;
padding: 10px;
background-color: white;
//height: auto;
height: 80px;
display:table;
border: 1px silver solid;
font-size: 18px;
}
.name {
font-weight: bold;
height: 40px;
color: #082654;
}
.description {
color: #4AB6E1;
}
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some description goes here
</div>
</div>
</div>
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some very long description goes here
</div>
</div>
</div>
<div class="col-xl-4 col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="widget">
<div class="name">
Block name
</div>
<div class="description">
Some description goes here
</div>
</div>
</div>
Use float:left to float an element to make the parent element take the cumulative height of it's childrens' height. Also height:auto; to be used in the parent element, as the height is unpredictable.
.widget {
width: 100%;
margin: 10px;
padding: 10px;
background-color: white;
/*height: auto;*/ /* removed this */
border: 1px silver solid;
font-size: 18px;
/* Added these 2 lines */
float:left;
height: auto;
}
.name {
font-weight: bold;
height: 40px;
color: #082654;
}
.description {
color: #4AB6E1;
}
Try below code it does "Adapt height of the elements based on siblings height" but you may need to change it little bit to suit ur needs
<div class="t-row">
<div class="col">Some description goes here Some description goes here</div>
<div class="col">on goes here</div>
<div class="col">here</div>
<div class="col">Some description goes here Some description goes here Some description goes here Some description goes hereSome description goes here Some description goes here</div>
</div>
and css
.t-row{
display: table-row;
}
.col{
display: table-cell;
width: 25%;
border: 1px solid silver;
padding: 15px;
background-color: white;
}
see fiddle here
I'm having issue trying to align my div in Bootstrap 3.
Here's what I am trying to accomplish:
I have worked with pull and push but I guess I'm not good enough with it.
Code:
<div class="container">
<div class="row">
<div class="col-md-9 col-md-push-3 blue">
blue
</div>
<div class="col-md-3 col-md-pull-9 red">
red
</div>
<div class="col-md-9 col-md-push-3 orange">
orange
</div>
<div class="col-md-3 col-md-pull-9 green">
green
</div>
<div class="col-md-3 col-md-pull-9 purple">
purple
</div>
</div>
</div>
http://jsfiddle.net/bva5z74w/1/
I was watching this to see if anyone would come answer the obvious: don't use bootstrap push and pull for this. Use float right and no floats. Blue must be taller than red at the min-width, other than that I think this will work smoothly.
DEMO: http://jsbin.com/degeju/1/
CSS:
.blue {
background: blue
}
.red {
background: red
}
.orange {
background: orange
}
.green {
background: green
}
.purple {
background: purple
}
.red,
.blue,
.green,
.orange,
.purple {
height: 50px
}
#media (min-width:992px) {
.blue {
height: 600px;
float: right;
}
.red {
height: 300px;
float: none;
}
.orange {
height: 400px;
float: right;
}
.green {
height: 200px;
float: none;
}
.purple {
height: 200px;
float: none;
}
}
HTML
<div class="container">
<div class="row">
<div class="col-md-9 blue">
blue
</div>
<div class="col-md-3 red">
red
</div>
<div class="col-md-9 orange">
orange
</div>
<div class="col-md-3 green">
green
</div>
<div class="col-md-3 purple">
purple
</div>
</div>
</div>
The HTML you have will work perfectly, just remove the pull on the purple block:
HTML:
<div class="container">
<div class="row">
<div class="col-md-9 col-md-push-3 blue">
blue
</div>
<div class="col-md-3 col-md-pull-9 red">
red
</div>
<div class="col-md-9 col-md-push-3 orange">
orange
</div>
<div class="col-md-3 col-md-pull-9 green">
green
</div>
<div class="col-md-3 purple">
purple
</div>
</div>
</div>
http://www.bootply.com/dMNG75tSf0
Edit:
The float on the left elements is causing them to collapse to fit their contents. However, with media queries you can set the height to be the same height as the adjacent right element.
#media (max-width:1199px) {
.red {
height: 360px;
}
}
#media (min-width:1200px) {
.red {
height: 300px;
}
}
The obvious drawback is this limits the fix to browsers that support media queries. Therefore, Christina's answer is better, assuming it works consistently across browsers.
div has a display type "block", by default this means that each div will be shown below another div.
this means that someway you've changed some of the css, specifically with floats or display:inline.
if you're not sure how to find and change this you can always make sure each div has width:100%;
that will make sure the div catches the whole width of it's parent container.
With Bootstrap 3, I'm trying to make an image stay within the boundaries of it's parent div.
This works fine for width, but no matter what I try, the image keeps falling out of the bottom of the parent div, because it's height doesn't update when I decrease browser height. Is it possible to make this work ? If so, how ? Thanks.
JSFiddle : http://jsfiddle.net/rensdenobel/9486t/
HTML :
<div class="container">
<div class="row">
<div class="col-sm-3 col-md-3 col-lg-4"></div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4 center">
<div class="content">
<div id="text">You chose this photo :</div>
<div id="thumbnail-container">
<img id="thumbnail-image" class="img-responsive" src="http://placehold.it/800x800" />
</div>
<div id="button-share">
<button class="btn btn-success">Share it !</button>
</div>
</div>
</div>
<div class="col-sm-3 col-md-3 col-lg-4"></div>
<div>
</div>
CSS :
html, body {
height:100%;
}
.container {
height:100%;
width:100%;
border:1px solid blue;
}
.row {
border:1px solid green;
height:100%;
}
.center {
display:table;
border:1px solid red;
padding-top:12px;
padding-bottom:12px;
text-align:center;
height:100%;
max-height:100%;
}
.content {
display:table-cell;
vertical-align:middle;
text-align:center;
height:auto;
border:1px solid green;
}
#thumbnail-image {
margin-left:auto;
margin-right:auto;
height:auto;
}
#thumbnail-container{
width:100%;
border:1px solid pink;
max-height:70%;
height:70%;
margin-top:12px;
margin-bottom:12px;
}
try
<div id="thumbnail-container col-sm-4 col-xs-4">
<img id="thumbnail-image" class="img-responsive" src="http://placehold.it/800x800" />
</div>
change the col-sm-* and col-xs-* to get your desired output
This approach works for me. Please try adding this to your css:
.container {
height:max-content;
}