Div element misbehave - html

My second inner div position is weirdly adjusted when my first inner div have a long link text. How to fix it?
My html code:
<div class='div-wrapper'>
<div class='inner-div1'>
This is a long link
</div>
<div class='inner-div2'>
Link 2
</div>
</div>
My css code:
.div-wrapper {
border: 1px solid black;
width: 200px;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-block;
border: 1px solid black;
width: 90px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}
link to the picture of the div:
https://www.dropbox.com/s/9zs4mgj7izuqsp1/question.png?dl=0

The problem is with your CSS. Particularly the .div-wrapper div
You need to change the display setting from inline-block to inline-table to get it inside the cell. You mentioned that you wanted the box inside the larger box, but you need to clarify how exactly you want the inner boxes to be placed inside the larger box (ex: small gap between the boxes, both perfectly fit inside the large box with equal sizes)

Just changed inline-block to inline-flex for your inner div and looks fine.
.div-wrapper {
border: 1px solid black;
width: 200px;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-flex;
border: 1px solid black;
width: 90px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}
<div class='div-wrapper'>
<div class='inner-div1'>
This is a long link
</div>
<div class='inner-div2'>
Link 2
</div>
</div>

Just have to fix this, I don't think any solution here explains why the problem exists. Just to add up, the problem with this is because vertical-align is set to baseline by default.
What you have to do is set the vertical-align to top
Insert it in your CSS:
.div-wrapper div {
vertical-align: top;
}
Link to solution: https://jsfiddle.net/Lnvgkfz3/

Small changes in CSS
.div-wrapper {
border: 1px solid black;
width: auto;
height:70px;
margin: auto;
margin-top: 10px;
padding: 0;
}
.div-wrapper div {
display: inline-block;
border: 1px solid black;
width: 190px;
height: 60px;
text-align: center;
}
.div-wrapper div a {
display: block;
text-decoration: none;
}

Related

DIV going down after using Inline Block [duplicate]

This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Closed 1 year ago.
.dashboard-monthly-sales
{
display: inline-block;
margin-top: 5px;
margin-left: 5px;
background-color: #252525;
border: 1px solid #595E57;
width: 40%;
height: 250px;
}
.dashboard-small-total-sales
{
display: inline-block;
margin-top: 5px;
margin-left: 5px;
background-color: #252525;
border: 1px solid #595E57;
width: 25%;
height: 250px;
}
<div class="dashboard-monthly-sales">
<h2>Monthly Sales - <?php echo date("Y"); ?></h2>
</div>
<div class="dashboard-small-total-sales">
</div>
This is my html code and css code when I add an element inside a div one block is going down. Is there any problem with display inline block part. I really can't figure out hope somebody will help
Thanks.
You can use this css hope it's work
.dashboard-monthly-sales{
display: inline-block;
margin-top: 5px;
margin-left: 5px;
background-color: #252525;
border: 1px solid #595E57;
width: 40%;
height: 250px;
vertical-align:top; // Add this line in your code
}
.dashboard-small-total-sales{
display: inline-block;
margin-top: 5px;
margin-left: 5px;
background-color: #252525;
border: 1px solid #595E57;
width: 25%;
height: 250px;
vertical-align:top; // Add this line in your code
}
otherwise, you can use display: flex it helps you what you want
Simply adding vertical-align: top; to the first div solves the problem. This tells the CSS to align the element to the top of the tallest element on the line.
.dashboard-monthly-sales
{
display: inline-block;
margin-top: 5px;
margin-left: 5px;
background-color: #252525;
border: 1px solid #595E57;
width: 40%;
height: 250px;
vertical-align: top;
}
.dashboard-small-total-sales
{
display: inline-block;
margin-top: 5px;
margin-left: 5px;
background-color: #252525;
border: 1px solid #595E57;
width: 25%;
height: 250px;
}
h2 {
display: block;
}
<div class="dashboard-monthly-sales">
<h2>Monthly Sales - <?php echo date("Y"); ?></h2>
</div>
<div class="dashboard-small-total-sales">
</div>
Try to use a main div containing the inner divs, so that all the inner divs will be aligned inside that main div. And use some css changes as below, so that adding the text inside the empty div will not affect the alignment.
.dashboard-monthly-sales
{
display: inline-block;
margin-top: 5px;
margin-left: 5px;
background-color: #252525;
border: 1px solid #595E57;
width: 40%;
height: 250px;
}
.dashboard-small-total-sales
{
display: inline-block;
margin-top: 5px;
margin-left: 5px;
background-color: #252525;
border: 1px solid #595E57;
width: 25%;
height: 250px;
}
.container{
display: inline-flex;
width: 100%;
}
<html>
<body>
<div class="container">
<div class="dashboard-monthly-sales">
<h2>Monthly Sales - <?php echo date("Y"); ?></h2>
</div>
<div class="dashboard-small-total-sales">
</div>
</div>
</body>
</html>
use one div as a container which will cover both divs. and in CSS use positioning properties and align it to according to your wish. first off all define the width of whole page.

Why does not margin-top less than a specific value work for an inline-block element?

In the following code when i specify margin-top for #thirdDiv, It doesn't work until i give it 36px.
What is the reason?
#Container {
border: 15px solid orange;
width: 350px;
}
#firstDiv {
display: inline-block;
border: 1px solid red;
font-size: 1em;
}
#secondDiv {
border: 1px solid green;
display: inline-block;
font-size: 2em;
}
#thirdDiv {
display: inline-block;
border: 1px solid pink;
font-size: 1em;
margin-top: 36px;
}
<div id="Container">
<div id="firstDiv"> a </div>
<div id="secondDiv"> b </div>
<div id="thirdDiv"> c </div>
</div>
Because the child elements of your Container element are based on the bottom of that div. If you add vertical-align: top to your child elements, any margin-top is possible. You can try it out in this CodePen where I copied you code and tidied the CSS up a bit. Note that you can choose to only put vertical-align: top in your #thirdDiv element. This way you can keep the other two divs in their original position.
What you are looking for is padding. The CSS margin properties are used to create space around elements, outside of any defined borders whereas the CSS padding properties are used to generate space around an element's content, inside of any defined borders.
Try the following instead of applying margin-top to #thirdDiv.
#Container {
padding-top: 36px;
border: 15px solid orange;
width: 350px;
}
#firstDiv {
display: inline-block;
border: 1px solid red;
font-size: 1em;
}
#secondDiv {
border: 1px solid green;
display: inline-block;
font-size: 2em;
}
#thirdDiv {
display: inline-block;
border: 1px solid pink;
font-size: 1em;
}
<div id="Container">
<div id="firstDiv"> a </div>
<div id="secondDiv"> b </div>
<div id="thirdDiv"> c </div>
</div>
You must specify a value for margin-top else it won't know how much margin to add.
What were typing for margin-top before, margin-top: ;?
What is your desired effect?

Why do my div move down when I add an image [duplicate]

All of the elements within .track-container should line up nice and in line, each side by side, constrained by the 200px height they've been given with no weird margins or padding. Instead, you have the strangeness that occurs in the aforementioned fiddle.
What is causing .album-artwork and .track-info to get pushed halfway down the page, and how can I fix it? Also, I acknowledge that a table may be a better way of approaching this whole setup, but I want to figure out the problem from the code above so I can learn from this mistake.
.track-container {
padding:0;
width: 600px;
height: 200px;
border: 1px solid black;
list-style-type: none;
margin-bottom: 10px;
}
.position-data {
overflow: none;
display: inline-block;
width: 12.5%;
height: 200px;
margin: 0;
padding: 0;
border: 1px solid black;
}
.current-position, .position-movement {
height: 100px;
width: 100%;
margin: 0;
padding: 0;
border: 1px solid black;
}
.album-artwork {
display: inline-block;
height: 200px;
width: 20%;
border: 1px solid black;
}
.track-info {
display: inline-block;
padding-left: 10px;
height: 200px;
border: 1px solid black;
}
<div class="track-container">
<div class="position-data">
<div class="current-position">1</div>
<div class="position-movement">2</div>
</div>
<div class="album-artwork">fdasfdsa</div>
<div class="track-info">fdafdsa</div>
</div>
Here's a JSFiddle.
10.8 Line height calculations: the 'line-height' and 'vertical-align' properties
The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge.
This is a common issue involving inline-block elements. In this case, the default value of the vertical-align property is baseline. If you change the value to top, it will behave as expected.
Updated Example
.position-data {
vertical-align: top;
}
The elements inside .track-container are inline-level boxes in the same line box.
Therefore, their vertical alignment is specified by the vertical-align property:
This property affects the vertical positioning inside a line box of
the boxes generated by an inline-level element.
By default, its value is baseline:
Align the baseline of the box with the baseline of the parent box. If
the box does not have a baseline, align the bottom margin edge with
the parent's baseline.
In this case, they all have baselines, which are calculated according to
The baseline of an 'inline-block' is the baseline of its last line box
in the normal flow, unless it has either no in-flow line boxes or if
its 'overflow' property has a computed value other than 'visible', in
which case the baseline is the bottom margin edge.
The following image clarifies what's happening (the red line is the baseline):
Therefore, you can
Change the vertical alignment of the elements, e.g. to top, middle or bottom
.track-container > * {
vertical-align: middle;
}
.track-container {
padding: 0;
width: 600px;
height: 200px;
border: 1px solid black;
list-style-type: none;
margin-bottom: 10px;
}
.position-data {
overflow: none;
display: inline-block;
width: 12.5%;
height: 200px;
margin: 0;
padding: 0;
border: 1px solid black;
}
.current-position,
.position-movement {
height: 100px;
width: 100%;
margin: 0;
padding: 0;
border: 1px solid black;
}
.album-artwork {
display: inline-block;
height: 200px;
width: 20%;
border: 1px solid black;
}
.track-info {
display: inline-block;
padding-left: 10px;
height: 200px;
border: 1px solid black;
}
.track-container > * {
vertical-align: middle;
}
<div class="track-container">
<div class="position-data">
<div class="current-position">1</div>
<div class="position-movement">2</div>
</div>
<div class="album-artwork">fdasfdsa</div>
<div class="track-info">fdafdsa</div>
</div>
Set the overflow of the elements to something different than visible, e.g. hidden or auto, so that their baseline will be their bottom margin edge.
.track-container > * {
overflow: hidden;
}
.track-container {
padding: 0;
width: 600px;
height: 200px;
border: 1px solid black;
list-style-type: none;
margin-bottom: 10px;
}
.position-data {
overflow: none;
display: inline-block;
width: 12.5%;
height: 200px;
margin: 0;
padding: 0;
border: 1px solid black;
}
.current-position,
.position-movement {
height: 100px;
width: 100%;
margin: 0;
padding: 0;
border: 1px solid black;
}
.album-artwork {
display: inline-block;
height: 200px;
width: 20%;
border: 1px solid black;
}
.track-info {
display: inline-block;
padding-left: 10px;
height: 200px;
border: 1px solid black;
}
.track-container > * {
overflow: hidden;
}
<div class="track-container">
<div class="position-data">
<div class="current-position">1</div>
<div class="position-movement">2</div>
</div>
<div class="album-artwork">fdasfdsa</div>
<div class="track-info">fdafdsa</div>
</div>
Make sure the elements have no in-flow line box, so that their baseline will be their bottom margin edge. That is, the contents should be out of flow:
An element is called out of flow if it is floated, absolutely
positioned, or is the root element. An element is called in-flow if it
is not out-of-flow.
So for example, you can place the contents of the elements in a wrapper, and style it with float: left:
.track-container > * > .wrapper {
float: left;
}
.track-container {
padding: 0;
width: 600px;
height: 200px;
border: 1px solid black;
list-style-type: none;
margin-bottom: 10px;
}
.position-data {
overflow: none;
display: inline-block;
width: 12.5%;
height: 200px;
margin: 0;
padding: 0;
border: 1px solid black;
}
.current-position,
.position-movement {
height: 100px;
width: 100%;
margin: 0;
padding: 0;
border: 1px solid black;
}
.album-artwork {
display: inline-block;
height: 200px;
width: 20%;
border: 1px solid black;
}
.track-info {
display: inline-block;
padding-left: 10px;
height: 200px;
border: 1px solid black;
}
.track-container > * > .wrapper {
float: left;
}
<div class="track-container">
<div class="position-data">
<div class="current-position wrapper">1</div>
<div class="position-movement wrapper">2</div>
</div>
<div class="album-artwork">
<span class="wrapper">fdasfdsa</span>
</div>
<div class="track-info">
<span class="wrapper">fdafdsa</span>
</div>
</div>
You need to add vertical-align:top to those two elements:
.album-artwork, .track-info {
vertical-align:top;
}
jsFiddle example
The default vertical alignment is baseline, but you are looking for top instead.
Or you could set float:left; to 3 elements.
http://jsfiddle.net/fC2nt/
Make sure the line-height ratio on all the elements you're trying to align is the same also. If you're using a mix of DIV, P, H1-5, DT, DD, INPUT, BUTTON tags this will also cause irregularities in vertical alignment depending on what you've already defined elsewhere.

Outer Div not Expanding to Inner Div

So I have checked nearly every example on SO, and none seem to fix my problem, can anyone tell me what I am doing wrong? All I'm trying to do is have a div within another div, and have text in that on one line (not wordwrap). The problem is that the outer div doesn't expand properly to the inner div.
Example Code:
<style>
.container {
left: 10%;
border: 0.2vw dashed black;
padding: 3%;
position: fixed;
white-space: nowrap;
}
.textbox {
font-size: 800%;
padding: 10%;
border: 0.2vw solid red;
display: inline-block;
}
</style>
<div class="container">
<div class="textbox" >
Test
</div>
</div>
Word-break was the answer instead of whitespace!
.container {
left: 10%;
border: 0.2vw dashed black;
padding: 3%;
position: fixed;
word-break:break-all;
}
.textbox {
font-size: 200%;
padding: inherit;
border: 0.2vw solid red;
display: inline-block;
}

Button vertically centers content but div does not

The following vertically centers the inner div
HTML
<button class="outer">
<div class="inner">
Hello<br>World
</div>
</button>
CSS
.outer {
position: absolute;
top: 25%;
left: 25%;
bottom: 25%;
right: 25%;
padding: 0;
background: none;
border: none;
outline: dashed 1px black;
font-family: sans-serif;
font-size: 16px;
text-align: center;
}
.inner {
background: #ccc;
}
JSFiddle
But if I use a div instead of a button for the outer element,
For semantic reasons, I want a div not a button.
What CSS styles do I need to add to the .outer class to produce the same vertical-alignment styling that the button had?
I need this to work in Chrome and FF.
This is What you need: Link: http://jsfiddle.net/WP8um/2/
OR If you don't want margin on outer div you can use top,left,bottom,right properties. http://jsfiddle.net/WP8um/3/
CSS
.outer {
display: inline;
margin:25%;
background: none;
outline: dashed 1px black;
border: none;
padding: 0;
width: 200px;
height:200px;
}
.inner {
background: #ccc;
text-align:center;
margin: 50% 0;
}