HTML content is adjusting my CSS in an unexpected way [duplicate] - html

This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Why is this inline-block element pushed downward?
(8 answers)
Closed 11 months ago.
I have a React component which is divided into 3 parts to hold 3 different data values - date, title and amount.
The layout looks good and aligned, but when I add a value in the first section (red), it will adjust my CSS is a very strange way which I can not figure out why.
First image shows the component itself, second image shows the component with HTML content inside it.
Expense.js
<div className="expense">
<div className="date">
<h6>DEMO!</h6>
</div>
<div className="title">
</div>
<div className="amount">
</div>
Expense.css
.expense {
border: 1px darkslategrey solid;
height: 100px;
display: flow-root;
}
.date {
display: inline-block;
background-color: darkred;
width: 20%;
height: 100%;
}
.title {
display: inline-block;
background-color: darkorange;
width: 60%;
height: 100%;
}
.amount {
display: inline-block;
background-color: darkgreen;
width: 20%;
height: 100%;
}

Add vertical-align: top to the three (inline-block) components. The default value for this is baseline, which is what you see in your second image.
Actually you don't need to add it three times, but can do it like this:
.expense > div {
vertical-align: top;
}
Full code (converted to plain HTML/CSS):
.expense {
border: 1px darkslategrey solid;
height: 100px;
display: flow-root;
}
.expense>div {
vertical-align: top;
}
.date {
display: inline-block;
background-color: darkred;
width: 20%;
height: 100%;
}
.title {
display: inline-block;
background-color: darkorange;
width: 60%;
height: 100%;
}
.amount {
display: inline-block;
background-color: darkgreen;
width: 20%;
height: 100%;
}
<div class="expense">
<div class="date">
<h6>DEMO!</h6>
</div><div class="title">
</div><div class="amount">
</div>
</div>

Related

Why html element gets misaligned when adding overflow:hidden to one of the siblings? [duplicate]

This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Closed 4 years ago.
I have two spans in a div positioned next to each other. But it gets misaligned the moment I add overflow: hidden to one of the span.
Why does this happen?
.parent {
border: 1px solid red;
}
.one {
height: 30px;
width: 30px;
background-color: #4784ff;
display: inline-block;
}
.two {
height: 30px;
width: calc(100% - 30px);
background-color: #08dd0f;
display: inline-block;
overflow: hidden;
}
<div class="parent">
<span class="one">One</span><span class="two">Two</span>
</div>
Try This:-
Use vertical-align: top;
.parent {
border: 1px solid red;
}
.one {
height: 30px;
width: 30px;
background-color: #4784ff;
display: inline-block;
vertical-align: top;
}
.two {
height: 30px;
width: calc(100% - 30px);
background-color: #08dd0f;
display: inline-block;
overflow: hidden;
}
<div class="parent">
<span class="one">One</span><span class="two">Two</span>
</div>
Alright so the way that I fixed it for you was using verticle-align:bottom.
The reason this happens is just because css has problems. It has improved greatly, but there are still issues with it. This is a great example of one.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div class="parent">
<span class="one">One</span><span class="two">Two</span>
</div>
<style>
.parent {
border: 1px solid red;
}
.one {
height: 30px;
width: 30px;
background-color: #4784ff;
display: inline-block;
}
.two {
height: 30px;
width: calc(100% - 30px);
background-color: #08dd0f;
display: inline-block;
overflow: hidden;
vertical-align: bottom;
}
</style>
</body>
By default display: inline-block set vertical-align: bottom; so you need to set vertical-align: top for span.
Note: Overflow only work with position, so here you check in snippet it's working fine.
.parent {
border: 1px solid red;
}
.one {
height: 30px;
width: 30px;
background-color: #4784ff;
display: inline-block;
}
.two {
height: 30px;
width: calc(100% - 30px);
background-color: #08dd0f;
display: inline-block;
}
<div class="parent">
<span class="one">One</span><span class="two">Two</span>
</div>
For more specific:
vertical-align has about 5 values:
- top
- bottom
- center
- baseline
- inherit
If your elements have different heights, you will see differents.
- top: all elements will be aligned to top
- bottom: all elements will be aligned to bottom
- center: all elements will be aligned to center
- baseline (default): Depends on font-size, line-height, they will be aligned.
- inherit: just simple inherit from parent.
And an extra information: If your markup does not provide any spaces between elements, it will works fine, but since they're inline-block, a single new-line or space will breaks them into two words (kind of) and so... between them, there're spaces. No matter how you set width for both (eg. 50% for two) they will still breaks into two or more lines because spaces.

Adding text to div breaks the initial alignment

On to the real question. I started learning HTML and I ran into a circumstance that I don't understand why it happens and was hoping someone could explain this to me.
Above is my code and what I don't get is why the introduction of the word text in box1 causes the whole box to be displayed on a new line.
Once the text is removed it realigns. any help is welcome.
I'm going to end this off by saying that I tried pasting in code (using the code blocks button and surrounding my code with <code> and <pre> tags but the editor would not display the code or would break the code block by every second)
Try vertical-align:top; with inline-block.
When we use inline-block we can align blocks in 3 different way's, like if 2 block have large height and one has small then using vertical-align:top; make all three block aligned at top using vertical-align:middle; make these three block aligned middle and using vertical-align: bottom; make these blocks aligned at the bottom side
div {
display: inline-block;
vertical-align:top;
margin: 25px;
}
p {
padding: 10px;
text-align: center;
}
#box1 {
width: 100px;
height: 100px;
background-color: red;
}
#box2 {
width: 100px;
height: 100px;
background-color: blue;
}
#box3 {
width: 100px;
height: 100px;
background-color: yellow;
}
<div id="container">
<div id="box1"><p>text</p></div>
<div id="box2"></div>
<div id="box3"></div>
</div>
It is because you have a pixel value for the width and height of all 3 of your divs. The text "increases" the width and pixel value of your divs.
UPDATE
Why not use the float property?
div {
float: left;
display: block;
margin: 25px;
}
p {
padding: 10px;
text-align: center;
}
#box1 {
width: 100px;
height: 100px;
background-color: red;
}
#box2 {
width: 100px;
height: 100px;
background-color: blue;
}
#box3 {
width: 100px;
height: 100px;
background-color: yellow;
}
<div id="container">
<div id="box1"><p>text</p></div>
<div id="box2"></div>
<div id="box3"></div>
</div>

Why margin doesn't work inside the block? [duplicate]

This question already has answers here:
CSS Margin Collapsing
(2 answers)
Closed 7 years ago.
I have this code:
<div class='block'>
<div class='container'></div>
</div>
.block {
display: block;
height: 100px;
width: 100px;
background: black;
}
.container {
display: block;
width: 30px;
height: 30px;
background: red;
margin: 50px;
}
I can not understand why margin does not work inside the block?
JsFiddle: https://jsfiddle.net/39yy7a0q/
Try below css. I have added position to .container, and adjusted margin value as it should relevant to parents width. https://jsfiddle.net/39yy7a0q/4/
.block {
display: block;
height: 100px;
width: 100px;
background: black;
}
.container {
display: block;
position:absolute;
width: 30px;
height: 30px;
background: red;
margin: 35px;
}

Align two inline-block div with content

I have two inline-block divs, each 50% width of its parent, and they are correctly shown next to each other. But, when I add a link to one of those divs, there's a gap on top of the second div
<div class="wrap">
<div class="resizable resizable1">
link1
link2
</div><!--
--><div class="resizable resizable2">second</div>
</div>
.wrap {
width: 100%;
font-size: 0;
}
.resizable {
width: 50%;
height: 120px;
background-color: coral;
display: inline-block;
}
.resizable2 {
background-color: lightblue;
}
.resizable a {
font-size: 12px;
}
Jsfiddle example http://jsfiddle.net/KyEr3/455/
How can align the two divs?
When using display: inline-block elements by default are set to baseline, instead set vertical-align: top
.resizable {
width: 50%;
height: 120px;
background-color: coral;
display: inline-block;
vertical-align: top;
}
FIDDLE
You can also float them both left, they will align next to each other in the wrapper.
.wrap {
width: 100%;
font-size: 0;
}
.resizable {
width: 50%;
height: 120px;
background-color: coral;
display: inline-block;
float:left;
}
.resizable2 {
background-color: lightblue;
float:left;
}
.resizable a {
font-size: 12px;
}

Why does adding text do a div change the margin [duplicate]

This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Closed 8 years ago.
I don't understand why adding a text do a div seems to be changing how the div is parsed by the browser? Looks like the margin-top is changed, though it isn't.
HTML
<div id="nav">
<div class="nav-left">left</div>
<div class="nav-logo"></div>
<div class="nav-right">right</div>
</div>
CSS
#nav {
width: 400px;
height: 30px;
background: #f5f5f5;
border: 1px solid grey;
text-align: center;
}
.nav-left, .nav-right, .nav-logo {
display: inline-block;
height: 30px;
}
.nav-left {
background: red;
}
.nav-right {
background: blue;
}
.nav-right, .nav-left {
width: 50px;
}
.nav-logo {
background: yellow;
width: 30px;
margin-left: 10px;
margin-right: 10px;
}
Code is also here: http://jsfiddle.net/NcA8r/
As #JoshCrozier said, you need to add a vertical-align to your 3 divs.
This:
.nav-left, .nav-right, .nav-logo {
display: inline-block;
height: 30px;
}
Should be:
.nav-left, .nav-right, .nav-logo {
display: inline-block;
height: 30px;
vertical-align:top;
It happens because you used display: inline-block; in your inner divs.
inline-block elements are vertical-align:baseline; by default.
Check this out this great answer.
"The default value for vertical-align in CSS is baseline."
And this one too.