Expand div to text length with absolute positioned elements - html

I am trying to create some container divs in a column and which have a paragraph within them of varying length.
I'd like to expand the div to the correct height to allow for all the text however as my elements are set as positioned: absolute, I think it is causing an issue.
Please see me fiddle: http://jsfiddle.net/p7pue2kx/1/
In the first box, the text fits nicely. However when the text is much longer, it overflows and the div container does not expand accordingly. I'd like to ensure there is a min-height but the max is determined by the text size.
Are there any better ways of doing this positioning that may allow for my text to expand the container.
Thanks in advance.

Don't use absolute positioning where there is no need to use it :)
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.masterContainer {
height: 800px;
background: blue;
width: 300px;
}
.container {
position: relative;
width: 100%;
background: yellow;
min-height: 60px;
margin: 10px 0px;
padding: 10px;
}
.summary {
clear: both;
width: 100%;
overflow: hidden;
background: lightblue;
}
.id {
float: left;
background: green;
}
.xyz {
float: right;
background: red;
}
<div class="masterContainer">
<div class="container">
<div class="id">
KEY / ID
</div>
<div class="xyz">ABCD EFGH</div>
<div class="summary">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
</div>
<div class="container">
<div class="id">
KEY / ID
</div>
<div class="xyz">ABCD EFGH</div>
<div class="summary">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
<div class="container">
<div class="id">
KEY / ID
</div>
<div class="xyz">ABCD EFGH</div>
<div class="summary">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
</div>

Always avoid absolute positioning when possible. Here is one solution with minimal CSS to accomplish what you want.
CSS:
.masterContainer {
height: 800px;
background: blue;
width: 300px;
}
.container {
width: 300px;
background: yellow;
margin: 10px 0px;
}
.summary {
background: lightblue;
display: block;
}
.id {
background: green;
}
.xyz {
background: red;
float: right;
}
HTML:
<div class="container">
<span class="id">
KEY / ID
</span>
<span class="xyz">ABCD EFGH</span>
<span class="summary">
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
</span>
</div>
<div class="container">
<span class="id">
KEY / ID
</span>
<span class="xyz">ABCD EFGH</span>
<span class="summary">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</span>
</div>
<div class="container">
<span class="id">
KEY / ID
</span>
<span class="xyz">ABCD EFGH</span>
<span class="summary">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</span>
</div>
</div>
Fiddle

Related

CSS Cards: How to set the same height of the image without knowing its height

This is my goal: https://i.stack.imgur.com/HnALf.png
The images are well aligned. Despite the different heights of the images and the text, each beginning of a section is aligned with the one next to it as you can see in the image.
How can I make that with CSS?
P.S:
I can use tables to achieve this easily! But I will need to create other elements with the same images and texts for the responsive version and I do not want to do that.
I can use Javascript to give the image container a minimum height equal to the tallest height of the three images but I feel like it can be done using CSS => much easier for the responsive version. Plus this trick will not work for the "title" part because on smaller screen sizes, it will just break (for both the "px" and the "vw" units)
/*window.onload = function(){
let imgs = Array.from(document.querySelectorAll('.imgContainer img'));
let oldMinHeight = 0;
imgs.forEach(function(singleImg){
if(singleImg.clientHeight > oldMinHeight){
oldMinHeight = singleImg.clientHeight;
}
});
Array.from(document.querySelectorAll('.imgContainer')).forEach(function(singleImgContainer){
singleImgContainer.style.minHeight = (oldMinHeight) + 'px';
});
}*/
//This is the script that gives the minHeight to the image containers.
*{
font-family: sans-serif;
}
section{
display: flex;
justify-content: center;
}
.product{
margin: 0 2vw;
padding-top: 2vw;
background:#ececec;
border-radius: 5px;
}
.product .imgContainer{
display: flex;
width:15vw;
height: auto;
margin:auto;
}
.product img{
margin:auto;
width:100%;
display: block;
}
.product .desc, .product .title{
width:15vw;
margin-left: 2vw;
margin-right: 2vw;
margin-bottom: 2vw;
}
<section>
<div class="product">
<div class="imgContainer"><img src="https://images.pexels.com/photos/12641780/pexels-photo-12641780.jpeg?cs=srgb&dl=pexels-stayhereforu-12641780.jpg&fm=jpg"> </div>
<div class="title">Title Title Title Title Title Title Title Title Title Title Title</div>
<div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</div>
</div>
<div class="product">
<div class="imgContainer"><img src="https://images.pexels.com/photos/12681236/pexels-photo-12681236.jpeg?cs=srgb&dl=pexels-vildan-hanne-do%C4%9Fan-12681236.jpg&fm=jpg"></div>
<div class="title">Title</div>
<div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.</div>
</div>
<div class="product">
<div class="imgContainer"><img src="https://images.pexels.com/photos/12489081/pexels-photo-12489081.jpeg?cs=srgb&dl=pexels-josh-hild-12489081.jpg&fm=jpg"></div>
<div class="title">Title</div>
<div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam</div>
</div>
</section>
You could use the property object-fit for the image and set a fixed height for its container.
* {
font-family: sans-serif;
}
section {
display: flex;
justify-content: center;
}
.product {
margin: 0 2vw;
padding-top: 2vw;
background: #ececec;
border-radius: 5px;
}
.product .imgContainer {
display: flex;
width: 15vw;
height: 150px; /* Changed */
margin: auto;
}
.product img {
margin: auto;
width: 100%;
display: block;
object-fit: contain; /* Added */
}
.product .desc,
.product .title {
width: 15vw;
margin-left: 2vw;
margin-right: 2vw;
margin-bottom: 2vw;
}
<section>
<div class="product">
<div class="imgContainer"><img src="https://images.pexels.com/photos/12641780/pexels-photo-12641780.jpeg?cs=srgb&dl=pexels-stayhereforu-12641780.jpg&fm=jpg"> </div>
<div class="title">Title Title Title Title Title Title Title Title Title Title Title</div>
<div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</div>
</div>
<div class="product">
<div class="imgContainer"><img src="https://images.pexels.com/photos/12681236/pexels-photo-12681236.jpeg?cs=srgb&dl=pexels-vildan-hanne-do%C4%9Fan-12681236.jpg&fm=jpg"></div>
<div class="title">Title</div>
<div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.</div>
</div>
<div class="product">
<div class="imgContainer"><img src="https://images.pexels.com/photos/12489081/pexels-photo-12489081.jpeg?cs=srgb&dl=pexels-josh-hild-12489081.jpg&fm=jpg"></div>
<div class="title">Title</div>
<div class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam</div>
</div>
</section>

Flex align-self property not working inside a parent with align-items: baseline [duplicate]

This question already has answers here:
Align child elements of different blocks
(3 answers)
Closed 3 years ago.
I want to align image captions for images with different heights. If I try to align the images vertically, the captions will not be aligned, on the other hand if I align the captions, the images will not be aligned as well.
I thought if I used align-self: center on the image divs I could ignore the align-items: baseline I used on the parent div.
So far, I have tried this:
align-items: center on the parent:
Notice how the captions are not aligned, but the images are (vertically).
align-items: baseline on the parent:
Notice now how the captions are aligned, but the third image "sticks" to the bottom
align-items: baseline on the parent and align-self: center; on the images, there is no difference visually between that and my second attempt.
What I want the website to look like:
If possible, to make the third picture bigger too
.container {
overflow: hidden;
width: 85%;
margin: auto;
}
#brands {
margin-top: 50px;
margin-bottom: 50px;
display: flex;
justify-content: center;
align-items: baseline;
flex-wrap: wrap;
}
#brands .brand-item {
margin: 10px;
min-width: 200px;
max-width: 400px;
width: 30%;
text-align: center;
}
#brands .brand-item h3 {
text-align: center;
margin: 10px 0 10px 0;
}
#brands .brand-item p {
text-align: justify;
}
#brands .brand-item img {
align-self: center; /* this being ignored?*/
width: 90px;
}
<section>
<div class="container">
<div id="brands">
<div class="brand-item">
<img src="https://via.placeholder.com/400x512" />
<h3>HTML 5 Markup</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et
dolore magna aliqua.</p>
</div>
<div class="brand-item">
<img src="https://via.placeholder.com/400x512" />
<h3>CSS3 Styling</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et
dolore magna aliqua.</p>
</div>
<div class="brand-item">
<img src="https://via.placeholder.com/885x200" />
<h3>Design</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et
dolore magna aliqua.</p>
</div>
</div>
</div>
</section>
.container {
overflow: hidden;
width: 85%;
margin: auto;
}
#brands {
margin-top: 50px;
margin-bottom: 50px;
display: flex;
justify-content: center;
flex-wrap: wrap;
}
#brands .brand-item {
margin: 10px;
min-width: 200px;
max-width: 400px;
width: 30%;
text-align: center;
}
#brands .brand-item h3 {
text-align: center;
margin: 10px 0 10px 0;
}
#brands .brand-item p {
text-align: justify;
}
.img img{
width:90px;
}
.img {
display:flex;
align-items:center;
height:200px;
justify-content: space-around;
}
<section>
<div class="container">
<div id="brands">
<div class="brand-item">
<div class="img">
<img src="https://via.placeholder.com/400x512" />
</div>
<h3>HTML 5 Markup</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et
dolore magna aliqua.</p>
</div>
<div class="brand-item">
<div class="img">
<img src="https://via.placeholder.com/400x512" />
</div>
<h3>CSS3 Styling</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et
dolore magna aliqua.</p>
</div>
<div class="brand-item">
<div class="img">
<img src="https://via.placeholder.com/885x200" />
</div>
<h3>Design</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et
dolore magna aliqua.</p>
</div>
</div>
</div>
</section>
u need to wrap image inside div with display class and specific height that's how i solve it! hope this help!

Border length of child div have width same parent div?

I want to add a border-bottom between to post. But my post created by featured image and except wrap by parent div.
Like this:
.wrapper {
position: relative;
padding-bottom: 48px;
}
.thumbnail {
position: absolute;
left: 0;
top: 0;
}
.except {
margin-left: 350px;
min-height: 235px;
border-bottom: solid 1px #000;
}
<div class="wrapper">
<div class="thumbnail">
<img width="324" height="212" src="https://sharengay.com/wp-content/uploads/2018/07/banner-thu-thuat-xem-phim-netflix-324x212.jpg">
</div>
<div class="except">
<span> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>
<div class="wrapper">
<div class="thumbnail">
<img width="324" height="212" src="https://sharengay.com/wp-content/uploads/2018/07/banner-thu-thuat-xem-phim-netflix-324x212.jpg">
</div>
<div class="except">
<span> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>
By some reason, I must add border-bottom in class except but it only draw a line for this element. I want to the property border-bottom is full in element wrapper.
You can test on production website at here: https://sharengay.com
You can see currently like this:
Just wrap it in a div
.wrapper {
position: relative;
padding-bottom: 48px;
}
.thumbnail {
position: absolute;
left: 0;
top: 0;
}
.except {
margin-left: 350px;
min-height: 235px;
}
.subwrapper { /* Target */
border-bottom: 2px solid #000;
margin-bottom: 20px;
}
<div class="wrapper">
<div class="subwrapper">
<div class="thumbnail">
<img width="324" height="212" src="https://sharengay.com/wp-content/uploads/2018/07/banner-thu-thuat-xem-phim-netflix-324x212.jpg">
</div>
<div class="except">
<span> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>
<div class="wrapper">
<div class="thumbnail">
<img width="324" height="212" src="https://sharengay.com/wp-content/uploads/2018/07/banner-thu-thuat-xem-phim-netflix-324x212.jpg">
</div>
<div class="except">
<span> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>
https://stackblitz.com/edit/angular-mpunwb
Do I understand correctly : you want border-bottom along the entire length of wrapper ?
if so you need to remove margin-left: 350px;
.except {
min-height: 235px;
border-bottom: solid 1px #000;
}
Code Snippet:
.wrapper {
position: relative;
padding-bottom: 48px;
}
.thumbnail {
position: absolute;
left: 0;
top: 0;
}
.except {
min-height: 235px;
border-bottom: solid 1px #000;
}
<div class="wrapper">
<div class="thumbnail">
<img width="324" height="212" src="https://sharengay.com/wp-content/uploads/2018/07/banner-thu-thuat-xem-phim-netflix-324x212.jpg">
</div>
<div class="except">
<span> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>
<div class="wrapper">
<div class="thumbnail">
<img width="324" height="212" src="https://sharengay.com/wp-content/uploads/2018/07/banner-thu-thuat-xem-phim-netflix-324x212.jpg">
</div>
<div class="except">
<span> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</span>
</div>
</div>

Flex: Image top, text bottom. Expand remaining height with aspect ratio image.

I'm trying to create a simple design, image on top, text underneath. I'm using flexbox.
I want to keep the images the same ratio so I'm using the padding top aspect ratio hack to do so, that works well.
What I'm struggling with is getting the text box beneath the image to take up the rest of the remaining space in the container. I was hoping that flexbox would make this easy but I think using it in conjunction with the aspect ratio hack is causing issues.
Here's a fiddle of the basic layout https://codepen.io/anon/pen/NEqdyN. Can anyone help me make each of the red boxes expand to fill the remaining height? I also need to add a border radius the bottom of each corner so I can't use the overflow hidden method.
Thanks in advance!
Code below
<div class="container">
<div class="card">
<div class="image-wrapper">
<img src="https://via.placeholder.com/350x150?Text=Image">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="card">
<div class="image-wrapper">
<img src="https://via.placeholder.com/350x150?Text=Image">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="card">
<div class="image-wrapper">
<img src="https://via.placeholder.com/350x150?Text=Image">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
</div>
CSS
body {
margin: 0;
}
.container {
display: flex;
}
.card {
width: 30%;
margin: 0 1%
}
.image-wrapper {
height: 0;
padding-bottom: 56.25%;
position: relative;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
position: absolute;
top: 0;
left: 0;
}
.text {
background: red;
padding: 20px
}
It should work by defining a flex container on each card and using flex: 1 0 auto on text blocks.
.card {
display: flex;
flex-direction: column;
}
.text {
flex: 1 0 auto;
}
Demo
body {
margin: 0;
}
.container {
display: flex;
}
.card {
display: flex;
flex-direction: column;
width: 30%;
margin: 0 1%;
}
.image-wrapper {
height: 0;
padding-bottom: 56.25%;
position: relative;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
position: absolute;
top: 0;
left: 0;
}
.text {
background: red;
padding: 20px;
flex: 1 0 auto;
}
<div class="container">
<div class="card">
<div class="image-wrapper">
<img src="https://via.placeholder.com/350x150?Text=Image">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="card">
<div class="image-wrapper">
<img src="https://via.placeholder.com/350x150?Text=Image">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="card">
<div class="image-wrapper">
<img src="https://via.placeholder.com/350x150?Text=Image">
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
</div>

Prevent wrapping without nowrap

I have a div like this :
<div class="container">
<div class="wrap">
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
</div>
.container {
position: relative;
padding:20px;
background: #ccc;
float:left;
}
.text {
position: relative;
float:left;
background: lightblue;
}
.wrap {
position: absolute;
}
Is it possible to avoid the wrapping of .wrap without using whitespace:nowrap or by specifying width to any div
http://jsfiddle.net/WmcjM/71/
Remove position: relative from the .container style.