A CSS margin does not work as expected in my HTML - html

div {
border: 1px solid black;
}
.div1 {
height: 600px;
width: 600px;
}
.div2 {
height: 300px;
width: 300px;
margin-right: 0px;
margin-top: 0px;
}
.div3 {
height: 150px;
width: 150px;
margin-left: 0px;
margin-bottom: 0px;
}
<div class="div1">
<div class="div2">
<div class="div3">
</div>
</div>
</div>
In above code I have a confusion, because in above code the margin was not affected to the inner div tags. What might be the problem?
Image of output
and what I want to do is
Desired output

No need to use margin , just use position property.
Here is the Snippet.
div {
border: 1px solid black;
}
.div1 {
height: 600px;
width: 600px;
position: relative;
margin: 30px auto;
}
.div2 {
height: 300px;
width: 300px;
position: absolute;
top: 0;
right: 0;
}
.div3 {
height: 150px;
width: 150px;
position: absolute;
bottom: 0;
left: 0;
}
<div class="div1">
<div class="div2">
<div class="div3"> </div>
</div>
</div>

To achieve the desired o/p with margin this should be your code
div {
border: 1px solid black;
}
.div1 {
height: 600px;
width: 600px;
}
.div2 {
height: 300px;
width: 300px;
margin-left: 300px;
margin-top: 0px;
}
.div3 {
height: 150px;
width: 150px;
margin-left: 0px;
margin-top: 150px;
}
But, this would be a wrong approach, the right approach is to manipulate positioning the inner divs to left or right. This is because of the concept of box model. You learn more about box-model to better understand when to use margin-l/r/t/b and when to use positioning
Correct Code :
div {
border: 1px solid black;
}
.div1 {
height: 600px;
width: 600px;
position : relative;
}
.div2 {
height: 300px;
width: 300px;
position : absolute;
right : 0;
}
.div3 {
height: 150px;
width: 150px;
position : absolute;
bottom : 0;
}

You can use a combination of float and position: relative/absolute settings in the combination and with the settings shown below to achieve the desired result shown in your image.
Note: All this has nothing to do with margins.
div {
border: 1px solid black;
}
.div1 {
height: 600px;
width: 600px;
}
.div2 {
float: right;
position: relative;
height: 300px;
width: 300px;
}
.div3 {
height: 150px;
width: 150px;
position: absolute;
bottom: 0;
left: 0;
}
<div class="div1">
<div class="div2">
<div class="div3">
</div>
</div>
</div>

You can use position: absolute; and then in place of margin-left or margin-top use left or top or bottom
div {
border: 1px solid black;
}
.div1 {
height: 600px;
width: 600px;
position: relative;
}
.div2 {
height: 300px;
width: 300px;
right: 0px;
top: 0px;
position: absolute;
}
.div3 {
height: 150px;
width: 150px;
left: 0px;
bottom: 0px;
position: absolute;
}
<div class="div1">
<div class="div2">
<div class="div3">
</div>
</div>
</div>
Hope this helps

Related

Make div grow up left instead of down right

The div should grow up left, however, it does the opposite as of now.
The margin-left and top is necessary by the way.
Quick gif showcasing the issue: https://gyazo.com/ce51c504698395c26cffefb9b74e7e3e
html, body {
width: 100%;
height: 100%;
}
#a {
width: 50%;
height: 100%;
border: 1px solid black;
}
#img-wrapper {
margin-left: 10%;
margin-top: 20%;
width: 50%;
position: relative;
border: 1px solid red;
}
img {
width: 100%;
}
<div id="a">
<div id="img-wrapper">
<img src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/12225358/Pug-On-White-01.jpg" alt="">
</div>
</div>
Try this:-
#a {
width: 70%;
height: 100%;
border: 1px solid black;
position: relative;
}
#img-wrapper {
width: 40%;
position: absolute;
bottom: 0;
right: 0;
border: 1px solid red;
}
If you want your image going from right to left by increasing width property, you should give it float property:
#img-wrapper {
float: right;
margin-top: 0; // if you want it to start from top right edge
}
added margin-right: 10%; float: right;
#img-wrapper {
margin-right: 10%;
margin-top: 20%;
width: 50%;
position: relative;
border: 1px solid red;
float: right;
}
html,
body {
width: 100%;
height: 100%;
}
#a {
width: 50%;
height: 100%;
border: 1px solid black;
position: relative;
}
#img-wrapper {
margin-right: 10%;
margin-top: 20%;
width: 52%;
position: absolute;
border: 1px solid red;
right: 0;
bottom: 50%;
transform: translateY(50%);
}
img {
width: 100%;
}
<div id="a">
<div id="img-wrapper">
<img src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/12225358/Pug-On-White-01.jpg" alt="">
</div>
</div>
Sounds like the problem isn't about getting the image to "grow up left" but is about positioning the #img-wrapper.
You can solve this by positioning the #img-wrapper absolutely and specifying its bottom and right position. I've added a :hover style so you can see it 'grow' on hover.
A word of warning though. Positioning something of unknown/variable size using percentages is going to give you very mixed results at different viewport sizes. Perhaps what you want isn't quite as described but I think you should be looking at a more flexible solution such as using flexbox.
html, body {
width: 100%;
height: 100%;
}
#a {
width: 50%;
height: 100%;
border: 1px solid black;
position:relative;
}
#img-wrapper {
right: 30%;
bottom: 30%;
width: 50%;
position: absolute;
border: 1px solid red;
}
#img-wrapper:hover {
width: 70%;
}
img {
width: 100%;
}
<div id="a">
<div id="img-wrapper">
<img src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/12225358/Pug-On-White-01.jpg" alt="">
</div>
</div>
html, body {
width: 100%;
height: 100%;
}
#a {
width: 50%;
height: 100%;
border: 1px solid black;
position: relative;
}
#img-wrapper {
width: 50%;
border: 1px solid red;
margin: 20% 0 0 20%;
position: absolute;
top:0;
left:50%;
transform: translateX(-50%);
}
img {
width: 100%;
}

Image (literally) runs away from div

The yellow dot "gif1" has to go inside the black box "gif" but as you can see I somehow managed to did the opposite.
How many things did I do wrong?
Livewave Preview
I already tried overflow:auto or hidden and changing the position attributes from relative to absolute and vice versa.
<html>
<head>
<body>
<center>
<div class="container">
<div class="img_sx"></div>
<div class="img_dx"></div>
<div class="quote"></div>
<div class="gif"><img class="gif1" src="https://upload.wikimedia.org/wikipedia/commons/f/ff/Scandal_-_Yellow_album_cover.jpg"></div>
<div class="burp"></div>
<div class="prot"></div>
</div>
</center>
<style>
.container {
width: 550px;
height: 430px;
background-color: burlywood;
display: table;
}
.img_sx {
width: 250px;
height: 430px;
background-color: cadetblue;
position: absolute;
overflow: hidden;
}
.img_dx {
width: 210px;
height: 390px;
background-color: cornflowerblue;
margin-left: 250px;
margin-top: 20px;
position: relative;
float: left;
}
.quote {
width: 230px;
height: 100px;
background-color: coral;
margin-left: 250px;
margin-top: 30px;
position: relative;
}
.gif {
width: 230px;
height: 100px;
background-color: black;
margin-left: 250px;
margin-top: 10px;
position: relative;
}
.gif1 {
width: 90px;
border-radius: 90px;
}
.gif2 {}
.burp {
width: 230px;
height: 90px;
background-color: white;
margin-left: 250px;
margin-top: 10px;
position: relative;
}
.prot {}
</style>
</head>
</body>
</html>
You are facing a complex situation where the float property is creating the issue. Basically the yellow "image" is wrapping around the floated element and that's why it goes out of the black box and under the blue one (the float element). To avoid this you can use absolute instead of float.
.container {
width: 550px;
height: 430px;
background-color: burlywood;
display: table;
margin: auto;
}
.img_sx {
width: 250px;
height: 430px;
background-color: cadetblue;
position: absolute;
overflow: hidden;
}
.img_dx {
width: 210px;
height: 390px;
background-color: cornflowerblue;
margin-left: 250px;
margin-top: 20px;
position: absolute;
}
.quote {
width: 230px;
height: 100px;
background-color: coral;
margin-left: 250px;
margin-top: 30px;
position: relative;
}
.gif {
width: 230px;
height: 100px;
background-color: black;
margin-left: 250px;
margin-top: 10px;
position: relative;
}
.gif1 {
width: 90px;
border-radius: 90px;
}
.gif2 {}
.burp {
width: 230px;
height: 90px;
background-color: white;
margin-left: 250px;
margin-top: 10px;
position: relative;
}
.prot {}
<div class="container">
<div class="img_sx"></div>
<div class="img_dx"></div>
<div class="quote"></div>
<div class="gif"><img class="gif1" src="https://upload.wikimedia.org/wikipedia/commons/f/ff/Scandal_-_Yellow_album_cover.jpg"></div>
<div class="burp"></div>
<div class="prot"></div>
</div>
This is the way to go:
.gif{
position: relative;
}
.gif1{
position:absolute;
}
Hope it helps.

CSS How to put 4 divs on each side of a parent div

I wish to include 4 divs inside a parent div in the following manner:
I could use fixed position and set right/left/top/bottom = 0 accordingly for each child div if they were not inside in a div, but right now, I can't figure out how to do this.
Here you go, but I'm not sure how this will fare in responsiveness since the parent has fixed sizes, but the child div should be able to adapt if the parent changes size. Some css can be combined, but I separated them all for reference
.parent {
width: 300px;
height: 300px;
position: relative;
border: 1px solid black;
}
.div1 {
position: absolute;
bottom: 0;
right: 0;
width: 80%;
height: 20%;
background-color: green;
}
.div2 {
position: absolute;
top: 0;
right: 0;
width: 20%;
height: 80%;
background-color: blue;
}
.div3 {
position: absolute;
top: 0;
left: 0;
width: 80%;
height: 20%;
background-color: red;
}
.div4 {
position: absolute;
bottom: 0;
left: 0;
width: 20%;
height: 80%;
background-color: brown;
}
<div class="parent">
<div class="div1">
DIV1
</div>
<div class="div2">
DIV2
</div>
<div class="div3">
DIV3
</div>
<div class="div4">
DIV4
</div>
</div>
Consider utilizing absolute positioning on nested div elements and offsetting their positions, within the containing element, appropriately and as required by declaring top, bottom, left and right properties respectively.
Code Snippet Demonstration
Note:
In the below demonstration, a containing element, with resizing properties, has been wrapped around the element in question, to demonstrate the responsive behaviour of this method.
Resize the element manually by clicking the icon, in the bottom-left corner of the containing element, and dragging vertically or horizontally.
* {
box-sizing: border-box;
font-family: arial;
}
.outer {
border: 3px solid black;
width: 100%;
height: 100%;
position: relative; /* required */
}
.outer-wrapper { /* purely for the sake of responsive demonstration */
padding: 10px;
resize: auto;
overflow: hidden;
border: 3px dashed gray;
width: 400px;
height: 400px;
}
.outer div {
position: absolute;
padding: 10px;
font-weight: bold;
font-size: 12px;
}
.outer div:nth-child(odd) {
width: 80%;
height: 20%;
}
.outer div:nth-child(even) {
width: 20%;
height: 80%;
}
.outer div:nth-child(1) {
background: #ed1c24;
left: 0;
top: 0;
}
.outer div:nth-child(2) {
background: #00a2e8;
right: 0;
top: 0;
}
.outer div:nth-child(3) {
background: #22b14c;
right: 0;
bottom: 0;
}
.outer div:nth-child(4) {
background: #b97a57;
left: 0;
bottom: 0;
}
<div class="outer-wrapper">
<div class="outer">
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
</div>
</div>
It will be helpful to you
.parent{
position: relative;
width: 100%;
height: 100vh;
border: 1px solid black;
}
.parent>div{
position:absolute;
text-align:center;
}
.one{
background-color: green;
bottom: 0;
right: 0;
width: 80%;
height: 20%;
}
.two{
background-color: blue;
top: 0;
right: 0;
width: 20%;
height: 80%;
}
.three{
background-color: red;
top: 0;
left: 0;
width: 80%;
height: 20%;
}
.four{
background-color: brown;
bottom: 0;
left: 0;
width: 20%;
height: 80%;
}
<div class="parent">
<div class="one"> Div1</div>
<div class="two">Div2</div>
<div class="three">Div3</div>
<div class="four">Div4</div>
</div>

How to crop image within a circle div in css?

Hi there, I want to crop the image within a div that is divided into two parts in a circle. One side is half cropped pic and the other side is just background color with the name on it. I am currently using following code :
width: 220px;
userdp {
height: 220px;
border: 4px solid red;
border-radius: 50%;
position: relative;
object-fit: none;
}
If your image is inside the div element that you're applying that styling to as below you should just need to add overflow: hidden to the CSS.
<div class="userdp">
<img src="..." />
</div>
And the styling.
.userdp {
height: 220px;
width: 220px;
border-radius: 50%;
overflow: hidden;
}
I've created an example here for you:
https://jsfiddle.net/20g4uL0j/1/
You can use the following,
**HTML**
<div class="circle">
<div class="image">
<img src="your-image.png" />
</div>
<div class="color">Text</div>
**CSS**
.circle{
width: 220px;
height:220px;
border-radius: 50%;
overflow:hidden;
}
.image, .color{
width:50%;
float:left;
height:100%;
}
.color{
background-color: #099;
}
You can do this as follow:
https://jsfiddle.net/ivan0013/f1a06cxe/
div {
background: #9e978e;
display: inline-block;
margin: 0 1em 1em 0;
}
.top,
.bottom {
height: 55px;
width: 110px;
}
.right,
.left {
height: 110px;
width: 55px;
}
.top {
border-top-left-radius: 110px;
border-top-right-radius: 110px;
}
.right {
border-bottom-right-radius: 110px;
border-top-right-radius: 110px;
}
.bottom {
border-bottom-left-radius: 110px;
border-bottom-right-radius: 110px;
}
.left {
border-bottom-left-radius: 110px;
border-top-left-radius: 110px;
}
<div class="top"></div>
<div class="right"></div>
<div class="bottom"></div>
<div class="left"></div>
overflow: hidden and a little more play with the positioning, z-index, and object-fit may help you achieve that.
Here is an example for you (EDITED after re-reading your question):
.userdp {
height: 220px;
width: 220px;
border: 4px solid black;
border-radius: 50%;
position: relative;
overflow: hidden;
}
.userdp-img {
z-index: 1000;
width: 100%;
height: 100%;
object-fit: cover;
}
.userdp-info {
z-index: 2000;
width: 50%;
height: 100%;
color: #ddd;
background-color: red;
border-right: 3px solid black;
}
.userdp-info-inner {
text-align: center;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.userdp-img,
.userdp-info {
position: absolute;
top: 0;
left: 0;
}
<div class="userdp">
<div class="userdp-info">
<div class="userdp-info-inner">
John Doe
</div>
</div>
<img src="https://unsplash.it/300/300?image=1005" class="userdp-img">
</div>
Hope it helped.

CSS position child under it's parent

Is it possible to position child element (C) under its parent (B), and above B's neighbor (C)?
It's a little bit difficult to describe, you can watch example here.
The question is to position blue div.inner above red div.neighbor AND under green div.outer.
To illustrate:
HTML code:
<div class="neighbor"> </div>
<div class="outer">
<div class="inner"></div>
</div>
CSS code:
.neighbor{
background-color: red;
height: 500px;
width: 500px;
}
.outer{
background-color: green;
width: 300px;
height: 300px;
position: fixed;
top: 0px;
left: 0px;
}
.inner{
background-color: blue;
width: 100px;
height: 100px;
position: fixed;
top: 0px;
left:250px;
}
JsFiddle
HTML:
<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>
CSS:
.red {
background-color: red;
height: 500px;
width: 500px;
z-index: 1;
}
.green {
background-color: green;
width: 300px;
height: 300px;
position: fixed;
top: 0px;
left: 0px;
z-index: 3;
}
.blue {
background-color: blue;
width: 100px;
height: 100px;
position: fixed;
top: 0px;
left: 250px;
z-index: 2;
}
.neighboor {
background-color: red;
height: 500px;
width: 500px;
position:fixed;
z-index:-200;
}
.outer {
background-color: green;
width: 300px;
height: 300px;
position: absolute;
top: 0px;
left: 0px;
}
.inner {
background-color: blue;
width: 100px;
height: 100px;
position:relative;
z-index: -100;
top: 0px;
left: 250px;
}