This question already has answers here:
Why does my image have space underneath?
(3 answers)
Closed 7 years ago.
It isn't a table, I just used "row" for lack of a better word.
My problem is that I've got a set of display:inline-block elements and an element under those, but there's a gap between them. Note that I'm not talking about the space between the inline elements, I'm talking about the space between the row of inline elements and the other element. I've tried the solution mentioned here, setting a line-height and font-size, but that didn't help at all (which is why it isn't in the code below)
Here's my code:
HTML
<div id="letterhead">
<div class="name"></div><div class="logo"></div><div class="name"></div>
<div class="subtext"></div>
</div>
CSS
body{
background:#eee;
width: 100%;
}
#letterhead {
position: absolute;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
height: 12rem;
font: 32px Tahoma;
color: #777;
padding: 2rem;
}
.logo {
display: inline-block;
width: 7rem;
height: 7rem;
background: #000;
}
.name {
display: inline-block;
height: 7rem;
width: calc((100% - 7rem) / 2);
background: #fff;
}
.subtext {
//position: absolute;
margin: 0;
padding: 0;
top: 9rem;
text-align: center;
width: 100%;
height: 1rem;
background: #555;
}
I made a Fiddle here.
Thanks in advance.
But it is because of your font-size and line-height. Set them to 0 on #letterhead and the unwanted white-space will dissapear. You wil have to set your font-size and line-height back on the children of #letterhead if you want to put content in them.
example:
http://jsfiddle.net/skeurentjes/69MkQ/1/
Just add margin:0 -4px 0 0 to your inline-block divs. And everything will be good. To remove space between rows you must set vertical:align:top. Check the DEMO
CSS
.logo {
...
display: inline-block;
margin:0 -4px 0 0;
vertical:align:top
...
}
.name {
...
display: inline-block;
margin:0 -4px 0 0;
vertical:align:top
...
}
Related
This very simple html structure
<div id="s1container"><!--
--><div class="s1box"></div><!--
--></div>
which has been commented to try to delete white spaces between the elements, with the following css still produces an unwanted padding in the bottom between both elements
#s1container {
background: gray;
text-align: center;
}
.s1box {
position: relative;
display: inline-block;
width: 200px;
height: 200px;
background: #ccc;
padding: 0;
margin: 0;
}
As you can see from this fiddle
https://jsfiddle.net/um1L4c0t/
The only way to ameliorate this problem, is to set the font-size to 0em on the parent and set it back to 1em to the children, which is not a solution considering i'm extensively using em units for the boxes dimensions, the nesting property of ems would make that kind of layout impossible if the parent's font-size is set to 0em
Why is the white space still present even after commenting out the white spaces between those elements?
The comments take out the right white space, vertical-align: top; get rid of the bottom.
* {
padding: 0;
margin: 0;
}
#s1container {
background: gray;
text-align: center;
}
.s1box {
position: relative;
display: inline-block;
width: 200px;
height: 200px;
background: #ccc;
padding: 0;
margin: 0;
vertical-align: top;
}
<div id="s1container"><!--
--><div class="s1box"></div><!--
--></div>
A more modern approach is to use flexbox, where you get the good from both worlds
* {
padding: 0;
margin: 0;
}
#s1container {
background: gray;
text-align: center;
display: flex;
}
.s1box {
width: 200px;
height: 200px;
background: #ccc;
padding: 0;
margin: 0 auto;
}
<div id="s1container">
<div class="s1box"></div>
</div>
Remove display: inline-block; and use margin: 0 auto;
In the following code:
<a href="*">
<h3>My really really long header</h3>
<img src="thumbnail.png" width="150" height="100" />
</a>
The h3 overflows its size. If I set the overflow to hidden, an extra margin is added to the bottom of the h3. If I change the header to short one, the extra bottom margin does not appear.
After searching SO I found something about collapsing margins. But the point is there is no margin applied to img nor to h3.
Here is the CSS:
h3 {
width: 300px;
height: 40px;
line-height: 40px;
padding: 0 0 0 10px;
margin: 0;
display: inline-block;
width: 150px;
background-color: #0f0;
}
a {
position: relative;
display: block;
margin-bottom: 20px;
padding: 4px;
border: 1px solid #000;
width: 160px;
height: auto;
}
img {
background-color: #00f;
}
Fiddle
If I set the font-size of tag a to zero, the extra margin doesn't show up.
Question: Is there a proper way (not setting font-size: 0) to solve this issue?
You have your h3 set to inline-block. By default, inline-block respects line-height and font-size. Change your h3 to display block.
css:
h3 {
width: 300px;
height: 40px;
line-height: 40px;
padding: 0 0 0 10px;
margin: 0;
display: block;
width: 150px;
background-color: #0f0;
overflow:hidden;
}
Remove display: inline-block from the h3, or add vertical-align:bottom.
That you are making the h3 an inline-block element is causing this, because as such it gets laid out in the line box in a way that leaves space for the descenders of characters of (potential) text content on that same line.
There are two issues that I'm trying to fix, but can't find a solution
The first one is that the space between the li fields is way wider than the 2px than it should be. How do I remove it?
And other is that the a fields are only as high as the text, although the field height is defined to be 50px.
I also have the normalize.css file enabled from GitHub.
Any suggestions?
HTML
<nav class="nav-box">
<div class="row">
<ul class="main-nav">
<li>YES</li>
<li>NO</li>
</ul>
</div>
</nav>
CSS
.row {
max-width: 1140px;
margin: 0 auto;
}
.nav-box {
position: fixed;
top: 0;
left: 0;
width: 100%;
box-shadow: 0 2px 2px #f2f2f2;
min-height: 65px;
}
.main-nav {
float: right;
margin-top: 7px;
}
.main-nav li {
list-style: none;
display: inline-block;
font-size: 100%;
}
.main-nav li a {
height: 50px;
background-color: #ee4723;
padding: 0 18px 0 18px;
font-size: 1.4rem;
color: #fff;
font-family:'Oswald', sans-serif;
border:solid #fff;
border-width: 0 1px 1px 0;
line-height: 54px;
}
Here's a fiddle.
For the spacing issue
This is an issue with inline-block elements (extra spacing appears between two such elements). One way to solve this is to give the parent element (in this case <ul>) font-size of 0 and then setting the font-size of the <li> element explicitly. There are other ways like negative margin but I find font-size: 0 method to be the most convenient.
You can read about other methods here https://css-tricks.com/fighting-the-space-between-inline-block-elements/
For the height issue
While you have given the inline-block property to the <li> elements, the child <a> elements are still inline. Attributes such as height and width would have no effect on inline elements. Add display: inline-block to the <a> element as well for the desired effect
You can use negative margins:
.main-nav li {
margin: 0 -2px;
}
.main-nav li a {
display: inline-block;
}
I have a problem where the number of line rows affects a box position. The more rows I have, the worse is it displaying.
The HTML I'm working with
<div class="block">
<img alt="*" src="http://placekitten.com/200/300" alt="*" />
<h2>Nameasdasd asd asdf wsd fgW</h2>
</div>
And the CSS
.block
{
width: 120px;
height: 150px;
display: inline-block;
border-radius: 10px;
margin: 13px 13px 0 0;
text-align: center;
padding: 5px;
position: relative;
behavior: url(/Media/PIE.htc);
cursor: pointer;
background: #f00;
}
.block img { height: 70px; width: 70px; display: block; margin: 0 auto 10px auto; }
.block h2 { font-size: 12px; padding: 0; margin: 0; font-family: 'Arial'; }
Fiddle: http://jsfiddle.net/3GVTK/2/
I've never encountered this problem before.
Edit: Thanks for all the response and solutions! All of them are valid and works (as of now).
I'm going to stick with Blazemongers solution as it doesn't have any side-effects for the current layout.
Add vertical-align: top; to the css for your .block element.
http://jsfiddle.net/mblase75/tsRKG/
If you look at your fiddle's blocks in a wide screen, you may notice that they're aligned to the last line of text. This is what baseline means, which is the default vertical alignment.
Simply add following property to .block css class
float: left;
Check here http://jsfiddle.net/3GVTK/4/
remove display:inline-block; and add float:left;
fiddle
just add display:inline-table to block h2 in your css
like
.block h2 { font-size: 12px; padding: 0; margin: 0; font-family: 'Arial'; display:inline-table }
you'll need to make sure, that the kitten's name is not overly long
After I did some changes, my feedback div no longer centers on screen and I can't figure out why.
To center a element one only have to set the width and then just do margin: 0 auto; That should normally be enough.
The goal is to have the div shown at the top of the screen, centered. You can see my fiddel here:
http://jsfiddle.net/3u3fd/
Code:
#feedback {
position: fixed;
top: 0px;
min-height: 50px;
width: 300px;
margin: 10px auto;
z-index: 9000;
font-weight: bold;
line-height: 24px;
border: solid 1px #d1d2d1;
padding: 10px;
background-color: #f7f2e7;
display: none;
border-radius: 5px;
-moz-border-radius: 5px; /* FF < 4.0 */
-webkit-border-radius: 5px; /* Rounded corners for Safari */
}
#feedback span { display: block; float: left;}
#feedback #feedback_icon { width: 24px; height: 24px; overflow: hidden; margin-right: 10px; }
#feedback #feedback_text { height: 24px; line-height: 24px; display: inline-block; }
<div class="clearfix" id="feedback" style="display: block;"><span class="dialogFail" id="feedback_icon"></span><div class="" id="feedback_text">Message here</div></div>
Any help appreciated!
auto margins do not work on elements with position: fixed.
Instead, you need to do this:
left: 50%;
margin-left: -Xpx;
width: Ypx;
box-sizing: border-box;
Where X = Y/2.
(The box-sizing: border-box ensures that even if you have padding or borders, it will still be centred. If that interferes with the desired width, then remove it and subtract the value of padding-left + border-left-width from the margin-left.)
You have a fixed position set. Get rid of it and it will center just fine.
In order for margin: 0 auto; to work, the parent element must have a specified width. It can be percentage or units, but it must have it.
For this solution to work in this case, you need to remove the position: fixed; and top declaraions and add a wrapping element.
http://jsfiddle.net/3u3fd/16/