Responsive Flexbox Divs - html

I want all inner divs containing text and with background color to be of the same height as the outer div (col).
Is there a way to accomplish that without jQuery?
.row {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction: row;
}
.col {
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
padding: 1em;
border: solid;
justify-content:center;
align-items:center;
overflow:hidden;
}
.col img{
width:100%;
height:auto;
}
<div class="row">
<div class="col">
<div><img src="" alt="" /></div>
<div style="background-color:#ff0000;">
This is a short text.
</div>
</div>
<div class="col">
<div><img src="" alt="" /></div>
<div style="background-color:#ff3654;" >
This is a text about how much I hate fucking around ith CSS when I'm not a pro. I wasted so much time on this issue I don't even mind writing this text. Sure you could use lorem ipsum but why bother, time you enjoy wasting is not wasted.
</div>
</div>
<div class="col">
<div><img src="" alt="" /></div>
<div style="background-color:#ff0000;">
This is CSS, just wanted to let you know that you suck.
</div>
</div>
</div>
https://jsfiddle.net/hcmnztof/1/

You can use flexible boxes again for .col:
.col {
display: flex;
flex-direction: column;
align-items: stretch; /* default value */
}
.col > :last-child {
flex-grow: 1;
}
.row {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction: row;
}
.col {
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
padding: 1em;
border: solid;
justify-content: center;
overflow: hidden;
display: flex;
flex-direction: column;
}
.col > :last-child {
flex-grow: 1;
}
.col img {
width: 100%;
height: auto;
}
<div class="row">
<div class="col">
<div>
Image1
</div>
<div style="background-color:#ff0000;">
Lorem ipsum.
</div>
</div>
<div class="col">
<div>
Image2 <br/> Image2
</div>
<div style="background-color:#ff3654;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue.
</div>
</div>
<div class="col">
<div>
Image3 <br/> Image3 <br/> Image3
</div>
<div style="background-color:#ff0000;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris.
</div>
</div>
</div>

thanks for your input. I used this page to gain some basic knowledge about flexbox. CSS-Tricks A guide to flexbox
Then I started from scratch and came up with what I wanted: jsfiddle
.row {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction:row;
padding: 10% 0 10% 0;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.row img{
width:100%;
height:auto;
}
.col {
-webkit-box-flex:1;
-moz-box-flex:1;
flex:1;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction:column;
align-items:stretch;
margin-right:3%;
}
.subcol{
-webkit-box-flex:1;
-moz-box-flex:1;
flex:1 auto;
margin-top:-1%;
-webkit-border-bottom-right-radius: 15px;
-webkit-border-bottom-left-radius: 15px;
-moz-border-radius-bottomright: 15px;
-moz-border-radius-bottomleft: 15px;
border-bottom-right-radius: 15px;
border-bottom-left-radius: 15px;
}
.subcol h2{
text-align:center;
color:#ffffff;
padding: 10% 10% 0 10%;
font-weight:200;
font-size:2.2em;
line-height:1;
text-transform:uppercase;
margin-bottom:13%;
}
.subcol p{
text-align:center;
padding: 0 10% 10% 10%;
margin-top:-1.2%;
color:#ffffff;
font-size:0.9em;
}
<div class="row">
<div class="col">
<img src="http://lorempixel.com/50/50" alt="" />
<div class="subcol" style="background-color:#ff0000;">
<h2>Test headline</h2>
<p>This is a short text.</p>
</div>
</div>
<div class="col">
<img src="http://lorempixel.com/120/150" alt="" />
<div class="subcol" style="background-color:#ff3654;" >
<h2>Test headline</h2>
<p>This is a text about how much I hate fucking around ith CSS when I'm not a pro. I wasted so much time on this issue I don't even mind writing this text. Sure you could use lorem ipsum but why bother, time you enjoy wasting is not wasted.</p>
</div>
</div>
<div class="col">
<img src="http://lorempixel.com/100/150" alt="" />
<div class="subcol" style="background-color:#ff0000;">
<h2>Test headline</h2>
<p>This is CSS, just wanted to let you know that you suck.</p>
</div>
</div>
</div>

Related

Flexbox image with caption in column style

I am currently making a carousel of images. (Carousel is based on a library, not in the scope of this question as i also cannot get it to work outside the carousel).
What i am trying to do is the following: I have a div and within that div i have an image and a caption (just a line of text or 2). I want the image above the caption and as large as possible (taking up all the space the caption does not take up). But the image keeps exceeding the parent div's height. It is never contained in the parent div. And if it is, it loses its ratio (keeps the same width but height is contained which gives a weird result obviously).
I tried a lot with flexbox like this:
<div class="container"> // <---- this wrapper need a specific height depending on screen size.
<img src="/images/thing.png" class="img" alt="pict">
<div class="caption">
<span>Text</span>
</div>
</div>
and the CSS:
.container {
height: 800px;
}
.img {
width: auto;
height: 100%; // this fills the container with the image but i wont see the caption anymore.
// also tried giving this one flex (NOT in combination with above. Just showing all i tried)
flex: 1 1 0%;
}
.caption {
flex: 1 1 0%; // Or 1 1 auto; no difference
}
How do i go about this? Maybe it is just me struggling with flexbox. Maybe i am approaching this wrongly. Any help/tips is appreciated!
/* Horizontal flex: rows
=====================
*/
.row {
display: flex;
}
.item {
flex: 1 1 30%;
padding-left: 1.5rem;
}
.item:first-child {
padding-left: 0;
}
/* Vertical flex: items
===================
*/
.item {
display: flex;
flex-direction: column;
}
.item-heading {
flex: 1;
}
/* Third flex: item heading
========================
*/
.item-heading {
display: flex;
justify-content: center;
flex-direction: column;
}
/* Not so important CSS - base styles:
===================================
*/
body {
margin: 2rem;
}
img {
max-width: 100%;
height: auto;
}
h2,
p {
margin: 0;
padding: 0;
}
.row {
margin-bottom: 1.5rem;
}
.item-heading {
padding: 1rem;
font-size: 1rem;
background: yellow;
text-align: center;
}
.meta {
color: #666;
margin-bottom: 3rem;
}
.meta p {
margin-bottom: .5rem;
}
<div class="meta">
<h1>
Image gallery with a vertically centered captions
</h1>
<p>
Yellow headings we want both vertical and horizontal centered.
</p>
<p>
There are three nested flexbox containers – row, item and item heading.
</p>
</div>
<div class="row">
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Lorem ipsum dolor sit amet
</h2>
</div>
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo
</h2>
</div>
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Consectetuer adipiscing elit
</h2>
</div>
</div>
<div class="row">
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Nunc dapibus tortor vel mi dapibus sollicitudin
</h2>
</div>
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Mauris elementum mauris vitae tortor
</h2>
</div>
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Aliquam ornare wisi eu metus
</h2>
</div>
</div>
codepen : Link
i hope this help you

Display flex a few block with text in the same line

I am trying to create a few block/box with image and two text - headline and subtext, but headline sometimes has one sentence, but another more, so if one has one and second one has two I would like to center vertically this first one.
For subtext I would like to vertically to the top.
Here is code which I am trying to use:
<div class="row row-home">
<div class="col-sm-4">
<img src="/images/home-1.png" alt="Schody" class="center-block img-responsive" />
<div class="text">
<h2>Schody</h2>
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit. Nulla volut pat sem id turpis tempus efficitur. Maecenas eu bibendum sem.</h3>
</div>
</div>
<div class="col-sm-4">
<img src="/images/home-2.png" alt="Balustrady" class="center-block img-responsive" />
<div class="text">
<h2>Balustrady i ogrodzenia</h2>
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit. Nulla volut pat sem id turpis tempus efficitur. Maecenas eu bibendum sem.</h3>
</div>
</div>
<div class="col-sm-4">
<img src="/images/home-3.png" alt="Meble" class="center-block img-responsive" />
<div class="text">
<h2>Meble</h2>
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit. Nulla volut pat sem id turpis tempus efficitur. Maecenas eu bibendum sem.</h3>
</div>
</div>
</div>
Here is CSS:
#home .text {
display: flex;
flex-direction: column;
-ms-flex-direction: column;
position: relative;
text-align: center;
padding: 10px 20px 30px;
margin: 0px auto 40px;
width: 100%;
height: 100%;
max-width: 236px;
z-index: 1;
}
#home .text h2 {
display: flex;
flex-direction: column;
-ms-flex-direction: column;
justify-content: center;
flex: 1 0 auto;
position: relative;
color: white;
font-size: 14px;
font-weight: bold;
text-transform: uppercase;
margin-top: 40px;
margin-bottom: 0px;
z-index: 2;
}
#home .text h3 {
position: relative;
color: white;
font-size: 12px;
text-align: justify;
text-transform: uppercase;
text-align-last: center;
z-index: 2;
}
It almost work, but if h3 has less text in one of blocks this text align to the bottom and h2 goes down.
Here is live website to test
I create picture to show how it should work
Since the h2 elements aren't aware of each other, I suggest you give them a height, or else you will need a extensive markup change
Here I added height: 28px; to the .row-home .text h2 rule
.row-home .text {
display: flex;
flex-direction: column;
-ms-flex-direction: column;
position: relative;
text-align: center;
padding: 10px 20px 30px;
margin: 0px auto 40px;
width: 100%;
height: 100%;
max-width: 236px;
z-index: 1;
}
.row-home .text h2 {
display: flex;
flex-direction: column;
-ms-flex-direction: column;
justify-content: center;
flex: 1 0 auto;
position: relative;
color: white;
font-size: 14px;
height: 28px;
font-weight: bold;
text-transform: uppercase;
margin-top: 40px;
margin-bottom: 0px;
z-index: 2;
}
.row-home .text h3 {
position: relative;
color: white;
font-size: 12px;
text-align: justify;
text-transform: uppercase;
text-align-last: center;
z-index: 2;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row row-home">
<div class="col-sm-4">
<img src="http://malykowal.linuxpl.eu/images/home-1.png" alt="Schody" class="center-block img-responsive" />
<div class="text">
<h2>Schody</h2>
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit. Nulla volut pat sem id turpis tempus efficitur. Maecenas eu bibendum sem.</h3>
</div>
</div>
<div class="col-sm-4">
<img src="http://malykowal.linuxpl.eu/images/home-1.png" alt="Balustrady" class="center-block img-responsive" />
<div class="text">
<h2>Balustrady i ogrodzenia</h2>
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit. Nulla volut pat sem id turpis tempus efficitur. Maecenas eu bibendum sem.</h3>
</div>
</div>
<div class="col-sm-4">
<img src="http://malykowal.linuxpl.eu/images/home-1.png" alt="Meble" class="center-block img-responsive" />
<div class="text">
<h2>Meble</h2>
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit. Nulla volut pat sem id turpis tempus efficitur. Maecenas eu bibendum sem.</h3>
</div>
</div>
</div>
</div>
Updated with a second version based on a comment
If you want this to be really dynamic, you'll need to break it down like this, where you make the col-sm-4 flex items
Updated again, with a media query, to utilize Flexbox's order property and re-position the elements on smaller screens
.row-home {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.row-home .col-sm-4 {
min-width: 0;
flex-basis: 33%;
padding: 10px 20px 30px;
}
.row-home .col-sm-4.vcentered {
align-self: center;
}
.row-home .col-sm-4 h2 {
font-size: 14px;
font-weight: bold;
text-transform: uppercase;
}
.row-home .col-sm-4 h3 {
font-size: 12px;
text-align: justify;
text-transform: uppercase;
text-align-last: center;
}
#media (max-width: 600px) {
.row-home .col-sm-4 {
flex-basis: 51%;
}
.row-home .col-sm-4 * {
text-align: center;
}
.row-home .col-sm-4:nth-child(3n+1) {
order: 1;
}
.row-home .col-sm-4:nth-child(3n+2) {
order: 2;
}
.row-home .col-sm-4:nth-child(3n+3) {
order: 3;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row row-home">
<div class="col-sm-4">
<img src="http://malykowal.linuxpl.eu/images/home-1.png" alt="Schody" class="center-block img-responsive" />
</div>
<div class="col-sm-4">
<img src="http://malykowal.linuxpl.eu/images/home-1.png" alt="Balustrady" class="center-block img-responsive" />
</div>
<div class="col-sm-4">
<img src="http://malykowal.linuxpl.eu/images/home-1.png" alt="Meble" class="center-block img-responsive" />
</div>
<div class="col-sm-4 vcentered">
<h2>Schody this is some extra text I added for test</h2>
</div>
<div class="col-sm-4 vcentered">
<h2>Balustrady i ogrodzenia</h2>
</div>
<div class="col-sm-4 vcentered">
<h2>Meble</h2>
</div>
<div class="col-sm-4">
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit.</h3>
</div>
<div class="col-sm-4">
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit. Nulla volut pat sem id turpis tempus efficitur. Maecenas eu bibendum sem.</h3>
</div>
<div class="col-sm-4">
<h3>Lorem ipsum dolor sit amet, consectetur adipi scing elit. Nulla volut pat sem id turpis tempus efficitur. Maecenas eu bibendum sem.</h3>
</div>
</div>
</div>

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.

Align two vertically-stacked rows of flex items next to one full-height flex item

I`m using flexbox and trying to make 6 things like this:
So far this is my code:
<section class="focus-item">
<div class="item focus-item-1">
<h4>Brand </h4>
<p>Identity
aliquam ipsum ante morbi sed ipsum mollis.
Sollicitudin viverra, vel varius eget sit mollis.
</p>
<img src="D:/htmlCss/landing/assets/img/focus-item-1.png" alt="" />
</div>
</section>
CSS
.focus-item {
display: flex;
flex-wrap: wrap;
}
.focus-item .item {
display: flex;
width: 50%;
text-align: left;
}
.focus-item .item p {
display: flex;
flex: 1;
align-items: flex-end;
}
.focus-item .item h4 {
display: flex;
align-items: flex-start;
}
I have problem with placing h4 on top of p. I would be great if someone can point me in the right direction.
As I already posted it in my first comment, you can do it by making the first flex item a flex box as well:
<div class="container">
<div class="item-1">
<h4>Header</h4>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<img src="https://cdn.tutsplus.com/net/uploads/legacy/958_placeholders/placehold.gif" />
</div>
.container {
display: flex;
align-items: flex-end;
}
.item-1 {
display: flex;
flex-direction: column;
align-items: flex-end;
}
See here for the working example: http://jsfiddle.net/0j7abre9/1/
The key to this layout is to create one flex container that holds two flex items – the first holding the <h4> and <p>, and the other holding the <img> – and align them in a row. Then convert the first flex item to a flex container as well, and align its two items in a column.
HTML
<section class="focus-item">
<div class="item focus-item-1"><!-- nested flex container 1 -->
<h4>Brand</h4>
<p>Identity aliquam ipsum ante morbi sed ipsum mollis.
Sollicitudin viverra, vel varius eget sit mollis.</p>
</div>
<div class="item focus-item-2"><!-- nested flex container 2 -->
<img src="http://i.imgur.com/60PVLis.png" height="200" width="200" alt="">
</div>
</section>
CSS
.focus-item {
display: flex;
}
.item {
display: flex;
flex: 1 1 50%;
}
.focus-item-1 {
flex-direction: column;
align-items: flex-end;
}
DEMO

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;