How to fit an image inside a table with 100% height? - html

I can't manage to get the image to fit 100% to the height of a css made table. As you see below, the table and image is totally ignoring the parent element's dimensions.
Everything needs to be dynamically that's why I work with percent.
.img {
max-width: 50%;
height: 100px; // <-- This should be able to be dynamic ( anything )
}
.img .table {
display: table;
width: 100%;
height: 100%;
}
.img .row {
display: table-row;
}
.img .cell {
display: table-cell;
}
.img .row.img-el,
.img .row.img-el .cell {
height: 100%;
}
.img img {
max-width: auto;
height: 100%;
}
<div class="img pull-left gap-right bigger">
<div class="table">
<div class="row img-el">
<div class="cell"><img src="https://1.bp.blogspot.com/-9QM7ciGXRkQ/V1hsB-wNLBI/AAAAAAAAMoA/eYbSHs00PTAjrI4QAmvYAIGCUe1AuRAnwCLcB/s1600/bryan_cranston_0095.jpg" style="height:100%;float:left" /></div>
</div>
<div class="row">
<div class="cell img-text">Hello</div>
</div>
</div>
</div>
Expected result:
Update: Is it possible to fix it with flex in any way?

Is this what you need?
.img {
max-width: 50%;
max-height: 100%;
}
.img .table {
display: table;
width: 100%;
height: 100%;
}
.img .row {
display: table-row;
}
.img .cell {
display: table-cell;
}
.img .row.img-el,
.img .row.img-el .cell {
height: 100%;
}
.img img {
max-width: auto;
height: 100%;
}
.cell img {
width: 100%;
height: auto;
}
<div class="img pull-left gap-right bigger">
<div class="table">
<div class="row img-el">
<div class="cell"><img src="https://1.bp.blogspot.com/-9QM7ciGXRkQ/V1hsB-wNLBI/AAAAAAAAMoA/eYbSHs00PTAjrI4QAmvYAIGCUe1AuRAnwCLcB/s1600/bryan_cranston_0095.jpg" style="float:left" /></div>
</div>
<div class="row">
<div class="cell img-text">Hello</div>
</div>
</div>
</div>

you have to give it a specific height like 200px it cant take a height of 100% of nothing. If you had a box with the height of 200px; you can make the image height be 100% which is 200px

Related

Scaling two different imgsto fit the screen

I'm trying to scale two images to fit a mobile screen with both keeping their original proportions without overflow. When I've tried to apply max-width it only kicks in when one image falls below screen width.
.one img {
position: absolute;
width: 200px;
height: 200px;
}
.two img {
position: absolute;
margin-left: 200px;
width: 300px;
height: 200px;
}
<div class="one"><img src="https://upload.wikimedia.org/wikipedia/commons/5/57/Distribution_H._leucocephalus.png"></div>
<div class="two"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Back_to_the_Six_Mile_Lake_eagles_%28Haliaeetus_leucocephalus%29.%22feed_me_mom%22._%2819159890706%29.jpg/800px-Back_to_the_Six_Mile_Lake_eagles_%28Haliaeetus_leucocephalus%29.%22feed_me_mom%22._%2819159890706%29.jpg"></div>
Try:
* {
margin: 0;
padding: 0;
}
.slider {
display: flex;
}
.slider .slide img {
width: 100%;
}
<div class="slider">
<div class="slide">
<img src="https://upload.wikimedia.org/wikipedia/commons/5/57/Distribution_H._leucocephalus.png" style="max-width: 200px;">
</div>
<div class="slide">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Back_to_the_Six_Mile_Lake_eagles_%28Haliaeetus_leucocephalus%29.%22feed_me_mom%22._%2819159890706%29.jpg/800px-Back_to_the_Six_Mile_Lake_eagles_%28Haliaeetus_leucocephalus%29.%22feed_me_mom%22._%2819159890706%29.jpg"
style="max-width: 300px;">
</div>
</div>
Check this:
.container {
display: flex;
}
.one img {
width: 100%;
max-width: 200px;
}
.two img {
width: 100%;
max-width: 300px;
}
<div class="container">
<div class="one"><img src="https://upload.wikimedia.org/wikipedia/commons/5/57/Distribution_H._leucocephalus.png"></div>
<div class="two"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Back_to_the_Six_Mile_Lake_eagles_%28Haliaeetus_leucocephalus%29.%22feed_me_mom%22._%2819159890706%29.jpg/800px-Back_to_the_Six_Mile_Lake_eagles_%28Haliaeetus_leucocephalus%29.%22feed_me_mom%22._%2819159890706%29.jpg"></div>
</div>
.one, .two {
width:50%; float:left
}
.one img, .two img {
width: 100%;
}
<div class="one"><img src="https://upload.wikimedia.org/wikipedia/commons/5/57/Distribution_H._leucocephalus.png"></div>
<div class="two"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Back_to_the_Six_Mile_Lake_eagles_%28Haliaeetus_leucocephalus%29.%22feed_me_mom%22._%2819159890706%29.jpg/800px-Back_to_the_Six_Mile_Lake_eagles_%28Haliaeetus_leucocephalus%29.%22feed_me_mom%22._%2819159890706%29.jpg"></div>
Can you please check this and let me know, if this is the same you are looking for.

How to achieve fixed width spacing between multiple images centred horizontally and vertically

This codepen will make clear what I am trying to do:
HTML:
<div class="slider-container">
<div class="table">
<div class="row">
<div class="cell">
<img src="https://images.robertharding.com/preview/RM/RH/VERTICAL/983-675.jpg">
</div>
<div class="padding-cell"></div>
<div class="cell">
<img src="https://images.robertharding.com/preview/RM/RH/VERTICAL/983-675.jpg">
</div>
<div class="padding-cell"></div>
<div class="cell">
<img src="https://images.robertharding.com/preview/RM/RH/VERTICAL/983-675.jpg">
</div>
</div>
</div>
</div>
CSS:
.slider-container {
left: 4.5em;
right: 4.5em;
position: fixed;
top: 4.5em;
height: calc(100vh - 9em);
}
.slider-container .table {
display: table;
height: inherit;
width: 100%;
}
.slider-container .table .row {
display: table-row;
width: 100%;
height: inherit;
vertical-align: middle;
text-align: center;
}
.slider-container .table .row .padding-cell {
display: table-cell;
height: inherit;
width: 1.5em;
}
.slider-container .table .row .cell {
display: table-cell;
height: inherit;
vertical-align: middle;
}
.slider-container .table .row .cell img {
max-height: 100%;
max-width: 100%;
}
https://codepen.io/simonhrogers/pen/WJJVgL
When the aspect ratio of the image is particularly tall, or the screen particularly squashed, the gaps between each image will begin to stretch much wider than the width of the specified padding cell between them. How can I prevent this behaviour so that the images are always separated by just the width of the padding cell?
I still need the images to shrink if too close to the top/bottom or sides of the screen; as I have here, but with a fixed-width gap between them instead of that changing too.
Desired:
Undesired:
You can get the desired behavior by doing some minor changes in your code:
First set width:100% for your image.
Second set a fixed padding in your class padding-cell in unit px or rem
.slider-container {
left: 4.5em;
right: 4.5em;
position: fixed;
top: 4.5em;
height: calc(100vh - 9em);
}
.slider-container .table {
display: table;
height: inherit;
width: 100%;
}
.slider-container .table .row {
display: table-row;
width: 100%;
height: inherit;
vertical-align: middle;
text-align: center;
}
.slider-container .table .row .padding-cell {
display: table-cell;
height: inherit;
width: 20px;
}
.slider-container .table .row .cell {
display: table-cell;
height: inherit;
vertical-align: middle;
}
.slider-container .table .row .cell img {
max-height: 100%;
max-width: 100%;
width: 100%;
}
<div class="slider-container">
<div class="table">
<div class="row">
<div class="cell">
<img src="https://images.robertharding.com/preview/RM/RH/VERTICAL/983-675.jpg">
</div>
<div class="padding-cell"></div>
<div class="cell">
<img src="https://images.robertharding.com/preview/RM/RH/VERTICAL/983-675.jpg">
</div>
<div class="padding-cell"></div>
<div class="cell">
<img src="https://images.robertharding.com/preview/RM/RH/VERTICAL/983-675.jpg">
</div>
</div>
</div>
</div>
Although you can achieve the layout more easily with flexbox.

css let element width auto grow while parent width auto

I have a html structure like this and some basic style
.container {
display: block;
width: auto;
/* this is must */
height: auto;
/* this is must */
max-width: 300px;
}
.container .row {
display: flex;
width: 100%;
height: auto;
}
.container .row .left {
display: inline-block;
width: 100px;
height: auto;
}
.container .row .right {
display: inline-block;
width: auto;
height: auto;
}
<div class="container">
<div class="row">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="row">
<div class="left"></div>
<div class="right"></div>
</div>
<div class="row">
<div class="left"></div>
<div class="right"></div>
</div>
</div>
The style may not working, I only pasted the basic part of it.
What I want to achieve is, the parent element has a max-width, it contains multiple rows, each row has two elements, 'left' and 'right'. I give a fixed width to 'left' element, and a min-width/max-width to 'right' element. I would like the width of the right element auto grow as the content grow until the max-width, but if the content is short, the right element shall also shrink.
I tried table and flex box, but no luck. Thanks for any help
The problem is because you have to specify the max-width to the .right class.
1) The overall container's max-width:300px and left one's width: is 100px, so the remaining 200px; you can give it to right row.
2) Have added background for better understanding.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<style>
.container {
display: block;
width: auto; /* this is must */
height: auto; /* this is must */
max-width: 300px;
}
.container .row{
display: flex;
width: 100%;
height: auto;
background-color:Red;
}
.container .row .left{
display: inline-block;
width: 100px;
background-color:yellow;
}
.container .row .right{
display: inline-block;
height: auto;
background-color:green;
max-width: 200px;
overflow:hidden;
}
</style>
<body>
<div class="container">
<div class="row">
<div class="left">qweqweqwe</div>
<div class="right">qweqweqwew<p>afdllssssssssdddddddddssssssssss</p><p>asdasdasdadsas</p></div>
</div>
<div class="row">
<div class="left">qweqweqwe</div>
<div class="right">qweqweqwe</div>
</div>
<div class="row">
<div class="left">qweqweqw</div>
<div class="right">qweqweqweqwe</div>
</div>
</div>
</body>
</html>
Find the codepen sample here and check it with your requirement. Also, let me know if there is something that you want to get from.
.container .row{
display: flex;
width: 100%;
height: auto;
}
.container .row .left{
display: inline-block;
width: 100px;
height: auto;
}
.container .row .right{
display: inline-block;
width: auto;
height: auto;
max-width: 150px;
overflow: hidden;
}
Add display: flex to your .row class and set min-width: 100px to .right - see a working jsfiddle here: https://jsfiddle.net/o3o9j4za/1/
<div class="container">
<div class="row">
<div class="left">1234</div>
<div class="right">4312</div>
</div>
<div class="row">
<div class="left">1235</div>
<div class="right">qweqwereqwr</div>
</div>
<div class="row">
<div class="left">5342</div>
<div class="right">3g43g3g3g</div>
</div>
.container {
display: block;
width: auto; /* this is must */
height: auto; /* this is must */
max-width: 300px;
}
.container .row{
width: 100%;
height: auto;
display:flex;
}
.container .row .left{
display: inline-block;
height: auto;
width: 100px;
background-color:#ff0;
}
.container .row .right{
display: inline-block;
width: auto;
min-width: 100px;
height: auto;
background-color:#f0f;
}
Note, that your flex container (.row) is affected by the max-width of .container.
(Edit: misread a bit - should be like you want it now?)
How about this flexbox solution? I think it comes closest to what you are looking for:
<div class="container">
<div class="row">
<div class="left">test left</div>
<div class="right">test right</div>
</div>
<div class="row">
<div class="left">test left</div>
<div class="right">test right</div>
</div>
<div class="row">
<div class="left">test left</div>
<div class="right">test right</div>
</div>
</div>
.container {
padding: 2px;
border: 1px solid green;
max-width: 300px;
}
.row {
display: flex;
padding: 2px;
border: 1px solid red;
max-width: 300px;
}
.left {
padding: 2px;
border: 1px solid blue;
width: 100px;
}
.right {
padding: 2px;
border: 1px solid purple;
width: 180px;
}
And the fiddle:
https://jsfiddle.net/xr7ebmsz/
I guess you can try this one for your class "right"
.right{
width:calc(100% - 100px);
}

How to keep three div images on the same line?

I am trying to create three separate rounded images on the same line. I managed to get two in the correct position but I can't get the last one to move up into the correct line.
.wrap {
width: 100%;
}
.image-left {
content: url(https://s16.postimg.org/qm1wc2syd/alexandru_stavrica_166342.png);
height: 250px;
float: left;
padding-left: 10%;
}
.image-centre {
content: url(https://s23.postimg.org/57nxodezv/jorg_angeli_128760.png);
height: 250px;
margin-left: auto;
margin-right: auto;
}
.image-right {
content: url(https://s3.postimg.org/ejuuxd6n7/jay_wennington_2250_min.png);
height: 250px;
float: right;
padding-right: 10%;
}
<div class="wrap">
<div class="image-left"></div>
<div class="image-centre"></div>
<div class="image-right"></div>
</div>
There's probably a better way to do this, but here's one that works: https://jsfiddle.net/5ybLh6vy/
<div class="wrap">
<div class="image-left">
<img src="https://s16.postimg.org/qm1wc2syd/alexandru_stavrica_166342.png">
</div>
<div class="image-centre">
<img src="https://s23.postimg.org/57nxodezv/jorg_angeli_128760.png">
</div>
<div class="image-right">
<img src="https://s3.postimg.org/ejuuxd6n7/jay_wennington_2250_min.png">
</div>
</div>
.wrap {
width: 100%;
display: table;
}
.wrap img {
box-sizing: border-box;
width: 100%;
padding: 5px;
}
.image-left, .image-centre, .image-right {
display: table-cell;
width: 33%;
}
How about using the image tag and wrapping them around a div like this?
.wrap {
width: 100%;
}
.image-wrapper{
width: 33%;
display: inline-block;
text-align: center;
}
.image-wrapper>img{
height:250px;
}
<div class="wrap">
<div class="image-wrapper">
<img src='https://s16.postimg.org/qm1wc2syd/alexandru_stavrica_166342.png'>
</div>
<div class="image-wrapper">
<img src='https://s23.postimg.org/57nxodezv/jorg_angeli_128760.png'>
</div>
<div class="image-wrapper">
<img src='https://s3.postimg.org/ejuuxd6n7/jay_wennington_2250_min.png'>
</div>
</div>
Float all three of the divs right, make them width: 33.33% and box-sizing: border-box.
This will make three evenly spaced images floated inline.
If you want them all in a neat row you'll have to add float:left; to all of them and or to the .wrap class but you would have to add display:inline; to each image which I think is the best solution. Problem is if the the viewport isn't wide enough it will push to the next line.
.wrap {
width: 100%;
float: left;
}
.image-left {
content:url(https://s16.postimg.org/qm1wc2syd/alexandru_stavrica_166342.png);
height: auto;
max-width: 25%;
padding-left: 10%;
display:inline;
}
.image-centre {
content: url(https://s23.postimg.org/57nxodezv/jorg_angeli_128760.png);
max-width: 25%;
height:auto;
display:inline;
}
.image-right {
content:url(https://s3.postimg.org/ejuuxd6n7/jay_wennington_2250_min.png);
height: auto;
max-width: 25%;
display:inline;
padding-right: 10%;
}
<div class="wrap">
<div class="image-left"></div>
<div class="image-centre"></div>
<div class="image-right"></div>
</div>
You could assign float: left; for all of your images, and then set correct margins.

adding a heading to a section that uses display:table and display:table-cell

I'm having a bit of difficulty in displaying a table. I use display:table and display:table-cell a lot for sections usually. Especially when I just want to center the content of a section vertically. So to say, I have the following HTML:
<div class="wrapper">
<div class="cell">
<div class="red">
</div>
</div>
</div>
and the following css applied to the html:
html,body {
height: 100%;
}
.wrapper {
display: table;
width: 100%;
height: 100%;
}
.wrapper * {
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
}
.red {
margin-right: auto;
margin-left: auto;
background: red;
height: 200px;
width: 200px;
}
Now there is a small problem. I want to add a section header to this particular section and the section header has to be a child of .wrapper, so the HTML changes as below :
<div class="wrapper">
<div class="section-heading">
<h1>section heading</h1>
</div>
<div class="cell">
<div class="red">
</div>
</div>
</div>
Now the problem with using display table and table-cell is that when I add a header to the section, I can't add it without it affecting the other child elements of .wrapper . So how do I add a heading (when the heading is added in the above HTML the .cell div seems to be moving horizontally slightly)?
Of course I could use absolute positioning, but I was just wondering, is there something that can be done, without taking the heading element out of the flow?
FIDDLE HERE
Did you try adding display:table-row to the section-heading?
.wrapper > .section-heading{
display:table-row;
height:auto;
}
Fiddle: http://jsfiddle.net/7xo353hg/4/
You can make heading container display: table-row:
.section-heading {
display: table-row;
text-align: center;
}
Check the demo:
html, body {
height: 100%;
}
.wrapper {
display: table;
width: 100%;
height: 100%;
}
.wrapper * {
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
}
.red {
margin-right: auto;
margin-left: auto;
background: red;
height: 200px;
width: 200px;
}
.section-heading {
display: table-row;
text-align: center;
}
<div class="wrapper">
<div class="section-heading">
<h1>section heading</h1>
</div>
<div class="cell">
<div class="red"></div>
</div>
</div>