Bootstrap grid not wrapping properly? - html

I am practicing this grid system and the grid isn't wrapping properly, it overlaps each other's text as can be seen in the screenshot below:
I am using the version: 3.3.7.
Here's the code that's resulting in this error:
Code:
<div class="container">
<div class="row">
<div class="col-md-8">
<p>ROW 1</p>
</div>
<div class="col-md-4">
<p>ROW 1.1</p>
</div>
Error:
<div class="container">
<div class="row">
<div class="col-md-8">
<p>ROW 1</p>
</div>
<div class="col-md-4">
<p>ROW 1.1</p>
</div>
</div>
<br>
<div class="row">
<div class="col-md-3">
<p>ROW 2 DASKLLLLDASSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS</p>
</div>
<div class="col-md-3">
<p>ROW 2.1 DASKLLLLDASSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS</p>
</div>
<div class="col-md-3">
<p>ROW 2.2 DASKLLLLDASSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS</p>
</div>
<div class="col-md-3">
<p>ROW 2.3 DASKLLLLDASSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS</p>
</div>
</div>
</div>
Any help as to why it won't wrap the text properly would be great.

I believe this is happening because the content inside the containers is one word that is too long for the column. If you simply must have that long of a word in each container you can try applying word wrap to your paragraph tags as follows:
<style>
p {
word-wrap:break-word;
}
</style>
Hope this helps!

Related

Is there a CSS layout that can reverse the arrangement of columns in a stack of rows for desktop view? (see image)

This image shows how I am trying to arrange
I am new to HTML and CSS. I am trying to arrange the desktop and mobile layouts to be ordered differently. Currently I am using the following code:
<div class="row ">
<div class="col-lg-6">
<img src="img/services/service1.jpg" alt=">
</div>
<div class="col-lg-6">
<h2> Service 1</h2>
<p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</p>
</div>
</div>
<div class="row ">
<div class="col-lg-6">
<img src="img/services/service2.jpg" alt="">
</div>
<div class="col-lg-6">
<h2> Service 2</h2>
<p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.</p>
</div>
</div>
<div class="row ">
<div class="col-lg-6">
<img src="img/services/service3.jpg" alt="">
</div>
<div class="col-lg-6">
<h2> Service 3</h2>
<p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.</p>
</div>
</div>
This arranges the layout of elements as:
1,2
3,4
5,6
and I want to be:
1,2
4,3
5,6
Any help will be greatly appreciated :)
EDIT: But I want it to remain 1,2,3,4,5,6 on the phone. hence I am not swapping 3 and 4
Boostrap 4 uses the grid system based on flexbox which allows reordering. Check reordering section in bootstarp documentation for more details.
It may look like this:
<div class="row ">
<div class="col-lg-6">
<img src="img/services/service1.jpg" alt=">
</div>
<div class="col-lg-6">
<h2> Service 1</h2>
<p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</p>
</div>
</div>
<div class="row ">
<div class="col-lg-6 order-sm-12">
<img src="img/services/service2.jpg" alt="">
</div>
<div class="col-lg-6 order-sm-1">
<h2> Service 2</h2>
<p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.</p>
</div>
</div>
<div class="row ">
<div class="col-lg-6">
<img src="img/services/service3.jpg" alt="">
</div>
<div class="col-lg-6">
<h2> Service 3</h2>
<p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.</p>
</div>
</div>

Bootstrap 4 nested rows with same height

Thought that this requirement was a simple one: The result should be a very basic landing page containing two topics. Each topic has a subtopic.
On mobile, there should be only one column containing Topic1, Subtopic1, Topic2, Subtopic2 consecutively.
On desktop the topics should appear side by side and the subtopics should be vertically aligned.
<div class="container-fluid">
<!-- Subtitles are on the same height but the order on mobile is wrong -->
<div class="row">
<div class="col-md-6">
<h1>Topic 1</h1>
<p>Four lines of text<br>Line2<br>Line3<br>Line4</p>
</div>
<div class="col-md-6">
<h1>Topic 2</h1>
<p>Two lines of text<br>Line2</p>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h2>Subtopic 1</h2>
<p>Number of lines<br>do not matter</p>
</div>
<div class="col-md-6">
<h2>Subtopic 2</h2>
<p>But Subtopic 1 and Subtopic 2 should be<br>vertically aligned while >=md<br>and when <md, Subtopic 1 shold be below Topic 1, not below Topic 2</p>
</div>
</div>
<hr>
<!-- Display on mobile is correct but subtitles on desktop are not aligned anymore -->
<div class="row">
<div class="col-md-6">
<h1>Topic 1</h1>
<p>Four lines of text<br>Line2<br>Line3<br>Line4</p>
<div class="row">
<div class="col">
<h2>Subtopic 1</h2>
<p>Number of lines<br>do not matter</p>
</div>
</div>
</div>
<div class="col-md-6">
<h1>Topic 2</h1>
<p>Two lines of text<br>Line2</p>
<div class="row">
<div class="col">
<h2>Subtopic 2</h2>
<p>But Subtopic 1 and Subtopic 2 should be<br>vertically aligned while >=sm<br>and when <sm, Subtopic 1 shold be below Topic 1, not below Topic 2</p>
</div>
</div>
</div>
</div>
</div>
I am badly surprised that I have no idea how to solve this. Any hints are deeply appreciated.
One way would be using order utility classes from Bootstrap and adjusting your markup for all elements to be at the same level:
<div class="container-fluid">
<div class="row">
<div class="order-0 order-md-0 col-md-6">
<h1>Topic 1</h1>
<p>Four lines of text<br>Line2<br>Line3<br>Line4</p>
</div>
<div class="order-2 order-md-1 col-md-6">
<h1>Topic 2</h1>
<p>Two lines of text<br>Line2</p>
</div>
<div class="order-1 order-md-2 col-md-6">
<h2>Subtopic 1</h2>
<p>Number of lines<br>do not matter</p>
</div>
<div class="order-3 order-md-3 col-md-6">
<h2>Subtopic 2</h2>
<p>But Subtopic 1 and Subtopic 2 should be<br>vertically aligned while >=md<br>and when <md, Subtopic 1
shold be below Topic 1, not below Topic 2</p> </div> </div> <hr>
</div>
</div>
</div>
Check the documentation here.
Another would be to set either a predefined fixed height for your topic columns, or get the highest topic height and set it to all others with JavaScript at page load and window resize.

Bootstrap 4 layout row first vs column first?

Most grid layout examples in Bootstrap 4 put columns inside rows. Is it wrong to put rows inside columns ? Like this,
<div class="col col-md-6">
<div class="row">
</div>
</div>
Yes, it's totally valid to use nested columns/row. Just be sure to always use a column inside a row. Else you'll get unexpected layouts.
<div class="col">
<div class="row">
<div class="col">
<div class="row">
<div class="col">
<div class="row">
<div class="col">
<!-- Your content goes here -->
</div>
</div>
</div>
</div>
</div>
</div>
</div>
It's a perfectly valid approach. Consider the example below:
<div class="row">
<div class="col-6">
<div class="row">
<div class="col-6"></div>
<div class="col-6"></div>
</div>
</div>
<div class="col-6">
<div class="row">
<div class="col-3"></div>
<div class="col-3"></div>
<div class="col-3"></div>
<div class="col-3"></div>
</div>
</div>
</div>
The [x]-col-[y] classes are all percentage based for widths. Go nuts with the nesting! Just make sure you wrap them in a row div else you'll get some funky margins

Adding left and right margin to elements in bootstrap row

Im a little confused why bootstrap wont apply left and right margins to the columns, all i want is a simple row with 3 columns that have like 20px horizontal margin in between each other. If try to do that bootstrap simply clips one of the divs to the next line.
html
<div class="container">
<div class="row">
<div class="col-md-4"><p>Box 1</p></div>
<div class="col-md-4"><p>Box 1</p></div>
<div class="col-md-4"><p>Box 1</p></div>
</div>
</div>
css
.col-md-4 {
background-color: tomato;
margin: 20px 5px;
}
Left and right margins to the columns would destroy Bootstrap's grid behaviour. Don't modify Bootstrap's classes. Add your own classes instead.
In your example put an extra <div> in the column. And give it a margin.
HTML
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
</div>
</div>
CSS
.spacer {
background-color: tomato;
margin: 20px 5px;
}
Demo
for a little more spacing you could do:
<div class="col-md-3 col-md-offset-1"><p>Box 1</p></div>
<div class="col-md-3 col-md-offset-1"><p>Box 1</p></div>
<div class="col-md-3 col-md-offset-1"><p>Box 1</p></div>
Bootstrap slips one in new row because it column layout is 12 columns max.
When you have 3 divs * (4 col width + margin-right) you excess the provided 12 columns.
You can set your div a little bit thinner, i.e. 3 medium bootstrap columns, like this:
<div class="col-md-3 custom-box"><p>Box 1</p></div>
<div class="col-md-3 custom-box"><p>Box 1</p></div>
<div class="col-md-3 custom-box"><p>Box 1</p></div>
and apply your css rule like this:
.custom-box{
margin-right: 20px 5px!important;
background-color: tomato;
}
here is a FIDDLE

Align divs from different columns - Twitter bootstrap

I am using twitter bootstrap and trying to align two divs that belong to different columns.
The code so far:
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<h2>header1</h2>
<div class="row">
<div class="col-md-2">
1
</div>
<div class="col-md-4">
2
</div>
</div>
<div class="row">
<div class="col-md-2">
1
</div>
<div class="col-md-4">
2
</div>
</div>
<div class="row">
<div class="col-md-2">
1
</div>
<div class="col-md-4">
2
</div>
</div>
<div class="row">
<div class="col-md-2">
1
</div>
<div class="col-md-4">
2
</div>
</div>
<div class="row">
<div class="col-md-2">
1
</div>
<div class="col-md-4">
2
</div>
</div>
<h2>header2</h2>
<div class="row">
<div class="col-md-2">
3
</div>
<div class="col-md-4">
4
</div>
</div>
</div>
<div class="col-md-6">
<h2>header3</h2>
<div class="row">
<div class="col-md-2">
3
</div>
<div class="col-md-4">
4
</div>
</div>
<h2>header4</h2>
<div class="row">
<div class="col-md-2">
3
</div>
<div class="col-md-4">
4
</div>
</div>
</div>
</div>
</div>
The above code produces the layout below:
Is there any way to align vertically header4 with header2 (and thus leave space between the end of the div of header3 and the start of the div of header4?
Edit: when I resize my window and make it smaller I want to keep (header1 and header2) AND (header3 and header4) together (ie header2 below header1). This is the current case in the provided html code.
Thank you
There is - you need to alter your HTML so instead of simply having a two column layout, you have a grid based two row two column layout, e.g (simplified)
<div class='row'>
<div class='col-md-6'>
<h1>Header 1</h1>
</div>
<div class='col-md-6'>
<h1>Header 3</h1>
</div>
</div>
<div class='row'>
<div class='col-md-6'>
<h1>Header 2</h1>
</div>
<div class='col-md-6'>
<h1>Header 4</h1>
</div>
</div>