Fluid Images (using max-width="100%") with px-based Parent? - fluid

I'm trying to use fluid images for a responsive project I'm working on. However, everything I've seen has just told me to put a max-width for the images and it should work. It does work, except for px-based container widths as seen here: http://codepen.io/anon/pen/cCfsF
Is it possible for px-based parents to have fluid images?
My HTML code is:
<div class="container">
<img src="https://www.google.com/logos/doodles/2013/franz_kafkas_130th_birthday-1976005-hp.png" />
</div>
<div class="container2">
<img src="https://www.google.com/logos/doodles/2013/franz_kafkas_130th_birthday-1976005-hp.png" />
</div>
and my CSS is:
.container {
width: 500px;
background: #f30;
}
.container2 {
width: 100%;
background: #f30;
}
img {
max-width: 100%;
}

In your example, .container has a fixed width of 500px, and the child images has a width of 100%, so the image will scale to fit the parent container. However, since .container has a fixed width, it will not change as you shrink or expand the window.
As you observed, for the case of .container2 with a percentage width that will respond to the window width, the image will re-size accordingly.
You are seeing the correct behavior, so the short answer to your question is no, at least for the layout that you are looking at.

Related

Play PWA with max width

My PWA in a desktop browser is not really cool (because i don't have a lot of information to show).
And I would like to limit the width to 768px.
I tried many solutions, but i can't to change elements in position "fixed" like v-navigation, v-footer, v-dialog, ...
they are always 100% of the width of the browser.
I tried this in app.vue or in index.html:
html,body,#app {
max-width:768px !important;
overflow: hidden;
}
and last time I tried this in index.html:
#mytable {
width: 100%;
overflow: hidden;
}
#mytable td{
width:50%
}
<table id="mytable">
<tr>
<td>
<div id="app"></div>
</td>
<td>other half</td>
</tr>
</table>
But no solutions works.
Thanks for your help
Marco
Your problem seems you need different layout in function of screen size. You have several solutions from media queries to simple margin. Here I will explain how to do what you want with just margin.
I advise you to use a div to wrap your content and do not apply all your style in your #app container. Why ? Because you can imagine for instance that you want a top bar that take all the width and a content that take only 768px. If you make your layout in only one block this will be very complex. Separation in several block is a good strategy to have a modulabe UI. So we will use this template. You can make whatever you want in the .content div.
<div id="app">
<!-- Here we can imagine a top bar -->
<div class="content">
<!-- Display whatever you want -->
</div>
</div>
First you need to prepare your app container to display your application in all screen. I suggest this css :
html,body,#app {
overflow: hidden; // If content is large it will display scroll bar
height: 100vh; // Tell to your browser to take 100% of the available viewport height
}
Then you can define the css of the .content block :
.content {
max-width: 768px; // Max-width you want
height: 100%; // Take all the height available
margin: 0 auto; // Display div at the center
}
Here is an example: jsFiddle
If you are very interested in layout design, I strongly advise you to look into flex box and css grid.

Fit image into ion-card

Hi I'm trying to fit a image into a ion-card-header, but the image doesn't fit.
I already tried to remove padding, margin and use background-size: cover.
<ion-card>
<ion-card-header no-padding>
<ion-img [src]="filter.url" no-margin no-padding style="width: 100%;"></ion-img>
</ion-card-header>
<ion-card-content>
<p text-center>{{filter.name}}</p>
</ion-card-content>
</ion-card>
So I want, is that the image stretch if it's small than the ion-card-header, to fit it's size, regardless of the device display.
I found the solution!!
I use a media query to set the height that i want and I use object fit (that I didn't know that exist!).
So stayed like this:
.image--size {
width: 100%;
height: 65.66px;
object-fit: cover;
}

"Position: Contain" in AMP Carousel

When creating an AMP Carousel, how do I make sure that pictures with different proportions stay contained in the same way? In other words, the pictures will display width: 100%, but the height will be cut in relation to the proportion of the picture.
You can achieve this combining a fixed-height layout for the amp-carousel with a fill layout for the amp-img:
<amp-carousel height="300"
layout="fixed-height"
type="slides">
<div class="fixed-height-container">
<amp-img class="contain"
layout="fill"
src="https://unsplash.it/500/400"></amp-img>
</div>
The trick is to use object-fit: contain|cover for the embedded img:
.contain img {
object-fit: cover;
}
Here is a sample.

Divs overlaps when animating with Dojo toolkit

I am learning Animation with Dojo Toolkit. I am trying to animate a div. I have created two div's upper and lower div. when i create animation object for upper div, lower div is moving to the upper div space and overlapping each other. how do i prevent lower div moving to upper div place while animating upper div.
the code i am trying to solve is here.
jsfiddleDOTnet/Mostwanted_cJ/26rhq/
The problem is that, in order to make a DOM node moveable like in the animation, they give it the following inline CSS: position: absolute. Due to this, it will be "removed" from the normal flow, so your second div will not be aware of the first one, so they will overlap.
To solve this problem, you should wrap your #box1 inside a container-div which has position: relative. Your #box2 will be aware of the container and your #box1 is relatively positioned towards your container. The full HTML would be:
<div class="container">
<div id='box1'></div>
</div>
<div id='box2'></div>
And the CSS would be:
#box1 {
width: 300px;
height: 100%;
background-color: #FF0000;
}
#box2 {
width: 300px;
height: 300px;
background-color: #686868;
}
.container {
position: relative;
height: 300px;
}
As you can see I also slightly changed the height. You need to give your container a height so that #box2 will be positioned correctly. Now, because you give the container a height, you can change the height of #box1 into 100%. Because it's relative towards the container, 100% is equal to 300px.
I also updated your JSFiddle.

CSS max-width not giving proportional resizing

I have a narrow view that I am inserting some HTML into. Every once in awhile there is an image that is too wide, so I have been wrapping the original HTML string, using this CSS to keep it under control:
<style type="text/css">p{max-width:100%;}img {max-width:100%;}</style>
<div style=\"word-wrap:break-word\">
.
. ...original HTML inside the div wrapper...
.
</div>
But it doesn't scale the width and height of the images proportionally, so the result is a very distorted image. Is there something else I can do, preferably with CSS or something equally as easy?
This is embedded inside a webview in an iOS application. I don't want write code to parse through the original HTML and look for images. I need a simple solution that takes advantage of the methods that are supported by the native UIView class.
UPDATE:
Using the example in the answer below from Kyle, I think the problem is occurring with images that have embedded width and height attributes:
Kyle's example:
<div style="word-wrap:break-word">
<img src="http://www.francodacosta.com/wp-content/uploads/resize_200_150.png">
</div>
<img src="http://www.francodacosta.com/wp-content/uploads/resize_200_150.png">
Modified, with width and height added to the first image link
<div style="word-wrap:break-word">
<img src="http://www.francodacosta.com/wp-content/uploads/resize_200_150.png" WIDTH="100%" HEIGHT="100%">
</div>
<img src="http://www.francodacosta.com/wp-content/uploads/resize_200_150.png">
Both using this CSS:
div {width:100px;}
p{max-width:100%;}
img{max-width:100%;}
It really isn't making any difference whether the container width attribute is set or not. I tried it both ways.
As a matter of fact, in this case (link) it looks like the image is first scaled up by the width and height attributes, followed by the width being scaled down by the CSS max-width. This leads a a very bizarre effect.
To maintain the right proportions use:
img {
max-width: 100%;
height: auto;
}
The height: auto fixes the proportions.
It's really easy, just add a width to the parent.
div
{
width: 100px;
}
Demo here.
I am not sure if I understood right your question, but here is an idea:
http://jsfiddle.net/FyC6r/4/
.mydiv
{
width:200px;
height:150px;
border: 1px solid red;
}
.mydiv img
{
width:100%;
}
<div class="mydiv">
<img src="http://www.francodacosta.com/wp-content/uploads/resize_200_150.png">
</div>
this way you'll have a image resized proportionally dependson what you need first, the width or height.. leaving only width 100% in css it means that image will be resized to fit the div by width and height will be resized the same times as the width so the scale will be respected.
well..if want them resized by width and height then you'll have a image distortion due to different scale. you can paly with width and height of img style to change them and fit one image, but when another one is added with different sizes you'l get again a wrong displayed image. I would go for width to keep it resized, cause in the height, there is always space under the screen :)
**you should add display: inline-block and overflow: scroll to make it work **
for more examples click Here .
.demo{display: inline-block;
max-width:380px;
min-width: 220px;
min-height: 160px;
resize: both;
overflow: scroll;};
<div class="demo">i'm resizeble</div>