oversized image centering with max-width and max-height - css

I have an img tag which contains an image and needs to be reduced to either max-width: 200px or max-height: 200px, which depends on whether the image is tall or wide. The current code I have:
CSS:
div#music_artwork {
position: relative;
width: 200px;
height:200px;
background:#eee;
}
.img-container {
position: relative;
overflow: hidden;
width:200px;
height:200px;
}
img {
position: absolute;
top: -9999px;
bottom: -9999px;
left: -9999px;
right: -9999px;
margin: auto;
}
HTML:
<div id="music_artwork">
<div class="img-container">
<img class="first" src="http://entertainmentmv.nl/wp-content/uploads/2014/06/breaking-1.png">
</div>
</div>
You'll see two containers which contain images. One of them is tall, one of them is wide. I'm looking for a way to combine these codes, without JavaScript.
Code in JSFiddle (http://jsfiddle.net/L9BnL/298/
)

There is currently no way for CSS to detect orientation of an image and to apply different rules based on that information. However, if you are able to alter your HTML and use background-images instead of img tags, this can easily be resolved by using background-size: cover;. Also, the required total CSS is much clearer.
Please not: this currently works in Chrome and Opera, only, with partial support in Safari. Firefox and IE do not support that feature, yet. See http://caniuse.com/#search=cover for more information.
.img-container {
width: 200px;
height: 200px;
margin: 20px;
background-size: cover;
}
<div class="music_artwork">
<div class="img-container" style="background-image: url('http://entertainmentmv.nl/wp-content/uploads/2014/06/breaking-1.png');">
</div>
</div>
<div class="music_artwork" style="margin-top:20px;">
<div class="img-container" style="background-image: url('http://cdn1.sciencefiction.com/wp-content/uploads/2013/08/Bryan-Cranston.jpg');">
</div>
</div>

Related

Keep Footer below dynamic content

so I'm trying to tame the Footer so that it stays below the dynamic content container, but whatever way I try it (Pos: Abs, Bottom: 0; etc etc) it either appears halfway up the content or fixed at the bottom. Either I don't want. It would be appreciated if someone could shine a light on my problem.
HTML:
<div id="Content">
<div id="G6"></div>
<div id="Post-Block">
<div id="block">
<div id="feat-img"></div>
<div id="date"></div>
</div>
</div>
<div id="Footer">
<div id="G7"></div>
<div id="FooterBreak"></div>
<div id="FooterBG"></div>
<div id="FooterLinks">
</div>
<div id="Copyright">
</div>
<div id="Copyright2">
</div>
<div id="FooterBreak2"></div>
</div>
</div>
CSS:
#Footer {
width: 100%;
height: 230px;
position: absolute;
left: 0;
bottom: 0;
}
#Content {
z-index: 7;
background: url(/images/content%20bg.jpg) repeat left top;
position: absolute;
top: 336px;
width: 999px;
height: auto;
color: #fff;
min-height: 950px;
margin: 0 0 230px;
}
html {
position: relative;
height: auto !important;
}
body {
z-index: 0;
background: url(/images/background-texture%20d.jpg);
left: 0;
}
#page {
z-index: 1;
width: 1000px;
height: 1000px;
margin-left: auto;
margin-right: auto;
}
EDIT: When I used the Chrome dev tools to inspect the Crow's Perch website, it looks like your problem is that the height of your HTML is smaller than your content (ie, you use negative bottom values in your absolute positioning for some of your content). Given this, you could add bottom: -865 to #footer, but given that you said your content is dynamic, that's an EXTREMELY brittle solution. Unfortunately, since you're pixel-pushing all of your elements, I don't think there's a way to have your footer respond dynamically to your changing content. More comprehensive refactoring of your code is likely necessary.
Good luck!

How to maintain a nice logo in a fix height div?

I want to have a nice looking box AND a nice looking logo in that box (logos are for example purpose only).
BUT I don't know how to do that.
I have my image tag look like this
<img class="col-sm-12 pull-right" src="/marketing_materials/{{$marketing_material->id}}/download/thumb_path" alt="" />
If I include the width="200" height="200" in the <img> tag, my view will look like this.
I got a nice looking box, but ugly logo(stretch).
Else if I include the width="200" only in the <img> tag, my view will look like this.
I got a nice looking logo, but ugly box(doesn't line up).
Either way, I chose will screw up my view. :(
You could put your image in a 200x200 div and center your image (but not stretch it) inside it like this:
.imgbox{
border:solid 4px grey;
border-radius:5px;
width:200px;
height:200px;
display: table-cell; vertical-align: middle
}
<div class="imgbox">
<img src="http://img3.wikia.nocookie.net/__cb20100520131746/logopedia/images/5/5c/Google_logo.png">
</div>
Using the background-size property is the simplest way.
Read more about it here: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
* { margin:0; padding:0 }
figure.logo {
width: 200px;
height: 200px;
background-image: url('http://i.imgur.com/F0RRqFy.jpg');
background-size: cover;
}
<figure class="logo"></figure>
Editable Demo: http://jsbin.com/gituzu/2/edit?html,css,output
you can center the images horizontal and vertical to the parent width and height by using position: absolute;:
HTML:
<div class="img_wrap">
<img src="https://pbs.twimg.com/profile_images/453545705622106112/6ERHbJBN.jpeg" />
</div>
<div class="img_wrap">
<img src="http://wottageek.com/wp-content/uploads/2014/10/Dropbox-Logo.png" />
</div>
<div class="img_wrap">
<img src="https://longren.io/wp-content/uploads/2014/04/do.png" />
</div>
CSS:
.img_wrap
{
width: 200px;
height: 200px;
position: relative;
float: left;
margin-right: 10px;
border: 5px solid #ccc;
}
.img_wrap img
{
max-width: 100%;
max-height: 100%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
example: http://jsfiddle.net/82bhzxfe/

Why does this div element not extend to the top of the screen?

Given the following bit of css
.reveal .fulldiv{
position:fixed;
background: green;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
}
With a simple html file <div class="fulldiv">foo</div> I get what I expect, a green background with foo in the upper left. When I'm using reveal.js to create an HTML5 presentation, I get instead:
I thought that using position:fixed wouldn't care about anything that it is wrapped up in. However it seems respect some container that it is in. How can I get this div to extend the full screen as before?
For completeness, here is the body (without the script calls) used for the picture:
<div class="reveal"><div class="slides">
<section class="vertical-stack">
<section class="vertical-slide">
<div class="fulldiv">foo</div>
</section>
</section>
</div></div>
Your missing a comma in the css
.reveal, .fulldiv{
position:fixed;
background: green;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
}

img { max-height: 100%; } causes img to exceed bounds

Is this a Chrome bug?
Here's the HTML:
<div><img src="test.png"></div>
Here's the CSS:
* { box-sizing: border-box; }
div { height: 200px; padding: 75px 0 60px; }
img { max-height: 100%; }
Expected result: The img should have a height of 65px.
Result in Chrome (v. 27.0.1453.116) on Mac OS (v. 10.6.8): The img has height of 135px and "bleeds" into the parent div's padding. If I change the padding of the div to 50px 0, oddly it renders properly.
Play with this in a codepen: http://codepen.io/anon/pen/jhbKz
Screenshots:
First block has padding of 50px 0. Second block has padding of 75px 0 60px.
Firefox (correct result)
Chrome (wrong?)
Try adding a container to your Image with width and height of 100%. This will give you the same output on chrome and FF.
<div class="b">
<div style='height:100%;width:100%;'>
<img src="some image url">
</div>
</div>
I cannot explain why this fix works currently, but I myself am trying to reason with it.
Years later, the issue seems to have spread to Firefox.
Pav's workaround did not work for me, maybe because I have "a" not "div". The only way in my case was to display as table:
<div style="display: table;">
<a style="height: 100px; display: table-cell;" href="#">
<img style="max-height: 100%; width: auto;" src="some image url">
</a></div>
An additional benefit of "table" is that vertical-align: middle; can be used to center the image (or other content) vertically.
You can achieve it using position: absolute for your image.
<div class="wrap">
<img class="img" src="https://cdn.photoswipe.com/photoswipe-demo-images/photos/1/img-2500.jpg" alt="">
</div>
body {
height: 100vh;
}
.wrap {
display: inline-block;
position: relative;
max-height: 500px;
height: 100%;
width: 100%;
background: rgba(0,0,0,0.1);
}
.img {
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
height: 100%;
}
jsfiddle

CSS: One column's width relative to resizable 100% height square?

This question is kind of hard so explain so I've created a jsfiddle:
http://jsfiddle.net/zLjHn/ (or see HTML/CSS below)
Basically I want a square shaped video on the right size of the screen with 100% height and the left column to fill the rest of the available screen space. I've been working on this problem for a few hours so any help would be appreciated!
Edit: Left column will contain a number of paragraphs/images that will be centered in the available space (not just a single paragraph as the example code shows.
CSS:
.profile_page {
width: 100%;
height: 100%;
position: absolute;
}
.left {
float:left;
width: 100%;
position: relative;
}
video {
top: 0;
right: 0;
border: 1px solid red;
float: right;
height: 100%;
position: absolute;
max-width: 80%;
}
HTML:
<div class="profile_page">
<div class="left">
<p>This paragraph's width should adjust according to the videos width.</p>
</div>
<video src="http://stream.flowplayer.org/trains/640x360.mp4" />
</div>
Thanks!
Taboo as this sounds, your answer is a table layout. A two-cell table row exhibits the layout behavior you're looking for without any javascript. If you're concerned about semantic HTML (and you should be) you can accomplish this using display:table-cell; and display:table-row; in your css.
This will limit the browsers that correctly display your layout.
<div class="profile_page">
<div class="left">
<p>This paragraph's width should adjust according to the videos width.</p>
</div>
<div class="right">
<video src="http://stream.flowplayer.org/trains/640x360.mp4" />
</div>
</div>
and the style...
.profile_page {
width: 100%;
height: 100%;
position: absolute;
display:table-row;
}
.left {
min-width: 100px;
display:table-cell;
border:1px solid green;
vertical-align:top;
}
.right
{
display:table-cell;
border:1px solid blue;
max-width:80%;
min-width:300px;
}
video {
top: 0;
right: 0;
height: 100%;
width: 100%;
}
See the fiddle: http://jsfiddle.net/NkeLc/1/
If you're looking for a quick fix, you can reorganize to wrap the <p> around the video like this: http://jsfiddle.net/zLjHn/4/
I believe the paragraph and video are competing for "flexibility". Seems like you want the video to flex according to height, and the paragraph to then flex with available width.
I think you can achieve the right flex-priority by leading with the video and wrapping the paragraph around it as the attached fiddle shows.
Try to modify your CSS as following:
.left {
float:left;
max-width:20%;
position: relative;
min-width: 100px;
}
demo: http://jsfiddle.net/4yEmJ/

Resources