CSS background-color on edges - html

I have a transparent .png image and I change the background color with css so i can use the same image with different colors.
This is a dummy image and the problem is with Chrome browser i get a line on top and bottom of that image when you zoom at certain points and on some smartphones like this:
It depends on page resolution I think.
This problem is not reproducable with Firefox whatsoever.
Has anyone seen something like this? How can this be solved?
Here is an example, and try to zoom in with Chrome or open it with smartphone:
.vertical-center {
position: relative;
top: 50%;
transform: translateY(-50%);
background-color: black;
background-clip: padding-box;
background-clip: content-box;
}
/* CSS used here will be applied after bootstrap.css */ .equal-col-height { display: flex; display: -webkit-flex; flex-wrap: wrap; }
<div class="panel-heading">Heading</div>
<div class="panel-body">
<div class="row equal-col-height">
<div class="col-xs-6 text-center">
<img class="vertical-center" src="https://s21.postimg.org/6hym65mtj/test.png">
</div>
</div>
</div>
update
The whole problem was actually seeing the background color on edges of the element like here Fiddle link you can reproduce it by opening the link on smartphone and try to zoom in. The background-color comes through the edges.
And the solution from Kosh Very solves the problem
Demo on fiddle

The problem is not caused by image itself, but by wrong subpixel rendering in some browsers.
The following code does the trick with the image in my Chrome 58.0.3029.110 (64-bit):
.vertical-center {
position: relative;
top: 50%;
transform: translate3d(-1px,-50%,0);
background-color: black;
background-clip: content-box;
display: block;
border: solid 0.1px transparent;
}
/* CSS used here will be applied after bootstrap.css */ .equal-col-height { display: flex; display: -webkit-flex; flex-wrap: wrap; }
<div class="panel-heading">Heading</div>
<div class="panel-body">
<div class="row equal-col-height">
<div class="col-xs-6 text-center">
<img class="vertical-center" src="https://s21.postimg.org/6hym65mtj/test.png">
</div>
</div>
</div>
Another part of the question was solving the similar problem with https://jsfiddle.net/5maqwgjg/2/ in SGS7 default browser (aka Samsung Internet). It can be solved adding background-clip:content-box; transform:scale(1.01); to .middle (https://jsfiddle.net/aw53wcg4/1/)

Images automatically have vertical-align: middle and that creates the gaps on the edges.
You can make the image a block element to remove the space with display: block. They are by default inline-block.
If you don't want the image to be a block element, add vertical-align: top or vertical-align: bottom to the image element with CSS.
Here is a reference to another SO question, but the accepted answer isn't a perfect solution. Read the others for more ideas.
Remove white space below image

Related

CSS: Div is Smaller Than Object

I have tried everything I can think of, like: overflow, box-sizing, messing with divs in the html, hard-coding div width, etc. Yet nothing is working and I'm not seeing any inherited characteristics that need overriding for any of these to work.
This is causing a problem where my image has a massive amount of whitespace around it and I can't figure out how to get it to stop (since my code is already working on other pages).
I've tried switching from .png to .jpg and back, but its been no use.
Any help is appreciated.
(Border-radius also doesn't work due to this, which was sort of my first hint it was a div size problem.)
My HTML:
<div class="content-wrapper">
<div class="content-header-wrapper">
<h2>Page Title.</h2>
</div>
<div class="title-img"><img src="../Images/headshot.jpg"></div>
<div class="content-message-wrapper">
<div class="p1-titlewrap">
<h3>1st Paragraph Header</h3>
</div>
</div>
</div>
My CSS:
.content-wrapper {
justify-items:center;
background-color: white;
background-attachment: scroll;
background-repeat: no-repeat;
background-size: 100%;
font-family: "Ubuntu Condensed", sans-serif;
height:auto;
}
.content-wrapper .content-header-wrapper {
font-size: 1.5em;
display: flex;
justify-content: center;
}
.content-wrapper .title-img {
display: flex;
justify-content: center;
transform: scale(.15,.15);
box-sizing: content-box;
}
Have you tried adjusting the transform: scale() property in the CSS? or is that not something you wanted to do in the first place?
Maybe set "background-size: cover;" that should fill the entire div with the image. If not why not try and resize the image so that it fills up more space.
Try to set image as css property background and set background-size:cover;. Hope it will help you to solve this problem.

Parent bg shows/ child doesn't fill 100% depending on zoom factor

Sometimes the background of my parent-div is showing, and it's driving me crazy.
Setup:
Container-div with 4x4 divs inside (lets call them "outer"; blue bg).
Each of the blue "outer"-divs contains another div ("inner"; green bg).
Why I want it that way:
The green is supposed to cover the blue, because eventually the green will disappear (via jQuery - click) and the blue will show (it will have a bg-image instead of just the blue bg).
Problem:
Even though I tried different approaches (see codepen below), sometimes the blue bg will show.
screenshot of the problem
The point is -sometimes-. If the browser is zoomed to a convenient zoomfactor, it will display exactly as intended: the green covers the blue 100%. If, however, the browser thinks it's an unconvenient zoomfactor, the blue bg shows through. It also varies depending on the browser. If you view the below codepen-example in firefox rather than chrome, opera or edge, you might not even see that error (I didn't test it in safari).
Question:
How can I ensure that the green will consistently cover the blue, no matter the zoomfactor (or browser)?
css:
<style>
#outer-container {
width: 600px;
/* height: 600px; */
display: flex;
flex-flow: row wrap;
}
.outer-box {
width: 150px;
height: 150px;
background-color: blue;
border: 1px solid black;
box-sizing: border-box;
}
.inner-box {
width: 100%;
height: 100%;
background-color: green;
}
</style>
html:
<div id="outer-container">
<div class="outer-box" id="outer-box01">
<div class="inner-box"></div>
</div>
<div class="outer-box" id="outer-box02">
<div class="inner-box"></div>
</div>
<div class="outer-box" id="outer-box03">
<div class="inner-box"></div>
</div>
<!-- there are 16 outer-divs in total (see codepen) -->
</div>
codepen-link
Additional info:
If I open that codepen with firefox, it behaves as intended, not in chrome or edge. If I open my visual studio code with firefox, I get the same background-showing-display-glitch(?) (depending on the zoomfactor).
I tried floats, flexbox and grid (see codepen); I used height/width in pixels, height: 100%, stretch (flexbox); pos: rel on outer and pos: abs, top,right,bottom,left: 0 on inner; display: block won't help, because the div is a block-element already (just to be sure I assigned it manually anyways); I use box-sizing: border-box - and I'm running out of ideas.
Yes, when I assign the border to the inner-divs the outer-bg doesn't show; but it's the outer-divs that are supposed to have the border, because once the green get's clicked away the border must still be there.
Thanks in advance for any suggestions!
I found a workaround to achieve my goal by now (which was to not let the blue show through) by just putting another container-div on top of the first one.
My original question, though, remains unanswered with that solution.
But just in case anyone was trying something similar:
html:
<div id="outer-container">
<div id="inner-container">
<div class="inner"></div>
<div class="inner"></div>
<div class="inner"></div>
<!-- 16 of those inner-divs here -->
</div>
<div class="bg"></div>
<div class="bg"></div>
<div class="bg"></div>
<!-- 16 of those bg-divs here -->
</div>
css:
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#outer-container {
width: 600px;
position: relative;
overflow: hidden;
}
#inner-container{
/* width: 600px; */
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: hidden;
}
.bg, .inner{
width: 150px;
height: 150px;
border: 1px solid black;
float: left;
}
.bg{
background-color: blue;
}
.inner{
background-color: green;
}
</style>
codepen link
I had a look at this and it's puzzling. My best guess at this point is that it's a browser quirk/bug. With HTML and CSS there are so many ways to achieve the same thing so your workaround isn't necessarily bad. I managed to make it behave by putting the border on the inner-box's instead of the outer.
The reason I think it may be a bug is that it isn't consistent, as you have noticed yourself. In Chrome it happens at zoom level 125% (on a standard 1080 screen). In FireFox it doesn't happen at all on any zoomlevels and Edge has it at various zoom levels.
It looks a bit like the outer div at some levels ends up extending half a pixel wider than it's supposed to but it's hard to be sure.

Why is my div resizing?

My header is structured as a table. I finally managed to make it so that the last li is floated to the right and X % to the left while still being compatible with different screen sizes. However, my 'profile picture' div is resizing and resembles a squished circle as a result. How do I make sure that the div is always 40px in width and height (if this is the right way to go around it)?
CSS
#hdr-profile {
align-items: center;
border: 1px dotted red;
display: flex;
margin-left: auto;
white-space: nowrap;
}
#hdr-profile-pic {
width: 40px;
height: 40px;
background: white url("https://scontent-lhr3-1.xx.fbcdn.net/hphotos-xfa1/v/t1.0-9/11760274_1109394289087955_3712628479579288500_n.jpg?oh=ff64d9b1a44338d53d414459ff92aa71&oe=574558FA") no-repeat;
background-size: 100% 100%;
border-radius: 50%;
cursor: pointer;
}
HTML
<li>
<div id="hdr-profile">
<div id="hdr-profile-pic" title="My Profile">
<div id="hdr-profile-country" title="Liam is in Spain"></div>
</div>
<span id="hdr-profile-name" class="select">Liam Macmillan</span>
<i class="material-icons md-26 icn-lft icn-hvr">arrow_drop_down</i>
</div>
</li>
An easy way would be to add a !important after the 40px in your css of the profile picture
I suspect it's because of the size of the drop-down. In the JSFiddle that Nenad Vracar linked, the picture comes in totally fine. Don't use !important to force the size, it's bad practice and causes unexpected behavior. Instead, try experimenting with the size of the dropdown. I don't have the rest of your code, or I'd have looked into it.

Strange behavior with image inside floated container

Given this simple layout:
HTML
<div class="container">
<div class="imgContainer">
<img src="http://placehold.it/400x400">
</div>
<div>
This should always stay to the right of the image.
</div>
</div>
CSS
.container {
height: 20vh;
}
.imgContainer {
float: left;
height: 100%;
}
img {
height: 100%;
}
Issue #1
Chrome, Firefox, and Opera correctly display it like this:
IE11 incorrectly puts the text 400 pixels to the right, based on the natural width of the image:
Issue #2
As you increase the window's height, the text should stay glued to the right of the image. This works correctly in Firefox.
However, the text overlaps the image in Chrome and Opera:
See the behavior in this Fiddle.
Question: Is there a style I can add that will cause all browsers to behave consistently?
[Note: I discovered this while working on this question. I thought I had a solution, until I realized it wasn't responsive in any browser except Firefox.]
The following might do the trick.
Instead of using float, I would suggest using CSS tables.
Apply display: table to .container and set the height as needed.
For the two child elements, .imgContainer and .panel, use display: table-cell and inherit the height from the parent block.
I think this is pretty close to what you need, should work in all browsers
(but I did not check...)
.container {
height: 20vh;
display: table;
}
.imgContainer, .panel {
display: table-cell;
height: inherit;
vertical-align: top;
}
img {
vertical-align:top;
height: inherit;
}
<div class="container">
<div class="imgContainer">
<img src="http://placehold.it/400x400">
</div>
<div class="panel">
This should always stay to the right of the image.
</div>
</div>

How can you achieve a "knockout" effect using CSS?

UPDATE - Pretty sure I figured this out. The code is somewhat long, but I threw a page up here so you can view the source: http://www.sorryhumans.com/knockout-header
The concept was based on: http://algemeenbekend.nl/misc/challenge_gerben_v2.html and then adapted for my needs.
The header is responsive and knocked out. (Please ignore the bad, 1 minute responsive bg image implementation!). This implementation also does not use any CSS3, so I would imagine that there wouldn't be many issues with compatibility.
The only issue I find is that when the browser width is an odd number (e.g. 1393px) in Chrome there is a 1px gap between the right hand fluid column and the main center column. I don't see this issue in the latest version of Firefox, Internet Explorer, or when the width is an even number (e.g. 1394px in Chrome). Any ideas?
Original Question:
I'm attempting to code a header that I designed, but am unable to figure out how to get the effect I'm looking for. Please look at the attached image (No, this is not actually what I'm working on :) just an example!)
The photo is a full-width responsive photo. The header is full-width, but its contents are on a responsive grid that does not exceed some arbitrary size (shown by the black lines), but can scale down. I can accomplish all of this, but what I am having trouble figuring out is how to make the make the header bar be transparent where the logo would be. In other words, rather than having the logo be on top of the bar, I would like to "knock it out" of the header.
Is this even possible?
There's no inherent support for knockout effects, so you'll have to have the text as part of an image.
The easiest way to do this would be to have the background behind the knockout effect be the solid part of the image. You can create a .png with a solid background and transparency where you want the knockout effect, and use css opacity to make the entire header partially transparent. You will need to set up the header with multiple sections so that the sections that are not images (i.e. outside the black bars) have a background color, while the sections with images do not.
Very roughly:
<div id="outerHeaderWithOpacity">
<div class="hasBackground">Left side, will stretch</div>
<div class="noBackground">Image(s) go here</div>
<div class="hasBackground">As many sets as you need</div>
<div class="noBackground">Image(s) go here</div>
<div class="hasBackground">Right side, will stretch</div>
</div>
http://jsfiddle.net/GZ8Xv/
not the prettiest solution but using the experimental css3 flexbox: (with display: table fallback)
<div class="wrapper">
<div class="left"><br /></div>
<div class="middle"><br /></div>
<div class="right"><br /></div>
</div>
.left, .right
{
height:100%;
border: 1px solid black;
display:table-cell;
display: -webkit-flexbox;
display: -moz-flexbox;
display: -ms-flexbox;
display: -o-flexbox;
-webkit-flex: 1;
}
.middle
{
display: table-cell;
display: -webkit-flexbox;
width: 500px;
height:100%;
border: 1px solid blue
}
.wrapper
{
display: table;
display: -webkit-flexbox;
display: -moz-flexbox;
display: -ms-flexbox;
display: -o-flexbox;
-webkit-box-orient: horizontal;
-moz-box-orient: horizontal;
-ms-box-orient: horizontal;
-o-box-orient: horizontal;
width: 100%;
height: 100px;
}
PLEASE NOTE: the flexbox w3c spec is still in flux and could change a third time. I only tested this in IE9 (both IE9 and IE8 modes. Does not work in IE7 mode) and Chrome 20 and 22
A few minor changes: http://jsfiddle.net/GZ8Xv/2/ and you have your 5 div layout without javascript.