Full width image inside a narrow parent - css

I'm trying to make a responsive, full width image work inside a narrow parent. So far, I can't clear these elements.
Javascript is ok, but fussing with the HTML isn't since it should work in a WordPress theme.
HTML:
<p>Visible content.</p>
<div class="feat-img">
<a href="#">
<img src="http://f.cl.ly/items/1e1515393T2l0D3I2503/feat-img.jpg"/>
</a>
</div>
<p>Hidden content :( </p>
</article>
CSS:
.feat-img img{
position: absolute;
width: 100% !important;
min-width: 400px;
min-height: auto;
height: auto;}
.feat-img img:empty{
left: 50%;
-webkit-transform: translate(-50%,0);
-moz-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
-o-transform: translate(-50%, 0);
transform: translate(-50%, 0);}
article{
width: 50%;
margin: auto;
background:#ccc;}
Live: http://jsfiddle.net/wzvLa/4/

I think it can not be don only with css, because when you set position: absolute to img it's parent no longer contain it. You can write a little javascript code to do that:
$('.feat-img').css({ height: $('.feat-img img').height() });
This way you set the height of .feat-im to be the same as the image in it. Don't forget to do it on $(window).resize() too, so it can be responsive.
Here is what I do: jsfiddle

Here's one way of doing it:
html,body { margin: 0; }
.feat-img img {
position: relative;
width: 133.33%; /* (100% divided by article width) */
min-width: 400px;
height: auto;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
}
article {
width: 75%;
margin: auto;
background: #ccc;
}
(jsfiddle demo)

Related

How to Make an Image Fill the Container Height and Retail Aspect Ratio with Bootstrap 4

How can an image fill the parent container height, and retain the aspect ratio?
I have used a similar pattern before (see below), but I believe that Bootstrap 4 may be interfering here. When viewing the image in a mobile view port, the image is stretching vertically.
div#parent { position: relative; }
h1 { color: #FFF }
img#child {
position: absolute;
min-width: 100%;
min-height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: -100;
}
<div id="parent">
<img id="child"
src="https://cdn.pixabay.com/photo/2016/07/08/08/59/background-1503863_1280.png" alt="Background" />
<h1>Very very very very very very very very very very very long heading
to go over image in mobile view. The image should not stretch
vertically.</h1>
</div>
Add background-size: contain; to your CSS
img#child {
position: absolute;
min-width: 100%;
min-height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: -100;
background-size: contain;
}
Output:
For more info: https://www.w3schools.com/csSref/css3_pr_background-size.asp

Fit image on p-galleria primeNg for Angular2 application

I used p_galleria of PrimeNg and set this attributes :
<p-galleria [images]="productImages"
panelWidth="560"
panelHeight="313"
autoPlay="false"
showFilmstrip="false"
id="product-galley"
showCaption="false">
</p-galleria>
also I added an style for rendered image panel :
.ui-panel-images {
/*height: inherit !important;
width: inherit !important;*/
/*max-height: inherit !important;
height: initial;
max-width: inherit !important;
width: initial;*/
max-width: 100%;
max-height: 100%;
width:auto;
height:auto;
}
But the image is always stretched in container, I want it to be fixed in scale. and be in center of panel.
Is there any idea how to change the style?
Maybe its not relevant, but I wrapped this gallery in a
bootstrap-modal.
Using primeng v6.0.0, I added this to my CSS to make the image resize itself, keeping the aspect ratio, to match the dimensions of the p-galleria container.
.ui-panel-images {
width : 100%;
height : 100%;
object-fit : contain;
}
<div class="col-md-5">
<p-galleria [images]="imagesGaleria"
styleClass="completa"
[showFilmstrip]="false"
[showCaption]="false"
effectDuration=2000></p-galleria>
And my css, there are some properties that you can remove
.completa {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: -100;
-ms-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
background-size: cover;}
.ui-panel-images{
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
/*z-index: -100;*/
-ms-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
/*background-size: cover;*/}
Neither of the previous solutions worked for me. My solution for a responsive galleria panel was to size a responsive container element and fit the panel attributes to the container:
<div #container>
<p-galleria [panelWidth]="container.offsetWidth"
[panelHeight]="container.offsetHeight"
[images]="images"></p-galleria>
</div>
What was especially helpful in my case, was to set my container to 100% width, and calculate an aspect ratio to match my images. Note, you don't need to set a container height in this method. Optionally, you could add more height if you're showing a filmstrip:
<div #container>
<p-galleria [panelWidth]="container.offsetWidth"
[panelHeight]="container.offsetWidth * (5/16)"
[images]="images" [showFilmstrip]="false"></p-galleria>
</div>
For the object-fit to work, I had to override the view encapsulation like so:
:host ::ng-deep img.ui-panel-images {
object-fit: contain !important;
height: inherit;
width: inherit;
}
Refering to near perfect answer of user sawprogramming. The code should be wrapped in
:host {
::ng-deep {
.ui-panel-images {
width: 100%;
height: 100%;
object-fit: contain;
}
}
}
I tried this and it worked fine.
` .ui-panel-images {
width: auto;
height: inherit;
object-fit: contain;
position: relative;
}
`

CSS 3 rotateY() and rotateX() not working as expected

I am trying out the new features in css3 while i found that rotateY() and rotateX() is not giving expected results.
I have a single div in the page
<div id="element"></div>
This is the css
#element{
position: relative;
margin: 0 auto;
width: 200px;
height: 200px;
top: 300px;
background-color: yellow;
transform: rotateY(45deg);
}
The blue shape is what i want and yellow is what i get
You need to add a container and give it perspevtive: 500px to get a 3D looking effect.
#container {
-webkit-perspective: 500px;
perspective: 500px;
}
#element {
position: relative;
margin: 0 auto;
width: 200px;
height: 200px;
top: 10px;
background-color: yellow;
-webkit-transform: rotateY(45deg);
transform: rotateY(45deg);
}
<div id="container">
<div id="element"></div>
</div>
You may want to complete your transform and perspective style rule:
jsfiddle demo
body{
-webkit-perspective:200px;
-moz-perspective:200px;
perspective:200px;
-webkit-perspective-origin:center 400px /* 300px + 200px/2 */;
-moz-perspective-origin:center 400px /* 300px + 200px/2 */;
perspective-origin:center 400px /* 300px + 200px/2 */;
-webkit-transform-style:preserve-3d;
-moz-transform-style:preserve-3d;
transform-style:preserve-3d;
}
#element{
position: relative;
margin: 0 auto;
width: 200px;
height: 200px;
top: 300px;
background-color: yellow;
-webkit-transform: rotateY(45deg);
-moz-transform: rotateY(45deg);
transform: rotateY(45deg);
-webkit-transform-origin:center;
-moz-transform-origin:center;
transform-origin:center;
}
<div id="element"></div>
The parent of #element (not necessary <body>) has to have:
perspective so your browser knows how "far" the viewport is from #element, and render the rotation effect accordingly;
perspective-origin so your browser knows where the "center" of your viewport is;
The transform-style:preserve-3d do not seem to be necessary in this specific case, and IE doesn't support this feature yet. I just added it out of habit.

Using CSS to auto resize & center popup

I'm trying to have a popup that auto sizes to fit content based on screen resolution, while also remaining in the dead center of the screen both horizontally & vertically.
This is where I'm at so far:
.reveal-modal {
background: none no-repeat scroll 0 0 #eee;
border-radius: 5px;
width: 50%;
height: 50%;
left: 25%;
top: 25%;
padding: 2%;
position: absolute;
visibility: hidden;
z-index: 101;
}
.reveal-child {
display: block;
position: relative;
width: 100%;
height: 100%;
}
The effect is close, but still doesn't work in certain resolutions. In some places the container is too large while others it is too small. I'd ideally like the container to be only as big as the content requires.
The demo can be viewed on 104.131.228.107 and clicking the Register button
I dont understand you, but if u want something like this site you linked, than watch here how to do popup.
u can use jQuery for that.
$('.button').click(function(){
$(".content").animate({opacity:"0.3"},500,function(){
$(".popup").fadeIn();
});
});
$(".popup").click(function(){
$(".popup").fadeOut(function(){
$(".content").animate({opacity:"1"},500);
});
});
This may be what you're looking for?
I made a fiddle where I made the width static so there is no unnecessary whitespace in the modal.
This one scales to the 50% width and height just as you had it and stops at the minimum for your content using the "min-width" and "min-height" properties.
If you want to create different styles for different screen resolutions use media queries
Hope it helped
#myModal {
position: absolute;
width: 50%;
height: 50%;
min-width:500px;
min-height:230px;
left: 50%;
top:50%;
border-radius: 5px;
background-color: #fff;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}

html -split a page into desired shapes as divs?

I'm trying to split a page into different shapes, as shown in this image:
The problem is I'm trying to create divs as the shapes in the image so I can put content in them and by changing the css styles change their colors and give them effects with JavaScript,
Searching the net I did come across some sites like CSS Tricks to create CSS Triangles, but that's not exactly what I want because I cant put content in such a div and cant get exactly the shapes I need, I was thinking maybe I could get such results with the element, but i don't really know if its logical to use instead of and can get the effect I want?
is there a way to divide an Html page into any desired shape?
hmm, you can use css3 transformations (rotation):
HTML:
<div class="shape1">
<div class="shape1-content"> ... </div>
</div>
CSS :
.shape1 {
-webkit-transform: rotate(45deg);
}
.shape1-content {
-webkit-transform: rotate(-45deg);
}
Of course, you shoud apply other styles (position: absolute, and others).
UPDATE:
copy'n'paste this code to see live example:
<html>
<head>
<style>
.wrapper {
border: 1px solid #ff8888;
height: 480px;
left: 50%;
margin: -240px 0 0 -320px;
overflow: hidden;
position: absolute;
top: 50%;
width: 640px;
}
.shape1 {
-webkit-transform: rotate(15deg);
-moz-transform: rotate(15deg);
background-color: #fff;
border: 1px solid black;
height: 50%;
left: -25%;
position: absolute;
top: 70%;
width: 150%;
}
.shape1-content {
-webkit-transform: rotate(-15deg);
-moz-transform: rotate(-15deg);
padding-left: 230px;
}
.shape2 {
-webkit-transform: rotate(15deg);
-moz-transform: rotate(15deg);
background-color: #fff;
border: 1px solid #88ff88;
bottom: 244px;
height: 100%;
position: absolute;
right: 50%;
width: 100%;
}
.shape2-content {
-webkit-transform: rotate(-15deg);
-moz-transform: rotate(-15deg);
bottom: 10px;
position: absolute;
right: 10px;
}
.shape3 {
-webkit-transform: rotate(30deg);
-moz-transform: rotate(30deg);
border: 1px solid #8888ff;
bottom: 40%;
height: 100%;
position: absolute;
right: 20%;
width: 100%;
}
.shape3-content {
-webkit-transform: rotate(-30deg);
-moz-transform: rotate(-30deg);
bottom: 50%;
position: absolute;
right: 10px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="shape3">
<div class="shape3-content">Hi there!</div>
</div>
<div class="shape1">
<div class="shape1-content">Hi there!</div>
</div>
<div class="shape2">
<div class="shape2-content">Hi there!</div>
</div>
</div>
</body>
</html>
In general you can't do that with CSS until the CSS Shapes and Exclusions stuff mentioned here gets added to browsers in a few years http://corlan.org/2012/03/16/css-bleeding-edge-features/
For now basic CSS3 will allow you to create shapes and rotate them, but not with much precision. Your best bet may be to use to use SVG.
Here's an example of using SVG to make a puzzle out of an existing image:
http://lavadip.com/experiments/jigsaw/
A lot more information can be found here:
https://developer.mozilla.org/en-US/docs/SVG/Tutorial
As mentioned earlier you can use a library like http://raphaeljs.com/ to help with creating your SVG graphics.
A warning though it might be a pain in the backside to do :-p

Resources