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

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

Related

CSS: Push flex container item to next row on overflow

I have a flex row with 2 divs (.box-1 and .box-1) both containing some content. What I would like is they both stay in the same row until the content in .box-1 gets long enough to push .box-2 to the next line.
.box-2
Should have a fix width at a desktop screen size.
Should be 50% width on tablet.
Should be full width on mobile.
Right now as you can see from the snippet below the contents in .box-2 will overflow but remain in the same line. What I want is for .box-2 to be pushed to a separate line when content overflows. OR the miniumum width of .box-2 to be wide enough to always hold the contents of my box.
NOTE: One issue I was facing using the col-lg-3 class is that is will become too small for the contents of my .box-2 at times.
I'm trying to use the css gird and stick with bootstrap classes for this. Trying to avoid custom styling as much as possible. The .wrappable class below is a standard in the library i'm using so can't be changed.
.container {
max-width: 300px;
}
.wrappable {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
-webkit-justify-content: space-between;
justify-content: space-between;
-webkit-align-items: center;
align-items: center;
margin-right: -0.625rem;
margin-bottom: -0.625rem;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="wrappable">
<div class="box-1 wrappable-flex border rounded item-1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, incididunt ut labore et
</div>
<div class="box-2 col-sm-12 col-md-6 col-lg-3 border rounded text-nowrap">
some other text I want to put in the same row
</div>
</div>
</div>
you could add to .box-2 min-width: fit-content;
.container {
max-width: 300px;
}
.wrappable {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
-webkit-justify-content: space-between;
justify-content: space-between;
-webkit-align-items: center;
align-items: center;
margin-right: -0.625rem;
margin-bottom: -0.625rem;
}
.box-2 {
min-width: fit-content;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="wrappable">
<div class="box-1 wrappable-flex border rounded item-1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, incididunt ut labore et
</div>
<div class="box-2 col-sm-12 col-md-6 col-lg-3 border rounded text-nowrap">
some other text I want to put in the same row
</div>
</div>
</div>
I was trying to stick to using bootstrap only classes and in my case a small change to the width classes helped.
So, I ended up changing col-lg-3 to col-lg-auto. Fairly simple in hindsight. Found info on this from the bootstrap site.
.container {
max-width: 300px;
}
.wrappable {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
-webkit-justify-content: space-between;
justify-content: space-between;
-webkit-align-items: center;
align-items: center;
margin-right: -0.625rem;
margin-bottom: -0.625rem;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="wrappable">
<div class="box-1 wrappable-flex border rounded item-1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, incididunt ut labore et
</div>
<div class="box-2 col-sm-12 col-md-6 col-lg-auto border rounded text-nowrap">
some other text I want to put in the same row
</div>
</div>
</div>

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

Best way to align images with text in Wordpress?

I'm new to Wordpress and and currently want to add multiple images with text aligned to it on a page. I thought about doing it with a table, but not sure if this is the best solution. Ill describe what I thought how I'd do it.
1. Row 1. Cell Picture
1. Row 2. Cell Header Text + Body text for a short image description
I need to do that multiple times so I thought its the easiest way...
Now I tried to do that with the TablePress Plugin, the only problem is that I cant add a header text for each sell, and there for cant change the font explicitly for the header.
Is there an alternative to TablePress that allows me to change fonts and colors of text within a cell?
Or should I do it manually?
Really appreciate your help.
Thanks
Is this what you're looking for?
.container {
width: 100%;
}
.image, .description {
width: 50%;
float: left;
box-sizing: border-box;
}
.image img {
width: 100%;
}
.description {
padding: 15px;
}
<div class="container">
<div class="image">
<img src="https://preview.ibb.co/ioNAzT/img02.jpg">
</div>
<div class="description">
<h2>Image title</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ut mollis lacus. Pellentesque euismod mauris justo. Aenean eu sagittis ex. Suspendisse potenti. Duis velit lacus, vestibulum sit amet ex sed, mattis scelerisque metus.</p>
</div>
</div>
I wouldn't use a table but a flexbox to store the products. For an example see below.
.row {
display: flex;
justify-content: space-around; /* Horizontal alignment */
}
article {
display: flex;
flex-direction: column;
align-items: center; /* Horizontal alignment */
}
<div class="row">
<article>
<div class="image"><img src="https://via.placeholder.com/100x100"></div>
<h2>Header text</h2>
<p>Detail text</p>
</article>
<article>
<div class="image"><img src="https://via.placeholder.com/100x100"></div>
<h2>Header text</h2>
<p>Detail text</p>
</article>
<article>
<div class="image"><img src="https://via.placeholder.com/100x100"></div>
<h2>Header text</h2>
<p>Detail text</p>
</article>
</div>

Responsive Flexbox Divs

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>

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;