I am trying to figure out how to get rid of extra height in my inner <div> which contain <img> tags. I have tried a css reset but it isn't doing a full reset as far as I can tell.
In the HTML and CSS below, I have 3 <img> tags stacked vertically, each 32px. I want there to be no room between them and the <div> which contains them, I expect to have a height of 96px. You can see in the js fiddle, that there is space between the <img> tags and the inner div has a height > 100px.
UPDATE The possible duplicate post linked to explains well what is happening:
By default, an image is rendered inline, like a letter. It sits on the
same line that a, b, c and d sit on. There is space below that line
for the descenders you find on letters like f, j, p and q. You can
adjust the vertical-align of the image to position it elsewhere.
However, adjusting the vertical-align: top did not completely solve the problem since there was still extra height. In my case, it was necessary to set line-height:0 to completely remove all extra height.
The HTML:
<div class="ss-buttons">
<div class="outer">
<div class="inner">
<a><img src="http://www.static.mseifert.com/img-common/blank.gif" class="ss-zoom sprite-32"></a>
<a><img src="http://www.static.mseifert.com/img-common/blank.gif" class="ss-zoom sprite-32"></a>
<a><img src="http://www.static.mseifert.com/img-common/blank.gif" class="ss-zoom sprite-32"></a>
</div>
</div>
</div>
The CSS
html,
body {
min-width: 100%;
height: initial;
}
body {
line-height: 1;
}
html,
body,
div,
span,
a,
img,
p,
pre {
border: 0;
padding: 0;
margin: 0;
}
img.sprite-32 {
display: inline-block;
width: 44px;
height: 32px;
z-index: 0;
}
.outer {
width: 44px;
display: inline-block;
position: relative;
height: 96px;
}
.inner {
width: 44px;
position: absolute;
display: inline-block;
left: 0px;
z-index: 2000;
top: 0px;
}
a {
display: inline-block;
height: 32px;
}
img.ss-zoom {
background: url(http://www.static.mseifert.com/img-common/slideshow-zoom-sprite.png) no-repeat 0px 0px scroll;
}
div.ss-buttons img {
border: none;
background-color: green;
}
Add line-height:0; to .inner
html,
body {
min-width: 100%;
height: initial;
}
body {
line-height: 1;
}
html,
body,
div,
span,
a,
img,
p,
pre {
border: 0;
padding: 0;
margin: 0;
}
img.sprite-32 {
display: inline-block;
width: 44px;
height: 32px;
z-index: 0;
}
.outer {
width: 44px;
display: inline-block;
position: relative;
height: 96px;
}
.inner {
width: 44px;
position: absolute;
display: inline-block;
left: 0px;
z-index: 2000;
top: 0px;
line-height: 0;
}
a {
display: inline-block;
height: 32px;
}
img.ss-zoom {
background: url(http://www.static.mseifert.com/img-common/slideshow-zoom-sprite.png) no-repeat 0px 0px scroll;
}
div.ss-buttons img {
border: none;
background-color: green;
}
<div class="ss-buttons">
<div class="outer">
<div class="inner">
<a><img src="http://www.static.mseifert.com/img-common/blank.gif" class="ss-zoom sprite-32"></a>
<a><img src="http://www.static.mseifert.com/img-common/blank.gif" class="ss-zoom sprite-32"></a>
<a><img src="http://www.static.mseifert.com/img-common/blank.gif" class="ss-zoom sprite-32"></a>
</div>
</div>
</div>
Add vertical-align: top; to your a element CSS styling (<a>). That will line them all up with no spacing. When you use inline-block, for some reason CSS3 defaults the default vertical-align property to baseline thus the spacing. If you change vertical-align to top it'll remove your spacing issue.
Here's the updated CSS snippet for <a> element styling:
/* ...rest of your CSS */
a {
display: inline-block;
height: 32px;
vertical-align: top;
}
/* rest of your CSS... */
Just add float:left to a tag in css
a{
float:left;
}
Your CSS will be
html,
body {
min-width: 100%;
height: initial;
}
body {
line-height: 1;
}
html,
body,
div,
span,
a,
img,
p,
pre {
border: 0;
padding: 0;
margin: 0;
}
img.sprite-32 {
display: inline-block;
width: 44px;
height: 32px;
z-index: 0;
}
.outer {
width: 44px;
display: inline-block;
position: relative;
height: 96px;
}
.inner {
width: 44px;
position: absolute;
display: inline-block;
left: 0px;
z-index: 2000;
top: 0px;
}
a {
display: inline-block;
height: 32px;
}
img.ss-zoom {
background: url(http://www.static.mseifert.com/img-common/slideshow-zoom-sprite.png) no-repeat 0px 0px scroll;
}
div.ss-buttons img {
border: none;
background-color: green;
}
a{
float:left;
}
Related
I am trying to understand the position in html and css by playing around with an example I have made up. In this example what I have created 3 divs which show color blocks. I am trying to make the first 2 blocks span the width of the screen and the third do just sit as it is on screen. I am trying to have all 3 blocks just stacked on top of each other.
in my html i have created 3 classes:
<div class="color-stripred">
</div>
<div class="color-stripblue">
</div>
<div class="color-stripgreen">
</div>
In my css i have defined the colors, shapes and positions of these blocks:
.color-stripred {
display: block;
height: 20px;
width: 100%;
background-color: red;
position: static;
top: 0;
left: 0;
}
.color-stripblue {
display: block;
height: 20px;
width: 100%;
background-color: blue;
left: 0;
}
.color-stripgreen {
display: block;
height: 20px;
width: 100%;
background-color: green;
left: 0;
}
The red block is on top followed by blue then green. It looks like the following picture:
The problem comes when I try and change the positioning in order to make red and box span the width of the screen. i change the red box css as follows:
.color-stripred {
display: block;
height: 20px;
width: 100%;
background-color: red;
position: fixed;
top: 0;
left: 0;
}
what happens is the redbox spans the width of the screen but the other two boxes shift upwards. how can i stop the blue box and the green box from shifting upwards?
The problem is caused by position: fixed; which you don't even need.
I think what you actually want is to set body { margin: 0; }.
According to W3Schools:
Most browsers will display the <body> element with the following
default values:
body {
display: block;
margin: 8px;
}
body:focus {
outline: none;
}
You can see in the snippet below, that if you add this to your CSS (i.e., remove the margin from the body), all three boxes become full viewport width (even though the width is set to 100%!).
See the snippet below.
body {
margin: 0;
}
.color-stripred {
display: block;
height: 20px;
width: 100%;
background-color: red;
top: 0;
left: 0;
}
.color-stripblue {
display: block;
height: 20px;
width: 100%;
background-color: blue;
left: 0;
}
.color-stripgreen {
display: block;
height: 20px;
width: 100%;
background-color: green;
left: 0;
}
<div class="color-stripred"></div>
<div class="color-stripblue"></div>
<div class="color-stripgreen"></div>
you could add margin-top:20px; to .color-stripblue
.color-stripred {
display: block;
height: 20px;
width: 100%;
background-color: red;
position: fixed;
top: 0;
left: 0;
}
.color-stripblue {
margin-top:20px;
display: block;
height: 20px;
width: 100%;
background-color: blue;
left: 0;
}
.color-stripgreen {
display: block;
height: 20px;
width: 100%;
background-color: green;
left: 0;
}
<div class="color-stripred">
</div>
<div class="color-stripblue">
</div>
<div class="color-stripgreen">
</div>
I'm trying to achieve the below:
So the darker blue box will be the parent div and then the teal blue one will be the child div, see the above image.
I have the below code, but can't seem to work out how to achieve it!
body {
padding: 0;
margin: 0;
background-color: #6ca591;
}
.container {
margin: 50px auto;
width: 1000px;
background-color: red;
padding: 80px;
}
.portfolio_main_img {
background-color: #327acd;
display: block;
position: relative;
padding-bottom: 30px;
z-index: 1;
}
.portfolio_main_img img {
margin: -28px auto 0 auto;
display: table;
position: relative;
width: 50%;
z-index: 99;
}
<div class="container">
<div class="portfolio_main_img">
<img src="https://via.placeholder.com/150C/O https://placeholder.com/">
</div>
</div>
If anyone could help me out or give me some tips it would be greatly appreciated!
You are facing margin collpasing where the negative margin is pulling the container instead of only the image. Change the image to inline-block to avoid this and center it using text-align
body {
padding: 0;
margin: 0;
background-color: #6ca591;
}
.container {
margin: 50px auto;
width: 1000px;
background-color: red;
padding: 80px;
}
.portfolio_main_img {
background-color: #327acd;
display: block;
padding-bottom: 30px;
text-align:center;
}
.portfolio_main_img img {
margin: -28px auto 0 auto;
display: inline-block;
width: 50%;
}
<div class="container">
<div class="portfolio_main_img">
<img src="https://via.placeholder.com/150C/O https://placeholder.com/">
</div>
</div>
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.
Hey I can't figure out why my divs are overlapping and what i should do...
You can watch the site here: http://hersing.dk/job/
I would like for the div carrying the hr to appear underneed the header-info div
Heres is the code from the site:
#font-face {
font-family: hersing;
src: url(lmroman10-regular.otf);
}
html,
body {
font-family: hersing;
height: 100%;
margin: 0px;
}
.container {
width: 90%;
height: 90%;
left: 5%;
top: 5%;
background: green;
position: absolute;
display: block;
clear: both;
}
.info-name {
left: 5%;
top: 10%;
position: absolute;
display: block;
}
.info-picture {
min-width: 250px;
min-height: 250px;
padding: 4px;
position: absolute;
top: 10%;
right: 5%;
background: black;
display: block;
}
.info-picture img {
height: 100%;
width: 100%;
}
#info-header {
font-size: 400%;
}
#info-title {
font-size: 150%;
font-weight: bold;
}
.header-info {
display: block;
padding: 20px;
position: relative;
width: 100%;
}
.stang-1 {
display: block;
width: 100%;
color: blue;
position: relative;
}
#hr-1 {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
background-color: #f1a857;
}
<div class="container">
<div class="header-info">
<div class="info-name" id="info-name">
...
</div>
<div class="info-picture" id="info-picture">
<img src="images/picture.png" />
</div>
</div>
<div class="stang-1" id="stang-1">
<hr id="hr-1">
</div>
</div>
I hope someome can figure this out, cause i'm pretty lost
Both .info-name and .info-picture are absolute positioned and .header-info has no height defined.
You'd rather use relative positioning + float + clear and/or display: inline-block for both .info-* rules and everything will be fine.
<div class="container">
<div class="header-info">
<div class="info-name" id="info-name">
.....
</div>
<div class="info-picture" id="info-picture">
<img src="images/picture.png" />
</div>
</div>
<div class="stang-1" id="stang-1">
<hr id="hr-1">
</div>
</div>
<style>
#font-face {
font-family: hersing;
src: url(lmroman10-regular.otf);
}
html,
body {
font-family: hersing;
height: 100%;
margin: 0px;
}
.container {
width: 90%;
height: 90%;
left: 5%;
top: 5%;
background: green;
position: absolute;
display: block;
clear: both;
}
.info-name {
left: 5%;
top: 10%;
position: absolute;
display: block;
}
.info-picture {
width: 250px;
height: 250px;
padding: 4px;
position: relative;
top: 10%;
left:70%;
background: black;
display: block;
}
.info-picture img {
height: 100%;
width: 100%;
}
#info-header {
font-size: 400%;
}
#info-title {
font-size: 150%;
font-weight: bold;
}
.header-info {
display: block;
padding: 20px;
position: relative;
width: 100%;
}
.stang-1 {
display: block;
width: 100%;
color: blue;
position: absolute;
}
#hr-1 {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
background-color: #f1a857;
}
</style>
I think this will solve your problem...
In this case, although very impractical, the solution would be to add a line break <br> after the .header-info div.
I repeat, this solution is not the best one by far, and you should, as pointed out in the comments by Paulie_D, change your positioning layout method.
Everything inside the absolutely positioned .container would be better positioned relative. Use css float:left; or float:right; to position elements and clear:both; when you want the next element to start below all floated elements. Use padding on the container and margins on the floated elements for positioning.
Also give .container css class of overflow:auto; to wrap around all elements inside without having to set the height every time.
I'm trying to create a hover effect on a rather complex layout. The hover effect works, but upon hover the background (or overlay) extends beyond the image (I would like it to be just as big as the image).
Does anyone know why that is and how to fix it?
HTML
<article>
<div class="img-crop">
<h2>Title</h2>
<img src="http://bit.ly/gUKbAE" />
</div>
</article>
CSS
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
article {
overflow: hidden;
}
.title-anchor {
display: inline-block;
position: absolute;
overflow: hidden;
z-index: 1;
width: 100%;
height: 100%;
}
.img-crop:hover .title-anchor {
background: rgba(0,0,0,0.5);
}
.img-crop {
display: inline-block;
position: relative;
overflow: hidden;
max-width: 100%;
margin: 0;
padding: 0;
}
h2 {
color: rgba(0,0,0,0);
margin: 0;
padding: 0;
z-index: 2;
line-height: 0;
position: absolute;
top: 50%;
left: 0;
width: 100%;
text-align: center;
}
.img-crop:hover h2 {
color: black;
}
It's here too: http://jsfiddle.net/kmjRu/39/
Just add
img {
display: block;
}
http://jsfiddle.net/kmjRu/41/
Images are replaced inline elements by default
You need to add this on the universal css:
img { border: 0; vertical-align: top;}
...
http://jsfiddle.net/Riskbreaker/kmjRu/43/
define the height of .img-crop:hover the same as .img-crop img. E.g.
.img-crop:hover {
background: rgba(0,0,0,0.5);
height: 100px;
}
.img-crop img{height: 100px;}