Image container taking more space than it should - html

How can I remove the empty space from the image container (blue)?
I need it to be compatible with ie11.
I don't want to hardcode the width/height of the container because images can be different aspect ratios and sizes.
main {
width: 100%;
height: 100%;
}
.video {
width: 100%;
display: flex;
max-height: 5em;
border: 1px solid red;
}
.video .image {
background: blue;
max-height: 100%;
max-width: 100%;
}
.video .image img {
max-width: 20vw;
max-height: 100%;
}
<main>
<div class="video">
<div class="image">
<img src="https://placehold.it/300/200">
</div>
<div class="content">
<p>title</p>
<p>description</p>
</div>
</div>
</main>

I find a strange thing: If we download the image and load it from the same server as the test page like this <img src="picture/200.png">, the following CSS code works well in IE:
main {
width: 100%;
height: 100%;
}
.video {
width: 100%;
display: flex;
height: 5em;
border: 1px solid red;
}
.video .image {
height: 100%;
}
.video .image img {
height: 100%;
}
I serve the image and the test page through localhost, you can see the result:
If you still want to use the online link as image source, you can try the following code without flexbox which can work well in IE:
main {
width: 100%;
height: 100%;
}
.video {
width: 100%;
height: 5em;
border: 1px solid red;
}
.video img {
overflow: hidden;
height: 100%;
float: left;
}
<main>
<div class="video">
<img src="https://placehold.it/300/200">
<div class="content">
<p>title</p>
<p>description</p>
</div>
</div>
</main>

Related

How to completely fill a div without a set height with an image

I have this JSX
<div style={{ display: 'flex' }}>
<div className='outer-div'>
<div className='first-div'>
a <br />
b <br />c
</div>
<div className='second-div'>
<img
src='https://upload.wikimedia.org/wikipedia/commons/0/0f/Eiffel_Tower_Vertical.JPG'
alt=''
/>
</div>
</div>
<div className='outer-div'>
<div className='first-div'>
a <br />
b <br />c
</div>
<div className='second-div'></div>
</div>
</div>
and this CSS
.outer-div {
width: 300px;
height: 500px;
background-color: blue;
display: flex;
flex-direction: column;
}
.first-div {
background-color: red;
}
.second-div {
height: 100%;
background-color: green;
}
.second-div > img {
height: 100%;
width: 100%;
}
I wanted the image to fit the div completely, but instead it is like this:
I noticed that if the screen is thinner, the problem doesn't happen, like here:
How can I make it so the image completely fills the second-div for any screen size, without having a set height?
You can make the image relative to the div.
.second-div {
position: relative;
height: 100%;
background-color: green;
}
.second-div > img {
position: absolute;
max-height: 100%;
max-width: 100%;
}
Found the solution: add overflow: hidden; to .second-div like this
.second-div {
height: 100%;
background-color: green;
overflow: hidden;
}
It works, but I'm not sure why. Would appreciate if anyone could explain it.

how i can change square size in CSS with the same class but different div?

How can I change image size of second div in CSS changing only square? with the same class but different div?
<div class="square"> <img class="square__img" src="square.png"> </div>
<div class="square square--small"> <img class="square__image" src="square.png"> </div>
Try this
.square {
width: 300px; /*standard image size*/
}
.square.square--small {
width: 150px; /*small image size*/
}
.square.square--small .square__image { /*apply change only to small img*/
width: 100%;
height: auto;
}
hello you can use this css selector:
.square img
{
width:100px;
height:100px
}
.square.square--small img{
width:50px;
height:50px
}
Make image to not overlap the div by setting its max-width to 100%, max-height to 100%
and height to auto like this:
max-width: 100%;
max-height: 100%;
height: auto;
then style your division elements like this for example:
.square {
width: 300px;
height: 300px;
margin: 30px;
}
.square.square--small {
width: 200px;
height: 300px;
}
Again, width and height are only for demonstration, modify as you like.
img of working code
<div class="square">
<img class="square__img" src="square.png" alt="img1">
</div>
<div class="square square--small">
<img alt="img2" class="square__image" src="square.png">
</div>
.square {
border: 1px solid red;
height: 200px;
width: 200px;
margin-bottom: 20px;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
float: left;
margin-right: 20px;
}
.square img {
width: 100px;
border: 1px solid green;
display: inline-block;
height: 100px;
}
.square--small {
border: 1px solid black;
}
.square--small img {
border: 1px solid red;
height: 150px;
width: 150px;
}

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 place cover div onto object fit images in responsive flex container?

Here is my demo to show what I mean:
* {
box-sizing: border-box;
}
body, html {
height: 100%;
width: 100%;
}
body {
background: #333;
margin: 0;
padding: 0;
}
.content {
display: flex;
height: 100%;
padding: 20px;
}
.panel {
flex: 0 0 200px;
display: flex;
margin-left: 20px;
}
.widget {
background: white;
width: 100%;
height: 100%;
}
.videoContainer {
display: flex;
flex: 1 1 100%;
flex-wrap: wrap;
}
.video {
height: 50%;
width: 50%;
padding: 2px;
position: relative;
}
.videoCover {
position: absolute;
background: red;
opacity: 0.4;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
img {
width: 100%;
height: 100%;
object-fit: contain; /* Default for videos */
}
<div class="content">
<div class="videoContainer">
<div class="video">
<div class="videoCover"></div>
<img width="600" height="400" src="http://thatgrapejuice.net/wp-content/uploads/2016/03/rihanna-thatgrapejuice-mariah-carey-billboard-600x400.jpg">
</div>
<div class="video">
<img width="600" height="400" src="http://www.radioandmusic.com/sites/www.radioandmusic.com/files/images/entertainment/2015/09/28/rihanna-%2812%29.jpg">
</div>
<div class="video">
<img width="600" height="400" src="http://m.buro247.com.au/thumb/600x960_0/images/640-rihanna-charity1.jpg">
</div>
<div class="video">
<img width="600" height="400" src="http://hjb.hu/wp-content/uploads/2014/02/rihanna2.jpg">
</div>
</div>
<div class="panel">
<div class="widget"></div>
</div>
</div>
Resizing the browser vertically or horizontally, the problem can be seen clearly. So basically I would like to place the red cover exactly onto the images. So I assume I need a div, which is exactly the same size as the image.
It seems object fit does its job well, but because of this, I cannot place over the red div.
Is it can be done pure css? How should I modify the dom and the css?
Thank you very much!
Clarifying what I would like to achive is:
The best I can come up with is this, where I wrapped all in a cover and then used a pseudo for the red cover.
I also added a few media queries, as one need to control the width of the video's, so they don't become wider than their ratio height, and if, one make them less wide.
You might need to elaborate with these settings, maybe one or two more queries will be needed, but I think one can do this pretty good, and be able to avoid the need of script.
And by cutting out object-fit, you also get a better cross browser solution.
As a side note, here is an answer I participated in, which shows what it takes to achieve what you want: Scale element proportional to Background Cover/Contain. It has a script and a CSS version
* {
box-sizing: border-box;
}
body, html {
height: 100%;
width: 100%;
}
body {
background: #333;
margin: 0;
padding: 0;
}
.content {
display: flex;
height: 100%;
padding: 20px;
}
.panel {
flex: 0 0 200px;
display: flex;
margin-left: 20px;
}
.widget {
background: white;
width: 100%;
height: 100%;
}
.videoContainer {
display: flex;
flex: 1 1 100%;
flex-wrap: wrap;
align-items: center;
}
.video {
height: 50%;
width: 50%;
padding: 2px;
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: center;
}
.videoCover {
position: relative;
overflow: hidden;
}
.video:nth-child(1) .videoCover::after {
content: '';
position: absolute;
background: red;
opacity: 0.4;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
img {
display: block;
width: 100%;
height: auto;
}
#media (min-aspect-ratio: 600/400) {
.video:nth-child(1) .videoCover::after {
width: 80%;
}
img {
width: 80%;
}
}
#media (min-aspect-ratio: 800/400) {
.video:nth-child(1) .videoCover::after {
width: 60%;
}
img {
width: 60%;
}
}
#media (min-aspect-ratio: 1000/400) {
.video:nth-child(1) .videoCover::after {
width: 40%;
}
img {
width: 40%;
}
}
<div class="content">
<div class="videoContainer">
<div class="video">
<div class="videoCover">
<img width="600" height="400" src="http://thatgrapejuice.net/wp-content/uploads/2016/03/rihanna-thatgrapejuice-mariah-carey-billboard-600x400.jpg">
</div>
</div>
<div class="video">
<div class="videoCover">
<img width="600" height="400" src="http://www.radioandmusic.com/sites/www.radioandmusic.com/files/images/entertainment/2015/09/28/rihanna-%2812%29.jpg">
</div>
</div>
<div class="video">
<div class="videoCover">
<img width="600" height="400" src="http://m.buro247.com.au/thumb/600x960_0/images/640-rihanna-charity1.jpg">
</div>
</div>
<div class="video">
<div class="videoCover">
<img width="600" height="400" src="http://hjb.hu/wp-content/uploads/2014/02/rihanna2.jpg">
</div>
</div>
</div>
<div class="panel">
<div class="widget"></div>
</div>
</div>

Setting 100% Height on an Absolutely Positioned Column

I have a design using some bootstrap styling which has a white column on the right. The height should be 100%, but it isn't rendering at 100%. It renders at 100% of the initial screen height, but when you scroll down it's no longer white.
I've looked at several other CSS solutions on this site. I've tried making all parent elements 100% height. I've tried making it a flexbox column. I've tried putting "position: relative;" in the body. Nothing has worked yet. I'd prefer not to use JS to achieve this.
Simplified version of my HTML:
<body>
<div class="container">
<div class="main">
<h1>This is the main content area</h1>
</div>
<div class="right pull-right">
<div class="content">
</div>
<div class="content">
</div>
<div class="content">
</div>
<div class="content">
</div>
</div>
</div>
</body>
The CSS:
body,html {
height: 100%;
background-color: #aaa;
}
body {
position: relative;
}
.main {
display: inline-block;
}
.container {
height: 100%;
}
.right {
display: inline-flex;
flex-direction: column;
background-color: #fff;
width: 350px;
height: 100%;
min-height: 100%;
border: 1px solid #E1E6E9;
margin-right: 100px;
position: absolute;
right: 100px;
}
.content {
height: 300px;
min-height: 300px;
margin: 10px 10px;
background-color: #ccc;
border: 1px solid #000;
}
Change your .right class to have height: auto;
It will size itself to fit with its content.
.right {
display: inline-flex;
flex-direction: column;
background-color: #fff;
width: 350px;
height: auto;
min-height: 100%;
border: 1px solid #E1E6E9;
margin-right: 100px;
position: absolute;
right: 100px;
}
http://codepen.io/smlariviere/pen/WrWgxQ