Inline block (containing images) taking extra width in Firefox - html

I'm designing a responsive, horizontal image gallery, a screenshot of which (in Firefox) is attached below :
Now, the same gallery in chrome,
Although both look almost same, the problem is that, in firefox, the widths of all the three rows are much larger than the actual width, as shown by chrome.
For example, in Chrome, the width of the first row is 4222px, which should be the actual width, however in firefox, the same row has a width of 8708px, which is more than double!
I think this has something to do with the actual image sizes.
Firefox :
Chrome :
Although my gallery's layout is controlled by JavaScript, here is the basic layout :
<div class="gallery-container">
<div class="gallery-background"></div>
<div class="gallery-content">
<div class="gallery-header">
<div class="gallery-title">Open Source Photography</div>
</div>
<div class="gallery-showcase">
<div class="gallery-scrollable">
<div class="gallery-row">
<div class="gallery-image-container">
<img class="gallery-image" />
</div>
<!--All the images of row 1-->
</div>
<br />
<div class="gallery-row">
<div class="gallery-image-container">
<img class="gallery-image" />
</div>
<!--All the images of row 2-->
</div>
<br />
<div class="gallery-row">
<div class="gallery-image-container">
<img class="gallery-image" />
</div>
<!--All the images of row 3-->
</div>
<br />
</div>
</div>
</div>
</div>
</div>
And here is my CSS code :
#import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic);
html {
min-height: 100%;
height: 100%;
position: relative;
}
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.gallery-container {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
overflow: hidden;
background-color: #000000;
font-family: 'Open Sans Light', sans-serif;
}
.gallery-background {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-repeat: repeat;
-webkit-transition : opacity 1s;
-moz-transition : opacity 1s;
-ms-transition : opacity 1s;
-o-transition : opacity 1s;
transition : opacity 1s;
}
.gallery-content {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
z-index: 3;
}
.gallery-content .gallery-header {
position: absolute;
top: 0;
left: 0;
right: 0;
padding: 10px;
text-align: left;
height: 20px;
background-color: #000000;
z-index: 2;
}
.gallery-content .gallery-header .right {
float: right;
}
.gallery-content .gallery-header .left {
float: left;
}
.gallery-content .gallery-header .alignment-selection {
}
.gallery-content .gallery-header .gallery-title {
font-size: 15px;
color: #eeeeee
}
.gallery-content .gallery-showcase {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
}
.gallery-content .gallery-showcase .gallery-scrollable {
height: 100%;
overflow-y: hidden;
overflow-x: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 40px 20px;
}
.gallery-content .gallery-showcase .gallery-scrollable .gallery-row {
display: inline-block;
height: 33%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding-top: 20px;
text-align: left;
padding-right: 20px;
white-space: nowrap;
}
.gallery-content .gallery-showcase .gallery-scrollable .gallery-row .gallery-image-container {
display: inline-block;
position: relative;
height: 100%;
margin-right: 15px;
}
.gallery-content .gallery-showcase .gallery-scrollable .gallery-row .gallery-image-container .gallery-image {
height: 100%;
max-height: 100%;
width: auto;
max-width: 100%;
position: relative;
}
PS : Please ignore the different backgrounds, they change periodically, and with a kinda parallax effect. Thanks!
UPDATE :
I tried wrapping each of the images in a container.
I have updated the HTML and CSS codes with the new ones.
Although the extra width taken by firefox is gone, now the problem I'm experiencing is that the image container, which has inline-block display, is not shrinking according to the image's width (I have kinda redesigned the header and changed backgrounds) :
After searching a lot, I found this answer. Quoting from the answer :
Check and be sure their container/parent element does not have a white-space:nowrap. That would cause them to not wrap.
But if I remove the white-space:nowrap, the horizontal scrolling breaks and the images move to the next line.
Can anybody provide me a short hint on what I'm doing wrong?

I fixed it by adding the property :
max-height: 100%;
to .gallery-row

Related

How to place sibling img/div on top of each other with identical size if parent has padding

I'd like to have an image with a div that covers the image exactly. I can get the div to overlay the image by using position: relative in the parent and position: absolute for the div, but background-color fills out the padding in the parent so they aren't overlayed properly.
Here's a snippet that demonstrates the problem.
.parent {
position: relative;
padding: 10px;
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
}
.overlay {
position: absolute;
background-color: red;
width: 100%;
height: 100%;
border-radius: 13px;
left: 0;
top: 0;
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>
I'm able to get it pretty close with some calc()'s to subtract the padding. This almost works, but the div fills out a little too much at the bottom. I'd like to not have a bunch of hardcoded values for padding anyway, so I wouldn't really like this solution even if it did work entirely.
Here's a snippet that shows the calc() approach.
.parent {
position: relative;
padding: 10px;
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
}
.overlay {
position: absolute;
background-color: red;
width: calc(100% - 2 * 10px);
height: calc(100% - 2 * 10px);
border-radius: 13px;
left: 10px;
top: 10px;
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>
This snippet does things a slightly different way, putting the img inside the overlay div and putting the actual green, lower opacity overlay as the overlay div's after pseudo element.
This way you don't have to build in any knowledge of the parent's padding.
.parent {
position: relative;
padding: 10px;
width: 40%;
background: red;
height: fit-content;
}
.image {
width: 100%;
border-radius: 13px;
top: 0;
left: 0;
}
.overlay {
position: relative;
padding: 0;
width: fit-content;
height: fit-content;
}
.overlay::after {
content: '';
position: absolute;
background-color: green;
width: 100%;
height: 100%;
border-radius: 13px;
left: 0;
top: 0;
opacity: 0.2;
padding: 0px;
}
<div class="parent">
<div class="overlay"> <img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156"></div>
</div>
When using HTML5, browser adds some padding to the bottom of the img tag. This can be avoided by making the image a block element. So just adding display: block to class .image and then it good.
And btw, to define witdh/height of an absolute element, beside calc() you can also define 4 values top, right, bottom, left of it.
:root {
--custom-padding: 10px;
}
.parent {
position: relative;
padding: var(--custom-padding);
width: 40%;
}
.image {
width: 100%;
height: 100%;
border-radius: 13px;
display: block;
}
.overlay {
position: absolute;
background-color: red;
border-radius: 13px;
bottom: var(--custom-padding);
right: var(--custom-padding);
left: var(--custom-padding);
top: var(--custom-padding);
opacity: 0.2;
}
<div class="parent">
<img class="image" src="https://cards.scryfall.io/normal/front/4/f/4f3deefe-28bc-4e45-a0a0-ab03167e2e81.jpg?1561942156">
<div class="overlay"></div>
</div>

HTML CSS can't make image fit screen diagonally

I don't have much knowledge about html and css and I couldn't find the answer on the internet so I am here.
Problem:
I am trying to make an image fill top part of the screen but one thing stops me from it and it's the default margin of the <body>. I've managed it by using margin: -10px; But now the image can't fill the screen by 20px, probably because there is no margin, image still thinks screen is that big.
html,body {
width: 100%;
height: 100%;
margin: -10px;
padding: 0;
overflow: hidden;
}
img {
width: 1600px;
height: 300px;
opacity: 70%;
object-fit: cover;
object-position: top 10px;
}
.cont {
position: relative;
text-align: center;
padding: 10px;
}
.main-text {
font-size: 100px;
color: white;
position: absolute;
top: 100px;
left: 70px;
}
<body>
<div class="cont">
<img src="https://i.stack.imgur.com/DWZAk.jpg">
<div class="main-text">Big Ass Title</div>
</div>
</body>
NOTE: If you have any questions or didn't understand anything about the question, please ask because I am ready for any answer. :) Thanks.
If your image is meant to be a decoration(design), then background is fine to use.
.cont can be a flex or grid element, to avoid position absolute and possible unwanted sides effects.
here an example with a background and grid:
body {
margin: 0;
min-height: 100vh; /* optionnal if it does not have a purpose */
}
.cont {
height: 300px; /* guessed from your code */
display: grid; /* default make a single column*/
background: url(https://picsum.photos/id/1015/600/300) 0 0 / cover; /* background covering */
}
.main-text {
margin-block: auto; /* vertical-centering in its row from here */
margin-inline-start:70px;
font-size: 100px; /* from your code */
color: white; /* from your code */
font-weight: normal; /* you looked for this? */
text-shadow: 0 0 1px #000; /*Optionnal increase readability*/
}
<div class="cont">
<h1 class="main-text">Big Ass Title</h1><!-- if that's a title, then make it a title ;) -->
</div>
Generally to eliminate all the margins and paddings you can add:
* {
margin: 0;
padding: 0;
}
By the way I attached a snippet where it's working as you requested. Is better to eliminate the margins than adding a negative margin, if you want to do it that way you must to compensate it in the width to achieve the 100% width intended.
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
img {
width: 100%;
margin: 0;
height: 300px;
opacity: 70%;
object-fit: cover;
}
.cont {
position: relative;
text-align: center;
}
.main-text {
font-size: 100px;
color: white;
position: absolute;
top: 100px;
left: 70px;
}
<html>
<body>
<div class="cont">
<img src="https://images2.alphacoders.com/941/thumb-1920-941898.jpg">
<div class="main-text">Big Ass Title</div>
</div>
</body>
</html>

1-px distance between a div and its vertical scrollbar, making visible the coloured background

If you run the following code snippet in Full page (Run > Full page), you'll see a vertical red 1-pixel border near the vertical scrollbar, in Chrome / Windows. Why?
I've found workarounds (see Note2 below), so my goal is not only to avoid it, but to understand the reason for this red border. There normally should be no distance between div #popup and its scrollbar (or am I wrong?) Why is this border there?
body, html { height: 100%; width: 100%; }
* { margin: 0; padding: 0; }
#popup { position: fixed; top: 0; left: 10%; width: 80%; height: 100%; background-color: white; z-index: 10; overflow-y: auto; outline: 0; }
#popupmain { height: 100%; width: 100%; font-size: 0.9em; padding: 5%; box-sizing: border-box; outline: 0; border: 0; }
#popupclose { top: 5px; right: 5px; position: fixed; cursor: pointer; color: white; }
#popupdarkbg { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; background-color: red; }
<div id="popup">
<div id="popupmain">
<p id="popuplongdescription">a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a</p>
</div>
<div id="popupclose">X</div>
</div>
<div id="popupdarkbg"></div>
Note: the #popupdarkbg div is normally black, with some opacity: background-color: rgba(0,0,0,.75);, and the #popup (by default: hidden) is normally triggered/opened by a click on a link, with Javascript. I removed this part here to show only the core problem in this minimal example.
Note2: a solution to remove this border is to remove the #popupclose div or to move it out of #popup div. It works, but why?

Firefox and IE border-box not working properly?

So I have been trying to figure this out for a day or so without any luck, and figured I would turn to the CSS masters of the universe here.
Anyway, in Chrome my page looks fine (like always), but Firefox and IE both seem to have issues w/resizing images. I basically have 2 parts, a 'left div' and a 'right div', and the on the left just has right-padding to make it be the entire width, minus the width of the 'right div'.
Inside 'left div', there is an image who's size is set to be 100% of the width and height of the containing element, which in Chrome, works out wonderfully, and leaves the image in the center and looking good. FF and IE don't resize it at all, and worse, they don't respect the padding set on 'left div' so it looks even more weird.
The simplified HTML:
<div>
<div class="dialog-bg"></div>
<div id="view-larger-dialog" class="mc_report_dialog dialog-container">
<div class="details-container staticimage">
<span id="openPostModal">
<span class="modal-body cardDetails">
<div class="closeOpenModal">×</div>
<div class="cardContent">
<div class="cardBody">
<div id="card-content" class="card-content-staticimage">
<span class="image">
<img class="annotatable" src="https://s-media-cache-ak0.pinimg.com/originals/5a/28/22/5a282241e64e41d605384bb261ea581f.jpg">
</span>
</div>
</div>
</div>
</span>
</span>
<span class="detailBox">
<div class="cardContent cardDetails">
<div class="content">
<p>
blank white space
</p>
</div>
</div>
</span>
</div>
</div>
</div>
The CSS:
.dialog-bg {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: black;
opacity: 0.6;
z-index: 1001;
}
.mc_report_dialog .details-container {
padding: 0px;
}
span#openPostModal {
position: fixed;
top: 0;
left: 0;
height: 800px;
margin-top: 0px;
margin-left: 0px;
display: table;
z-index: 5000;
height: 100%;
background: none;
width: 100%;
box-sizing: border-box;
padding-right: 24rem;
border: none;
}
span.detailBox, span.shareNewBox {
width: 24rem;
height: 100%;
padding: 0;
position: fixed;
top: 0px;
right: 0px;
background-color: white;
z-index: 5005;
}
span#openPostModal .modal-body {
border: 0px solid #ffffff;
padding: .6rem 1rem;
display: table-cell;
vertical-align: middle;
width: 100%;
max-height: 50%;
background: none;
overflow-y: visible;
}
.closeOpenModal {
font-size: 2rem;
color: #fff;
float: right;
position: absolute;
right: 1rem;
top: 1rem;
font-weight: 700;
cursor: pointer;
padding-right: 24rem;
opacity: 0.8;
}
span#openPostModal .cardContent {
background: none;
border: none;
position: relative;
width: 90%;
margin: auto;
}
span#openPostModal .cardContent .cardBody {
padding: 0;
}
span#openPostModal .cardContent .cardBody #card-content {
height: 100%;
padding: 0;
}
#card-content.card-content-staticimage .image {
position: relative;
display: inline-block;
width: 100%;
height: 100%;
}
#card-content.card-content-staticimage .image img {
max-height: 100%;
max-width: 100%;
}
You can see the result of that here on my jsFiddle
Any help would be greatly appeciated.
Apparently the whole display: table; and display: table-cell were messing it up. Just changing those to display as block worked. Sorry for the question.
Your problem isn't box-sizing:border-box, it's display:table.
Just add table-layout:fixed; right after the display:table declaration and you should be ok.

Constrain table height and image to height of container

I have the following test here: http://dev.driz.co.uk/gallery/index2.php
The idea is that an image should be centred within the gallery div and have 72px of padding all the way around it. If the image is smaller than the screen, then it will be centred (this part works), however if the image is larger than the screen then it should resize itself to fit depending on the best ratio.
This is achieved by setting the image max-height and max-width to 100% so the image is constrained by its container element. And in this case the container is two faked tables with CSS to centre it on the page.
What's actually happening is the image is just ignoring the max-height property and only applying the width constrain, so it appears off the page.
Any ideas on what the issue is? In the past I have just used JavaScript to position the image in the middle, but would prefer to use just CSS like in the example.
Full code is as follows:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Center</title>
<style type="text/css">
*
{
margin: 0;
padding: 0;
border; 0;
}
html,body
{
height: 100%;
width: 100%;
}
body
{
overflow: hidden;
}
.gallery
{
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}
.gallery-background
{
background: #333333;
top: 0;
left: 0;
right: 0;
bottom: 0;
position: fixed;
padding: 72px;
}
.gallery-outer
{
display: table;
width: 100%;
height: 100%;
border-collapse: collapse;
table-layout:fixed;
}
.gallery-inner
{
text-align: center;
display: table-cell;
vertical-align: middle;
width: 100%;
height: 100%;
}
.gallery-image
{
position: relative;
}
.gallery-image img
{
max-width: 100%;
max-height: 100%;
vertical-align: middle;
}
</style>
</head>
<body class="default">
<div class="gallery">
<div class="gallery-background">
<div class="gallery-outer">
<div class="gallery-inner">
<div class="gallery-image">
<img src="EmpireState.jpg">
</div>
</div>
</div>
</div>
</div>
</body>
</html>
When you give an image a max-height of 100%, it looks for its direct parent tag's height. If that doesn't have a height or constrained in anyway, then it can't apply the rule to height of the image. Looking at your HTML/CSS, I would strip it back and simplify it like this:
<div class="gallery">
<div class="gallery-background">
<img src="EmpireState.jpg">
</div>
</div>
And the CSS
.gallery {
bottom: 0;
height: 100%;
left: 0;
position: fixed;
right: 0;
top: 0;
width: 100%;
}
.gallery-background {
background: none repeat scroll 0 0 #333333;
bottom: 0;
left: 0;
padding: 72px;
position: fixed;
right: 0;
text-align: center;
top: 0;
}
.gallery-background:before {
content: ' ';
display: inline-block;
vertical-align: middle;
height: 100%;
}
.gallery-background img {
max-height: 100%;
max-width: 100%;
vertical-align: middle;
}
Hopefully that should sort it out