How does negative margin works? - html

From this article(https://www.quirksmode.org/blog/archives/2020/02/negative_margin.html) I know that negative margin-right influences any element to the right of the element where it's written but not an element itself.
In the book, "Meloni J. - PHP, MySQL & JavaScript All in One (6th Edition) (Sams Teach Yourself) - 2018" there is an example where I don't see any element to the right. But it still works and it pulls the element somehow. Can somebody explain me how does it work? Why it influences the element where it's written?
body {
margin: 0;
padding: 0;
}
#wrapper {
float: left;
padding-left: 200px;
padding-right: 125px;
}
#left_side {
position: relative;
float: left;
width: 200px;
background-color: #52f471;
right: 200px;
margin-left: -100%;
}
#right_side {
position: relative;
float: left;
width: 125px;
background-color: #f452d5;
margin-right: -125px; /* THIS MARGIN-RIGHT */
}
#content_area {
position: relative;
float: left;
background-color: #FFFFFF;
width: 100%;
}
<div id="wrapper">
<div id="content_area">CONTENT</div>
<div id="left_side">LEFT SIDE</div>
<div id="right_side">RIGHT SIDE</div>
</div>

Remove some of the properties to better understand what is happening initially
body {
margin: 0;
padding: 0;
width:800px;
}
#wrapper {
float: left;
padding-left: 200px;
padding-right: 125px;
border:1px solid red;
}
#left_side {
position: relative;
float: left;
width: 200px;
background-color: #52f471;
/*right: 200px;
margin-left: -100%;*/
}
#right_side {
position: relative;
float: left;
width: 125px;
background-color: #f452d5;
/*margin-right: -125px;*/ /* THIS MARGIN-RIGHT */
}
#content_area {
position: relative;
float: left;
background-color: #FFFFFF;
/*width: 100%;*/
}
<div id="wrapper">
<div id="content_area">CONTENT</div>
<div id="left_side">LEFT SIDE</div>
<div id="right_side">RIGHT SIDE</div>
</div>
We have 3 float elements side by side. One with auto width and two with explicit width. The total width will define the width of the wrapper W.
Now if we add width:100% to #content_area it will take 100% of W push the other to the next row:
body {
margin: 0;
padding: 0;
width:800px;
}
#wrapper {
float: left;
padding-left: 200px;
padding-right: 125px;
border:1px solid red;
}
#left_side {
position: relative;
float: left;
width: 200px;
background-color: #52f471;
/*right: 200px;
margin-left: -100%;*/
}
#right_side {
position: relative;
float: left;
width: 125px;
background-color: #f452d5;
/*margin-right: -125px;*/ /* THIS MARGIN-RIGHT */
}
#content_area {
position: relative;
float: left;
background-color: #FFFFFF;
width: 100%;
}
<div id="wrapper">
<div id="content_area">CONTENT</div>
<div id="left_side">LEFT SIDE</div>
<div id="right_side">RIGHT SIDE</div>
</div>
Now if you add margin-left:-100% to the left element it means shift this element by -W thus it will overlap the #content_area element
body {
margin: 0;
padding: 0;
width:800px;
}
#wrapper {
float: left;
padding-left: 200px;
padding-right: 125px;
border:1px solid red;
}
#left_side {
position: relative;
float: left;
width: 200px;
background-color: #52f471;
/*right: 200px;*/
margin-left: -100%;
}
#right_side {
position: relative;
float: left;
width: 125px;
background-color: #f452d5;
/*margin-right: -125px;*/ /* THIS MARGIN-RIGHT */
}
#content_area {
position: relative;
float: left;
background-color: #FFFFFF;
width: 100%;
}
<div id="wrapper">
<div id="content_area">CONTENT</div>
<div id="left_side">LEFT SIDE</div>
<div id="right_side">RIGHT SIDE</div>
</div>
Then we shift it using right equal to its width to remove the overlap and make it on the top of the right padding:
body {
margin: 0;
padding: 0;
width:800px;
}
#wrapper {
float: left;
padding-left: 200px;
padding-right: 125px;
border:1px solid red;
}
#left_side {
position: relative;
float: left;
width: 200px;
background-color: #52f471;
right: 200px;
margin-left: -100%;
}
#right_side {
position: relative;
float: left;
width: 125px;
background-color: #f452d5;
/*margin-right: -125px;*/ /* THIS MARGIN-RIGHT */
}
#content_area {
position: relative;
float: left;
background-color: #FFFFFF;
width: 100%;
}
<div id="wrapper">
<div id="content_area">CONTENT</div>
<div id="left_side">LEFT SIDE</div>
<div id="right_side">RIGHT SIDE</div>
</div>
For the right element it's almost the same trick but the negative margin-right which is equal to the width will negate the width effect making the element taking 0 space on the layout so it will move back to the first row and will overlap the padding area on the right.
Here is an illustration of the right element alone to better understand:
body {
margin: 0;
padding: 0;
}
#wrapper {
float: left;
border:2px solid green;
}
#right_side {
position: relative;
float: left;
width: 125px;
background-color: #f452d5;
margin-right: -125px;
animation:change 5s linear infinite alternate;
}
#keyframes change{
from {
margin-right:0;
}
}
<div id="wrapper">
<div id="right_side">RIGHT SIDE</div>
</div>
Notice how the width of wrapper become 0 when width+margin of the right element is 0
And if we add another element inside you will see the jump to the first row:
body {
margin: 0;
padding: 0;
}
#wrapper {
float: left;
border:2px solid green;
}
#right_side {
position: relative;
float: left;
width: 125px;
background-color: #f452d5;
margin-right: -125px;
animation:change 5s linear infinite alternate;
}
#keyframes change{
0% {
margin-right:0;
}
90% {
margin-right:-125px;
}
}
<div id="wrapper">
<div style="float:left;width:100%">aaa</div>
<div id="right_side">RIGHT SIDE</div>
</div>
And if you add padding-right we make the right element overlap that padding when it jumps to the first row
body {
margin: 0;
padding: 0;
}
#wrapper {
float: left;
border:2px solid green;
padding-right:125px;
background:linear-gradient(yellow,yellow) right/125px no-repeat; /* to illustrate the padding */
}
#right_side {
position: relative;
float: left;
width: 125px;
background-color: #f452d5;
margin-right: -125px;
animation:change 5s linear infinite alternate;
}
#keyframes change{
0% {
margin-right:0;
}
90% {
margin-right:-125px;
}
}
<div id="wrapper">
<div style="float:left;width:100%">aaa</div>
<div id="right_side">RIGHT SIDE</div>
</div>

Related

struggling with exact calculation of heights and margins

I'm currently working on a website where I work with perfect squares and rectangles. These need to perfectly fit on mobile devices and laptop screens, tablets and so on. Therefore I want exactly now and be in control how much space every element is taking.
My problem: it goes about the light blue color, these div is taking 50% of the width and a height of 100%. Next I set the purple div to a height of 60% with a margin on top of 10% (so 70%), then I have the green div with a height of 30% which will bring the total to 100%. As you see in the example it isn't taking 100% but more than that.
I've red that the margin is calculated from the parent div (so the light-blue div I suppose), so I need to change my way of thinking-calculating I suppose but don't know how. Someone who can help me out?
.toegelatenDagWeek {
float: left;
background-color: yellow;
}
.verhoudingTijd {
float: right;
background-color: red;
}
.extraTijdDagWeek {
float: right;
background-color: silver;
}
.square-box{
position: relative;
width: 50%;
overflow: hidden;
}
.square-box:before{
content: "";
display: block;
padding-top: 50%;
}
.square-content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
color: white;
text-align: center;
margin: 1%;
}
.vierkanttt{
width: 35%;
float:left;
text-align:center;
margin-left: 37.5%;
margin-right: 37.5%;
margin-top: 1%;
margin-bottom: 2%;
position: relative;
}
.vierkanttt-marges {
flex: 1;
margin: 1px 1px 1px 1px;
position: relative;
}
.inputTimeSmall {
background-color: #b721ff;
border: none;
border-radius: 0.5em;
padding: 15% 0% 20% 0%;
width:100%;
text-align:center;
font-size:0.8em;
}
input {
color: white;
}
.inputTimeSmall::placeholder {
color: white;
}
.inputTime:focus {
outline: none;
}
.inputTime {
background-color: #b721ff;
border: none;
border-radius: 0.5em;
padding: 15% 0% 20% 0%;
width:100%;
text-align:center;
font-size:4em;
color: white;
}
.gespeeldeTijdTitel {
color: white;
width: 100%;
padding-left: 5%;
float: left;
text-align: left;
text-decoration-line: underline;
//background: purple;
font-size: 1.5em;
padding-bottom: 3%;
padding-top: 3%;
background-color: blue;
}
.toegelatenTijdTitel {
background: blue;
height: 30%;
width: 100%;
position: relative;
}
.toegelatenTijdTitel div {
position: absolute;
top: 50%;
color: white;
text-decoration-line: underline;
transform: translateY(-50%);
margin-left: 5%;
font-size: 1.5em;
}
.testje {
width: 100%;
height: 70%;
background-color: black;
}
.spaceInputTimeSmall {
background-color: #21d4fd;
border-radius: 0.5em;
float:left;
width: 50%;
height: 100%;
}
.inputTimeMini {
width: 80%;
background-color: #b721ff;
font-size:2em;
height: 60%;
margin-top: 10%;
margin-left: 10%;
margin-right: 10%;
border-radius: 0.5em;
display: table;
}
.textBoxSmall {
height: 30%;
width: 80%;
background-color: green;
margin: 00% 10% 0% 10%;
vertical-align: center;
display: table;
}
.centerText {
display: table-cell;
text-align: center;
vertical-align: middle;
}
.boxtienprocent {
background-color: grey;
}
#container {
width: 90%;
height: 90%;
margin: 5%;
position: relative;
color: white;
}
<div class="square-box toegelatenDagWeek">
<div class='square-content '>
<div class="toegelatenTijdTitel">
<div>
toegelaten tijd
</div>
</div>
<div class="testje">
<div class="spaceInputTimeSmall">
<div class="boxtienprocent"></div>
<!--<input type="text" class="inputTimeSmall" id="inputHoursMaandag" name="maandagUren" placeholder="00" maxlength="3">-->
<div class="inputTimeMini" name="uren" id="DisplayToegelatenHours" ><div class="centerText">05</div></div>
<div class="textBoxSmall"><div class="centerText">uren</div></div>
</div>
<div class="spaceInputTimeSmall">
<!--<input type="text" class="inputTimeSmall" id="inputMinutesMaandag" name="maandagMinuten" placeholder="00" maxlength="2">-->
<div class="inputTimeMini" name="uren" id="DisplayToegelatenMinutes" ><div class="centerText">05</div></div>
<div class="textBoxSmall"><div class="centerText">minuten</div></div>
</div>
</div>
</div>
</div>
Instead of margins, you need to use padding, because padding is the space between the content and the border, meanwhile margin is the space outside the border. In your example, you used margins, so it pushed the rectangular outside.

CSS: slide a div out to the right only to width of the parent div

I'm having some CSS issues I hope someone here can help with. I basically am trying to set a group of inline divs that slide out to the right, expanding to the width of the parent div containing them. As you can see from the jsfiddle I have (https://jsfiddle.net/0o9bw101/), the divs expand to the width of the parent div, instead of expanding only to the rightmost border of the parent div. If anyone can help I'd be quite thankful. Thank you in advance!
Here is the CSS I'm using in case you want to see it here:
.container {
width: 70%;
height: 100px;
background-color: black;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
vertical-align: top;
color: black;
}
.greyDiv:hover {
transition: 2s;
width: 70%;
position: absolute
}
Try this
.container {
width: 70%;
height: 100px;
background-color: black;
position:relative;
overflow:hidden;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
vertical-align: top;
}
.greyDiv:hover {
transition: 2s;
width: 100%;
position: absolute;
}
<div class='container'>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
</div>
EDIT:
The trick is to add another box inside main container.
DEMO: https://jsfiddle.net/0o9bw101/3/
<div class='container'>
<div class='invisible_container'>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
</div>
</div>
previous answer:
It's hard to do when you mix parent's with in % with children margins in px.
Also having parent's position set to something other than default helps a bit.
Here is working example for you:
.container {
width: 70%;
height: 100px;
background-color: black;
position: absolute;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
margin-left: 2%;
vertical-align: top;
}
.greyDiv:hover {
transition: 2s;
width: 96%;
position: absolute
}
DEMO: https://jsfiddle.net/0o9bw101/2/
This might not be quite what you were going for, but here elements will grow to the full width of the container (left to right) regardless of it's starting position using 2 extra elements per object.
The .inner is used to grow the background to the full width
The .placeholder is used to keep the other elements from collapsing left
#keyframes grow {
from {width: 0%;}
to {width: 92%;}
}
.container {
width: 70%;
height: 100px;
position: relative;
background-color: black;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: black;
display: inline-block;
margin: 20px 4%;
vertical-align: top;
position: relative;
z-index: 2;
}
.inner {
width: 0%;
height: 100%;
display: none;
background-color: transparent;
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
.placeholder {
display: none;
background-color: transparent;
height: 60px;
width: 60px;
margin: 20px 4%;
}
.greyDiv:hover {
width: 100%;
position: absolute;
left: 0;
margin: 20px 0;
background-color: transparent;
z-index: 4;
}
.greyDiv:hover + .placeholder {
display: inline-block;
}
.greyDiv:hover .inner {
display: inline-block;
left: 4%;
width: 100%;
background-color: white;
animation-name: grow;
animation-duration: 2s;
animation-fill-mode: forwards;
z-index: 5;
}
<div class='container'>
<div class='greyDiv'>1a<div class="inner">1x</div></div><div class="placeholder"></div>
<div class='greyDiv'>2a<div class="inner">2x</div></div><div class="placeholder"></div>
<div class='greyDiv'>3a<div class="inner">3x</div></div><div class="placeholder"></div>
<div class='greyDiv'>4a<div class="inner">4x</div></div><div class="placeholder"></div>
</div>

CSS div position in another div

I am new at CSS positioning but could not understand of positioning the boxes.
<div class="box">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
</div>
.box {
width: 260px;
overflow: hidden;
background: #e2e2e2;
padding: 10px;
position:relative;
}
.first {
width: 50px;
height: 50px;
background: #BDBDBD;
float: left;
margin: 10px;
}
.second {
width: 60px;
height: 60px;
background: #889B7F;
float: left;
margin: 10px;
}
.third {
width: 70px;
height: 70px;
background: #B98F91;
float: left;
margin: 10px;
position:absolute;
top:70px;
left:30px;
}
Demo
If I do not set the .box position, third box is appearing up front.
If I set the .box position as relative, third box is appearing under box
If I set set third box position as relative, it goes right.
What is the inner div position rule?
Remove position:absolute; from .third and it will look like This
Snippet:
.box {
width: 260px;
overflow: hidden;
background: #e2e2e2;
padding: 10px;
position:relative;
}
.first {
width: 50px;
height: 50px;
background: #BDBDBD;
float: left;
margin: 10px;
}
.second {
width: 60px;
height: 60px;
background: #889B7F;
float: left;
margin: 10px;
}
.third {
width: 70px;
height: 70px;
background: #B98F91;
float: left;
margin: 10px;
top:70px;
left:30px;
}
<div class="box">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
</div>
hi just remove the third box position absolute and check it and it will be look like this then

HTML Positioning using CSS

I'm having trouble with HTML positioning. I messed around for a while and this was the farthest I got. Here is the HTML and CSS
.group:after {
content: "";
display: block;
clear: both;
}
article {
width: 400px;
border: 1px solid black;
}
#tip,
#title {
padding: 0 0 0 0;
margin: 0 0 0 0;
}
#tip {
background: yellow;
}
#title {
background: orange;
}
#box_1 {
float: left;
width: 60px;
height: 60px;
background: pink;
}
#box_2 {
float: right;
width: 60px;
height: 120px;
background: blue;
}
#box_3 {
float: left;
clear: left;
width: 60px;
height: 60px;
background: green;
}
<article class="group">
<h2 id="tip">tip</h2>
<h2 id="title">title</h2>
<div id="box_1"></div>
<div id="box_3"></div>
<div id="box_2"></div>
</article>
I want my #box_3 to be right below #box_1 and I got it to work by giving #box_3 clear: left;. But I also want my #box_2 to be rendered right below #title on the right.
Something to note is that the #box_3 comes before #box_2 in my HTML code and the height of #box_1 is not fixed to just 60px. Here is my jsfiddle.
http://jsfiddle.net/92um5o2c/2/
if you suffle your html code little bit so its not a problem. You should place #box1 first then #box2 and then #box3
Here is the example
.group:after {
content: "";
display: block;
clear: both;
}
article {
width: 400px;
border: 1px solid black;
}
#tip, #title{
padding: 0 0 0 0;
margin: 0 0 0 0;
}
#tip {
background: yellow;
}
#title {
background: orange;
}
#box_1 {
float: left;
width: 60px;
height: 60px;
background: pink;
}
#box_2 {
float: right;
width: 60px;
height: 120px;
background: blue;
}
#box_3 {
float: left;
clear: left;
width: 60px;
height: 60px;
background: green;
}
<article class="group">
<h2 id="tip">tip</h2>
<h2 id="title">title</h2>
<div id="box_1"></div>
<div id="box_2"></div>
<div id="box_3"></div>
</article>
Assuming the height of the boxes is fixed and will not grow, you can simply give #box_2 a negative margin which equals the height of box_1:
#box_2 {
float: right;
width: 60px;
height: 120px;
background: blue;
margin-top:-60px;
}
http://jsfiddle.net/92um5o2c/4/
You mean like this right? Just change the order in the HTML like below.
<div id="box_1"></div>
<div id="box_2"></div>
<div id="box_3"></div>
#box_1 {
float: left;
width: 60px;
height: 60px;
background: pink;
}
#box_2 {
float: right;
width: 60px;
height: 120px;
background: blue;
}
#box_3 {
clear: left;
width: 60px;
height: 60px;
background: green;
}
http://jsfiddle.net/xn0pL6mn/
You can add "position: absolute;" and "right: 0;" to #box_2 and then "position: relative;" to article.
#box_2 {
position:absolute;
right: 0;
}
article {
position: relative;
}
https://jsfiddle.net/92um5o2c/9/

How to position div div above another divs

I'm trying to set div's position like this:
but i can't set image (green box) in position.
orange box is on top
blue and lightgreen div are buttons
red frame is static distant under orange box
green box is link with image inside, covering partly blue and lightgreen buttons.
every links must stay clickable every time.
I can't centering green image and set it above orange div partly.
Example code here
<div class="header-container">
<div class="nav-container">
<div class="logo">
Click!
</div>
<div class="nav">
Click!
</div>
</div>
<div class="header-image">
<div class="image">
Click!
</div>
</div>
<div class="menu-container">
Click!
</div>
.nav-container{
width: 100%;
height: 50px;
background: orange;
}
.logo{
width: 25%;
height: 40px;
margin: 5px;
background-color: lightblue;
float: left;
}
.nav{
width: 25%;
height: 40px;
margin: 5px;
background-color: lightgreen;
float: right;
}
.header-image{
width: 100%;
border: 1px solid green;
position: relative;
z-index: 2;
text-align: center;
}
.image{
height: 100px;
width: 60%;
background: green;
opacity: 0.6;
}
.header-image a{
padding: 40px 0;
}
.menu-container{
width: 100%;
border: 1px red solid;
height: 40px;
margin-top: 50px;
}
I've uploaded your jsfiddle here.
Addded the following css:
.header-image {
position: absolute;
top: 40px;
left: 20%;
}
also added extra margin-top for the .menu-container
.menu-container {
margin-top: 80px; //instead of 50px
}
I've positioned it absolute because this way it will go wherever you want it based on the body relative positioning.
adding this to image should work:
margin:0 auto;
position:relatve;
z-index:66;
margin-top:-10px
http://jsfiddle.net/o3oyuzb9/2/
try this
only changed the css
body,html{margin: 10px;}
.header-container{
width: 100%;
}
a{
text-decoration:none;
color:#000;
padding: 10px 0px;
display: block;
text-align: center;
}
.nav-container{
width: 100%;
height: 50px;
background: orange;
}
.logo{
width: 25%;
height: 40px;
margin: 5px;
background-color: lightblue;
float: left;
}
.nav{
width: 25%;
height: 40px;
margin: 5px;
background-color: lightgreen;
float: right;
}
.header-image{
width: 100%;
border: 1px solid green;
position: relative;
z-index: 2;
text-align: center;
}
.image{
height: 100px;
width: 60%;
margin: 0 auto;
margin-top: -20px;
background: green;
opacity: 0.6;
}
.header-image a{
padding: 40px 0;
}
.menu-container{
width: 100%;
border: 1px red solid;
height: 40px;
margin-top: 50px;
}
just add this to your image class:
margin: 0 auto;
margin-top: -20px;