This question already has answers here:
Change div order with CSS depending on device-width
(1 answer)
How can I reorder my divs using only CSS?
(27 answers)
How to change order of divs on smaller screens?
(1 answer)
Closed 3 years ago.
How can i reorder class on mobile? i want to put the title first, then the image, then paragraph and the button.
<div class="container">
<div class="row">
<div class="col-md-6">
<h3 class="pt-5">Title</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit
</p>
submit
</div>
<div class="col-md-6 col-xs-12 align-self-center">
<img src="img/duo.png" alt="image" />
</div>
</div>
</div>
Please check the image below for more details
If you are using Bootstarp 4 then you can use order class to reorder your div
for eg:
<div class="order-sm-2 order-1"></div>
<div class="order-sm-1 order-2"></div>
You can do by CSS also. Make sure you are using FLEX properties
.your_class{
order: 2;
}
.your_class1{
order: 1;
}
I'm not too familiar with bootstrap, but for non bootstrap ideas, I could think of two potential solutions one could use here. First you could add the image under the title and hide it on desktop screens and then use display: block for mobile screens using media queries.
Second, I'm not sure if you can use jquery or not, but here is a potential jquery solution using .insertAfter(). Then once you detect a mobile size, you can place that image wherever you would like. Like such:
var isMobile = 768;
if ($(window).width() <= isMobile) {
$('.duo').insertAfter('.pt-5');
}
You can test that here: http://jsfiddle.net/3buty5oL/1/
$(function(){
if($(window).width() < 767){//first check the device width
$(".image img").insertAfter(".pt-5");//move the image after title
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-6">
<h3 class="pt-5">Title</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit
</p>
submit
</div>
<div class="col-md-6 col-xs-12 align-self-center image">
<img src="https://picsum.photos/200/300" alt="image" />
</div>
</div>
</div>
it can be done using juery in two steps
1. check for device width.
2. if its mobile then move the image next to title.
Normally you can use bootstrap4 order classes to recorder the elements on the screen. But based on the structure (i.e., on Desktop you kind of have 2 columns but on Mobile you have 1 vertical column where Image sits in between Title and Paragraph), it is not so easy to just use order classes to achieve the desired result.
I think the best way to achieve the result is to put the Image on 2 places and show/hide one of them based on the screen width.
HTML
<div class="container">
<div class="row">
<div class="text-center col-md-6 text-md-left">
<h4>Title</h4>
<img class="img-fluid d-md-none" src="..." />
<p>
Paragraph. Lorem ipsum dolor sit amet, consectetur
adipiscing elit
</p>
More
</div>
<div class="col-md-6">
<img class="img-fluid d-none d-md-block" src="... />
</div>
</div>
</div>
On mobile, the image on the right column is not showing due to d-none:
On desktop, the image on the right column is showing due to d-md-block, but the image between the Title and Paragraph is not showing due to d-md-none:
fiddle: http://jsfiddle.net/aq9Laaew/235510/
Related
I'm using bootstrap to distribute my header elements in two columns.
So there is a row, with 2 col-md-6 to separate elements to the left and to the right. This is ok.
But when resizing to Smartphones, I'd like to show items in this order (top to bottom):
1) text (I'd like to show it on top but reduce font-size)
2) Buttons (1 on top of the other)
3) Image (Smaller Image doesn't matter).
How to do that using Bootstrap?
CodePen:
https://codepen.io/ogonzales/pen/ebKNoK
HTML
<header class="header" id="header1">
<div class="row">
<div class="col-md-6">
<div class="circle">
<br>
<div class="caption">
<h2 class="title display-3">Stickers <strong>Personalizados</strong></h2>
<p>Lorem m nisi! Eum vitae ipsam veniam, ullam explicabo quaerat asperiores veritatis nam
reprehenderit necessitatibus sequi.</p>
</div>
<div class="row">
<div class="col-md-5">
Comprar
</div>
<div class="col-md-5 my-home-banner-image">
<a href="{% url 'shop:SamplePackPage' %}" class="btn btn-naranja text-white btn-block">Muestras
</a>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<br>
<img class="" src="https://www.austinhomebrew.com/assets/images/sticke-alt-8989.png" width="440px" height="300px">
</div>
</div>
</header>
Reference:
UPDATE 1:
I'm getting this results from AnnieP's answer. But:
How to give spacing between sections and specially between buttons?
What you have should work, as far as the Bootstrap goes. col-md-6 is saying "I want this column to be 6-wide on screens greater than 767 px" and because you don't specify anything for col-xs and col-sm, by default every div with a .col- will be col-12, or full-width, on screens 767 and smaller. It looks like you don't have bootstrap hooked up to the Codepen, so it won't act responsively there, but it should when Bootstrap is working. It looks like you have a lot of CSS in there that's trying to handle what Bootstrap's columns would do for you. For instance the following CSS that you have should all be handled with <div class="col-md-6">:
/*=== Large devices (desktops, 992px and up) ===*/
#media (min-width: 992px) {
.header .center {
width: 50%;
}
What you need to do is change the CSS using media queries for the text size change and the image sizing/positioning. Your image is set with absolute positioning, which is why it's on top of the buttons when it responsively adjusts to a smaller screen. Instead, utilize Bootstrap's grid system and give it col-md-6 as well. Here's the general outline of all you should need:
<div class="container">
<div class="row">
<div class="col-md-6">
<h2>Stickers Personalizados</h2>
<p>Lorum impsum</p>
<div class="row">
<div class="col-md-6">
Comprar
</div>
<div class="col-md-6">
Muestras
</div>
</div>
</div>
<div class="col-md-6">
<img class="" src="https://raw.githubusercontent.com/alphadsy/alpha-ui/master/images/man.png" width="440px" height="300px">
</div>
</div>
</div>
Revisit the Bootstrap getting started docs if you need help setting it up, and you likely don't need most of the CSS in your Codepen.
Update:
To achieve spacing below sections, you can add a wrapper div with class="row", but that won't work for the buttons because they start on the same row. In that case, you'll want to add margin-bottom in the css. For example:
.btn {
margin-bottom: 10px;
}
im stuck again with "basics", anyway, I'm having trouble making this example :
Basicly I want icon and "Feature 1" to be aligned on top and centered as seen on picture (the cyan lines) and then below them a random paragraph.
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="top-align text-center">
<i class="fa fa-paper-plane-o"></i>
<h3>We're Creative</h3>
</div>
<p class="text-center">Lorem ipsum dolor sit amet<br>
Lorem ipsum dolor sit amet<br>
Lorem ipsum dolor sit amet<br>
Lorem ipsum dolor sit amet</p>
</div>
</div>
</div>
</body>
CSS3
.top-align{
display:inline;
}
So, my idea here was to make icon and "Feature 1" paragraph be "inline" so i made custom css and separate div tag for these 2 things (icon and paragraph), but unfortunately it wont work, here's live version if anyone is interested
http://i1cevic.com
I noticed you are using Bootstrap and Font Awesome, so my fiddle reflects that.
https://jsfiddle.net/Vuice/Ljkoatog/
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="text-center">
<i class="fa fa-paper-plane-o"></i>
<h3 style="display:inline;">We're Creative</h3>
</div>
<p class="text-center">Lorem ipsum dolor sit amet<br>
Lorem ipsum dolor sit amet<br>
Lorem ipsum dolor sit amet<br>
Lorem ipsum dolor sit amet</p>
</div>
</div>
</div>
</body>
Your wrapper div does not need display: inline. An inline element will render as if it is a letter in a word - each character is an inline element. A <span> defaults to display: inline - it takes up the next available space on the line.
Once the wrapper div.top-align has display: inline removed, give the h3 a display property of inline or inline-block. Either will do. This will allow the icon and h3 to act like two adjacent words and they will respond to the text-align: center applied by the wrapper div.
Even better, you can accomplish this without a wrapper div around the icon + h3. The icon defaults to a display of inline-block. Put that icon's span inside the h3 and it will always render as a single unit, just like the icon was a word in the h3. Since the icon is wrapped in a span, you can still target it individually if you need to.
Here's a fiddle illustrating how:
Making your icon an integral part of your heading
(I started with your code and commented out what no longer applies so you can see the progression.)
I am going over the 12 columns per row in bootstrap 3.2.0, and according to bootstrap and this post this is totally OK.
If more than 12 columns are placed within a single row, each group of extra columns will, as one unit, wrap onto a new line.
The problem I have is that when I use 4 col-md-4 I get the 4th column to the right like the picture below.
<div class="row">
<div class="col-md-4">
<a href="#" title="Test">
<img width="225" height="150" src="blog.jpg />
</a>
<h4>
Test
</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus odio nisi, sodales nec commodo at, viverra eget eros. In hac habitasse platea dictumst. Sed semper […]</p><!-- EXCERPT -->
<p>Read More</p>
</div>
<div class="col-md-4">
//Loop Repeats
</div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
If I add a 5th or even 6th one, everything floats to the left nicely like in the image below.
<div class="row">
<div class="col-md-4">
//Loop Repeats
</div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
Any Ideas?
The image is giving you the answer.
See, Bootstrap floats the columns to the left, just as you say. The float model means that the element will be floated to the left blocking the flow of the next element. Thus, in your first picture, see how your second column, first row is slightly longer and probably has some margin and padding which is blocking the flow of the element in that following row. In the second picture you don't see it because the long element is at the side. And the best description of a symptom was given by yourself:
I am generating this content through a wordpress loop in a custom
shortcode, and I noticed that somehow if I remove this line in the
Shortcode function the columns float just fine as in this jsFiddle:
$output .= '<p>' . get_the_excerpt() . '</p>';
There you have. The excerpt is somehow 'randomish' in the length of the containing block, so your problem is something that happens almost every single day to any WP developer. There are many different ways to solve it, but the easiest one is this:
.col-md-4{min-height:400px /* test the height you need to enable normal flow of the floats */}
And voilá, problem solved!
The wrapping issue is happening because the content of your columns are different heights which causes "gaps" in the grid. You can iterate and use the clearfix DIV every X columns, OR you can use a CSS-only solution like this..
http://codeply.com/go/BGSOAQi72l
#media (min-width: 992px) {
.row > .col-md-4:nth-child(3n+1) {
clear: left;
}
}
The 992px is used since that's where the md breakpoint starts. Read more on when to use Bootstrap's row class.
More details: Bootstrap row with columns of different height
Another way to solve this:
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="clearfix custom-class"> <!-- Before the loop starts again -->
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
And then adding some CSS to make it visible only starting with medium screen size
.custom-class {
display: none;
}
#media (min-width: #screen-md) {
.custom-class {
display: block;
}
}
This way there's no need to predetermine the minimal height of blocks and works perfectly fine
I'm using Bootstrap 3.
On large screens I want to have a sidebar on the left and the maincontent on the right. On small screens I want to have important blocks of the sidebar on top, then the maincontent, then the less important blocks of the sidebar. Is there a way to achieve that?
Here's a JS Bin showing the problem: http://jsbin.com/wibucopi/1/ and below is the current code (which, however, displays all sidebar content on top on small screens).
<div class="container-fluid">
<div class="row">
<div class="col-sm-3">
<div class="upper" style="background:red">
<h3>I want to be <b>above</b> the main content on small screens!</h3>
</div>
<div class="lower" style="background:green">
<h3>I want to be <b>below</b> the main content on small screens!</h3>
</div>
</div>
<div class="col-sm-9">
<h1>Main content</h1>
<p>Lorem ipsum dolor sit amet</p>
</div>
</div>
</div>
I've already played around with col-sm-pull/push-x, but I could only achieve that the whole sidebar is displayed below the maincontent on small screens.
I don't want to duplicate content and show / hide it with visible-XY, hidden-XY, as the page would get bigger and it feels just wrong.
It would be great to have a pure Bootstrap css solution, or at least a css only one (I wouldn't like to use js).
You could do something like this:
Bootply Demo
HTML:
<div class="container-fluid">
<div class="row">
<div class="upper col-sm-3" style="background:red">
<h3>I want to be <b>above</b> the main content on small screens!</h3>
</div>
<div class="col-sm-9 col-sm-pull-right">
<h1>Main content</h1>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
</div>
<div class="lower col-sm-3" style="background:green">
<h3>I want to be <b>below</b> the main content on small screens!</h3>
</div>
</div>
</div>
CSS:
#media (min-width: 768px) {
.col-sm-pull-right {
float: right;
}
}
.lower {
clear: left;
}
Is it ok to keep a tag directly inside in bootstrap 3? or it's preferable to have every inside .row>.col-md-#
is this structure ok or can create problem on mobile
<div class="container">
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</h1>
<div id="ajaxID">
<div class="row">
<div class="col-md-6">Some pic</div>
<div class="col-md-6">Some text</div>
</div>
<div class="row">
<div class="col-md-6">Some pic</div>
<div class="col-md-6">Some text</div>
</div>
</div>
</div>
Yes, you can nest directly within container if you don't need a multicolumn section. Check out the source of the home page of Bootstrap docs:
<div class="bs-docs-featurette">
<div class="container">
<h2 class="bs-docs-featurette-title">Designed for everyone, everywhere.</h2>
<p class="lead">Bootstrap makes front-end web development faster and easier. It's made for folks of all skill levels, devices of all shapes, and projects of all sizes.</p>
<hr class="half-rule">
<div class="row">
<div class="col-sm-4">
<img src="assets/img/sass-less.png" alt="Sass and Less support" class="img-responsive">
<h3>Preprocessors</h3>
<p>In addition to vanilla CSS, Bootstrap includes support for the two most popular CSS preprocessors, Less and Sass.</p>
</div>
<div class="col-sm-4">
<img src="assets/img/devices.png" alt="Responsive across devices" class="img-responsive">
<h3>One framework, every device.</h3>
<p>Bootstrap easily and efficiently scales your project with one code base, from phones to tablets to desktops.</p>
</div>
<div class="col-sm-4">
<img src="assets/img/components.png" alt="Components" class="img-responsive">
<h3>Comprehensive docs</h3>
<p>With Bootstrap, you get extensive and beautiful documentation with hundreds of live examples, code snippets, and more.</p>
</div>
</div>
<hr class="half-rule">
<p class="lead">Bootstrap is open source. It's hosted, developed, and maintained on GitHub.</p>
View the GitHub project
</div>
</div>
Notice the p.lead and button are both directly under the container. Rows and columns should only be used if you're actually making columns.
If h1 is centered I don't see why it wouldn't work fine, if it's aligned to the left there might be a margin/padding issue. I'm not sure whether it's correct or not, but why not placing that h1 and #ajaxID above the container or in col-*-12 column just in case?
According to Bootstrap documentation, you can't do this:
Content should be placed within columns, and only columns may be
immediate children of rows.
Source: Bootstrap documentation, grid introduction