This question resulted from a fix suggested to this related question
I have 3 nested divs: outer, inner and item.
<div class="outer">
<div class="inner"><div class="item"></div></div>
</div>
Given the following basic CSS:
.outer{
width:50%;
height:100px;
border: 1px solid red;
position:relative;
overflow-x:hidden;
box-sizing:border-box;
}
.inner{
border:1px solid blue;
height:100%;
position: absolute;
box-sizing: border-box;
line-height:98px;
}
.item{
width:100px;
height:94px;
background-color:yellow;
display: inline-block;
border:1px solid green;
box-sizing:border-box;
vertical-align:middle;
}
The challenge is to center the 'item' div vertically with equal (or no) gaps above/below, and no vertical scrollbars appearing.
Codepen here
Update:
As pointed out below, I should add that multiple items of different heights must be centered. The best answer so far is to add a negative margin to each item, resulting in the following: http://codepen.io/anon/pen/dYWEYZ
However, this smells bad (to me) as it requires an offset that depends on 3 other properties.
The simple solution is http://codepen.io/anon/pen/jbmRjo
to add this to your .item class:
margin-top: -5px;
It's because of the borders on your outer and inner classes, as well as the line-height of the inner class. If you make the borders 2px instead of 1px, then the margin top becomes -9px. If you leave borders at 1px and make the line-height 96px the margin top becomes -3px.
So why can't you use a negative margin top?
If you use flexbox, this is easily achieved:
.inner{
border:1px solid blue;
height:100%;
position: absolute;
box-sizing: border-box;
line-height:98px;
display:flex;
align-items:center;
}
You can use display: table-cell; to get the vertical centering you want:
.outer{
width:50%;
height:100px;
border: 1px solid red;
position:relative;
overflow-x:hidden;
box-sizing:border-box;
}
.inner{
border:1px solid blue;
height:100%;
position: absolute;
box-sizing: border-box;
line-height:98px;
display: table; //must change this as well
}
.item{
width:100px;
height:94px;
background-color:yellow;
border:1px solid green;
box-sizing:border-box;
vertical-align:middle;
display:table-cell; //update display type
}
Related
I want to display some CSS3 animation inside a div box. So I need to apply overflow:hidden for unwanted overflow.
In case of test at fiddle its work well. But If I apply overflow:hidden at parent div in my page, its not display child content anymore.
Please see my fiddle and help me.
My Css:
.movcontainer{
position: absolute;
top:200px;
width:100%;
height:89%;
overflow:hidden;
z-index:55;
display: block;
border: 2px solid blue;
}
.contentbody{
position:fixed;
top:0px;
width:100%;
height:100%;
border: 2px solid green;
}
.parent{
position: absolute;
top:10%;
left:10%;
width:80%;
height:80%;
border: 2px solid red;
text-align:center;
display: inline-block;
clear: both;
overflow:hidden;
}
.child {
background-image:url('http://fallmeeting.agu.org/2012/files/2012/09/Dust-and-Aerosols-SWIRL-graphic.jpg');
border: 2px solid green;
width:600px;
height:600px;
background-position: center center;
background-repeat: no-repeat;
margin:auto;
transform-origin: center;
}
Update:
Here I have two more div which create for few positioning. One .movcontainer and other .contentbody. Here .contentbody have fixed position and problem occurred here. But this two div is essential for me.
Here is my update fiddle
Solved this problem by replace position:absolute by position:fixed on .parent div.
Here is the JSFiddle example of how my layout is: http://jsfiddle.net/qKP2v/13/
I want to emulate the look of a desktop application like Outlook or Photoshop for example where the left and right side columns are fixed and occupy the full height of the screen.
In my application there is a header at the top of the page. So I want my header and sidebars to be fixed and not move when the user scrolls. Only the #content area should move.
Is this too big a task to ask on here I don't know. I'm using CSS2 only (can't use CSS3 yet).
You can use position:fixed; to make it work
#header {
width:100%;
border: 1px solid #444444;
height: 100px;
margin-bottom:15px;
position:fixed;
top:0;
}
#wrapper {
width:100%;
margin: 0 auto;
background-color: #FFF;
min-height:200px;
border: 1px solid #F0F;
}
#aside-left {
float:left;
width:100px;
border: 1px solid #9C0;
position:fixed;
top:100px;
left:20px;
}
#content{
margin:0 130px;
margin-top:100px;
border: 1px solid red;
}
#aside-right {
float:right;
width:100px;
text-align: right;
border: 1px solid #9C0;
position:fixed;
top:100px;
right:20px
}
FIDDLE
I want to make a 1 px line from the left side of the screen to the end of a centered div.
The div is centered with margin: auto;.
This image shows how it should look:
Here's an example using calc:
.box{
width:200px;
height:200px;
border:1px solid blue;
margin:0 auto;
}
.line{
border: 1px solid red;
width: calc(((100% - 200px)/2) + 200px);
}
JSFiddle
Browser support
How about this solution? no extra markup needed, cross browser and does not depend on the width of the element
#content {
width:400px;
height: 200px;
margin:auto;
position: relative;
}
#content:before{
content: '';
height: 1px;
background: red;
position: absolute;
top: -5px;
right: 0;
width: 999%; /*a large number*/
}
Demo fiddle
here is another solution and it is cross browser http://jsfiddle.net/9qrSy/3
<div class="inner"></div>
<div class="wrapp"></div>
css
body {
padding:8px;
}
div.wrapp {
width:300px;
height:300px;
border:2px solid green;
margin:auto;
position:relative;
}
div.wrapp:before {
content:'';
position:absolute;
width:100%;
height:1px;
right:0;
top:-6px;
background:blue;
z-index:1;
}
.inner {
width:50%;
float:left;
position:absolute;
height:1px;
left:0;
top:12px;
background:blue;
}
I am not sure if this works in all browsers, but I believe hr takes up all the space you provide it with. Therefore you can give it a large negative left-margin and put it inside the centered div. Instead of a hr-element, you could use an empty div too, which might or might not be easier to use. You can set the border-top style of that div to a wider range of border-types (dotted for example).
<div id="content">
<hr id="bar" />
<div id="realcontent">
Something here
</div>
</div>
With CSS:
#content {
width: 400px;
margin: auto;
color: white;
}
#bar {
margin-left: -1000px;
margin-bottom: 5px;
background: blue;
}
#realcontent {
background-color: #000000;
}
css
#content2
{
clear:both;
width:1024px;
height:auto;
position:relative;
}
#content2 div:first-child
{
background:#E4ECF7;
width:445px;
height:25px;
margin:15px 0px 0px 223px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
border:1px solid #E0DCD1;
padding:5px 0px 0px 5px;
position:absolute;
}
#content2 div:last-child
{
width:1024px;
height:200px;
position:absolute;
border:1px solid #E0DCD1;
clear:both;
}
Html
<div id="content2">
<div>content</div>
<div>content</div>
</div>
Result
div1 is showing inside div2
I need
div1 then
div2
Please help me.
why using position absolute,no need for clear property for last-child
#content2
{
clear:both;
width:1024px;
height:auto;
position:relative;
}
#content2 div:first-child
{
background:#E4ECF7;
width:445px;
height:25px;
margin:15px 0px 0px 223px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
border:1px solid #E0DCD1;
padding:5px 0px 0px 5px;
/*position:absolute;*/
}
#content2 div:last-child
{
width:1024px;
height:200px;
/*position:absolute;*/
border:1px solid #E0DCD1;
/*clear:both;*/
}
div are cleared by default unless after using float property
remove position absolute from both child divs...
If you are using Position: absolute; specify the margin for both div's.
eg:
#content2 div:last-child
{
width:1024px;
height:200px;
position:absolute;
margin-top: xxx; /* specify the top margin */
border:1px solid #E0DCD1;
clear:both;
}
i think you are looking like this :- http://tinkerbin.com/3qRLgscO
Actually you made CSS bit of complicated for yourself.You can get your desired results through very simple CSS without using of positioning.
And if we are using float than we should use the clear for clearing the floated div's otherwise no need to use the clear property.
Here is the simple code of yours i have some simple changes in your CSS.....
HTML
<div id="content2">
<div>div1</div>
<div>div2</div>
</div>
CSS
#content2 {
background: none repeat scroll 0 0 red;
height: 200px;
width: 1024px;
}
#content2 div:first-child {
background: none repeat scroll 0 0 #E4ECF7;
height: 45px;
}
#content2 div:last-child {
background: none repeat scroll 0 0 yellow;
border: 1px solid #E0DCD1;
height: 45px;
}
I hope this will help you........
you can use the following properties in your div class.
float:left
clear:right;
I am using CSS to float a div next to another one. This div only appears if the user is looking at their own "business." When I don't clear anything, a large space appears between these divs and the next one. When I do clear the float, the text in the next div is pushed to the left. I think I am misunderstanding something about how to use the float and clear. I'm not very good with CSS.
How can I remove the space without destroying the "fs" div?
Here are pictures to show what is happening:
Here's the CSS and HTML code:
div.stuff {
border-bottom:dotted 1px;
border-left:dotted 1px;
border-right:dotted 1px;
border-top:dotted 1px;
padding:10px;
margin:10px;
width:35%;
height:65px;
border-radius: 5px;
}
div.container {
border-bottom:dotted 1px;
border-left:dotted 1px;
border-right:dotted 1px;
border-top:dotted 1px;
padding:10px;
padding-left:25px;
margin-bottom:10px;
position:relative;
height:65px;
width:45%;
top:-97px;
right:10px;
border-radius: 5px;
overflow: hidden;
float:right;
clear:right;
}
div.fs {
border-style:double;
text-align:center;
padding:10px;
margin:10px;
margin-left:20%;
width:60%;
border-radius: 5px;
}
<div class=stuff>
<img src=/economy/images/cash.png> Cash on Hand: 10,245<br>
<img src=/economy/images/worker.png> Workers Employed: 6<br>
<img src=/economy/images/machine.png> Machines Leased: 4
</div>
<div class=container>
Click Here to Manage Cash on Hand.<br>
Click Here to Manage this Business.<br>
Click Here to Disband this Business.
</div>
<br>
<div class=fs><a href=/economy.php?section=fs&id=7>Historical Financial Statements</a></div>
You need to float your left hand div, and use clear:both on the div at the bottom. I've made some changes in this jsFiddle.
perhaps this:
div.container {
border-bottom:dotted 1px;
border-left:dotted 1px;
border-right:dotted 1px;
border-top:dotted 1px;
padding:10px;
padding-left:25px;
margin-bottom:10px;
position:relative;
height:65px;
width:45%;
/*top:-97px;*/
margin-top:-97;
right:10px;
border-radius: 5px;
overflow: hidden;
float:right;
/*clear:right;*/
}
I would float your div.stuff to the left and your div.container to the right and just use clear: both on the div.fs element. I made a small fiddle to illustrate this. In this fiddle I added a wrapper class for clarity where I set a min-width to prevent that the right div floats down one line when the browser window is resized. Try it out!
Here's the CSS:
div.stuff {
border: 1px dotted black;
padding:10px;
margin:10px;
width:35%;
height:65px;
border-radius: 5px;
float: left;
}
div.container {
border: 1px dotted black;
padding:10px;
padding-left:25px;
margin-bottom:10px;
position:relative;
height:65px;
width:45%;
margin: 10px;
border-radius: 5px;
overflow: hidden;
float:right;
}
div.fs {
clear: both;
border-style:double;
text-align:center;
padding:10px;
margin:10px;
margin-left:20%;
width:60%;
border-radius: 5px;
}