I'm trying to get a simple grid-like webpage done where there are 8 DIVs spanning across 9 positions.
I've done the following for another part of the website using Bootstrap's grid system without running into any kind of issue (Not the actual content, just a mockup using Photoshop)
What I want to do is this:
Having in mind that when the site is viewed in a mobile device, the order of the elements must be more-or-less preserved. Also Bootstrap standard margins/paddings must be used
Ideally this would be fixed-height DIVs inside of which I'll put an image or some text, depending on which one. The double-height one will contain a picture
<div class="wrapper" id="mypage">
<section class="grid-container service">
<ul class="small-grid grid-col-3">
<li class="col-3">
<div id="subtitle">
SUBTITLE 1
</div>
</li>
<li class="col-3">
<div id="title">
TITLE
</div>
</li>
<li class="col-3">
<div id="subtitle">
SUBTITLE 2
</div>
</li>
<a href="">
<li class="col-3 img-dynamic-size" style="background-image: url('../1.jpg')">
</li>
</a>
<a href="">
<li class="col-3 img-dynamic-size" style="background-image: url('../2.jpg')">
</li>
</a>
Sample code of the 1st screen
Thank you, I can't figure out how to do it after thinking about it for some time...
Okay.. I think I'm following you, I've just drawn up some sample bootstrap code for you to see what I'm talking about. You can achieve this look by just stripping out relevant padding/margin and using the Bootstrap equal-height columns experiment
I have just stripped out all relevant margin and padding on the nested rows in the example but you can obviously play with these values only cancelling out or amending the vertical/horizontal margin/padding as required for your own use.
HTML
<div class="container">
<div class="row">
<div class="col-xs-4 border">
<div id="subtitle">SUBTITLE 1</div>
</div>
<div class="col-xs-4 border">
<div id="title">TITLE</div>
</div>
<div class="col-xs-4 border">
<div id="title">TITLE</div>
</div>
</div>
<div class="row row-eq-height">
<div class="col-xs-8 nopadding">
<div class="row nomargin">
<div class="col-xs-6 nomargin border">Some Content...</div>
<div class="col-xs-6 nomargin border">Some More Content...</div>
</div>
<div class="row nomargin">
<div class="col-xs-6 nomargin border">Some Content Below...</div>
<div class="col-xs-6 nomargin border">Some More Content Below...</div>
</div>
</div>
<div class="col-xs-4 border">Content matching height using the 'row-eq-height' class</div>
</div>
CSS
.nomargin {
margin:0;
}
.nopadding {
padding: 0;
}
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.border {
border: 1px solid red;
min-height: 50px;
}
You can see a jsFiddle here
Related
I'm trying to create a layout that looks like the component on the image.
It's a 12 col bootstrap grid. I'm looking to make the text take up 6 cols, 1 col spacer in-between the text and the last col, and then I'd like the last col (image) to take up the rest of the horizontal space, in and out of the container.
I've trying mixing the container and container-fluid classes (not advisable according to Bootstrap docs) and it doesn't work.
How would one achieve this type of a layout using Bootstrap 4?
Image:
Existing code:
<div class="info-side">
<div class="container-fluid">
<div class="row">
<div class="col-6">
<h2>#Model.TitleText</h2>
<p>#Model.AreaText</p>
</div>
<div class="col-1">
</div>
<div class="col-5">
<img src="#Model.Image.Url)" alt="#Model.Image.AlternativeText" style="width: 100%; height:100%;">
</div>
</div>
</div>
</div>
try this
<div class="info-side" >
<div class="container">
<div class="row col">
<div class="col-6 bg-info">
<h2>#Model.TitleText</h2>
<p>#Model.AreaText</p>
</div>
<div class="col-1 bg-warning">
</div>
<div class="col-5 bg-info">
<img src="#Model.Image.Url)" alt="#Model.Image.AlternativeText" style="width: 100%; height:100%;">
</div>
</div>
</div>
</div>
I added col to class row #div tag. It expands to all space available.
In another hand, mix containers regular and fluid is possible, but you have to take care of hierarchy: fluid is widest as regular, so regular must be included alone or inside a fluid class.
See this answer: link
Good Luck!
I've come to the following solution which creates the layout in my original image in the question.
I had to make one row position absolute, this way the two containers are overlapping each other.
<div class="info-side">
<div class="text-side container" style="position: relative;" >
<div class="text-area" style="position: absolute;">
<div class="row">
<div class="col-6 d-flex align-items-center" style="height: 600px;">
<div class="text-items">
<h2>#Model.TitleText</h2>
<p>#Html.Raw(Model.AreaText)</p>
</div>
</div>
</div>
</div>
</div>
<div class="image-side container-fluid">
<div class="row">
<div class="col-7"
<div class="col-5" style="height: 600px; ">
<img src="#Model.Image.Url" alt="#Model.Image.AlternativeText">
</div>
</div>
</div>
</div>
I have a bootstrap row on my page that's divided into three columns. I wanted to add some space between the 3 columns and split each individual column internally into 2 parts. After extensive Googling, this is what I arrived on
<div class="container row h-100 mx-auto" style="margin-top: -60px;">
<div class="col-sm-4 ">
<div class="mx-3">
<div class="" style="width:50%; float: right;">
<h4>About Us</h4>
<p>Find out more about our team of working professionls</p>
</div>
</div>
</div>
<div class="col-sm-4 ">
<div class="mx-3 rounded">
<div style="width: 50%; float: left;">
<i class="fa fa-users" aria-hidden="true"></i>
</div>
<div style="width: 50%; float: right;">
<h4>Our Purpose</h4>
<p>Come up with some stuff to be put in here</p>
</div>
</div>
</div>
<div class="col-sm-4 ">
<div class="mx-3">
<h4>Our products and services</h4>
<p>Check out our vast array of products and services that we offer.</p>
</div>
</div>
</div>
Here's how it looks:
Ignore the three blue lines, that is experimental code.
Now it all works fine except I can't add a background colour to my columns. Every time I try adding any colour, it just doesn't show up. I write the line something like this:
<div class="mx-3 rounded" style="background: red;">
Now apart from this, I have a .card class defined in my main CSS file, it goes as follows:
.card{
border-radius: 15px;
}
That's it. Adding this class gives a rounded edge to a div (column) and adds a white background. However, every time I do that, the spacing is gone and the content inside my columns just sticks to the left instead of floating left and right.
How can I add a background to these columns without breaking any formatting? Or does anyone have an idea what's going on here?
First of all, I would suggest you should only have rows inside containers. If you put .container and .row together, the Bootstrap grid system doesn't work anymore because rows don't have the flexbox container parent.
Secondly, if you want 2 columns internally, why not declare a row and 2 columns? You've used that to create 3 column outer layout already. Using inline styles with floats defeat the purpose of using Bootstrap flexbox style grid system:
<div class="rounded">
<div class="row">
<div class="col-6">...</div>
<div class="col-6">...</div>
</div>
</div>
You can also utilize the built-in .card class to help you with the paddings:
<div class="row">
<div class="col-sm-4">
<div class="card border-0">
<div class="card-body">
<div class="row">
<div class="col-6"></div>
<div class="col-6">
<h4 class="card-title">Our Purpose</h4>
<p class="card-text">
...
</p>
</div>
</div>
</div>
</div>
</div>
</div>
To add background color to the .card, it's as easy as putting background color classes in:
<div class="card border-0 bg-danger">
...
</div>
demo: https://jsfiddle.net/davidliang2008/ed8bav14/36/
In bootstrap 4 I have the following code:
<div class="container-fluid">
<div class="row no-gutter">
<div class="d-none d-md-flex col-md-12 flex-wrap">
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
</div>
</div>
</div>
Issue I have is as soon as there is a 'wrapped item', it appears to get stuck to the bottom of the items above. When I try to apply a margin-bottom:5px to each of the divs, it appears to have no effect. What is the correct way to handle this?
A few things in your code...
why use float with flex? Bootstrap 4 uses flex instead of float (which was used by bootstrap 3)
the divs in pink are from your code with classes removed to show the effect in the browser/snippet
the divs in blue show the solution with flex (using bootstrap 4 classes)
margin-bottom works in both
working snippet below:
.flex-wrap>div {
width: 100px;
height: 50px;
display: block;
}
.my-divs {
float: left;
margin-bottom: 10px;
background: lightpink;
}
.my-flex-div {
background: lightblue;
margin-bottom: 5px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container-fluid">
<div class="row no-gutter">
<div class=" d-md-flex col-md-12 flex-wrap">
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
</div>
<hr/>
<div class="d-flex flex-wrap">
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
</div>
</div>
</div>
It is most likely your use of float that is causing the problem. However this whole layout can be achieved using only Bootstrap default classes and no additional CSS.
First, there is no need for a row/col layout if you are only using a col-12 full-width single column. Simply remove the row no-gutter and col-12 classes.
Second, BS4 includes utility classes for margin. Read about them here.
Try the example below. I have used multiple background colors for clarity.
.flex-wrap div {
width: 100px;
height: 50px;
}
<div class="container-fluid">
<div class="d-flex flex-wrap">
<div class="mb-5 bg-primary">Div</div>
<div class="mb-5 bg-secondary">Div</div>
<div class="mb-5 bg-warning">Div</div>
<div class="mb-5 bg-success">Div</div>
<div class="mb-5 bg-danger">Div</div>
</div>
</div>
Here is a working codepen example.
I am facing a problem with the Bootstrap-4 grid system. There are 12 columns, and whenever I use less than 12 columns, the rows are automatically centered. I want left alignment, how can this be fixed?
A snippet of my code:
<div class="row justify-content-start con-flex">
<?php foreach($books as $book):?>
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">
<div id="single-book">
<div id="book-image">
<?php print '<img src = "'.strip_tags($book->book_image).'" alt = "">';?>
<div id="addto-cart"><i class="fas fa-shopping-cart"></i> Add to cart</div>
</div>
<div class="book-text">
<div id="book-name"><?= substr(htmlentities($book->book_name),0,21) ?></div>
<div id="author">By <i><?= htmlentities($book->author) ?></i></div>
<div id="price"><?= htmlentities($book->price) ?>.TK</div>
<div id="book-details">
<?php print 'View details'; ?>
</div>
</div>
</div>
</div>
<?php endforeach;?>
</div>
image of code:
enter image description here
According to Bootstrap doc that should solve your problem
<div class="container">
<div class="row justify-content-start">
<div class="col-4">
content here...
</div>
<div class="col-4">
content here...
</div>
</div>
</div>
The code you have should be working fine, columns will be aligned left by default; maybe what you are seeing is the result of using .container instead of .container-fluid which uses the full width of the viewport. It's either this or you might have some custom CSS that's affecting the layout of the columns
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row ">
<div class="col-4 col-md-4 border">content here...</div>
<div class="col-4 col-md-4 border">content here...</div>
</div>
</div>
<div class="container-fluid">
<div class="row ">
<div class="col-4 col-md-4 border">content here...</div>
<div class="col-4 col-md-4 border">content here...</div>
</div>
</div>
This problem was for my other base CSS.
Instead of:
*{
margin: 0 auto;
}
For this margin: 0 auto; in my main style.css file My bootstrap's grids were not working properly. Now I remove this margin: 0 auto from *.
Now
I write
* {
margin: 0;
padding: 0;
}
Now my bootstrap grids are working properly
I am trying to center the first column in the middle vertically , for the mobile mode xs:
<div class="row toplineheight vcenteritems totalblue visible-xs">
<div class="col-xs-6 vcenteritems small">Your order</div>
<div class="col-xs-3">
<div class="col-xs-12"><span class="tiny">Upfront</span></div>
<div class="col-xs-12 dollar large middle">0</div>
</div>
This is my css(bootstrap 3):
.cart {
.verticalline {
border-left: thin solid #f7f7f7;
}
At the moment the 'your order' text stays stuck to the top of the div. How can I align this in the center?
Codepen here: codepen
using the class visible-xs on your .row element is overriding your display:flex css value. Moving the class to a containing element
CODEPEN
HTML
<div class="cart">
<div class="visible-xs"> <!-- move visible-xs class to a parent container -->
<div class="row toplineheight vcenteritems totalblue">
<div class="col-xs-6 vcenteritems small">Your order</div>
<div class="col-xs-3">
<div class="col-xs-12"><span class="tiny">Upfront</span></div>
<div class="col-xs-12 dollar large middle">0</div>
</div>
<div class="col-xs-3">
<div class="col-xs-12"><span class="tiny">Monthly</span></div>
<div class="col-xs-12 dollar large middle">0</div>
</div>
</div>
</div>
</div>