Random 5 px space at bottom of div - html

I have the following code in this fiddle:
HTML
<div id="overview">
<div id="banner">
<img src="http://www.placehold.it/1200x600"/>
<div id="info">
<div id="identity">
<span id="name">Name</span><br/>
<span id="title">Title of Name g</span>
</div>
</div>
</div>
</div>
CSS
* {
box-sizing:border-box;
}
#banner {
width:100%;
position:relative;
}
#banner > img {
width:100%;
}
#info {
position:absolute;
bottom:0;
width:100%;
text-align:center;
}
#identity {
display:inline-block;
text-align:left;
}
#name {
font-size:2em;
font-weight:bold;
}
#title {
font-size:1em;
font-weight:bold;
}
I have not yet tested this in any browsers besides Chrome, however there is an extra 5px being added to banner's height. When stripping out styles and elements so that it is just the image and the containing banner the gap is still present. I have included a g in the title to demonstrate how this is problematic.
I at first I thought it was due to the nature of inline elements and thew browser taking into account line breaks/tabs in the code as white space. However when condensing it all down to a single line, the problem remained.
Can someone explain what is going on, and how to fix it?

Make your image either a block or inline-block element:
#banner > img {
width:100%;
display: block;
}
See: http://jsfiddle.net/audetwebdesign/cf6vwy5h/
Be default, images are inline elements and their bottom edge is positioned on the baseline. There is a small amount of space (leading) below the baseline to allow room for the descenders of certain letters like "y" or "j".
* {
box-sizing: border-box;
}
#banner {
width: 100%;
position: relative;
border: 1px dashed blue;
}
#banner > img {
width: 100%;
display: block;
}
#info {
position: absolute;
bottom: 0;
width: 100%;
text-align: center;
border: 1px dotted blue;
}
#identity {
display: inline-block;
text-align: left;
}
#name {
font-size: 2em;
font-weight: bold;
}
#title {
font-size: 1em;
font-weight: bold;
}
<div id="overview">
<div id="banner">
<img src="http://www.placehold.it/1200x600" />
<div id="info">
<div id="identity">
<span id="name">Name</span>
<br/>
<span id="title">Title of Name g</span>
</div>
</div>
</div>
</div>
Reference:
If you look at the CSS2 specification:
http://www.w3.org/TR/CSS21/visudet.html#line-height
read the section related to vertical-align, specifically, baseline, which says:
Align the baseline of the box with the baseline of the parent box. If
the box does not have a baseline, align the bottom margin edge with
the parent's baseline.
Images are replaced elements and do not have a baseline; hence, their bottom edge is aligned with the baseline of the containing block, which is what gives the small vertical space below the image and the edge of the parent block.

This issue is due to img layout, that is similar to inline, you can change it to block to resolve the issue:
#banner > img {
width:100%;
display:block;
}
JSFiddle
Or you can also change img default vertical alignment from baseline to top, suppressing top indent:
#banner > img {
width:100%;
vertical-align:top;
}
JSFiddle

Related

How can I make a text appear strictly on the bottom of an image aligned with the left side borer of the above Image-regardless of parent alignment?

I am trying to implement a view in HTML/CSS/Bootstrap where There is an image and some text immediately below the image BUT STRICTLY THE TEXT MUST START AT THE LEFT-SIDE BORDER OF THE IMAGE REGARDLESS OF THE LENGTH OF THE TEXT!
here is the required view:
Problem is If I apply text-alignment:center; to the divs of the bootstrap columns (I am using bootstrap grids here) the picture goes to the center of the div but the alignment of the texts below the pic also come centrally below the picture but as I stated earlier I wanted to align the text to the bottom left no-matter what parent css is!
Here is what I tried(I am including only one column of the bootstrap grid used as others also contain similar information):
<div class="container main-div">
<div class="row">
<div class="col-auto col-md-3 col-xs-6 col-sm-4">
<div class="card-d">
<img src="pics/tea.jpg" alt="tea">
<div class="desc">
<span><h4>Tea | 1 Kg</h4></span>
<p>The price is : 56.0</p>
<button>View</button>
</div>
</div>
</div>
CSS:
.main-div{
width:90%;
background-color:white;
padding:5% 1%;
text-align:center;
margin-left:auto;
}
.col-auto{
position:relative;
border:1px solid blue;
margin:10px auto;
padding-left:6%;
padding-bottom:4%;
}
.col-auto > .desc{
width:100%;
justify-content:left;
font-family:Arial;
text-align:left;
}
.col-auto img{
width:140px;
height:100px;
padding:5px;
display:inline-block;
margin:0 auto;
border: 1px solid #088837;
}
button{
background-color:green;
color:white;
font-size:1em;
border:0.1px;
border-radius:3px;
padding: 5px 10px;
}
After repeated try its the same I am getting:
Here is my Fiddle:
https://jsfiddle.net/vrw7ph13/
The problem is that in your css you have been targeting the direct children, when you write > it takes the direct children, but your .desc is not direct child of col-auto.
check my modified version of jsfidle https://jsfiddle.net/752bkhj9/3/
You have written
.col-auto > .desc
but you don't have direct child .desc in your .col-auto, check your html
And the text-align rule is inheritable, it inherits the styles from parent, you thought you were overwriting this rule using col-auto > .desc, but using > you were targeting non existing element.
Yes, the problem is that there is no .col-auto > .desc in your html. Looking at your html you have .col-auto > .card-d > .desc
So instead of:
.col-auto > .desc {
width: 100%;
position: relative;
left: 0%;
font-family: Arial;
text-align: left;
}
you could try:
.col-auto .desc {
width: 140px; /* this is the width of your '.col-auto img' selector set earlier */
position: relative;
left: 0%;
font-family: Arial;
text-align: left;
margin: auto; /* use this to auto-center the div, but since it is the same width */
/* as your img, it will auto-align itself with the left edge of the img */
}
or if you want to keep parent > child relationship:
.col-auto > .card-d > .desc {
width: 140px; /* this is the width of your '.col-auto img' selector set earlier */
position: relative;
left: 0%;
font-family: Arial;
text-align: left;
margin: auto; /* use this to auto-center the div, but since it is the same width */
/* as your img, it will auto-align itself with the left edge of the img */
}

HTML Header With Image And Text - Align Text To Bottom?

I am writing an HTML page with CSS. At the top of my page I want to show a header with an image and text (image to the left of the text). The image size is 64 x 64 pixels and I want the text to be large.
I was able to do almost everything except I want to align the text at the bottom but, no matter what I do, I can't seem to get the text to stop placing itself at the top.
Here is the HTML for my header:
<div id="container" class="container">
<div class="header">
<div class="header image"></div>
<div class="header text">Header Text</div>
</div>
</div>
and here is the CSS;
.container .header {
height: 65px;
border:2px solid red;
}
.container .header .image {
background: url("../images/icon64.png") no-repeat;
float: left;
display: inline-block;
width: 65px;
border:2px solid green;
}
.container .header .text {
float: left;
display: inline-block;
vertical-align: bottom;
font-family: sans-serif;
font-size: x-large;
border:2px solid blue;
}
I have been reading several web pages after searching for how to do this. I found one page that seemed pretty straight forward. They said you have to use inline-block for the display property in order for vertical-align to be honored.
I changed my CSS to what you see above but that still did not work. Here is what my header looks like:
(Note the border coloring is just for visualizing what's going on.)
Can anyone tell me what I am doing wrong and how to fix it so that my text is vertically aligned at the bottom?
Thank you.
That is correct, set elements as inline-blocks and use vertical-align. However, that means not to float the elements! Floated elements are floats and you negate the display: inline-block declaration: http://jsfiddle.net/qQtG9/2/ (I've cleaned your code some).
HTML:
<div class="header">
<div class="image"></div><div class="text">Header Text</div>
</div>
CSS:
.header {
border:2px solid red;
}
.header .image {
background: url("http://placehold.it/64x64")
no-repeat;
width: 65px;
height: 65px;
border:2px solid green;
}
.header .text {
font: x-large sans-serif;
border:2px solid blue;
}
.header .image,
.header .text {
display: inline-block;
vertical-align: bottom;
}
You can also try giving the #header a position:relative
and then give the .text position absolute, so if you give bottom:0; it will be stack to the bottom of the #header div

How equal separate 3 div inside 1 div

I'm currently learning HTML. I'm trying to add 3 images inside a div, the images need to have the same amount of space between them. How to do this?
Example: https://docs.google.com/drawings/d/1WZdL0WVz-VndX2qP0Ig0S8fZnCGW2k37RHvWXLdgWz0/edit?usp=sharing
The code I currently have:
<style type="text/css">
.maindiv{
position: relative;
width:90%;
height:50%;
border-style:solid;
border-color:Red;
border-width:2px;
}
.imgbott{
height:auto;
width:auto;
max-width:200px;
max-height:200px;
float:left;
text-align: center;
}
</style>
<body>
<div class="maindiv">
<div class="imgbott">
<img src="https://sites.google.com/a/itcld.com.br/portal-de-treinadores/_/rsrc/1377018552616/imagens/images.jpg" alt="">
<a>TESTE</a>
</div>
<div class="imgbott">
<img src="https://sites.google.com/a/itcld.com.br/portal-de-treinadores/_/rsrc/1377018552616/imagens/images.jpg" alt="">
<a>TESTE</a>
</div>
<div class="imgbott">
<img src="https://sites.google.com/a/itcld.com.br/portal-de-treinadores/_/rsrc/1377018552616/imagens/images.jpg" alt="">
<a>TESTE</a>
</div>
</div>
</body>
Code runing: https://script.google.com/a/macros/itcld.com.br/s/AKfycbyjeAIFhKnAXzvXd8lS3S-ND4H0n63i-FBxr-i9Z1omeFmBYtA/exec
Thank you.
Change your css to:
.imgbott{
margin: 0px 10px;
height:auto;
width:auto;
max-width:200px;
max-height:200px;
float:left;
text-align: center;
}
The margin: 0px 10px means 0px margin to the top and bottom, and 10px margin to the left and right. Maybe one would expect 20px margin between the divs then, but there is a effect called "margin collapsing" which prevents that.
is this what you looking for
http://jsfiddle.net/Gfnjz/
.box {
display:table;
table-layout:fixed;
min-width:900px; /* some minimum width is a good idea. */
border-spacing:20px 0; /* note that spacing is also applied to right and left ends */
background-color:#666;
margin:0 auto;
}
.box div {
display:table-cell;
width:33%;
vertical-align:middle;
border:1px solid #bbb;
background-color:#eee;
padding:30px;
}
You can do something like this:
.divName{
width:300px;
display: inline-block;
margin: 0 20px 0 0;
float: left;
}
Then for the last box, apply a .lastBox class as well to force no margin, that way they are perfectly centered, assuming your parent container is centered that is:
.lastBox{
margin-right: 0;
}
The HTML:
<div class="divName">
<p>stuff</p>
</div>
<div class="divName">
<p>stuff</p>
</div>
<div class="divName lastBox">
<p>stuff</p>
</div>
if you only want the same space between the "imgbott" divs, set their margin instead of width attribute.
Your class will looks like
.imgbott{
margin: 0px 10px;
float: left;
text-align: center;
}
.imgbott a
{
display:block;
}
Then doesn't matter what is the width of the images inside, the space will always be 20px between the images.
In additional you can remove the margin-left of the first image using the first-child selector
.imgbott:first-child {
margin-left:0px;
}
You can achieve this result by using inline-blocks and text-align: justify, with adding some fake content before and after the divs to be aligned via pseudo-elements:
.maindiv{
width:90%;
border: 2px solid red;
text-align: justify; /* turns on justification 'magic' */
line-height: 0; /* removes extra space below divs because of extra line */
}
.maindiv:before {
font-size: .1px;
content: 'i'; /* adds nearly invisible fake content in the beginning of the line */
}
.maindiv:after {
font-size: .1px;
content: 'i i'; /* adds nearly invisible fake content in the of the line */
word-spacing: 99in; /* huge word-spacing assures that the 2nd 'i' wraps to the next line making 'justify' work */
background: #ccc;
}
.imgbott{
display: inline-block;
line-height: 1; /* restore the normal line height inside divs */
}
JSFiddle
Optionally, you can prohibit the wrapping of the divs if the container is narrower than the sum of their widths by adding white-space: nowrap to the container and normal to its :after: see edited JSFiddle
This solution may look a bit tricky, but it works for arbitrary number of blocks of arbitrary (possibly different) widths.

Inline-block elements expanding space below

Creating a page layout using inline-block elements (vertically aligned to the top). The only issue, is that inline-block elements below another set of inline block elements will not fold into open space like floated elements do. It's almost as if it obeys row-like rules. Are there any fixes for this?
Layout example in JSFiddle
CSS
* {
font-family:helvetica;
font-size:18px;
}
.container {
margin:0 auto;
width:90vp;
}
.main_content {
background:red;
display:inline-block;
vertical-align:top;
box-sizing:border-box;
width:76.04%;
min-height:200px;
}
.content_details {
background:blue;
display:inline-block;
vertical-align:top;
box-sizing:border-box;
width:22.39%;
margin-left:01.56%;
min-height:250px;
}
.comments {
background:green;
display:inline-block;
vertical-align:top;
box-sizing:border-box;
width:76.04%;
min-height:150px;
}
HTML
<div class="container">
<div class="main_content">
<h1>Main Content</h1>
</div
><div class="content_details">
<h2>Details</h2>
</div
><div class="comments">
<h2>Comments</h2>
</div>
</div>
Please note I can change the mark-up to create only two inline-block elements (creating two columns), however I would like to know if there is a fix for 3 separate inline-block elements (like in the JSFiddle example), that way I wouldn't need to add extra mark-up.
No there isn't.. Not like you are talking about. You'd have to use:
<div id="col1">
<div id="maincontent"></div>
<div id="comments"></div>
</div>
<div id="details"></div>
Then you would have #col1 and #details as inline-block elements.
The whole point of an inline-block is that it is inline (i.e. on a line with other elements) it isn't acting like a table as you suggested, it's acting like a line of text (as it should) that is wider than it's container and breaking to the next line down.
See here: http://jsfiddle.net/GXmM6/ for a working example
Neither floats nor inline-block will do what you want there, unless you wrap each column in its own div. Short of that, there are JavaScript solutions for doing this, such as Masonry. (It involves a lot of positioning, though.)
Did I get it right that you wanted the .content_details to be a sidebar? Then I just changed it from display: inline-block to float: right to place .comments seamlessly beneath your .main-content. See http://jsfiddle.net/koivo/7UqqF/ for working example. Think that even works just with display: block ...
* {
font-family: helvetica;
color: white; /* added */
font-size: 18px;
}
.container {
margin: 0 auto;
width: 90vp;
}
.main_content {
background: red;
display: inline-block;
vertical-align: top;
box-sizing: border-box;
width: 76.04%;
min-height: 200px;
}
.content_details {
background: blue;
/* display: inline-block; */
float: right; /* added */
vertical-align: top;
box-sizing: border-box;
width: 22.39%;
margin-left: 01.56%;
min-height: 250px;
}
.comments {
background: green;
display: inline-block;
vertical-align: top;
box-sizing: border-box;
width: 76.04%;
min-height: 150px;
}

Vertical align multiple lines of text to the middle

I need to align multiple lines of text to the middle. Here is a rough guide of the markup I am working with.
<ul>
<li>
<a href='#'>This should be centered.</a>
<li>
</ul>
So as you can see from my image, the "work" link should be centered vertically. I have the width and height set with vertical-align: middle;. I know you need to set the line height for it to actually work but theres the problem. If I set the line height to 72px (the height of the element) then some of the links will stretch down the page due to them taking up two lines.
Is there a way of aligning multiple lines of text to the middle without using line-height?
Use display:table-cell; in your li element.
li {
width:200px;
height:200px;
vertical-align:middle;
display:table-cell;
}
This will give you this effect:
write like this
a{
display:inline-block;
vertical-align:middle;
}
& you can give display:table-cell; to it like this
li {
vertical-align:middle;
display:table-cell;
}
but it's not work in IE7 & below
I came up with this to handle vertically-aligned 100% height/width anchors inside containers:
http://jsfiddle.net/khaustic/KDfN6/
markup:
<div class="links one">
One
</div>
<div class="links two">
Two Two
</div>
css:
* {
/*ie box model forever!*/
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
.links {
height: 5.0em;
text-align:center;
outline: 1px solid #333;
float:left;
margin: 0 1.0em;
overflow:hidden;
}
.links.one { width: 8em; }
.links.two { width: 4em; }
.links a {
width:10em;
text-align: center;
display: table-cell;
vertical-align:middle;
height: inherit;
}
You can try to change display to block for hyperlink and use paddings:
li a {display: block; padding: 30px 10px 30px 10px}