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.
Related
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%);
}
I have this JSFiddle. Can someone explain, why is the anchor position misaligned relative to its siblings? I know I can correct it with position relative and negaitve top offset, but I don't understand, why it is like this in the first place.
HTML:
<div class="container">
<div class="left"></div>
Some link
<div class="right"></div>
</div>
CSS:
.container {
height: 25px;
white-space: nowrap;
}
.container .left {
border: 1px solid black;
display: inline-block;
margin: 0;
height: 25px;
width: 80px;
padding: 0;
}
.container .right {
border: 1px solid black;
display: inline-block;
margin: 0;
height: 25px;
width: 80px;
padding: 0;
}
.container a {
display: inline-block;
border: 1px solid black;
height: 25px;
width: 300px;
margin: 0;
}
The reason of this behaviour is due to the absence of text inside your .left and .right elements.
By default inline-block elements have vertical-align: baseline, but since you have empty elements there's no baseline, so they will be automatically aligned to the parent baseline (if you add some text inside them — even a — you would istantly solve the problem)
In order to prevent this behaviour you could also set a common vertical-align to all .container children.
You can add
vertical-align: top;
to .container a
This wil align the anchor with the divs.
You need to provide vertical-align property when you are declaring an inline-block.
Here you go.
WORKING DEMO
The CSS Change:
.container a {
display: inline-block;
border: 1px solid black;
height: 25px;
width: 300px;
margin: 0;
vertical-align:top;
}
You can use so many Option
1. Remove Display:inline-block and add float:left
Here the Demo
2. Use css vertical-align:top
Here demo
I'm trying to create a square box that should be clickable and lead the user to another page. For this, and to add some hover effects later, I am using the < a > element.
Here is the html-markup:
<a id="about" href="">
<p>About Me</p>
</a>
Here is the CSS markup for the element:
a#about {
display: block;
width: 300px;
height: 300px;
border: 1px solid black;
}
Now, I can center the text horizontally by using the text-align property without problems. However, I have yet to find a way on how to center the text vertically.
I tired using margins and paddings, but they will just change the width of the containing box.
Do you guys have any suggestions?
Thanks!
If the text will only ever be one line high, set line-height:300px to match the height of the element.
like this
DEMO
CSS
a#about {
display: table-cell;
width: 300px;
height: 300px;
border: 1px solid black;
vertical-align:middle;
}
p{
text-align:center;
}
Try this:
a#about {
display: table-cell;
vertical-align: middle;
}
p {
text-align: center;
}
demo
here you go
http://jsfiddle.net/tMfA7/
a#about {
display: block;
width: 300px;
height: 300px;
line-height:300px;
border: 1px solid black;
text-align:center;
margin:0;
}
Css:
a#about {
display: block;
width: 300px;
height: 300px;
line-height:300px;
border: 1px solid black;
}
Note: if you had text two lines of text then the second line will be 300px below the first
For this you can try this Css code in your style sheet.
vertical-align:central;
I have three divs inside one div.
<div class="parent">
<div class="child"> Text</div>
<div class="child">Text</div>
<div class="child">Text</div>
</div>
I want the child divs to fill up the width of the parent (the combined width of the children should be the parent). However, this is not happening, I'm falling short. I think this is because the child divs are setting width based on their content.
How do I achieve what I want?
CSS-
.child {
background: white;
color: #a7a9ac;
cursor: pointer;
font-size: 15px;
border-right: 1px;
border-top: 0;
border-bottom: 0;
border-left: 0;
border-style: solid;
border-color: #faded-grey;
padding-right: 10px;
margin: 0 auto;
height: 100%;
}
.parent {
border-top: 1px;
border-left: 0;
border-bottom: 1;
border-style: solid;
border-color: #faded-grey;
border-right: 0;
width: 100%;
margin-right: 0px;
height: 80px;
}
If you know there will always be three children, you can simply use:
.parent > .child {
float: left;
width: 33%;
}
.parent {
overflow: auto; /*or whatever float wrapping technique you want to use*/
}
If you do not know how many children there are, you will need to use CSS tables, flexbox, or perhaps combine inline-blocks with text-align: justify.
You can add these three properties to the .child CSS rule:
width:33%;
float:left;
box-sizing: border-box;
The last line makes sure it will also work when you add borders, padding and margin to the boxes.
ONLINE DEMO
Ps: not directly related but there is also an error in the border-bottom for parent, corrected in fiddle above. When you use non-0 value you need to specify unit:
border-bottom:1px;
I needed for my solution to accommodate a variance in the number of elements for them to be completely responsive.
I discovered an interesting solution to this. It essentially displays like a table. Every element shares available width, and this also works for images very well:
http://jsfiddle.net/8Qa72/6/
.table {
display: table;
width: 400px;
}
.table .row {
display: table-row;
}
.table .row .cell {
display: table-cell;
padding: 5px;
}
.table .row .cell .contents {
background: #444;
width: 100%;
height: 75px;
}
I'm surprised that no one suggested the css3 solution using flex namely:
.parent {
display: flex;
flex-wrap: wrap;
}
.child {
flex: 1;
}
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.