display: inline-block leaves gap with respect to height after div element - html

I have a div and an image in one div. Parent div has the background color. display: inline-block is given to both child div and the image.
<div style="background-color: black;">
<div style="display: inline-block; width: 20px; height: 105px; background-color: #27ae60; margin: 0;"></div>
<img style="display: inline-block; padding: 0px 10px;" src="http://cdn01.coupondunia.in/sitespecific/media/generated/merchantlogos/logo_5e29580_97.jpg?v=1413531812" />
</div>
jsfiddle link
http://jsfiddle.net/hv9szL92/2/
Gap below ebay image and green block must be removed. Thanks

The gap is because you set child elements as display: inline-block, and inline/inline-block elements respect white spaces, including new-line characters.
The simplest fix is to set zero font-size on the parent container in order to make those white spaces zero sized.
<div style="background-color: black; font-size: 0;">
/* content unchanged */
</div>
Remember to reset font-size back to some reasonable value for any nested element if you need to display text in them.
And it's better not to use inline styles, but I assume this is just an example in your case.
Demo: http://jsfiddle.net/hv9szL92/4/

As asked by OP, "Gap below ebay image and green block must be removed. Thanks"
http://jsfiddle.net/hv9szL92/5/
set the vertical-align property on the image and you're done (see Get rid of space underneath inline-block image) :
<img style="display: inline-block; padding: 0px 0px; vertical-align: top;" src="http://cdn01.coupondunia.in/sitespecific/media/generated/merchantlogos/logo_5e29580_97.jpg?v=1413531812" />
As for the green block, just remove the nested div element

You can just edit the margin of your img
<div style="background-color: black;" >
<div style="display: inline-block; width: 20px; height: 105px; background-color: #27ae60; margin: 0;" ></div>
<img style="display: inline-block; padding: 0px 10px; margin-bottom: -3.1px;margin-left: -13.5px;" src="http://cdn01.coupondunia.in/sitespecific/MEDIA/generated/merchantlogos/logo_5e29580_97.jpg?v=1413531812" />
</div>
Giving the image a negative margin should prove to be helpful
Any problems , let me know

Properly aligned and formatted using CSS-tables and Unordered List.http://codepen.io/anon/pen/WvGJqq
<div id="container">
<ul>
<li id="green-block"></li>
<li id="logo-wrap"><img id="logo" src="http://cdn01.coupondunia.in/sitespecific/media/generated/merchantlogos/logo_5e29580_97.jpg?v=1413531812" /></li>
</ul>
</div>
By using CSS tables you are able to use 'vertical-align: bottom;' to align the image with the bottom of the css cell.
Structure as follows:
- div#container [display: inline-table]
- ul [display: table-row]
- li [display: table-cell, vertical-align:bottom]
- img#logo [display: block, vertical-align:bottom]
Its pure css, but the same concept besides table layout creating from the mid 90's.
/* css reset */
ul {
padding: 0;
margin: 0;
list-style: none;
}
li {
padding: 0;
margin: 0;
width: 0;
}
/* css */
#container {
width: 100%;
height: 105px;
background: #000;
margin: 0;
padding: 0;
display: inline-table;
}
ul {
display: table-row;
}
#green-block {
width: 20px;
height: 105px;
background-color: #27ae60;
margin: 0;
display: table-cell;
}
#logo-wrap {
display: table-cell;
vertical-align: bottom;
}
#logo {
display: block;
vertical-align: bottom;
margin: 0 10px;
}
A really nice article out lying many of the concepts used for CSS tables.
http://colintoh.com/blog/display-table-anti-hero

Related

Align multiple divs vertically each div having same percentage height

My pen
http://codepen.io/helloworld/pen/dqGDk
I want to vertically align 3 divs inside a wrapper div. Each of the 3 divs should have a height of 33%. I can make the layout work when the divs has a height of 33px but I need it as percentage because the wrapper div`s height changes dynamically. Sometimes its 100px height, sometimes 70px etc...
I just want that all 3 are always correct align by using percentage height.
What is the approach aligning divs with percentage?
HTML
<div id="wrapperDiv" style="height:100px;">
<div id="navigationWheelerContainer">
<div id="navigationWheeler" >
<div id="previewTemplate" >1</div>
<div id="previewTemplate" style="background-color: #0094ff;">2</div>
<div id="previewTemplate" >3</div>
</div>
<div id="toggleButtonRight" >◄</div>
</div>
</div>
CSS
#navigationWheeler {
height: 100%;
text-align: center;
width: 100%;
vertical-align: middle;
border: black solid 1px;
background-color: lightgray;
display: inline-block;
}
#navigationWheelerContainer {
float: right;
height: 100%;
}
#previewTemplate {
vertical-align: middle;
line-height: 33%; /* 33px; works but is not dynamic to the wrapper div */
}
#toggleButtonRight {
width: 40px;
border: black solid 1px;
cursor: pointer;
text-align: center;
}
I use flex-box, works way better the list items plus you can place ul,ol,il in flex-box.
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes
It's much easier if you change to use an unordered list. Also the ID previewTemplate should be a class as the ID has to be unique.
HTML
<div id="wrapperDiv" style="height:100px;">
<div id="navigationWheelerContainer">
<ul id="navigationWheeler" >
<li class="previewTemplate" >Testing</li>
<li class="previewTemplate" style="background-color: #0094ff;">2</li>
<li class="previewTemplate" >3</li>
</ul>
<div id="toggleButtonRight" >◄</div>
</div>
</div>
Then in your CSS you can set .previewTemplate in the CSS to have a height of 33%. Also add list-style: none; to get rid of the bullets. Then in #navigationWheeler set padding-left: 0; to get rid of the spacing.
CSS
#navigationWheeler {
height: 100%;
text-align: center;
width: 100%;
vertical-align: middle;
border: black solid 1px;
background-color: lightgray;
display: inline-block;
padding-left: 0;
}
#navigationWheelerContainer {
float: right;
height: 100%;
}
.previewTemplate {
height: 33%;
list-style: none;
}
#toggleButtonRight {
width: 40px;
border: black solid 1px;
cursor: pointer;
text-align: center;
}
Now when the height of your #wrapperDiv changes the li with class .previewTemplate will change it's height.
Here's a working JSFiddle. Also you should avoid using inline CSS. Have all your styles inside your CSS file. Makes it easier to manage in the long run.
EDIT Adding padding-left: 0 to the CSS for the #navigationWheeler
EDIT Updating the JSFiddle to take in account the padding-left for the <ul>
Try using list items instead of div's. You can easily style and their syntax is more focused on alignment then a div. Also, Your wrapper div's need to be a percentage as well, you can't do a 10% height in a 100px wrapper div..

Unwanted spacing below image

I'm trying to create a fixed-position footer at the bottom of my page. but there's an issue with spacing below the image and the bottom of the viewpoint that is unwanted:
Base Image:
The Issue:
The padding below the image is unwanted.
HTML:
<div id="containerBarKonge">
<ul>
<li><img src="./kongelogo.png" /></li>
</ul>
</div>
CSS:
#containerBarKonge {
position: fixed;
bottom: 0px;
width: 100%;
z-index:9999;
padding: 0px;
margin: 0px;
}
#containerBarKonge > ul {
position: relative;
list-style-type: none;
padding: 0px;
padding-left: 2px;
margin: 0px 20px;
min-width: 1053px;
background-color: #900;
}
#containerBarKonge > ul * {
padding: 0px;
margin: 0px;
}
Try setting the vertical align to bottom on the image:
#containerBarKonge img { vertical-align: bottom; }
The issue comes from the image having a default property of "display: inline;" - which is the equivalent to saying "have this image run along like text."
Images should rarely be employed as inline containers. Instead, an image should be defined as either display: block or inline-block. This gives you much precise control over your iamges versus - just align this to the top or bottom. What if you want the image 1px from the bottom? With vertical-align you can not.
So the solution is to do the following:
#containerBarKonge > ul li {
display: block;
height: 20px; /* or however tall it is */
}
#containerBarKong > ul li img {
display: inline-block;
/* Assuming it is 18px tall and you want it at the bottom: 20 - 18 = 2px */
margin: 2px 0 0 0;
}
there you go. You have PRECISe control of the positioning of the image while it retains its ability to run along like text.

Is it possible for inline-block element to auto fill the available width?

I have a <div id="content">, which contains <div id="sub-navigation> and <div id="main container">, which themselves are inline-blocks. I would like to be able to make the main container fill the rest of the available page width. Is that possible?
I need columns-strip to expand or shrink based on the number and width of column elements. If the width of the columns-strip exceeds the width of the main container, then a horizontal scroll bar should appear.
* {
margin: 0px;
padding: 0px;
font-size: 10pt;
white-space: normal;
}
#wrapper {
margin: 0px 20px;
background-color: red;
}
#header {
margin: 25px 10px 10px 10px;
height: 50px;
background-color: purple;
color: white;
}
#content {
margin: 10px;
padding: 10px;
font-size: 0pt;
white-space: nowrap;
overflow: hidden;
background-color: white;
}
#sub-navigation {
width: 200px;
height: 150px;
display: inline-block;
vertical-align: top;
background-color: forestgreen;
color: white;
}
#main-container {
padding: 10px;
display: inline-block;
overflow: auto;
background-color: yellow;
}
#columns-strip {
padding: 10px;
font-size: 0pt;
white-space: nowrap;
background-color: mediumturquoise;
}
.posts-column {
margin: 0px;
width: 200px;
height: 200px;
display: inline-block;
vertical-align: top;
overflow: auto;
}
#footer {
margin: 10px 10px 25px 10px;
height: 50px;
background-color: navy;
}
<div id="wrapper">
<div id="header"></div>
<div id="content">
<div id="sub-navigation"></div>
<div id="main-container">
<div id="columns-strip">
<div class="posts-column" style="background-color: lightgray;"></div>
<div class="posts-column" style="background-color: darkgray;"></div>
<div class="posts-column" style="background-color: gray;"></div>
</div>
</div>
</div>
<div id="footer"></div>
</div>
You have to remove the inline-block styles and float the #sub-navigation div. inline-block is not suited for what you are trying to achieve. When you add no display styles, the div element will be the default value which is block, block elements take up all the available space by default. By floating the #sub-navigation element you make it only take up the space required for its contents.
#sub-navigation {
width: 200px;
height: 150px;
float : left;
vertical-align: top;
background-color: forestgreen;
color: white;
}
#main-container {
padding: 10px;
overflow: auto;
background-color: yellow;
}
make sure to add a clear: left element after the #main-container
That's not how inline-blocks are supposed to be used. Best thing to do here is make your navigation box float:left and leave the default display value alone.
If your header, footer and wrapper have specific widths, then yes, you can have your main-container fill the available space. But if you're not specifying widths in your CSS, then you need to determine how big your main-container CAN be based on the rendered width of the containing element (wrapper). The only way to determine that width after the page loads is with javascript. If you want your site to have a dynamic width but still have your content (sub-navigation and main-container) fill the screen, you would either need to use javascript or percentages, and percentages can get ugly when you start looking at varying resolutions of monitors, laptops, etc...
Ever heard of flex box model!!
It is made just for that.
Note in flexbox model all child elements act as flex box model you cant opt out certain things. Which mean if page has navigation and under it content div + side div. You can't make top navigation out of it. Which has implications. So solution is to have all things only that need flex box in one div.

Vertical align within DIV, with a block element inside the div

This has always driven me crazy and never found the right answer.
I want to achieve the following:
http://juicybyte.com/stack-overflow.jpg
Meaning, I want to have an image on a div on the left, and text that nicely vertical-aligns itself depending on how much content there is. Height of the text div can be fixed.
However, everything is no go.
<div id="widgetWhite">
<div id="widgetWhiteIcon">
<img src="/images/iconWhiteIconTn.png" alt="White Icon" />
</div>
<div id="widgetWhiteContent">
<p>I would love it if this worked.</p>
Download PDF
</div>
</div>
The CSS:
#widgetWhiteIcon {
width: 82px;
margin: 0 10px 0 20px;
display: inline-block;
vertical-align: middle;
}
#widgetWhiteContent {
width: 108px;
font: normal normal 11px/14px Arial, sans-serif;
height: 110px;
display: inline-block;
vertical-align: middle;
}
#widgetWhiteContent a {
color: #f37032;
}
Don't really care about IE6.0, but IE7.0 is required unfortunately.
Thanks for any help!
Here, I put together a solution for you based on the site I linked. I didn't bother mapping your existing css into it, but I think you will get the idea.
http://jsfiddle.net/M3h6v/5/
<div class="ie7vert1">
<img src="http://placehold.it/120x150" alt="White Icon" />
<div class="ie7vert2">
<div class="ie7vert3">
<p>I would love it if this worked.</p>
Download PDF
<br style="clear: both;" />
</div>
</div>
</div>
.ie7vert1 {
display: table;
#position: relative;
overflow: hidden;
border: 1px dashed gray;
float: left;
width: 100%;
}
.ie7vert2 {
#position: absolute;
#top: 50%;
display: table-cell;
vertical-align: middle;
}
.ie7vert3 {
#position: relative;
#top: -50%;
border: 1px dashed red;
}
The vertical-align property has two prerequisites for use:
The elements you are trying to vertically-align must be siblings.
The elements you are trying to vertically-align must not be block-level elements.
That being said, this is actually quite easy to solve:
<div id="widgetWhite">
<div id="widgetWhiteIcon">
<img src="http://placehold.it/100x100" alt="White Icon" />
</div><div id="widgetWhiteContent">
<p>I would love it if this worked.</p>
Download PDF
</div>
</div>
Note that the closing div for #widgetWhiteIcon and the opening div for #widgetWhiteContent are touching: </div><div id="widgetWhiteContent">. This allows for you to control the spacing between these two elements, since normally any space between inline elements in your markup is shown in the presentation.
Edit: You could equivalently set font-size: 0 on #widgetWhite without worrying about whitespace. font-size is inherited in the children elements, so you would need to explicitly set that after, like so: #widgetWhite { font-size: 0; } #widgetWhite * { font-size: 12px; }
CSS:
p { margin: 0; }
#widgetWhite > div {
vertical-align: middle;
display: inline-block; }
#widgetWhiteContent { margin: 0 0 0 4px; }
#widgetWhiteContent a {
margin: 1em 0 0;
display: block; }
Preview: http://jsfiddle.net/Wexcode/DcWB8/
You have to set a fixed height for the wrapper div (div#widgetWhiteContent) first in order for vertical-align to work. To keep everything in div#widgetWhiteContent vertically aligned with div#widgetWhiteIcon, both div's should be at the same height.
So a good solution would be to set a height for the outer div, and then set the height of both child div's to 100%.
Your CSS goes like this
<style>
#widgetWhite {
height: 110px;
}
#widgetWhiteIcon {
width: 82px;
margin: 0 10px 0 20px;
display: inline-block;
height: 100%;
}
#widgetWhiteContent {
clear: left;
width: 108px;
font: normal normal 11px/14px Arial, sans-serif;
height: 100%;
display: inline-block;
vertical-align: middle;
}
#widgetWhiteContent a {
color: #f37032;
}
</style>

prevent div's from taking 100% width

<div style="position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px;">
<div style="
border: 2px solid black;
margin: 0 auto;
text-align: center;
padding: 3px;">
Hello<br />Hola
</div>
<div style="
border: 2px solid black;
margin: 0 auto;
text-align: center;
padding: 3px;">
Another Sentence
</div>
</div>
I have a problem: the borders of the inner div's reach over the whole width of the page, but i want them to only frame the content inside them. If i use: display: inline the borders frame each line separately and overlap, so that doesn't work - can somebody help?
P.S the style's aren't declared like this in the original document but in a stylesheet
Assign a width to the absolutely positioned element? If you're looking for shrink-wrapping, float:left or display:inline-block are perfect for that.
Try display:inline-block, it always helps me in situations like this.
http://jsfiddle.net/FaYLk/
Its not as simple to just do:
display: inline-block;
You must do more than that to cross browser this.
display: inline-block;
display: -moz-inline-stack; /* for firefox 2 */
*display: inline; /* for ie 6 and 7 */
Put a container around all the content. E.g
<div class='container'> <div>I wont be 100%</div> <div>Nor will I :)</div> </div>
.container{ display: inline-block; }