kepp text in the middle and position 3 images in the middle with text below it - css

I am trying to position a text in the middle while having 3 images in a row with text below each other and the last text and image next to each other as shown below. I have been trying to use flexbox. If someone can be kind enough to help I would appreciate it. thank you the design and code are at the bottom.
here is my code
<div class="wrapper">
<div class="title">
<h2>Become a Pro with our camp</h2>
</div>
<section>
<img src="img/diet-icon.png" alt="diet">
<h2>Diet</h2>
<p>Eat what the pros eat.</p>
</section>
<section>
<img src="img/training.png" alt="workout">
<h2>Workout</h2>
<p>Workout how the pros do.</p>
</section>
<section>
<img src="img/basketball-training.jpg" alt="basketball training">
<h2>Basketball Training</h2>
<p>Train like how the pros train.</p>
</section>
<section class="section-champ">
<h2>The beginning towards winning the championship starts here</h2>
<img src="img/championship-img.jpg" alt="championship" class="champ">
</section>
</div>
.wrapper {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.wrapper div h2 {
text-align: center;
font-weight: bold;
font-family: sans-serif;
margin-top: 13px;
padding-bottom: 20px;
width: 100%;
}
.wrapper section {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-bottom: 25px;
}
.wrapper section img {
width: 50px;
}
.wrapper .section h2 {
text-align: center;
font-family: sans-serif;
}
.wrapper section .champ {
width: 300px;
}

This should get you there. That is if I am understanding the question correctly. Codepen example https://codepen.io/nickberens360/pen/RwxLKMd
<div class="wrapper">
<div class="title">
<h2>Become a Pro with our camp</h2>
</div>
<div class="stuff">
<section class="icon-group">
<img src="img/diet-icon.png" alt="diet">
<h2>Diet</h2>
<p>Eat what the pros eat.</p>
</section>
<section class="icon-group">
<img src="img/training.png" alt="workout">
<h2>Workout</h2>
<p>Workout how the pros do.</p>
</section>
<section class="icon-group">
<img src="img/basketball-training.jpg" alt="basketball training">
<h2>Basketball Training</h2>
<p>Train like how the pros train.</p>
</section>
</div>
<section class="section-champ stuff">
<h2>The beginning towards winning the championship starts here</h2>
<img src="img/championship-img.jpg" alt="championship" class="champ">
</section>
</div>
<style>
.wrapper {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.stuff {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.icon-group {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
flex-wrap: wrap;
padding: 0 12px;
border: 1px solid red
}
</style>
[1]: https://codepen.io/nickberens360/pen/RwxLKMd

You need to wrap your images/titles sections in a div with flex box.
Below is an example of how the markup and css would look.
HTML:
<div class="wrapper">
<h1>Become a pro at our camp</h1>
<div class="img-wrapper">
<div class="img 1">
<img src="./food.png"/>
<h5>Diet</h5>
<p>Eat what the pros eat</p>
</div>
<div class="img 2">
<img src="./dumbbell.png"/>
<h5>Workout</h5>
<p>Workout how the pros do.</p>
</div>
<div class="img 3">
<img src="./basketball.png"/>
<h5>Basketball Training</h5>
<p>Train like the pros train.</p>
</div>
</div>
</div>
CSS:
* {
padding: 0;
margin: 0;
}
.wrapper {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-family: 'Helvetica';
}
.img-wrapper {
margin-top: 2vw;
width: 100%;
display: flex;
justify-content: center;
gap: 2vw;
}
ion-icon {
font-size: 5vw;
}
h5 {
font-size: 1vw;
}
.img {
display: flex;
flex-direction: column;
align-items: center;
gap: .5vw;
}
You can find a codepen example of this working here

Related

Layout gap/margin center two last elements with flex-wrap

I have issues with a layout where I want to center the last two elements on a row. First I tried with grid and couldn't find any proper solution so I found a way to solve it with "flex-wrap" and it looks pretty good.
The issue I'm facing now is with the gap/margin. I can't find any good solution while I'm setting the width to 33,33%.
Is there any other good way to solve this? I'm kind of stuck and have been looking at this for hours now :) (see layout attached image)
#container {
display: flex;
flex-wrap: wrap;
justify-content: center;
flex-direction: column;
}
#media only screen and (min-width: 640px) {
#container {
display: flex;
flex-wrap: wrap;
justify-content: center;
flex-direction: row
}
}
.item {
width: 100%;
background: teal;
color: white;
border-radius: 10px;
text-align: center;
}
#media only screen and (min-width: 640px) {
.item {
width: 33.33%;
}
}
<div id="container">
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
</div>;
You can use the gap parameter, see the example below.
Update:
Solution 1
Stretched Last element
#container {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 20px;
}
.item {
width: 100%;
background: teal;
color: white;
border-radius: 10px;
text-align: center;
}
/* Start Media Query*/
#media only screen and (min-width: 640px) {
#container {
display: flex;
justify-content: center;
flex-direction: row;
}
.item {
flex: 1 1 30%;
}
} /* End Media Query*/
<div id="container">
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
</div>
Solution 2
Not stretched last element
#container {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 20px;
}
.item {
width: 100%;
background: teal;
color: white;
border-radius: 10px;
text-align: center;
}
/* Start Media Query*/
#media only screen and (min-width: 640px) {
#container {
display: flex;
justify-content: center;
flex-direction: row;
}
.item {
flex: 0 0 calc(33.33% - 15px);
}
} /* End Media Query*/
<div id="container">
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
<div class="item">
<p>Item</p>
</div>
</div>

align-content: center not working in Chrome

I am trying to vertically align divs with flexbox, by using the align-content: center. It works fine in Firefox and IE, but it does not vertically align in Chrome. In Chrome the divs stays on top.
<div class="products-wrapper">
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
</div>
And CSS:
.products-wrapper {
width: 100%;
min-height: 100vh;
/* FLEX */
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: center;
}
.product {
max-width: 250px;
padding: 10px;
}
.product img {
display: block;
margin: 0 auto;
max-height: 250px;
max-width: 250px;
}
I have also been trying align-items: center; which centers the divs vertically, but it will not be a straight line with the images and text if I have different amount of text for example.
Anyone with a solution how to vertically center divs (straight), which works in all browsers?
Image example:
Edit: If you don't need the height to be dynamic, align-items: center; will work with (for example) max-height: 400px; on the .product div. Not really what I wanted though.
In order to achieve what you want you'll need to use align-items: flex-start, this way all items will be aligned to the top of their line. And to center vertically you're looking for the align-items: center property which aligns the items in the flex container along the cross axis, which in your case you'll have to use in an outer wrapper to make sure the products div is centered in the page.
The align-content: center property comes into play only when there is more than one line (wrapped content).
A better explanation about the difference between those two properties is in the answer to this stackoverflow question
.outer-wrapper {
width: 100%;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.products-wrapper {
width: 100%;
/* FLEX */
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
background-color: gray;
align-items: flex-start;
}
.product {
max-width: 250px;
padding: 10px;
background-color: red;
margin: 5px;
}
.product img {
display: block;
margin: 0 auto;
max-height: 250px;
max-width: 250px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="outer-wrapper">
<div class="products-wrapper">
<div class="product">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
</div>
</div>
</body>
</html>
Make the .product element, flex container. This so you can align the content inside them.
.product {
max-width: 250px;
padding: 10px;
display: flex;
justify-content: center;
flex-direction: column;
}
Hope this helps :)
.products-wrapper {
width: 100%;
min-height: 100vh;
/* FLEX */
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: center;
}
.product {
max-width: 250px;
padding: 10px;
display: flex;
justify-content: center;
flex-direction: column;
}
.product img {
display: block;
margin: 0 auto;
max-height: 250px;
max-width: 250px;
}
<div class="products-wrapper">
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
<div class="product">
<img src="images/temporary.jpg" alt="Temporary">
<h3>Title</h3>
<p>Text.</p>
</div>
</div>

Strange Margin showing up in a flexbox

I have tried negative margins and other hacks but as far as i know, there shouldn't be any margins there in the first place. With just a simple box i can get zero margins between the tags but somehow the stuff outside that is messing with it.
The problem is with the neon blue badges in the bottom right corner.
.info{
display: flex;
flex-direction: row;
// justify-content: space-between;
}
.column {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 48%;
min-height: 61.9px;
// background-color: #00ffff;
display: flex;
flex-direction: column;
}
.info-block{
display: flex;
flex-direction: column;
}
.spacer{
height:14.7px;
width: 1px;
}
.tag{
line-height: 0;
display:inline-block;
height: 15.8px;
width:40px;
padding-right: 4px;
padding-left: 4px;
padding-top: 2px;
background-color: #00ffff;
border-radius: 4px;
}
.tag-container{
line-height: 0;
width: 168px;
height:78px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-content: flex-start;
}
.tag-text{
width:20px;
font-family: HelveticaNeue-Bold;
font-size: 8px;
font-weight: normal;
font-style: normal;
font-stretch: normal;
text-align: center;
color: #ffffff;
}
<div class="info">
<div class="column">
<div class="info-block">
<div class="info-header">Location</div>
<div class="info-text">place</div>
</div>
<div class="spacer"></div>
<div class="info-block">
<div class="info-header">Mobile</div>
<div class="info-text">+44 (0) 788-588</div>
</div>
</div>
<div class="column">
<div class="info-block">
<div class="info-header">Menu</div>
<div class="info-text"><a>bk.com</a></div>
</div>
<div class="spacer"></div>
<div class="info-block ">
<div class="info-header">Tags</div>
<div class="tag-containter">
<div class="tag"><div class="tag-text">h</div></div>
<div class="tag"><div class="tag-text">Som</div></div>
<div class="tag"><div class="tag-text">Somethg</div></div>
<div class="tag">
<div class="tag-text">ng</div>
</div>
<div class="tag">
<div class="tag-text">Somhing</div>
</div>
</div>
</div>
</div>
You only made a silly mistake.
Just check your class name .tag-containter in css and html.
you use .tag-container instead of tag-containter class.
you mispelled the class tag-container either in the html (tag-containter) or in the css . notice the extra T in html and missing in css selector
.info {
display: flex;
flex-direction: row;
/* justify-content: space-between;*/
}
.column {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 48%;
min-height: 61.9px;
/* background-color: #00ffff;*/
display: flex;
flex-direction: column;
}
.info-block {
display: flex;
flex-direction: column;
}
.spacer {
height: 14.7px;
width: 1px;
}
.tag {
line-height: 0;
height: 15.8px;
width: 40px;
padding-right: 4px;
padding-left: 4px;
padding-top: 2px;
background-color: #00ffff;
border-radius: 4px;
}
.tag-containter {
width: 168px;
height: 78px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-content: flex-start;
}
.tag-text {
font-family: HelveticaNeue-Bold;
font-size: 8px;
font-weight: normal;
font-style: normal;
font-stretch: normal;
text-align: center;
color: #ffffff;
}
<div class="info">
<div class="column">
<div class="info-block">
<div class="info-header">Location</div>
<div class="info-text">place</div>
</div>
<div class="spacer"></div>
<div class="info-block">
<div class="info-header">Mobile</div>
<div class="info-text">+44 (0) 788-588</div>
</div>
</div>
<div class="column">
<div class="info-block">
<div class="info-header">Menu</div>
<div class="info-text"><a>bk.com</a>
</div>
</div>
<div class="spacer"></div>
<div class="info-block ">
<div class="info-header">Tags</div>
<div class="tag-containter">
<div class="tag">
<div class="tag-text">h</div>
</div>
<div class="tag">
<div class="tag-text">Som</div>
</div>
<div class="tag">
<div class="tag-text">Somethg</div>
</div>
<div class="tag">
<div class="tag-text">ng</div>
</div>
<div class="tag">
<div class="tag-text">Somhing</div>
</div>
</div>
</div>
</div>
.tag is set to display: inline-block;, which is an inline element. Spaces between inline elements are preserved in HTML, so if you have spaces (or returns) between the elements, you will see a space between the elements on the page. To remove the spaces between the .tag boxes, remove the actual spaces between those elements.

Flex-box layout make no-stretching raw

Is it possible to make dynamic content from database not to stretch on height in wrapping raws. Here is what I've achieved.
.main-raw {
width: 250px;
height: 100%;
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
-webkit-align-content: flex-end;
align-content: flex-end;
}
.item-1, .item-2, .item-3 {
width: 100px;
margin: 3px;
background-color: black;
}
.item-1 {
height: 100px;
}
.item-2 {
height: 200px;
}
.item-3 {
height: 150px;
}
<div class='main-raw'>
<div class='item-1'>&nbsp</div>
<div class='item-2'>&nbsp</div>
<div class='item-3'>&nbsp</div>
</div>
And here is what I am looking for:
wasn't too sure by what you meant, when you said 'not stretch on height'
but I contained the elements in there own wrappers in order to properly position them.
<div class='main-raw'>
<div class="first-row">
<div class='item item-1'>&nbsp</div>
<div class='item item-2'>&nbsp</div>
</div>
<div class="second-row">
<div class='item item-3'>&nbsp</div>
<div class='item item-4'>&nbsp</div>
</div>
<div class="third-row">
<div class='item item-5'>&nbsp</div>
<div class='item item-6'>&nbsp</div>
</div>
</div>
here is an example:
https://jsfiddle.net/ax0mfc0p/2/

get image and caption into one flex item?

AI want to put an image and its caption into one flex-item and have several such flex-items in one flex-container. I want the image to be above its caption. But what happens is that each caption is beside its image. How do I get them one above the other?
I tried putting the captions into another flex-container below the images. But when the screen size is less wide, the images stack, then the captions stack instead of being image, then its caption, image, then its caption.
<div class="flex-item-popup" id="popup">
<div class="close"><i class="fa fa-2x fa-times-circle"></i></div>
<h2></h2>
<div class='text'></div>
<div class="videos"></div>
<div class="flex-container images"></div>
<div class="flex-container captions"></div>
</div>
css:
#popup {
width: 90%;
height: auto;
padding: 20px;
border-radius: 10px;
background-color: black;
color: white;
}
#popup .close {
/*position: absolute;
right: 10px;
top: 10px;*/
float: right;
color: white;
opacity: 1;
}
#popup .close:hover {
opacity: .7 !important;
}
.images, .captions {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
align-content: flex-start;
align-items: flex-start;
}
.images {
margin-top: 20px;
}
.images .flex-item, .captions .flex-item {
display: flex;
flex-grow: 0;
flex-shrink: 0;
flex-basis: 300px;
}
Look into <figure> and <figcaption> HTML5 tags. I am pretty certain that is going to help you and it is what you are looking for.
Here is updated fiddle link.
And the SO code snippet here...
.flex-container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: flex-start;
align-items: center;
}
.flex-item {
/*width: 350px;
height: null;*/
flex-grow: 1;
flex-shrink: 0;
flex-basis: 200px;
justify-content: flex-start;
display: flex;
padding-left: 20px;
}
.flex-item img {
cursor: pointer;
}
<div class="flex-container images">
<div class="flex-item">
<figure>
<img src="http://newsinteractive.post-gazette.com/hbcu/morehouse/img/grads.jpg" />
<!--<div style="clear: both;"></div>-->
<figcaption>Our grads.</figcaption>
</figure>
</div>
<div class="flex-item">
<figure>
<img src="http://newsinteractive.post-gazette.com/hbcu/morehouse/img/building.jpg" />
<figcaption>A building</figcaption>
</figure>
</div>
<div class="flex-item">
<figure>
<img src="http://newsinteractive.post-gazette.com/hbcu/morehouse/img/campus.jpg" />
<figcaption>The campus.</figcaption>
</figure>
</div>
<div class="flex-item">
<figure>
<img src="http://newsinteractive.post-gazette.com/hbcu/morehouse/img/trio.jpg" />
<figcaption>Three people</figcaption>
</figure>
</div>
</div>
Hope this helps

Resources