HTML/CSS White spaces still present even after commenting new lines - html

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;

Related

Why is the height of <form> more than the height of the <textarea> inside it?

Minimal example for my question:
console.log(document.getElementById('form').offsetHeight)
console.log(document.getElementById('textarea').offsetHeight)
form {
background: orange;
}
textarea {
background: lightblue;
border: 0;
box-sizing: border-box;
height: 100px;
}
<form id="form"><textarea id="textarea"></textarea></form>
The <textarea> has a height of 100px but <form> has a height of 4px? Where is the extra 4px of height for the <textarea> coming from? How can I eliminate the extra height?
I think it's because the textarea's display property is initially set as inline-block (see the Styling with CSS section). This stack overflow post's accepted answer provides a nice explanation of why it's displaying the additional "height" (that you're experiencing). According to the post, the following solutions seem to remedy the issue:
set the display property to block or
set the vertical-align property to top
With display: block:
form {
background: orange;
padding: 0;
}
textarea {
padding: 0;
margin: 0;
background: lightblue;
height: 100px;
display: block;
}
<form id="form"><textarea id="textarea"></textarea></form>
With vertical-align: top:
form {
background: orange;
padding: 0;
}
textarea {
padding: 0;
margin: 0;
background: lightblue;
height: 100px;
vertical-align: top;
}
<form id="form"><textarea id="textarea"></textarea></form>
A <textarea> is inherently a display-block (essentially adding a 'space').
Try adding display: block
form {
background: orange;
padding: 0;
}
textarea {
padding: 0;
margin: 0;
display: block;
background: lightblue;
height: 100px;
}
<form id="form"><textarea id="textarea"></textarea></form>

Css max-width not working as expected in div

I have an HTML structure like:
.container {
width: 100%;
height: 300px;
background-color: blue;
}
.dots-container-wrapper {
width: 100%;
}
.dots-container {
max-width: 55px;
overflow: hidden;
min-width: 1px;
display: block;
padding: 0;
margin: 0 auto;
height: 0.875rem;
position: relative;
}
.dots-container>ul {
padding: 0;
display: flex !important;
transition: all 0.25s;
position: relative;
margin: 0;
list-style: none;
transform: translateX(0);
align-items: center;
bottom: unset;
height: 100%;
}
.dots-container>ul li {
width: 6px;
height: 6px;
margin: 0 2.5px;
background-color: #fff;
border: none;
border-radius: 50%;
display: inline-block;
opacity: .7;
}
<div class="container">
<div class="dots-container-wrapper">
<div class="dots-container">
<ul class="dots">
<li></li>
<li></li>
</ul>
</div>
</div>
</div>
The problem is that the div "dots-container" has a property max-width: 55px. But in case the width is less than 55px, I would like to use the real width, however, the div is always 55px. This is a problem because I´m using this in a carousel with dots functionality. When there are 5 pictures, you can see 5 dots aligned in the center, but in case there are fewer pictures, let´s say 2, the div is still 55px and the dots don´t seem to be aligned in the center. See example screenshots.
Your .dots-container is displayed as a block. By default a block will always try to fill up the entire width. By making the container .dots-container-wrapper display flex, it's children will only take up as much space as they need (while also centering them if needed).
.dots-container-wrapper {
width: 100%;
display: flex; // change to flex
}
.dots-container {
max-width: 55px;
overflow: hidden;
min-width: 1px;
display: block;
padding: 0;
margin: 0 auto;
height: 0.875rem;
position: relative;
}

CSS vertical align of inline-block not working

HTML:
<div id='inner'>I am sam <em>I am em</em> I am sam</div>
CSS:
#inner {
width: 400px;
height: 400px;
background-color: cyan;
}
em {
margin: 1em;
padding: 1em;
display: inline-block;
vertical-align: top;
}
Result: https://jsfiddle.net/cbukkt3m/1/
Why does it throw the surrounding text to the top and not the em itself?
It is aligned to the top, it's being pushed down by:
margin: 1em;
padding: 1em;
try removing these or changing them to
margin: 0 1em;
padding: 0 1em;

Two elements - Fixed and flexible width (100% - 170px)

At the top level of my website layout are 4 div tags.
The first one is a full width header section, with css:
#header {
margin-top: 0px;
height: 70px;
border: 4px double rgb(255,255,255);
border-radius: 20px;
background: rgb(88,150,183) no-repeat fixed left top;
padding: 0px;
}
At the bottom is a full width footer:
#footer {
clear: both;
margin: 0px;
color:#cdcdcd;
padding: 10px;
text-align: center;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
On the left is my main menu section:
#categories {
float:left;
width:150px;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
All of those 3 elements work fine. They're in the right place and that doesn't change whatever screen resolution the user has on their monitor, or whether they view it on not maximum screen size.
My problem is with the main element of the page - where all the interesting stuff is. It's directly to the right of the menu div - or rather, it should be. My css is:
#main {
float:right;
min-height: 440px;
width: 80%;
margin-bottom: 20px;
padding:20px;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
width 80% works OK for most of my users, but for those with less resolution, the main element shifts below the menu, which is ghastly.
What I would ideally like is for the width set in the css #main to be something like (100% - 170px), thus leaving a nice margin between the menu and the main bit at all times and never pushing it below the menu. However, css standards don't fulfil that desire yet!
Could someone suggest how I amend my css to give me a nice clean page that's clean for all my users? Or do I need to go back to setting out my page using tables?
Using CSS3 flex
* { box-sizing: border-box; margin: 0; }
#parent{
display: flex;
}
#aside{
width: 170px; /* You, be fixed to 170 */
background: #1CEA6E;
padding: 24px;
}
#main{
flex: 1; /* You... fill the remaining space */
background: #C0FFEE;
padding: 24px;
}
<div id="parent">
<div id="aside">Aside</div>
<div id="main">Main</div>
</div>
Using CSS3 calc
width: calc(100% - 170px);
Example:
* { box-sizing: border-box; margin: 0; }
#aside {
background: #1CEA6E;
width: 170px;
float: left;
padding: 24px;
}
#main {
background: #C0FFEE;
width: calc(100% - 170px);
float: left;
padding: 24px;
}
<div id="aside">Aside</div>
<div id="main">Main</div>
Using float: left; and overflow
* { box-sizing: border-box; margin: 0; }
#aside{
width: 170px; /* You, be fixed to 170 */
float: left; /* and floated to the left */
padding: 24px;
background: #1CEA6E;
}
#main {
background: #C0FFEE;
padding: 24px;
overflow: auto; /* don't collapse spaces */
/* or you could use a .clearfix class (Google for it) */
}
<div id="aside">Aside</div>
<div id="main">Main</div>
Using style display: table;
* { box-sizing: border-box; margin: 0; }
#parent{
display: table;
border-collapse: collapse;
width: 100%;
}
#parent > div {
display: table-cell;
}
#aside{
width: 170px; /* You, be fixed to 170 */
background: #1CEA6E;
padding: 24px;
}
#main{
background: #C0FFEE;
padding: 24px;
}
<div id="parent">
<div id="aside">Aside</div>
<div id="main">Main</div>
</div>
Is this what you are looking for? You don't need any css3
Dont need any css3
.wrapper {
width: 800px;
height: 800px;
background-color: blue;
}
.content {
width: auto;
height: 100%;
background-color: yellow;
}
.menu {
width: 170px;
height: 100%;
float: left;
background-color: red;
}
<div class="wrapper">
<div class="menu">Menu</div>
<div class="content">
Aside
</div>
</div>
You can use 'calc' function supported by all modern browsers and IE9+, or switch to flexbox (supported by IE11+)
See this pen: https://codepen.io/neutrico/pen/MyXmxa
width: calc(100% - 170px);
Keep in mind that all borders matter unless you set 'box-sizing' to 'border-box' (or just remove these borders and apply them on child elements).

Unwanted space between row of inline-block elements and another row [duplicate]

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
...
}