CSS position relative forcing the height of element - html

I have two (almost) clone elements (#container and #container-shadow). The same css rules are supposed to be applied equally to them. However, the second .box3 div element is four times the height it should be. Why is that?
codepen -> http://codepen.io/thiagoh/pen/aJwbOZ
CSS code
#container {
position: relative;
top: -90px;
left: 400px;
float: left;
}
#container-shadow div,
#container div {
width: 280px;
box-sizing: border-box;
position: relative;
}
.box3 {
background-color: lightgray;
line-height: 24px;
padding: 4px;
border: 1px solid black;
}
.box4 {
background-color: darkgray;
border: 1px solid black;
border-top: 0px;
padding: 0;
left: 10px;
}
.box4 ul {
padding: 0;
margin: 0;
}
.box4 li {
list-style: none;
line-height: 24px;
padding: 4px;
border-bottom: 1px solid lightgray;
}
HTML code
<div style="height: 100px;"></div>
<div id="container">
<div class="box3">
</div>
<div class="box4">
<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
<li>line 4</li>
</ul>
</div>
</div>
<div id="container-shadow">
<div class="box3">
why this element is this height?
</div>
<div class="box4">
<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
<li>line 4</li>
</ul>
</div>
</div>
current result
expected result
PS: Note the position of the boxes. That's the required position in my situation. clear:both on #container-shadow does not fix my problem.

You should not use the float:left on the #container, it should look like this:
#container {
position: relative;
top: -90px;
left: 400px;
}
here is an updated codepen:Codepen

This is because you have a float: left on your #container which is not cleared - add this after the #container:
<div style="clear:both"></div>
to clear the float - see demo below:
#container {
position: relative;
top: -90px;
left: 400px;
float: left;
}
#container-shadow div,
#container div {
width: 280px;
box-sizing: border-box;
position: relative;
}
.box3 {
background-color: lightgray;
line-height: 24px;
padding: 4px;
border: 1px solid black;
}
.box4 {
background-color: darkgray;
border: 1px solid black;
border-top: 0px;
padding: 0;
left: 10px;
}
.box4 ul {
padding: 0;
margin: 0;
}
.box4 li {
list-style: none;
line-height: 24px;
padding: 4px;
border-bottom: 1px solid lightgray;
}
<div style="height: 100px;"></div>
<div id="container">
<div class="box3">
</div>
<div class="box4">
<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
<li>line 4</li>
</ul>
</div>
</div>
<div style="clear:both"></div><!-- ADDED THIS -->
<div id="container-shadow">
<div class="box3">
why this element is this height?
</div>
<div class="box4">
<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
<li>line 4</li>
</ul>
</div>
</div>
EDIT:
If you don't want the #container-shadow to drop down, you can relatively position it and float it to left using something like this:
#container-shadow {
float: left;
left: -275px;
position: relative;
}
See demo below:
#container {
position: relative;
top: -90px;
left: 400px;
float: left;
}
#container-shadow div,
#container div {
width: 280px;
box-sizing: border-box;
position: relative;
}
.box3 {
background-color: lightgray;
line-height: 24px;
padding: 4px;
border: 1px solid black;
}
.box4 {
background-color: darkgray;
border: 1px solid black;
border-top: 0px;
padding: 0;
left: 10px;
}
.box4 ul {
padding: 0;
margin: 0;
}
.box4 li {
list-style: none;
line-height: 24px;
padding: 4px;
border-bottom: 1px solid lightgray;
}
#container-shadow {
float: left;
left: -275px;
position: relative;
}
<div style="height: 100px;"></div>
<div id="container">
<div class="box3">
</div>
<div class="box4">
<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
<li>line 4</li>
</ul>
</div>
</div>
<div id="container-shadow">
<div class="box3">
why this element is this height?
</div>
<div class="box4">
<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
<li>line 4</li>
</ul>
</div>
</div>
I would suggest removing float and use inline-block for #container and #container-shadow (note that I have swapped their position in the markup) and then apply positioning to the #container to get the desired result:
#container {
position: relative;
top: -90px;
left: 50px;
display: inline-block;
}
#container-shadow {
display: inline-block;
}
#container-shadow div,
#container div {
width: 280px;
box-sizing: border-box;
position: relative;
}
.box3 {
background-color: lightgray;
line-height: 24px;
padding: 4px;
border: 1px solid black;
}
.box4 {
background-color: darkgray;
border: 1px solid black;
border-top: 0px;
padding: 0;
left: 10px;
}
.box4 ul {
padding: 0;
margin: 0;
}
.box4 li {
list-style: none;
line-height: 24px;
padding: 4px;
border-bottom: 1px solid lightgray;
}
<div style="height: 100px;"></div>
<div id="container-shadow">
<div class="box3">
why this element is this height?
</div>
<div class="box4">
<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
<li>line 4</li>
</ul>
</div>
</div>
<div id="container">
<div class="box3">
</div>
<div class="box4">
<ul>
<li>line 1</li>
<li>line 2</li>
<li>line 3</li>
<li>line 4</li>
</ul>
</div>
</div>

The solution is simple.
Remove this:
float: left;

An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.
The float: left attempts to force container-shadow to occupy this space causing distortion.
It's your same code just comment the float:
#container {
position: relative;
top: -90px;
left: 400px;
/* float: left;*/
}
#container-shadow div,
#container div {
width: 280px;
box-sizing: border-box;
position: relative;
}
.box3 {
background-color: lightgray;
line-height: 24px;
padding: 4px;
border: 1px solid black;
}
.box4 {
background-color: darkgray;
border: 1px solid black;
border-top: 0px;
padding: 0;
left: 10px;
}
.box4 ul {
padding: 0;
margin: 0;
}
.box4 li {
list-style: none;
line-height: 24px;
padding: 4px;
border-bottom: 1px solid lightgray;
}

Related

Overflow defeats negative margin to hide border

I am designing a simple tab bar with a content panel.
To hide the bottom border for the current active tab, I use a negative margin-bottom and a border-bottom to hide the border for that tab.
* {
box-sizing: border-box;
}
#container {
width: 500px;
height: 200px;
border: 1px solid black;
background-color: lightgray;
}
#wrapper {
}
ul {
display: flex;
margin: 0;
padding: 2px 2px 0 2px;
border-bottom: 1px solid black;
}
li {
padding: 10px;
margin: 0 2px -1px 0;
list-style: none;
border-top: 1px solid black;
border-left: 1px solid black;
border-right: 1px solid black;
border-bottom: 0;
}
li.active {
border-bottom: 2px solid lightgray;
}
#content {
padding: 10px;
}
<div id="container">
<div id="wrapper">
<ul>
<li class="active">Tab 1 (active)</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
<div id="content">
My content
</div>
</div>
It works perfect.
The problem comes when I want to add more tabs, so it overflows the container. The solution that comes to my mind is using overflow-x: auto;. The problem is that this also adds a vertical scrollbar, and I am not sure why. I could overcome this by using overflow-y: hidden;. But the real problem is that it makes impossible to "delete" the black bottom border of the current active tab anymore.
You can run the snipped below, also you can uncomment the commented html li elements.
* {
box-sizing: border-box;
}
#container {
width: 500px;
height: 200px;
border: 1px solid black;
background-color: lightgray;
}
#wrapper {
}
ul {
display: flex;
margin: 0;
padding: 2px 2px 0 2px;
border-bottom: 1px solid black;
overflow-x: auto;
}
li {
padding: 10px;
margin: 0 2px -1px 0;
list-style: none;
border-top: 1px solid black;
border-left: 1px solid black;
border-right: 1px solid black;
border-bottom: 0;
white-space: nowrap;
}
li.active {
border-bottom: 2px solid lightgray;
}
#content {
padding: 10px;
}
<div id="container">
<div id="wrapper">
<ul>
<li class="active">Tab 1 (active)</li>
<li>Tab 2</li>
<li>Tab 3</li>
<li>Tab 4</li>
<li>Tab 5</li>
<li>Tab 6</li>
<li>Tab 7</li>
<!--<li>Tab 8</li>
<li>Tab 9</li>
<li>Tab 10</li>-->
</ul>
<div id="content">
My content
</div>
</div>
Does anyone has a solution? If not, what would be an alternate way to accomplish this?
What I really want is to keep hiding the bottom border of the active tab, and also have the possibility of showing an horizontal scroll bar when there is horizontal overflow.
Thank you.
To hide the bottom border for the current active tab, I use a negative
margin-bottom and a border-bottom to hide the border for that tab.
Instead, you can use box-shadow: 0 -1px black inset; for <ul> and box-shadow: 1px -1px lightgray inset; for li.active.
Once you have done all of this, you don't need negative margin-bottom anymore.
The problem is that this also adds a vertical scrollbar, and I am not
sure why. I could overcome this by using overflow-y: hidden;.
It adds a vertical scrollbar to ul because you added margin-bottom: -1px; to it's child (li) and it overflows Y axis.
I don't know how to fix horizontal scrollbar but i think you can fix it with a javascript carousel like owlcarousel, slick carousel etc.
Here is the example working:
* {
box-sizing: border-box;
}
#container {
width: 500px;
height: 200px;
border: 1px solid black;
background-color: lightgray;
}
#wrapper {
}
ul {
display: flex;
margin: 0;
padding: 2px 2px 0 2px;
/* border-bottom: 1px solid black; */
box-shadow: 0 -1px black inset;
overflow-x: auto;
}
li {
padding: 10px;
margin: 0 2px 0 0; /* you don't need to use bottom margin anymore. */
list-style: none;
border-top: 1px solid black;
border-left: 1px solid black;
border-right: 1px solid black;
border-bottom: 0;
white-space: nowrap;
}
li.active {
/* border-bottom: 2px solid lightgray; */
box-shadow: 1px -1px lightgray inset;
}
#content {
padding: 10px;
}
<div id="container">
<div id="wrapper">
<ul>
<li class="active">Tab 1 (active)</li>
<li>Tab 2</li>
<li>Tab 3</li>
<li>Tab 4</li>
<li>Tab 5</li>
<li>Tab 6</li>
<li>Tab 7</li>
<!--<li>Tab 8</li>
<li>Tab 9</li>
<li>Tab 10</li>-->
</ul>
</div>
<div id="content">
My content
</div>
</div>
"delete" the black bottom border of the current active tab
You can use border-bottom-color: transparent.
* {
box-sizing: border-box;
}
#container {
width: 500px;
height: 200px;
border: 1px solid black;
background-color: lightgray;
}
#wrapper {
}
ul {
display: flex;
margin: 0;
padding: 2px 2px 0 2px;
}
li {
padding: 10px;
margin: 0 2px 0 0;
list-style: none;
border: 1px solid black;
}
li.active {
border-bottom-color: transparent;
}
#content {
padding: 10px;
}
<div id="container">
<div id="wrapper">
<ul>
<li class="active">Tab 1 (active)</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
<div>
<div id="content">
My content
</div>
</div>
showing an horizontal scroll bar when there is horizontal overflow
I think you forgot to close one of your divs, and your content was inside .wrapper! So just fix that up, and stick on overflow: auto and a max width to make sure it doesn't expand.
* {
box-sizing: border-box;
}
#container {
width: 500px;
height: 200px;
border: 1px solid black;
background-color: lightgray;
}
#wrapper {
overflow-x: auto;
max-width: 100%;
}
ul {
display: flex;
margin: 0;
padding: 2px 2px 0 2px;
}
li {
padding: 10px;
margin: 0 2px 0 0;
list-style: none;
border: 1px solid black;
}
li.active {
border-bottom-color: transparent;
}
#content {
padding: 10px;
}
<div id="container">
<div id="wrapper">
<ul>
<li class="active">Tab 1 (active)</li>
<li>Tab 2</li>
<li>Tab 3</li>
<li>Tab 3</li>
<li>Tab 3</li>
<li>Tab 3</li>
<li>Tab 3</li>
<li>Tab 3</li>
<li>Tab 3</li>
<li>Tab 3</li>
<li>Tab 3</li>
<li>Tab 3</li>
</ul>
</div>
<div id="content">
My content
</div>
</div>

Div with backgroun color within Div with pre-determined width

I feel like I should be able to figure this out but I really can't...
I basically have a div that is contains another set of divs/elements. I want the first div within this container to have a background color to effectively give the parent div a colored top bar/portion. The closest I can get is using display: flex; to give it full height coloring, but I can't get it the way I want. Any help is appreciated.
.container {
border: 1px solid grey;
border-radius: 4px;
padding: 0px 10px 10px 10px;
}
.content {
height: 400px;
}
.sp-h3 {
display: flex;
background-color: #008ed0;
color: white;
}
* {
box-sizing: border-box;
}
.container::before,
.container::after {
content: "";
clear: both;
}
<div class="container">
<div class="sp-h3">
<h3>Bob McBob</h3>
</div>
<div class="content">
<p>Just for something</P>
<ul>
<li>The number 1</li>
<li>The number 2</li>
<li>The number 3</li>
<li>The number 4</li>
</ul>
</div>
</div>
You could set padding to the child elements, instead of setting it to the whole .container.
.container {
border: 1px solid grey;
border-radius: 4px;
}
.content {
height: 400px;
padding: 0px 10px 10px 10px;
}
.sp-h3 {
display: flex;
background-color: #008ed0;
color: white;
padding: 0px 10px 10px 10px;
}
* {
box-sizing: border-box;
}
.container::before,
.container::after {
content: "";
clear: both;
}
<div class="container">
<div class="sp-h3">
<h3>Bob McBob</h3>
</div>
<div class="content">
<p>Just for something</P>
<ul>
<li>The number 1</li>
<li>The number 2</li>
<li>The number 3</li>
<li>The number 4</li>
</ul>
</div>
</div>
You can consider a simple gradient coloration on the container so you won't have the issue related to padding:
.container {
border: 1px solid grey;
border-radius: 4px;
padding: 0px 10px 10px 10px;
background:
linear-gradient(to bottom,#008ed0 60px,transparent 0);
}
.content {
height: 400px;
}
.sp-h3 {
display: flex;
color: white;
}
* {
box-sizing: border-box;
}
.container::before,
.container::after {
content: "";
clear: both;
}
<div class="container">
<div class="sp-h3">
<h3>Bob McBob</h3>
</div>
<div class="content">
<p>Just for something</p>
<ul>
<li>The number 1</li>
<li>The number 2</li>
<li>The number 3</li>
<li>The number 4</li>
</ul>
</div>
</div>

Why do my list items overlap?

I want my list items to be displayed next to each other but for some reason they always overlap. Can someone tell me how to fix this?
#background {
height: 1000px;
background-image: url("https://static.pexels.com/photos/33045/lion-wild-africa-african.jpg");
background-position: center center;
background-size: cover;
}
#menu {
background-color: white;
border: 2px solid grey;
}
ul {
list-style: none;
background-color: white;
}
li {
display: inline-block;
padding: 5px 10px 5px 10px;
border: 2px solid grey;
overflow: none;
position: fixed;
background-color: white;
}
<div id="background">
<div id="menu">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
</div>
Thank you in advance!
You need to remove position: fixed in 'li' element, because if you giving every 'li' element position fixed that will make your item always overlap.
May be you can try update your 'ul' and 'li' element style like this code bellow:
ul {
list-style: none;
position: fixed;
}
li {
display: inline-block;
padding: 5px 10px 5px 10px;
border: 2px solid grey;
overflow: none;
background-color: white;
}
That's because you have define position: fixed for li tags.
#background {
height: 1000px;
background-image: url("https://static.pexels.com/photos/33045/lion-wild-africa-african.jpg");
background-position: center center;
background-size: cover;
}
#menu {
background-color: white;
border: 2px solid grey;
}
ul {
list-style: none;
background-color: white;
}
li {
display: inline;
padding: 5px 10px 5px 10px;
border: 2px solid grey;
overflow: none;
//position: fixed;
background-color: white;
}
<div id="background">
<div id="menu">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
</div>
Take out the position: fixed. This fixes an element within the browser viewport and removes it from the flow. Not what you want.
https://developer.mozilla.org/en-US/docs/Web/CSS/position
fixed
The element is removed from the normal document flow; no space is created for the element in the page layout. Instead, it is positioned relative to the screen's viewport and doesn't move when scrolled. Its final position is determined by the values of top, right, bottom, and left.
give position fixed to ul and you will get list item properly
#background {
height: 1000px;
background-image: url("https://static.pexels.com/photos/33045/lion-wild-africa-african.jpg");
background-position: center center;
background-size: cover;
}
#menu {
background-color: white;
border: 2px solid grey;
}
ul {
list-style: none;
background-color: white;
position: fixed;
padding:5px;
}
li {
display: inline-block;
padding: 5px 10px 5px 10px;
border: 2px solid grey;
overflow: none;
background-color: white;
}
<div id="background">
<div id="menu">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
</div>

CSS - How to place absolute div correctly

I have the following code:
.menu{
border: solid red;
border-width: 1px 1px 0px 1px;
background-color:black;
color:white;
width: 60px;
}
.dropdown{
position:absolute;
background-color: grey;
width:100px;
}
.dropdown ul{
list-style:none;
padding:10px;
margin: 0;
}
.zoom{
zoom:300%;
}
<div class="menu zoom">
Click me
<div class="dropdown">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</div>
How can I place my dropdown menu to the same x position as the parent, without removing the border? I already tried 'box-sizing: border-box', but somehow it doesn't work.
Set position: relative on parent element and on child set position left to same negative value as left border width of parent element.
.menu {
border: solid red;
border-width: 1px 1px 0px 1px;
background-color: black;
color: white;
width: 60px;
position: relative;
}
.dropdown {
position: absolute;
background-color: grey;
width: 100px;
left: -1px;
}
.dropdown ul {
list-style: none;
padding: 10px;
margin: 0;
}
.zoom {
zoom: 300%;
}
<div class="menu zoom">
Click me
<div class="dropdown">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</div>
Keeping the parent as positon:relative and giving the child position:absolte with top:100%; and left:-1px ( where -1 is taken because the width of border is 1 from left)
Here is the working snippet:
.menu {
border: solid red;
border-width: 1px 1px 0px 1px;
background-color: black;
color: white;
width: 60px;
position: relative;
}
.dropdown {
position: absolute;
background-color: grey;
width: 100px;
left: -1px;
top:100%
}
.dropdown ul {
list-style: none;
padding: 10px;
margin: 0;
}
.zoom {
zoom: 300%;
}
<div class="menu zoom">
Click me
<div class="dropdown">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</div>

New list items shadowing previous items due to negative margin

I'm working on a step indicator which I implemented as a list:
<ol>
<li>Step 1</li>
<li class="active">Step 2</li>
<li>Step 3</li>
</ol>
Each list element has a rounded edge to it's right in order to indicate progress, so I have the following CSS:
li{
display: block; background-color: white; width: 33%; border: 1px solid #ddd; text-indent: 40px;
float: left;
margin: 0 0 0 -20px;
border-radius: 0 15px 15px 0;
}
My problem is that later elements are shadowing the earlier, thus the rounded edge are hidden. I've tried to set a decreasing z-index for each element, but it doesn't work (besides I couldn't use this solution anyway). I acheive the desired presentation by changing to float:right but that renders the list items in descending order...
Check this jsfiddle for details: http://jsfiddle.net/fMRbr/
You can use the :before
li{
display: inline-block;
width: 33%;
margin: 0 0 0 -20px;
border: 1px solid #ddd;
border-radius: 0 15px 15px 0;
background-color: white;
text-indent: 40px;
position: relative;
}
li.active{
background-color: red;
}
li:before{
content: '';
width: 15px;
height: 19px;
display: inline-block;
background: #fff;
border: 1px solid #ddd;
border-width: 0 1px 1px 0;
border-radius: 0 15px 15px 0;
position: absolute;
top: 0;
left: -3px;
}
li.afteractive:before {
content: '';
width: 15px;
height: 19px;
display: inline-block;
background: #f00;
border: 0;
border-radius: 0 15px 15px 0;
position: absolute;
top: 0;
left: -3px;
}
<ol>
<li class="active">Step 1</li>
<li class="afteractive">Step 2</li>
<li>Step 3</li>
</ol>
<br /><br />
<ol>
<li>Step 1</li>
<li class="active">Step 2</li>
<li class="afteractive">Step 3</li>
</ol>
<br /><br />
<ol>
<li>Step 1</li>
<li>Step 2</li>
<li class="active">Step 3</li>
</ol>
Instead of using border-radius and negative margin values, have you considered a Tbackground image at the top right of each <li> which looks like this:
The active (red) <li> would have a similar background but colored red. The result should look something like this:
Add a span tag to your li's with display: inline-block so they automatically grow to the right width:
html
<ol>
<li><span>Step 1</span></li>
<li class="active"><span>Step 2</span></li>
<li><span>Step 3</span></li>
</ol>
css
li {
display: block;
float: left;
width: 33%;
margin: 0 0 0 -20px;
background-color: white;
text-indent: 40px;
}
li.active {
}
li.active span {
background-color: red;
}
li span {
display: inline-block;
border: 1px solid #ddd;
border-radius: 0 15px 15px 0;
padding-right: 10px;
}
See a jsfiddle of this solution here:
http://jsfiddle.net/c4urself/HYQSJ/