child div align center on parent div with overflow? - javascript

Hello Stackoverflow Team,
How can the child div inside the parent div with overflow have a right and left margin? I'm trying to solve the issue but it does not give a clean solution for it.
Attempt:
margin-right wont work
div {
border: 1px solid black;
}
.parent {
width: 300px;
height: 300px;
margin: auto;
position: relative;
overflow: auto;
}
.child {
width: 350px;
height: 150px;
top: 50px;
margin-left: 20px;
margin-right: 20px;
position: absolute;
display: inline-block;
}
<div class="parent">
<div class="child"></div>
</div>
My unclean Solution:
div {
border: 1px solid black;
}
.parent {
width: 300px;
height: 300px;
margin: auto;
position: relative;
overflow: auto;
}
.child {
width: 350px;
height: 150px;
top: 50px;
margin-left: 20px;
border-right: 20px solid red;
position: absolute;
display: inline-block;
}
<div class="parent">
<div class="child"></div>
</div>
any better way to solve the issue?

Since you are using position: absolute for the child, best way to achieve what you want is remove position: absolute then add the margins you need.
div{
border: 1px solid black;
}
.parent {
width:300px;
height:300px;
margin:auto;
position: relative;
overflow: auto;
}
.child {
width:350px;
height:150px;
top: 50px;
margin: 50px 20px 0;
display: inline-block;
}
<div class="parent">
<div class="child"></div>
</div>
Update
If you need the child div to be position: absolute you will have to wrap it in another div as follow:
div{
border: 1px solid black;
}
.parent {
width:300px;
height:300px;
margin:auto;
position: relative;
overflow: auto;
}
.child {
border-color: red;
position: absolute;
top: 20px;
height: 150px;
}
.sub-child {
width:350px;
height:150px;
margin: 0 20px;
display: inline-block;
}
<div class="parent">
<div class="child">
<div class="sub-child"></div>
</div>
</div>

Related

CSS positioning the search menu correctly

I have main outer div.
----Inside that I have outer div
--------and inside that I have inner search menu div
--------and search menu content list div
Now I have given fixed position to inner search menu div.
When I scroll outer div part It's position is fixed correctly.
The problem is when I scroll the main div [outer most]then also menu item div is still fixed and it is not scrolling with its outer div.
How can I make it scroll with outer div.
.main{
width:600px;
height: 700px;
border:1px solid black;
position:relative;
}
.outer{
width:500px;
height: 500px;
border:1px solid black;
position:relative;
overflow:scroll;
}
.inner{
width:400px;
height: 40px;
border:1px solid black;
position:fixed;
}
.content{
margin-top:45px;
height:600px;
border:1px solid red;
width:100px
}
<html>
<head>
</head>
<body>
<div class="main">
<div class="outer">
<div class="inner">
</div>
<div class="content">
</div>
</div>
</div>
</body>
</html>
You could use position: sticky instead of fixed and define top and left:
.inner {
...
position: sticky;
top: 0;
left: 0;
}
In this case .main doesn't need a position.
Working example:
.main {
width: 600px;
height: 700px;
border: 1px solid black;
}
.outer {
width: 500px;
height: 500px;
border: 1px solid black;
position: relative;
overflow: scroll;
}
.inner {
width: 400px;
height: 40px;
border: 1px solid black;
position: sticky;
top: 0;
left: 0;
}
.content {
margin-top: 45px;
height: 600px;
border: 1px solid red;
width: 100px
}
<div class="main">
<div class="outer">
<div class="inner"></div>
<div class="content"></div>
</div>
</div>
You could also use position: absolute and define top and left:
.inner {
...
position: absolute;
top: 0;
left: 0;
}
In this case you have to define position: relative for .main but not for .outer.
Working example:
.main {
width: 600px;
height: 700px;
border: 1px solid black;
position: relative;
}
.outer {
width: 500px;
height: 500px;
border: 1px solid black;
overflow: scroll;
}
.inner {
width: 400px;
height: 40px;
border: 1px solid black;
position: absolute;
top: 0;
left: 0;
}
.content {
margin-top: 45px;
height: 600px;
border: 1px solid red;
width: 100px
}
<div class="main">
<div class="outer">
<div class="inner"></div>
<div class="content"></div>
</div>
</div>

Resizing of a Absolute Positioned Element

The Element is not resizing and browser is not re-calculating the position of the Element even though all the widths and heights are mentioned in percentages only. This code basically renders a Squared DIV with Two elements one with Centered and another at right top corner of the SQuare.
<html>
<head>
<style>
.board {
font-size: 8em;
}
.cellContainerOuter {
width: 100%;
padding-bottom: 100%;
position: relative;
background-color: yellow;
border: 1px solid black;
}
.cellContainer {
width: 100%;
padding-bottom: 100%;
position: absolute;
display: table;
}
.cellContainerInner {
padding-bottom: 50%;
padding-top: 50%;
padding-left: 50%;
padding-right: 50%;
position: relative;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.text {
border: 0px dotted white;
position: absolute;
bottom: 20%;
top: 20%;
left: 20%;
right: 20%;
overflow: hidden;
text-overflow: clip;
display: block;
}
.weight {
position: absolute;
right: 0px;
top: 0px;
margin: 2px;
border: 1px solid white;
}
</style>
</head>
<body>
<div class="board">
<div id="cell3_C_1" class="cellContainerOuter" title="ఇ">
<div id="cell2_C_1" class="cellContainer" title="ఇ">
<div id="cell_C_1" class="cellContainerInner" title="ఇ">
<span id="weight_C_1" class="weight" title="6">6</span>
<span id="text_C_1" class="text" title="ఇ">ఇ</span>
</div>
</div>
</div>
</div>
</body>
</html>
Any help to resolve this issue is highly appreciated.
CodePen: https://codepen.io/mdileep/full/MGrNgw/
Refactored your CSS.
Let me know if this is the behaviour you are expecting.
OR with responsive you mean to change your height of 6 as well?
body {
margin: 0;
}
.cellContainer {
position: relative;
background: yellow;
width: calc(100vw - 20px);
height: calc(100vh - 20px);
margin: 10px;
}
.cellContainerInner {
border: 1px solid;
height: 100%;
display: flex;
}
.weight {
font-size: 8em;
position: absolute;
right: 0px;
top: 0px;
border: 1px solid;
}
.text {
flex: 1;
font-size: 8em;
/* border:2px solid; */
align-self: center;
text-align: center;
}
<div class="board">
<div id="cell3_C_1" class="cellContainerOuter" title="ఇ">
<div id="cell2_C_1" class="cellContainer" title="ఇ">
<div id="cell_C_1" class="cellContainerInner" title="ఇ">
<span id="weight_C_1" class="weight" title="6">6</span>
<span id="text_C_1" class="text" title="ఇ">ఇ</span>
</div>
</div>
</div>
</div>

divs not filling white-space in display: table;

I have created a slideshow and to the right of it there are divs, they will be enlarged when you hover over them and when you take your cursor off the div it will shrink. But, the aside (the divs parent) is in the correct place where it should be, it's the divs that aren't filling the top of the aside element. How can I get the divs to fill the aside element and not break anything else?
.thing {
height: 120px;
width: 250px;
z-index: 10;
position: relative;
border: 5px solid brown;
}
.thing:hover {
position: absolute;
height: 100%;
top: 0;
left: 0;
z-index: 11;
}
.report {
text-align: left;
vertical-align: top;
display: table;
width: 100%;
}
.aside {
display: table-cell;
padding-top: 5px;
width: 250px;
border-radius: 10px;
border: 2px solid black;
overflow: hidden;
position: relative;
height: 385px;
}
.container {
position: relative;
width: 750px;
height: 400px;
border-radius: 5px;
border: 1px solid black;
overflow: hidden;
}
<div class="report">
<div id="imgGallary" class="container">
<img src="images/companies.png" alt="" width="750" height="400" />
<img src="gallery" alt="" width="750" height="400" />
</div>
<aside class="aside">
<div id="c1"></div>
<div class="thing" style="background-color: blue;">
<h1>Find Us!</h1>
</div>
<div class="thing" style="background-color: orange;"></div>
<div class="thing" style="background-color: pink"></div>
</aside>
</div>
Your CSS layout is confusing display: table and display: relative. They aren't compatible like you have them. The preferred way to layout your .container and the aside would be to use floats. I revised your example to float those two containers next to each other (roughly at a 80/20 split for the width). This has the added bonus of making your layout responsive.
Working codepen:
http://codepen.io/staypuftman/pen/vKoPmw
.thing {
height: 120px;
width: 250px;
position: relative;
border: 5px solid brown;
}
.thing:hover {
position: absolute;
height: 100%;
top: 0;
left: 0;
z-index: 1;
}
.report {
text-align: left;
vertical-align: top;
display: table;
width: 100%;
}
.aside {
padding-top: 5px;
width: 18%;
border-radius: 10px;
border: 1% solid black;
overflow: hidden;
float: left;
position: relative;
height: 385px;
}
.container {
float: left;
width: 80%;
height: 400px;
border-radius: 5px;
border: 1px solid black;
overflow: hidden;
}

Height 100% with two divs using float left and right (with clear)

Is very simple:
HTML:
<div>
<section class="left">
</section>
<section class="right">
</section>
<div class="clear"></div>
</div>
CSS:
div, section { border: 1px solid #000; }
.left { height: 100%; width: 200px; float: left; height: 200px; }
.right { width: 300px; float: right; height: 300px; }
.clear { clear: both; }
Fiddler: http://jsfiddle.net/H2c6g/
How I do for the div use the 100% of height?
You can use width: 100% on the inner <section>s as long as you also define a height on the wrapping <div>. Try this CSS:
div { height: 400px; background: #ccc; }
.left { height: 100%; width: 200px; float: left; background: #c00; }
.right { width: 300px; float: right; height: 300px; background: #00c; }
.clear { clear: both; }
Working Fiddle
Just use:
body,html {
height: 100%;
}
And make the div's height: 100%
As seen in this updated fiddle
Hmm not sure if this is what you are trying to do
<div class="outer">
<section class="left">
</section>
<section class="right">
</section>
<div class="clear"></div>
</div>
div, section { border: 1px solid #000; }
.left { height: 100%; width: 200px; float: left; height: 200px; }
.right { width: 300px; float: right; height: 300px; }
.clear { clear: both; }
div.outer{
position:absolute;
height:100%;
}
You can use flexbox if you don't need to support older IE browsers.... Check it out below.
display:flex
Notice that I am only setting a height for the .left class the .right class matches the height.
http://jsbin.com/curoruni/1/edit
Here is how I would do it, not using any floats
<div id="wrapper">
<div class="left"></div><div class="right"></div>
</div>
And
#wrapper {
border: 1px solid red;
height: 500px;
width: 100%;
overflow: hidden;
position: absolute; top: 0; left: 0;
}
.left {
position: relative;
background-color: yellow;
display: inline-block;
height: 100%; width: 50%;
margin: 0px;
}
.right {
position: relative;
background-color: blue;
display: inline-block;
width: 50%; height: 100%;
margin: 0px;
}
http://jsfiddle.net/fU379/

Overflow causing the hover menu display and its cutting not displaying properly

CSS:
.main{
width: 500px;
margin: 0px auto;
overflow-x: scroll;
background: #ccc;
min-height: 500px;
position: relative;
}
.inner{
position: absolute;
top: -20px;
width: 50px;
height: 50px;
background: blueviolet;
left: 200px;
display: none;
}
.menu{
color: #000;
text-decoration: none;
font-size: 14px;
padding:0px 0 0 10px;
}
.main .menu:hover >.inner{display: block !important;}
HTML:
<div class="main">
Menu <div class="inner"></div>
</div>
help me if Anyone can...
Please find fiddle
i have doubt, why you putting css top: -20px;,
if u change top:0; visible correctly

Categories

Resources