White space under image [duplicate] - html

This question already has answers here:
Image inside div has extra space below the image
(10 answers)
Closed 3 years ago.
I can't remove white space under my images after i added boarder.
How do i get rid of this?
<div style="width: 1200px;">
<div style="float: left; width: 358 px; margin: 0px 7px 0px 0px; border: 1px solid #021a40;"><img src="{{media url="wysiwyg/3row/3row-no-boarder_07.jpg"}}" alt="" /></div>
<div style="float: left; width: 358 px; margin: 0px 7px 0px 7px; border: 1px solid #021a40;"><img src="{{media url="wysiwyg/3row/3row-no-boarder_04.jpg"}}" alt="" /></div>
<div style="float: left; width: 358 px; margin: 0px 0px 0px 7px; border: 1px solid #021a40;"><img src="{{media url="wysiwyg/3row/3row-no-boarder_09.jpg"}}" alt="" /></div>
<div style="clear: both;"> </div>
</div>
</div>
</div>
http://postimg.org/image/5rvgc5h8p/
Fiddle: https://jsfiddle.net/mastervision/zap4smbm/
I use the Magento editor: http://postimg.org/image/c62ljlmoj/

Add this
div{
line-height: 0;
}
Fiddle

In your floated container, give your images display: block; and width: 100%.
This will cause the image to stretch it's width to it's parents width and if this parent has no height defined it should fit exactly.
If that doesn't work please post a comment under my answer and I'll setup a case for you when I find the time ;)
OFFTOPIC
Oh and ofcourse you'll want to get rid of all that inline CSS.
inline css is pure duplication of code and can be solved with external stylesheets.
In such a stylesheet you can style by name basically so you can do this in your stylesheet:
.my-image-box {
position: relative;
float: left;
width: 300px;
height: auto;
}
.my-image-box img {
display: block;
width: 100%;
}
Then in your HTML, after linking your fresh stylesheet you can do this:
<div class="my-image-box">
<img src="..." />
</div>
This means you don't have all that bulky CSS in your HTML file - making it alot smaller and as an added bonus, when you change something in your stylesheet you'll change it everywhere at the same time :D
IMO that is a double win!

It's because the source of your image is supposedly not the right size or aspect ratio for this size. If you could post a fiddle, I would be able to test this theory. You can fix it by using the object-fit property or using the background property.

Try
img {display: block;}
if images dimensions are right and you are still having white space.

One option would be to use the vertical-align property; e.g.
img {
vertical-align: baseline;
}

Related

Bootstrap div border taking spaces

Hi!
I'm using bootstrap 4 and making my own project. After some time I found a problem which I can't fix. I have placed an image into a div section and made the div to be 200px of height & width and overflow will be hidden. Then I made the height of the image 100% of the div and gave a background color(bg-light) and border(5px solid black) to the div. But the background color is seen 1px left, top and right of the div and the image is placed after the 1 px left, right and top.
HTML:
<div class="profile-image-container">
<img class="profile-image" src="image.jpg" />
</div>
CSS:
.profile-image-container{
width: 200px;
height: 200px;
border: 5px solid black;
overflow: hidden;
}
.profile-image{
height: 100%;
}
I didn't understand your question properly.. please tell us what you want as a output.
Find the code below, whatever I understand so far
<img class="profile-image" src="img.jpg" width=100% />
Below is the code from the original question, with a placeholder image.
Unlike the image posted to the question, there is no white border between the enclosing div and the image.
The may be some additional CSS that was not included in the question to account for the white border between the div and the image.
.profile-image-container{
width: 200px;
height: 200px;
border: 5px solid black;
overflow: hidden;
}
.profile-image{
height: 100%;
}
<body style="background-color:#e0e0e0; padding:0.5rem">
<div class="profile-image-container">
<img class="profile-image" src="https://via.placeholder.com/210x200" />
</div>
</body>

To display the image circular even the image is not loaded

I am displaying the images in a circular way inside a div tag like this:
My issue is: Suppose an image(ex 2nd image) is not loaded/ present. Even then it should display it in a circular manner. But it is displaying in a square like below image:
I want image tag to be circular even though the image is not present.
Fiddle link
Then also it should display in circular. But it is displaying as square like below image
I'd argue that you probably want to fallback to some other image or some other placeholder. You can achieve it like so:
<img src="https://material.angul.io/assets/img/examples/shiba2.jpg"
class="company"
onerror="this.src='https://via.placeholder.com/70x70'">
Note the onerror. You can attach a handler function to do more complex things like hiding the img element, and showing some other placeholder element.
You can use CSS clip-path to force a circular cut-out, which will then apply to any image that you try to put in (good or broken).
Demo:
.image { width: 60px; height: 60px; }
.clip-circle { clip-path: circle(30px at center); }
<div style="background: #ffddcc">
Broken images: <img src="does-not-exist.png" class="image">
<img src="does-not-exist.png" class="image clip-circle">
</div>
<br />
<div style="background: #ffddcc">
Good images: <img src="https://via.placeholder.com/60x60" class="image">
<img src="https://via.placeholder.com/60x60" class="image clip-circle">
</div>
Not sure if this gives a better display than the other answers... but at least it answers the question :-) Also, browser support for this is not quite universal: see https://caniuse.com/#feat=css-clip-path.
In this scenario you have to use Javascript because you can't know with HTML/CSS if an image is broken
So here is a solution to either hide the image or replace it with another image just like #MrSaints has mentioned
To Hide:
<img src="Error.src" onerror="this.style.display='none'"/>
To Replace:
<img src="Error.src" onerror="this.src='fallback-img.jpg'"/>
Javascript solution for multiple broken images:
document.addEventListener("DOMContentLoaded", function(event) {
document.querySelectorAll('img').forEach(function(img){
img.onerror = function(){this.style.display='none';};
})
});
<img src="error.src">
<img src="error.src">
<img src="error.src">
<img src="error.src">
You can put the images in div and apply the radius on the div by adding overflow:hidden
.img-container{
border-radius: 50%;
height:70pt;
width:70pt;
float: left;
overflow:hidden;
margin:5px;
}
img{
height:70pt;
width:70pt;
}
<div class="img-container"><img src="https://material.angular.io/assets/img/examples/shiba2.jpg" class="company"></div>
<div class="img-container"><img src="https://material.angular.io/assets/img/examples/shiba2.jpg" class="company"></div>
AFAIK we can't make the invalid image tag as circular. But we can do some workarounds by wrapping the img tag in a div and then specifying border-radius: 50%; overflow: hidden to the div and enclose the image within.
.company {
border-radius: 50%;
height: 100%;
width: 100%;
}
div {
border-radius: 50%;
height: 70pt;
width: 70pt;
float: left;
overflow: hidden;
border: 1px solid #555;
}
<div><img src="https://material.angular.io/assets/img/examples/shiba2.jpg" class="company">
</div>
<div>
<img src="https://material.angular.io/assets/img/examples/shib2.jpg" class="company">
</div>
JSFiddle
You can tackle this issue with JS as #MrSaints is showing, but also this can fail as well. I was thinking more as a CSS solution and defaulting this to a color/nothing, but keep the circle shape.
.circle-image {
height:70pt;
width:70pt;
float: left;
border-radius: 50%;
margin: 5px;
background-size: cover;
background-position: center;
background-color: transparent;
background-image: url('https://material.angular.io/assets/img/examples/shiba2.jpg');
box-shadow: 0px 1px 3px rgba(0,0,0,0.3);
box-sizing: border-box;
}
.broken-image {
background-image: url('https://material.angular.io/assets/img/examples/shiba2.jpgssss');
}
<div class="circle-image">
</div>
<div class="circle-image broken-image">
</div>

Vertically center divs inside div

I am a beginner and trying to to my own wordpress theme from scratch (using bootstrap, stripping away everything and adding my own). Now I have got stuck on an annoying problem.
I have a div containing 3 smaller divs which gonna be menu-items as images. I have gotten the 3 to be horizontally centered in comparison to the outer div but not vertically.
I have set the inner divs to be as high as the outer but im not able to get the inner contents to also be vertically centered.
This is my code:
HTML
#menu {
margin: 0 auto;
width: 60%;
height: 300px;
}
div.menu_item {
display: inline-block;
height: 100%;
}
div.menu_item img {
-webkit-border-radius: 15px;
border: 1px solid #CCC;
border-radius: 15px;
clear: both;
height: 150px;
margin: 4px 15px;
padding: 10px;
width: 150px;
}
<div id="menu">
<div class="menu_item">
<p>
<a title="" href="myimg"><img alt="" src="/myimg.png" width="125" height="125" /></a>
</p>
</div>
<div class="menu_item">
<p>
<a title="" href="myimg"><img alt="" src="/myimg.png" width="125" height="125" </a></p>
</div>
<div class="menu_item">
<p>
<a title="" href="myimg"><img alt="" src="/myimg.png" width="125" height="125" </a></p>
</div>
</div>
Im aware of that this might be very messy and almost certain some things arent needed and could be stripped away but I have tried to figure it out for myself and this is what Ive came to this far.
Screenshots of it is here:
outer div
one of the inner divs
The first shows the outer div, the second shows one of the inner divs.
UPDATE: Solution was fiddling around with flexbox and it was quite easy even for me. Also, I tried to strip away as much as I could from the CSS and still have the same result and apparently almost no code is required.
This is my CSS now
#menu
{
margin:0 auto;
width:60%;
height:300px;
display: flex;
justify-content: center;
align-items: center;
}
This makes the content of #menu to be centered both vertically and horizontally.
This should work:
div.menu_item
{
width: X%; /*change to display: table; if you're targeting IE8 or greater, requires !DOCTYPE */
margin: 0 auto;
...
}
The width just must be less than the outer div, I put X to represent the percentage.
The same can be done with height:
...
height: X%;
...

How to get two divs inline beside eachother?

Need help getting two divs to align beside each other, one contains 2 pictures, the other a video.
HTML
<img src="example3.png" alt="CopeLogo" height="200" width="200" class="exlogo" >
</div>
</div>
CSS
#pics{
text-align: center;
background-color: #194b6f;
height: 200px;
width: 500px;
float: left;
border-radius: 5px;
margin-left: 50px;
border: 2px dashed black;}
#vid{
text-align: center;
background-color: #194b6f;
height: 490px;
width: 660px;
float: right;
border-radius: 5px;
border: 2px dashed black;
margin-right: 50px;}
just use float:left; for both, also you can use display:inline-block; for both as well if you don't want to use float.
I get your point but please do add your working codes to be more clear in your question.
You can actually do this:
HTML
<div class="header">
<div class="pic">
<img src="http://placehold.it/150x150" alt="">
<img src="http://placehold.it/150x150" alt="">
</div>
<div class="vid">
<img src="http://placehold.it/400x150" alt="">
</div>
</div>
CSS:
.header {
display: inline-block;
width: 100%;
border: 1px solid #ccc;
}
.vid {
float: right;
}
.pic {
float: left;
}
Working fiddle
Think of it this way: use DIVs to group each entity that you want to float:left.
You want to align the two images, so put each into a div, and float those divs left.
You want to align the box containing the two images with the box containing the video. Float each of those left.
When you use float:left (or right), the floated DIV is removed from the HTML "flow" and therefore has zero height. Therefore, the DIV containing a floated DIV (or other element) has zero height. Not good. To fix that, apply the clearfix hack to the parent div.
Here is a better explanation (although it uses a different method to clear the floats -- both methods work fine): Customising Insightly HTML contact form (aligned, spaced fields)
/* CSS: */
body{min-width:650px;min-height:650px;}
#pics{float:left;border-radius:5px;XXXmargin-left:50px;border:2px dashed black;}
#picLeft {float:left;height:200px;width:200px;}
#picRight{float:left;height:200px;width:200px;}
#vid{float:left;height:200px;width:200px;border-radius: 5px;border:2px dashed black;}
.clearfix:after{content:"";clear:both;display:block;}
<!-- HTML: -->
<div id="container" class="clearfix">
<div id="pics" class="clearfix">
<div id="picLeft">
<img src="http://placekitten.com/200/200" alt="MyLogo" height="200" width="200" class="mainlogo">
</div>
<div id="picRight">
<img src="http://placekitten.com/195/195" alt="CopeLogo" height="200" width="200" class="exlogo" >
</div>
</div>
<div id="vid">
<iframe src="https://www.youtube.com/watch?v=8E8xMcXmI9E" width="195"></iframe>
</div>
</div>
Additional References:
CSS-Tricks: Clearfix hack
Another example
Ahmed Alaa's answer also has a good tip: display:inline-block is also useful - +1
float both elements left and adjust there widths accordingly.

Align images vertically (flush top), not flush bottom

I have a div tag, that contains images. The default seems to align images flush bottom, assuming different vertically sized images. How do I change the code, such that I get the images to align flush top?
Here is the complete HTML code.
CSS
#page {
position: relative;
padding: 0px 0px 0px 0px;
float: left;
border: none;
}
.divPortfolioImageRowFirst
{
display: table-row;
}
.divPortfolioImageCol1 {
vertical-align: top;
}
HTML
<div id="page">
<div>
<div id="divProductLogos">
<div class='divPortfolioImageRowFirst'>
<div class='divPortfolioImageCol1' style='line-height: 78px' id="divProductLogos_1">
<img style='margin-left: 0px;' src="./images/portfolio/ProductLogos_1.png"/>
<img style='margin-left: 15px;' src="./images/portfolio/ProductLogos_2.png"/>
</div>
</div>
</div>
</div>
</div>
The addition of "vertical-align: top;" and "line-height: 78px;" did not do the trick.
What did I forget / not do?
I think you want something like the following:
The HTML:
<div id="divProductLogos">
<div class='divPortfolioImageRowFirst'>
<div class='divPortfolioImageCol1'>
<img src="http://placekitten.com/150/200" />
<img src="http://placekitten.com/150/250" />
</div>
</div>
</div>
and the CSS:
.divPortfolioImageRowFirst {
border: 2px dotted gray;
padding: 10px;
}
.divPortfolioImageCol1 {
border: 1px dotted gray;
}
.divPortfolioImageCol1 img {
vertical-align: top;
}
Originally, you had too many floats affecting the layout, and that was causing some problems.
You could also use CSS table cells depending on other design considerations.
Demo fiddle: http://jsfiddle.net/audetwebdesign/9fhCS/
About the Vertical-align Property
The vertical-align property is not inherited, so it as to be applied to the inline elements that need to be adjusted. Specifying vertical-align on the parent container will not affect the child elements.
Reference: http://www.w3.org/TR/CSS2/visudet.html#propdef-vertical-align
First, valign isn't a valid property, you should use vertical-align: top;
You will also need to set line-height to the height of the tallest image.