Suppose I have two nested divs. Something like below:-
<div id="div1">Some name here
<div id="div2">DIV2</div>
</div>
Suppose the height and width of div1 is 100px. And the height and width of div2 is 50px. How do I make them appear concentric i.e div2 must lie inside div1 equidistant from all sides (using CSS).
If the two divs got fixed dimensions, you can simply put a margin on the second div. In your case :
#div2 {
margin: 25px;
}
Or, if the divs got variable dimensions, try :
#div1 {
position: relative;
}
#div2 {
position:absolute;
left:50%;
top:50%;
transform: translate(-50%,-50%);
}
OR :
#div1 {
text-align: center;
}
#div2 {
display: inline-block;
vertical-align: middle;
}
That's all the way I know to achieve that :)
I agree with the comment written by #Praveen but I would do some adjustments:
#div1{
display: table-cell;
vertical-align: middle;
}
#div2{
margin: auto;
}
You can use the automatic left and right margins like the following:
<style>
div#container {
border: 1px solid #000000;
width: 500px;
height: 200px;
}
div#inner {
border: 1px solid #FF0000;
width: 200px;
height: 100px;
margin: 0 auto;
}
</style>
<div id="container">
<div id="inner"></div>
</div>
JSFiddle Demonstration >
First, you might want to use percentages for widths in order to make them responsive.
To center div2 horizontally, insert the following in your css:
#div2 {
margin:0 auto;
}
To center div2 vertically,
#div1 {
display:block;
height: 100%;
}
#div2 {
vertical-align: middle;
display:block;
}
<style>
#div1
{
border: 1px solid blue;
width: 100px;
height:100px;
}
#div2
{
margin-left:20px;
border: 1px dotted red;
width:50px;
height:50px;
}
</style>
<div id="div1">Some name here
<div id="div2">DIV2</div>
</div>
here is the code to show div in another div with centre css
//If you have height of outer box and inner box both in px or em etc not in % or auto. Here just a logic given using a formula
#div1 {
display:block;
height: 500px;
width:50%;
margin: 0 auto;
border: 1px solid red;
}
#div2 {
display:block;
width:50%;
height:60px;
border: 1px solid green;
margin: -moz-calc( (500px - 60px) / 2 ) auto; // (height of outer box - height of inner box )/2
}
//may be you can manage height dynamically using js this should work for height in % also
<script type="text/javascript">
var div1_height = $('#div1').height();
var div2_height = $('#div2').height();
var top_margin = (div1_height-div2_height)/2;
$('#div2').css('margin',top_margin+'px auto');
</script>
#div1 {
display: grid;
place-content: center;
}
---
#div1 {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
---
#div1 {
display: flex;
}
#div2 {
margin: auto;
}
---
#div1 {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Related
FIDDLE
.a,.c
{
width: 100px;
height: 300px;
background-color: red;
display:inline-block;
}
.b
{
background-color: gray;
display:inline-block;
border: 1px solid;
}
.main
{
width:100%;
display:inline-block;
height: 300px;
}
Why does the div b is at the bottom. Please set height at the fiddle and check. It ll grow down. Does anybody know the reason?
inline-block default value for vertical-align in CSS is baseline. You need to set the vertical-align property to fix that.
.b
{
background-color: gray;
display:inline-block;
border: 1px solid #ccc;
vertical-align:top;
}
DEMO
Add vertical-align: top; rule to class b or all the classes that have the rule display: inline-block. display: inline-block is by default bottom aligned.
you can use table-cell instead of inline-block;
.a,.c
{
width: 100px;
height: 300px;
background-color: red;
display:table-cell;
}
.b
{
background-color: gray;
display:table-cell;
position: relative;
border: 1px solid;
vertical-align:middle;
}
.main
{
width:100%;
display:table;
height: 300px;
}
Jsfiddle
inline-block behave as the block level when your browser will be re-sized larger or if your contents exceeds than its width while display: table-cell; won't. You'll also find the gap between block when you apply display: inline-block;
more can be read on this question.
question
There are rules in display: in-line block that mess it up in your case. Just change them to float: left as in this jsfiddle
.a,.c
{
width: 100px;
height: 300px;
background-color: red;
float: left;
}
.b
{
background-color: gray;
float: left;
border: 1px solid;
}
.main
{
width:100%;
float: left;
height: 300px;
}
You don't have any contents on first and last divs.
Because all the divs are displayed inline-block the default position will go to baseline. Try adding some contents to the .a and .c divs, you will see different behaviors.
When you are all setup with the contents you need to adjust the vertical-align to have your desired look.
I have a wrapper div and a content div.
<style type="text/css">
#wrapper {
width: 500px;
border: 1px solid red;
overflow: auto;
}
#content {
border: 1px solid blue;
}
.column {
display: inline-block;
width: 300px;
}
</style>
<div id="wrapper">
<div id="content">
<div class="column">hello</div>
<div class="column">world</div>
</div>
</div>
The columns appear on two rows instead of the same row. If I style then with display: table-cell they each take up 50% of the width, but still won't expand the content beyond the wrapper's width.
How do I make the content div expand to fit both columns on the same row and cause scrolling on the wrapper div?
if you change you styles to the following you will achieve what you want:
#wrapper {
width: 500px;
border: 1px solid red;
overflow: auto;
}
#content {
display: inline-block;
border: 1px solid blue;
white-space:nowrap;
}
.column {
vertical-align:top;
display: inline-block;
width: 300px;
white-space:normal;
}
Example
Ohh I think I see the problem now, try:
#wrapper {
width: 500px;
border: 1px solid red;
overflow: auto;
}
#content {
white-space:nowrap;
border: 1px solid blue;
}
.column {
display: inline-block;
width: 300px;
}
http://jsfiddle.net/krowe/z3TS8/
In .column if you use display:table-cell, the column will be aligned in the same row as you need. And its working fine for me. Hope this is what you are looking for JSFiddle
#wrapper {
width: 500px;
border: 1px solid red;
overflow: auto;
display:table;
}
#content {
overflow: hidden;
display:table-row;
}
.column {
width:50%;
border: 1px solid blue;
display:table-cell;
}
This is in style.css:
#top{
background: white;
text-align: center;
height: 180px;
border-bottom: solid 1px #efeecc;
}
#topLogo{
background: yellow;
width: 25%;
float:left;
}
#topDescription {
background: red;
width: 75%;
text-align: center;
}
#topDepartment {
background: blue;
float:right;
width: 25%;
text-align: right;
}
This is index.html:
<div id="top">
<div id="topLogo">
<img src="img/book.jpg" width="170" height="160" border="0"/>
</div>
<div id="topDescription">
Page title
</div>
<div id="topDepartment">
Category
</div>
</div>
This is the expected outcome:
This is the current outcome:
http://jsfiddle.net/p2T5q/
Here's a demo: http://jsfiddle.net/p2T5q/7/
CSS:
#top{
background: white;
text-align: center;
display:table;
height: 180px;
border-bottom: solid 1px #efeecc;
}
#topLogo{
background: yellow;
width: 25%;
display:table-cell;
}
#topDescription {
background: red;
display:table-cell;
width: 50%;
text-align: center;
}
#topDepartment {
background: blue;
display:table-cell;
width: 25%;
text-align: right;
}
Well, the quick answer is that your div's total width is 125%, so you would need to change that so it equals 100%.
Second, you might want to put a clear fix on that top div so it encompasses all of the floating ones.
Also, don't use inline styles because you can put them in the CSS file. You are giving a fixed width to an image that sits inside a div with a percentage width. That is sure to break if the user reduces the size of his viewport.
Here you go, I fixed the fiddle and copied the answer here:
http://jsfiddle.net/p2T5q/2/
#top{
height: 180px;
border: solid 1px #555555;
position:relative;
}
#top * {
float:left;
}
#topLogo{
background: yellow;
width: 25%;
}
#topDescription {
background: red;
width: 50%;
}
#topDepartment {
background: blue;
width: 25%;
}
Basically make sure the width percentages add up to 100% and float all elements to left so they know in relation to what they should position themselves. You can use the * to select all subelements. I would appreciate you accepting this answer so I can finally get my 50 damn coins and make comments, lol.
Given this css:
#parent {
width: 100px;
height: 100px;
background-color: #090;
}
.childs {
width: 50px;
height: 50px;
background-color: #009;
border: 1px solid #999;
}
and this html:
<div id="parent">
<div class="childs"><p>aaa</p></div>
<div class="childs"></div>
<div class="childs"></div>
</div>
this is demo
http://jsfiddle.net/A3PJu/2/
I want that children divs placing in horizontal and not in vertical (as are they now), how make this?
float: left for children tags, not working in this case
You can use display:inline-block with white-space:nowrap. Write like this:
#parent {
width: 100px;
height: 100px;
background-color: #090;
white-space:nowrap;
font-size:0;
}
.childs {
width: 50px;
height: 50px;
background-color: #008;
border: 1px solid #999;
display:inline-block;
*display:inline;/* For IE7 */
*zoom:1;/* For IE7 */
white-space:normal;
font-size:13px;
vertical-align:top;
}
Check this http://jsfiddle.net/A3PJu/3/
The problem is that the width of the parent element is not big enough for 3 times 50px .childs. If you increase the #parent width to say 200px, float: left will work.
I have the following DIVs structure:
<div id="d1">
<div id="d2">
</div>
<div id="d3">
<div id="d4"></div>
<div id="d5"></div>
</div>
</div>
DIV d2 is the left column (float: left), while d3 is on the right (float: right). d4 and d5 all go into right column d3, one below the other. The widths are all fixed.
DIVs d4 has fixed height of 300px.
The content of div d2 has dynamic height but it is at least 300px high.
My goal is to make DIV d5's height to fill the remaining space in the right column (d3) so that the total height of this column is equal to the dynamic height of the left column (d2).
I would like a pure CSS solution, no JS.
This is the CSS I am using:
#d1 {
width: 990px;
border: 1px solid black;
background-color: pink;
}
#d2 {
float: left;
width: 300px;
background-color: yellow;
}
#d3 {
float: right;
width: 690px;
}
#d4 {
background-color: blue;
width: 690px;
height: 300px;
}
#d5 {
background-color: brown;
width: 690px;
}
You can use display: tale; and table-cell; with a heightof 100% on d5.
#d1 {
width: 990px;
border: 1px solid black;
background-color: pink;
padding: 3px;
display: table;
height: 10000px;
}
#d2 {
display: table-cell;
width: 300px;
background-color: yellow;
}
#d3 {
display: table-cell;
width: 690px;
}
#d4 {
background-color: blue;
width: 690px;
height: 300px;
}
#d5 {
background-color: brown;
width: 690px;
height: 100%;
}
http://jsfiddle.net/nnjse/1/
Add to main block #d1 border-right 690px and using negative margin -690px put right column #d3 over it. Border will look like right column's background. If #d2 gets higher #d1's border will make illusion that right column fills whole parent height. The same principle with negative margins but in vertical direction can be used for #d4 and #d5.
#d1{
width:300px;
margin:0 auto;
border-right:690px solid #a52a2a;
background:#ffff00;
}
#d2{
width:300px;
float:left;
}
#d3{
width:690px;
float:right;
margin-right:-690px;
border-top:300px solid #EEE;
background:#a52a2a;
}
#d4{
height:300px;
margin-top:-300px;
background:#0000ff;
}
#d5{
height:100%;
}