Align card contents with other card contents (photo) - html

Today I've faced with specific design: there is row of cards and text inside card is aligned with another text from other cards. So title is aligned with title from other cards, text is aligned with other texts. It is very difficult for me to explain it clearly so I make a screenshot of the thing I'm trying to reach
By now I'm ready to completely ignore this issue due to impossibility of realization by pure css, but who knows, may be there is some solution?
UPDATE: I'm sorry for lack of explanation. Here is the code. My aim is to make the same alignment as in screeenshot above without using <br>s and fixed heights.
.list {
display: flex;
}
.item {
flex: 0 0 auto;
display: flex;
flex-direction: column;
align-items: center;
width: 120px;
padding: 10px;
background-color: #fff;
border-radius: 10px;
text-align: center;
background-color: rebeccapurple;
color: #fff;
}
.item>* {
flex: 0 0 auto;
}
.item>*+* {
margin-top: 10px;
}
.item+.item {
margin-left: 20px;
}
.icon {
background-color: red;
border-radius: 100%;
width: 20px;
height: 20px;
}
.title {
text-transform: uppercase;
font-weight: bold;
}
.text {
float: left;
clear: left;
}
<div class="list">
<div class="item">
<div class="icon"></div>
<div class="title">Lorem ipsum dolor sit amet.</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est, amet.</div>
</div>
<div class="item">
<div class="icon"></div>
<div class="title">Lorem ipsum.</div>
<div class="text">Lorem ipsum dolor sit amet</div>
</div>
<div class="item">
<div class="icon"></div>
<div class="title">Lorem ipsum dolor.</div>
<div class="text">Lorem ipsum dolor sit amet</div>
</div>
<div class="item">
<div class="icon"></div>
<div class="title">Lorem ipsum dolor sit amet, consectetur.</div>
<div class="text">Lorem ipsum dolor sit amet</div>
</div>
</div>
Thanks in advance.

What you are trying to do can be easily achieved using CSS grids and a bit of HTML restructuring.
The other way to do this would be to give fixed heights to your text elements.
If you are not familiar with CSS grids and don't like the idea of giving fixed heights to your elements, you can achieve similar result by using a little bit of JavaScript. Check the attached snippet.
I have written a small function equalizeClass() to equalize heights of all the elements belonging to a particular class. What it does is basically scans through all the elements belonging to a particular class and finds the element with maximum height. It then sets the heights of all the elements equal to the calculated maximum height.
Don't forget to call equalize() every time you update your related DOM elements.
I have not changed anything in your HTML structure.
In CSS, I have just added justify-content to your item class.
justify-content: space-between;
function equalizeClass(className) {
var images = document.getElementsByClassName(className);
var max_height = 0;
for (var i = 0; i < images.length; i++) {
if (images[i].clientHeight > max_height) {
max_height = images[i].clientHeight;
}
}
for (var i = 0; i < images.length; i++) {
images[i].style.height = max_height + 'px';
}
}
function equalize() {
equalizeClass("title");
equalizeClass("text");
}
window.addEventListener("orientationchange", equalize());
window.addEventListener('resize', equalize());
.list {
display: flex;
}
.item {
flex: 0 0 auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
width: 120px;
padding: 10px;
background-color: #fff;
border-radius: 10px;
text-align: center;
background-color: rebeccapurple;
color: #fff;
}
.item>* {
flex: 0 0 auto;
}
.item>*+* {
margin-top: 10px;
}
.item+.item {
margin-left: 20px;
}
.icon {
background-color: red;
border-radius: 100%;
width: 20px;
height: 20px;
}
.title {
text-transform: uppercase;
font-weight: bold;
}
.text {
float: left;
clear: left;
}
<div class="list">
<div class="item">
<div class="icon"></div>
<div class="title">Lorem ipsum dolor sit amet.</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est, amet.</div>
</div>
<div class="item">
<div class="icon"></div>
<div class="title">Lorem ipsum.</div>
<div class="text">Lorem ipsum dolor sit amet</div>
</div>
<div class="item">
<div class="icon"></div>
<div class="title">Lorem ipsum dolor.</8>
</div>
<div class="text">Lorem ipsum dolor sit amet</div>
</div>
<div class="item">
<div class="icon"></div>
<div class="title">Lorem ipsum dolor sit amet, consectetur.</div>
<div class="text">Lorem ipsum dolor sit amet</div>
</div>
</div>

You need to make your title div a fixed height, ideally a height that is taller than the title text itself. See the code below for your title div:
.title {
text-transform: uppercase;
font-weight: bold;
height: 100px;
max-width: 100%;
}
Then give your text div this style:
.text{
display: flex;
justify-content: center;
align-items: top;
}
See this pen.

Related

why does the div and the text go out in mobile?

i'm doing some front-end stuffs and i got interrupt by an error
I want to get two cards in a section and i want them aligned horizontally. The problem is that in desktop version it works as well, but when i watch the mobile version it looks weird.
https://prnt.sc/XBbBVW2suZmE
the card goes off the section (the yellow space), the text goes outside the card and it isn't horizontally aligned.
<section class="blogdiv">
<div class="cards">
<div class="card" style="width: 18rem;">
<img class="card-img-top">
<div class="card-body">
<h5 class="card-title">Vespa</h5>
<p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
leggi di più
</div>
</div>
<div class="card" style="width: 18rem;">
<img class="card-img-top">
<div class="card-body">
<h5 class="card-title">Vespa</h5>
<p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
leggi di più
</div>
</div>
</div>
</section>
i'm using sass so, here's the scss of the blogdiv
&.blogdiv{
display: flex;
justify-content: center;
align-items: center;
background-color: $color2;
color: $color1;
.cards{
.card{
#include selection($color2, $color1);
display: inline-block;
margin: 0.5rem;
max-width: 60vw;
max-height: 45vh;
border: 1px solid $color1;
color: $color1;
.card-img-top{
max-width: 60vw;
max-height: 60vh;
}
.card-body{
padding: 1rem;
.card-title{
font-size: 3vh;
font-weight: 600;
margin: 0;
}
a{
#each $prefix in $prefixes {
#{$prefix}user-select: none;
}
cursor: default;
color: $navcolor2;
text-decoration: none;
&:hover{
color: black;
}
}
}
}
}
}
first you need to add a mobile reponsive section, using #media screen
#media screen and (max-width: 960px){
.card{
display: block;
margin: 0 auto; /*align horizontal auto*/
}
}
and than, is just use the width --- heigth px to make proportional....

Width of a flex item doesn't change when I use %

As far as I know, width of a flex item adjusts to its content(when flex-direction: row;).
Here you see, the width of second .item is too long even though I set the width of h1 to 50%.
* {
margin: 0;
padding: 0;
}
.container {
display: flex;
justify-content: space-between;
border: 2px solid red;
padding: 10px;
}
.item {
border: 1px solid black;
}
.test {
width: 50%;
}
<div class="container">
<div class="item">Lorem ipsum dolor sit amet.</div>
<div class="item">
<h1 class="test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi,
quo.
</h1>
</div>
</div>
But when I use px instead of %, the result that I wanted comes out. (Please view it in a full page)
* {
margin: 0;
padding: 0;
}
.container {
display: flex;
justify-content: space-between;
border: 2px solid red;
padding: 10px;
}
.item {
border: 1px solid black;
}
.test {
width: 400px;
}
<div class="container">
<div class="item">Lorem ipsum dolor sit amet.</div>
<div class="item">
<h1 class="test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi,
quo.
</h1>
</div>
</div>
I can't understand how % is calculated in the first code. Can somebody help? Thanks
You have to apply the width on the .item element.
* {
margin: 0;
padding: 0;
}
.container {
display: flex;
justify-content: space-between;
border: 2px solid red;
padding: 10px;
}
.item {
border: 1px solid black;
}
.item:nth-child(2) {
width: 50%;
}
<div class="container">
<div class="item">Lorem ipsum dolor sit amet.</div>
<div class="item">
<h1 class="test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi, quo.
</h1>
</div>
</div>
You are not applying 50% to children (.item) but rather to (.test), which is not child of display:flex. Fix it and you'll get result!
* {
margin: 0;
padding: 0;
}
.container {
display: flex;
justify-content: space-between;
border: 2px solid red;
padding: 10px;
}
.item {
border: 1px solid black;
width: 50%;
}
.test {
background: yellow;
}
<div class="container">
<div class="item">Lorem ipsum dolor sit amet.</div>
<div class="item">
<h1 class="test">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi,quo. </h1>
</div>
</div>

Vertically align parent elements to line up middle child elements

I have content blocks that each contain a title, an image, and copy. I need to vertically align the blocks so that the images line up any time they are in the same row.
The code below illustrates a width at which my layout breaks. I can't make the title or copy a fixed height without breaking the spacing between rows. I can't put the titles or copy in their own row because the title image and copy have to stay together as the blocks flow to new rows.
#import url('https://fonts.googleapis.com/css?family=Lato:400,700');
.container {
font-family: 'lato', sans-serif;
max-width: 600px;
margin: 0 auto;
display: flex;
align-items: flex-start;
flex-wrap: wrap;
}
.block {
flex-basis: 190px;
flex-grow: 1;
}
h3 {
font-size: 20px;
letter-spacing: .5px;
text-align: center;
width: 90%;
margin: 0 auto .5em auto;
}
img {
display: block;
width: 90%;
max-width: 225px;
margin: 0 auto;
}
p {
font-size: 16px;
letter-spacing: .5px;
text-align: center;
width: 85%;
/* height: 4.75em; */
margin: .25em auto 0 auto;
<div class="container">
<div class="block">
<h3>The Title One</h3>
<img src="http://via.placeholder.com/225">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing eli.</p>
</div>
<div class="block">
<h3>The Title Two</h3>
<img src="http://via.placeholder.com/225">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.</p>
</div>
<div class="block">
<h3>The Longer Title Three</h3>
<img src="http://via.placeholder.com/225">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.</p>
</div>
</div>
I think align-items: center and justify-content: center will help you.
#import url('https://fonts.googleapis.com/css?family=Lato:400,700');
.container {
font-family: 'lato', sans-serif;
max-width: 600px;
margin: auto;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
.block {
flex-basis: 190px;
flex-grow: 1;
}
h3 {
font-size: 20px;
letter-spacing: .5px;
text-align: center;
width: 90%;
margin: 0 auto .5em auto;
}
img {
display: block;
width: 90%;
max-width: 225px;
margin: 0 auto;
}
p {
font-size: 16px;
letter-spacing: .5px;
text-align: center;
width: 85%;
/* height: 4.75em; */
margin: .25em auto 0 auto;
<div class="container">
<div class="block">
<h3>The Title One</h3>
<img src="http://via.placeholder.com/225">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing eli.</p>
</div>
<div class="block">
<h3>The Title Two</h3>
<img src="http://via.placeholder.com/225">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.</p>
</div>
<div class="block">
<h3>The Longer Title Three</h3>
<img src="http://via.placeholder.com/225">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod.</p>
</div>
</div>
CSS alone can’t align the images vertically, because the individual blocks aren’t related – one title doesn’t know how tall another is. The answer above is a reasonable stab at the problem, but not very robust, as you noticed.
You could come closer if you can use CSS Grid, but then you’d lose the wrapping behaviour of the flex row.
I can’t think of anything but a JavaScript solution: figuring out which blocks are on the same row, measuring which title is the tallest, and assigning that height to the others.
Another approach could be to change the design, e.g. by moving the titles to below the images. After all, aligning these images while having titles of arbitrary lengths above them seems like a strange combination of constraints.

strange misalignment between divs container due to internal font-size elements

I'm experiencing a little problem, probably trivial, that I can't really solve. I've two div's, each containing a div with some text inside.
I noticed that changing the font height within those two inner containers, misaligns the outer ones. I know i could probably play with absolute positions, but can someone tell me why?
body {
margin: 0;
}
.outBox {
width: 300px;
height: 300px;
background: rgb(173, 247, 136);
display: inline-flex;
align-items: center;
}
.inBox {
width: 120px;
background: rgba(53, 186, 222, 1);
margin: 0 auto;
text-align: center;
padding: 5px;
}
.inBox h2 {
margin: 0;
font-size: 16px;
color: white;
}
.inBox p {
margin: 0;
font-size: 18px;
color: white;
}
<div class="outBox">
<div class="inBox">
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</h2>
</div>
</div>
<div class="outBox">
<div class="inBox">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
I'm sure someone will come along who can explain why inline-flex on an element differs to flex on the parent (I don't fully understand it), but I do know that if you take off the "display: inline-flex" on your outBox, and put them inside a container element with "display: flex" (or put that on the body), it will solve your problem.
Try wrapping the divs in a "wrapper" with it display set to "flex"
.wrapper {
display: flex;
}
body {
margin: 0;
}
.wrapper {
display: flex;
}
.outBox {
width: 300px;
height: 300px;
background: rgb(173, 247, 136);
display: inline-flex;
align-items: center;
}
.inBox {
width: 120px;
background: rgba(53, 186, 222, 1);
margin: 0 auto;
text-align: center;
padding: 5px;
}
.inBox h2 {
margin: 0px;
font-size: 16px;
color: white;
}
.inBox p {
margin: 0px;
font-size: 18px;
color: white;
}
<div class="wrapper">
<div class="outBox">
<div class="inBox">
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</h2>
</div>
</div>
<div class="outBox">
<div class="inBox">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
</div>

HTML/CSS line break when text overflows container

On my website, the Lorem Ipsum text overflows the container. How can I automatically add a line break when the text reaches its container's borders.
I have created this JSFiddle to demonstrate my issue.
HTML:
<body>
<div id="wrapper">
<div id="content">
<div class="flex">
<div class="flexchild">
<img src="img.jpg" width="200"></img>
</div>
<div class="flexchild">
<p>
Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.
</p>
</div>
</div>
</div>
</div>
</body>
CSS
body,html {
background: #fff;
width: 90%;
margin-left: auto;
margin-right: auto;
height: 100%;
}
#wrapper{
background: #ccc;
height: 100%;
}
.flex {
width: 100%;
height: 340px;
display: -webkit-box;
display: flex;
}
.flexchild {
-webkit-flex: 1 0 auto;
flex: 1 0 auto;
overflow: auto;
display: block;
}
You need to use 0 instead of auto:
.flexchild {
-webkit-flex: 1 0 0;
flex: 1 0 0;
overflow: auto;
display: block;
}
See this JSFiddle, or run the code snippet below.
body,
html {
background: #fff;
width: 90%;
margin-left: auto;
margin-right: auto;
height: 100%;
}
#wrapper {
background: #ccc;
height: 100%;
}
.flex {
width: 100%;
height: 340px;
display: -webkit-box;
display: flex;
}
.flexchild {
-webkit-flex: 1 0 0;
flex: 1 0 0;
overflow: auto;
display: block;
}
<body>
<div id="wrapper">
<div id="content">
<div class="flex">
<div class="flexchild">
<img src="http://www.engraversnetwork.com/files/placeholder.jpg" width="200"></img>
</div>
<div class="flexchild">
<p>Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet.</p>
</div>
</div>
</div>
</div>
</body>
Further Reading:
MDN Documentation for flex-basis, the third parameter of flex
I'm not familiar with the css flex bits but removing these 2 lines makes the text flow as you want.
-webkit-flex: 1 0 auto;
flex: 1 0 auto;