Responsive row of same-height elements breaks to two rows in small breakpoint - html

I have 6 cells of data, in medium+ breakpoints, they are all in the same row and the same height; in the small breakpoint, there are 3 per row, and everything within the row is the same height.
I had initially tried to use display: table-cell;, and was able to achieve both scenarios separately, but the markup needed to change. Is there a way to make this approach work?
I know flexbox could be an option, but I cannot use it, as I need to support IE9.
Would like to solve this without the use of any JS.
tl;dr
3 cells in a row for small viewports
6 cells in a row for medium viewports
All cells in a row should be the same height
Dynamic height, based on content length
No JS - CSS/Markup only.
Support for IE9+.
Works in small breakpoint (3 per row):
.table {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
vertical-align: middle;
width: 16.66667%; /* 1/6 */
border: 1px solid #000;
text-align: center;
}
#media (max-width: 500px) {
.cell {
width: 33.33333%; /* 1/3 */
background: #eee; /* Just to see when the breakpoint is applied */
}
}
<div class="table">
<div class="row">
<div class="cell">Cell 1 Pellentesque pretium neque at lacinia faucibus.</div>
<div class="cell">Cell 2</div>
<div class="cell">Cell 3</div>
</div>
<div class="row">
<div class="cell">Cell 4</div>
<div class="cell">Cell 5</div>
<div class="cell">Cell 6 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu ornare mi. Ut convallis suscipit sapien, at feugiat tellus commodo nec.</div>
</div>
</div>
Works in medium+ breakpoints (6 per row):
.table {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
vertical-align: middle;
width: 16.66667%; /* 1/6 */
border: 1px solid #000;
text-align: center;
}
#media (max-width: 500px) {
.cell {
width: 33.33333%; /* 1/3 */
background: #eee; /* Just to see when the breakpoint is applied */
}
}
<div class="table">
<div class="row">
<div class="cell">Cell 1 Pellentesque pretium neque at lacinia faucibus.</div>
<div class="cell">Cell 2</div>
<div class="cell">Cell 3</div>
<div class="cell">Cell 4</div>
<div class="cell">Cell 5</div>
<div class="cell">Cell 6 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu ornare mi. Ut convallis suscipit sapien, at feugiat tellus commodo nec.</div>
</div>
</div>

Since its a tabular data I would suggest to go with Table...
.table {
width: 100%;
border-collapse: collapse;
}
.cell {
width: 16.66667%;
border: 1px solid #00F;
text-align: center;
}
#media (max-width: 500px) {
.cell {
width: 33.33333%; /* 1/3 */
background: yellow; /* Just to see when the breakpoint is applied */
}
}
<table class="table" >
<tr class="row">
<td class="cell">Cell 1 Pellentesque pretium neque</td>
<td class="cell">Cell 2</td>
<td class="cell">Cell 3</td>
</tr>
<tr class="row">
<td class="cell">Cell 4</td>
<td class="cell">Cell 5</td>
<td class="cell">Cell 6 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu ornare mi. Ut convallis suscipit sapien, at feugiat tellus commodo nec.</td>
</tr>
</table>
jsFiddle

If I understood correctly this is what you want to achieve... here I am using bootstrap listGrid concept to achieve this.
.cell {
border: 1px solid #000;
text-align: center;
height: 200px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="table">
<div class="cell col-xs-4 col-sm-2">Cell 1 Pellentesque pretium neque at lacinia faucibus.</div>
<div class="cell col-xs-4 col-sm-2">Cell 2</div>
<div class="cell col-xs-4 col-sm-2">Cell 3</div>
<div class="cell col-xs-4 col-sm-2">Cell 4</div>
<div class="cell col-xs-4 col-sm-2">Cell 5</div>
<div class="cell col-xs-4 col-sm-2">Cell 6 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu ornare mi. Ut convallis suscipit sapien, at feugiat tellus commodo nec.</div>
</div>
Note1: If in case you do not want to use bootstrap let me know, I can find some other solution.
Note2: Bootstrap has pre-define values in pexels for devices, and if you want to change as per your need you can at http://getbootstrap.com/customize/

Related

Swiper JS carousel not fitting inside of Bootstrap 4 col

I'm trying to put a Swiper JS carousel inside a div with the col class of Bootstrap 4, but my carousel doesn't stay inside the col div. To illustrate, this is the layout I'm trying to achieve:
But for some odd reason when I put the carousel code in the second col div, my page ends up like this:
Messing with the carousel properties with the Inspect Element tool, I found out that what causes this is the property display: flex that the swiper-wrapper has, and I still haven't been able to figure out how to make the carousel fit nicely within my second col div.
I've tried encapsulating the swiper-container in another div, setting max-width: 100%and min-width: 100% but nothing seems to work, it's like the display: flex property of the row class conflicts with the display: flex of the swiper-wrapper div.
Here's the code for this part of the layout:
HTML
<div id="myaboutdiv" class="row">
<div class="col topic">
<p>TEXT</p>
</div>
<div class="col mt-5">
<p class="text-left whoami"><i>This is some text</i></p>
<p class="text-justify">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent lorem purus, venenatis vel magna et, consectetur cursus libero. In augue est, iaculis sit amet faucibus ut, condimentum vitae ante. Donec et leo eu dolor suscipit viverra at et mauris. Quisque dapibus leo at ipsum elementum, sit amet interdum sapien ornare. Integer justo lorem, porttitor in gravida in, porttitor at tellus. Sed aliquet malesuada luctus. Duis ac nisl vitae nibh mattis luctus eget a ex.
</p>
<br>
<p class="text-left whoami"><i>Here's the Swiper Carousel</i></p>
<div class="swiper-container" id="academichistory">
<div class="swiper-wrapper">
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</div>
</div>
CSS
.topic {
font-weight: bold;
font-size: 2.5rem;
}
.whoami {
color: grey;
font-weight: 500;
}
And finally, here are some screenshots:
swiper-wrapper with display: flex:
swiper-wrapper without the display: flex property:
I'm using:
Swiper v5.3.6 and Bootstrap 4.
And just in case, here's the JS code that initializes the Swiper carousel:
JavaScript
var swiper = new Swiper('#academichistory', {
loop: true,
pagination: {
el: '.swiper-pagination',
type: 'progressbar',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
You can solve it using absolute positioning.
Since .swiper-container already has position: relative, you can add position: absolute to .swiper-wrapper. In addition, position it using left: 0 and top: 0.
Finally, the swipper absolutely positioned needs an explicit height, so add the height you want in .swiper-container.
.swiper-container {
height: 270px;
}
.swiper-wrapper {
position: absolute;
left: 0;
top: 0;
}

How to fit an image to “above the fold” without vertical scrollbar [duplicate]

This question already has answers here:
Image inside div has extra space below the image
(10 answers)
How wide is the default `<body>` margin?
(4 answers)
Closed 4 years ago.
starting from this question I have created the following layout:
html, body {
height: 100%;
}
.flexcontainer {
height: 100%;
display: flex;
flex-direction: row;
}
.left-container {
flex: 3 0 0;
}
.left-container .header {
display: flex;
}
.right-container {
flex: 1 0 0;
}
.right-container img {
height: 100%;
width: 100%;
object-fit: contain;
object-position: top right;
}
<div class="flexcontainer">
<div class="left-container">
<div class="header">
<div class="avatar">
<img src="https://i2.wp.com/oneword365.com/wp-content/uploads/oph-Avatar.png?resize=73%2C73"/>
</div>
<div class="user">
<span class="title">Sue Smith</span>
<div class="subtitle">PincoPallino</div>
<div class="time">15th August 2017</div>
</div>
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas vel porttitor risus. Integer dictum massa ac mollis posuere. Etiam dapibus odio euismod lacus tempus tempor. Donec sagittis eget purus non rhoncus. Ut ac est turpis. Ut et ornare felis. Vestibulum at facilisis sed.
</div>
</div>
<div class="right-container">
<img src="http://www.fulltimefba.com/wp-content/uploads/2014/04/bigstock-obstacle-ahead-caution-for-dan-41515888.jpg"/>
</div>
</div>
(CodePen mirror).
I would like to have that the image on the right should fit the above the fold section, and even have some padding arount it. As you can see it can't and a vertical scrollbar appears.
How can i fit the image to the above the fold?
If I set padding and margin of body to 0 it works and I get rid of the vertical scroll bar. But... isn't this a dirty solution?

How to vertically center bootstrap column text?

I want to display the text (and if the image is smaller, then the image) in the middle by vertically. But i can't accomplish it. Any help?
Here is what i have right now: Jsfiddle
HTML:
<div class="container overview-sm">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
</div>
</div>
CSS:
.box2 {
margin: 5px 5px 5px 0;
text-align: center;
display: inline-block;
width: 100%;
}
.image{
width: 100%;
max-width: 450px;
}
.row{
padding-bottom: 30px;
}
Image of what i want it to be: Image
The easiest way to do this would be to use Flexbox. Note that older browsers may not have great/any Flexbox support: https://caniuse.com/#search=flexbox)
If you are using Bootstrap 4, its grid uses Flexbox, so the columns in the row should already match each other's height. You can then use Bootstrap 4's Flexbox utility classes to vertically center the column's content: class="d-flex align-items-center". Ex:
<div class="row">
<div class="col d-flex align-items-center">Centered Content</div>
<div class="col">
<ul>
<li>Longer</li>
<li>Multi</li>
<li>Line</li>
<li>Content</li>
</ul>
</div>
</div>
Codepen
If you are using Bootstrap 3, you're going to have to add the appropriate flexbox code to both get your columns to match in height and vertically center your content. Here is a little cheatsheet I find helpful for building out flexbox layouts: http://jonibologna.com/flexbox-cheatsheet/
Edit: Here is the same example with Bootstrap 3:
HTML:
<div class="row row-flex">
<div class="col-md-6 col-v-centered">Centered Content</div>
<div class="col-md-6">
<ul>
<li>Longer</li>
<li>Multi</li>
<li>Line</li>
<li>Content</li>
</ul>
</div>
</div>
CSS:
.row-flex {
display: flex;
}
.col-v-centered {
display: flex;
align-items: center;
}
Codepen
If you want the text to be displayed underneath the second row then you will need to create another image div and add a class so it only displays on mobiles.
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 visible-xs ">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
<div class="box2">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 hidden-xs ">
<div class="box2">
<img class="image" src="http://via.placeholder.com/450x450">
</div>
</div>
https://jsfiddle.net/7goLo3xy/2/
If you need more control than CSS provides you can use a simple jQuery or JavaScript implementable.
$(window).resize(function() {//on resize
setBoxSizes();
});
function setBoxSizes() {
$('.image').each(function() {
let $imageBox = $(this).closest('.box2');
let imageBoxTop = $imageBox.offset().top;
let $otherBoxes = $imageBox.closest('.row').find('.box2');
$otherBoxes.each(function(){
$box = $(this);
if(!$box.find('.image').length) {//doesn't contain .image
if(Math.abs($box.offset().top - imageBoxTop) < 3) {//on the same layout row
$box.height($imageBox.height());
}
else $box.css('height', 'auto');//restores single column layout
}
});
});
}
setBoxSizes();//initial configuration
Fiddle Example! - Handles multiple column layout, changing columns layouts, and ensures centering is performed against equal horizon boxes.
If you don't want to use Flexbox you could always just set a height of 350px and use table and table-cell with vertical-align set to middle.
However this method does restrict you to having a fixed height so, if the height of your image changes the box height won't be relative. If you want the box to relative you could always set a height of 0 and use padding-bottom. More on this here
<div class="box2">
<div class="center">
<div class="center-v">
<h1 class="content-title">Lorem impsum</h1>
<hr>
<p class="content-sub-title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sit amet purus ac turpis finibus auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec in dictum arcu, dapibus porta lorem. </p>
</div>
</div>
</div>
.center {
display: table;
height: 345px;
}
.center-v {
display: table-cell;
vertical-align: middle;
}

flexbox three column layout with scrollable content works in Chrome but broken in IE

I have tried to create a page layout with three scrolling columns with flexbox.
Google Chrome displays the result nicely:
but Edge or Internet Explorer behave differently:
So far i have tried to adjust
.box {
display: flex;
flex: 1;
flex-direction: column;
}
But none of the combinations did the trick.
I'm quite confused because it is working with .sidebar
Here is my code:
html,
body {
padding: 0;
margin: 0;
}
.headerPane {
height: 66px;
background-color: #ccc;
}
.footerPane {
position: absolute;
height: 49px;
bottom: 0;
left: 0;
width: 100%;
background-color: #ccc;
}
.body {
position: absolute;
top: 66px;
right: 0;
bottom: 49px;
left: 0;
display: flex;
}
.actionPane {
height: 40px;
background-color: #999;
}
.sidebar {
width: 100px;
overflow-y: scroll;
}
.main {
flex: 1;
display: flex;
flex-direction: column;
}
.content {
flex: 1;
display: flex;
overflow: auto;
flex-direction: column;
}
.box {
display: flex;
}
.column {
padding: 10px;
}
.column > div {}
.column:nth-child(1) {
overflow-y: scroll;
width: 100px;
}
.column:nth-child(2) {
width: 250px;
overflow-y: scroll;
}
<div class="headerPane">headerPane (no scoll)</div>
<div class="body">
<div class="sidebar">sidebar
<br />sidebar
<br />sidebar
<br />sidebar
<br />sidebar
<br />sidebar
<br />sidebar
<br />sidebar
<br />sidebar
<br />sidebar
<br />sidebar
<br />
</div>
<div class="main">
<div class="actionPane">actionPane with buttons (no scroll)</div>
<div class="content">
<div class="box">
<div class="column">
<div>Sub 1</div>
<div>Sub 1</div>
<div>Sub 1</div>
<div>Sub 1</div>
<div>Sub 1</div>
<div>Sub 1</div>
<div>Sub 1</div>
<div>Sub 1</div>
<div>Sub 1</div>
</div>
<div class="column">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ut mattis dui. Suspendisse consequat tincidunt ligula quis pretium. Praesent facilisis turpis urna, vitae mollis turpis condimentum quis. Fusce molestie nulla venenatis erat consectetur,
feugiat congue leo mollis. Nam ac magna ut metus venenatis dapibus. Suspendisse ornare ullamcorper tellus, quis tempus nisi aliquet vel. Sed congue sollicitudin libero id posuere. Nunc maximus aliquam eros.</div>
</div>
</div>
</div>
</div>
</div>
<div class="footerPane">Footer (no scroll)</div>
i have also created a fiddle here: https://jsfiddle.net/p3e7ka20/8/
Thanks in advance.
Add following css:
.box {
height: 100%;
}
overflow-y needs some defined height to work properly.

Two p tag in same line

I have two p tags
<p style="margin: 0; display: inline;">content1</p>
<p style="margin: 0; display: inline;" align="right">content2</p>
The Output is content1content2. My expectation is like this:
content1 content2
Can anyone help. I want one "content1" in the left p and "content2" in the right 'p'.
You can use CSS flexbox for this. Below is the minimal CSS for the requested layout:
<div style="display: flex; justify-content: space-between;">
<p style="background-color: papayawhip;">Lorem ipsum dolor sit amet.</p>
<p style="background-color: palegoldenrod;">Donec eget luctus lacus.</p>
</div>
For longer content, you can use fixed-width columns:
<div style="display: flex; justify-content: space-between;">
<p style="flex-basis: 49.5%; background-color: papayawhip;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eget luctus lacus. Cras consectetur elementum mi sed consequat.</p>
<p style="flex-basis: 49.5%; background-color: palegoldenrod;">Pellentesque aliquet condimentum augue in mattis. Praesent sagittis nisl magna, a volutpat arcu imperdiet vel. Quisque et orci sed ligula cursus luctus.</p>
<!-- 49.5% + 49.5% = 99%, remaining 1% is distributed according to justify-content -->
</div>
You could do it with floats:
<p style="margin:0;display:inline;float:left">content1</p>
<p style="margin:0;display:inline:float:right" >content2</p>
The idea of the tag <p></p> is to display a paragraph. So HTML offers you the <div></div> which is a container conecpt. So you should use Salman A's Solution, because there aren't just different tags in html for no reason. Actually you can style a paragraph with css so it is getting displayed the same as a div container, but it is not meant to be like that.
I don't want to say to you, what you have to do. I just wanna help you using the "correct" tags for the things they were made for.
What you really want is something that doesn't assume sufficent width to fit both paragraphs into one line:
* { box-sizing: border-box; }
.two { width: 30em; max-width: 100%; }
.two p { display: inline-block; max-width: 50%; }
.two p:nth-child(1) { float:left; }
.two p:nth-child(2) { float:right; }
<div class="two">
<p>This is the first paragraph of two.</p>
<p>This is the second paragraph of two.</p>
</div>
Here's another quick turnaround to achieve this:
p{
text-align: center;
}
.item p{
display: inline-block;
}
.leftContent{
text-align: left;
width: 50%;
}
.rightContent{
text-align: right;
width: 50%
}
<br>
<!--Use both P tags in the same line without space -->
<article class="item">
<p class="leftContent">Content1</p><p class="rightContent">Content2</p>
</article>
float:left, float:right.... or
width:49.9%;
display:inline;
text-align:left;
text-align:right;