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>
Related
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>
I have this table-style DIV code for a used vehicle sales platform:
.mainwrapper {
border: 2px solid;
display: table;
}
.itemwrapper {
display: table-row;
width: 706px;
}
.mainwrapper {
margin-bottom: 20px;
}
.item {
width: 700px;
border: 1px solid;
padding: 1em;
background-color: blue;
color: white;
}
.item:nth-child(2) {
float: left;
margin: -2px;
}
.item1 {
display: table-cell;
text-align: left;
margin-left: -30px;
}
.item1 p {
margin-top: -30px;
}
.item-price {
width: 300px;
background-color: blue;
padding: 1em;
color: white;
text-align: center;
}
.picture, .item {
display: table-cell;
border: 1px solid;
}
.picture {
width: 90px;
margin: 1px;
border: 2px solid;
}
.picture img {
height: 185px;
}
<div class="mainwrapper">
<div class="itemwrapper">
<div class="item">1992 ELDDIS PAMPEROS XLi</div>
<div class="item-price">£1,000</div>
</div>
<div class="itemwrapper">
<div class="picture"><img src="https://complianz.io/wp-content/uploads/2019/03/placeholder-300x202.jpg.webp"></div>
<div class="item1"><p>2 berth, good condition</p></div>
</div>
</div>
<div class="mainwrapper">
<div class="itemwrapper">
<div class="item">2008 SWIFT CHALLENGER 540</div>
<div class="item-price">£13,000</div>
</div>
<div class="itemwrapper">
<div class="picture"><img src="https://complianz.io/wp-content/uploads/2019/03/placeholder-300x202.jpg.webp"></div>
<div class="item1"><p>4 berth end bedroom</p></div>
</div>
</div>
What I am trying to do is ensure the class item1 is opposite the image, with the text like this if you didn't have the £ per month div and list as table:
Basically, what I am trying to fix is the text that's in class item1 opposite the image (not with all the description or colored DIV there); see the image below.
I tried margin-left and margin-top, but it won't quite put the image opposite.
This is the result of my code:
I can't quite get it to work as I'd expected, text opposite image and size of DIV in the CSS; if anyone can help, I'd much welcome this.
It works OK - no major coding errors, but isn't quite esthetically working out, and that's the basic problem.
I'm trying this as basic HTML first before attempting anything with Javascript, just to ensure it works as a standalone design.
Edit: I tried vertical-align for text, that worked, but it's fixing the gap between image div and text that's the issue. There's a large amount of space I don't know how to fix.
As the answer for the text is solved. You can change the column width by changing the css property of item. you can do it as follows. The width was 700px in your code you can reduce to get a smaller width. I changed it to 400px.
.item {
width: 400px;
border: 1px solid;
padding: 1em;
background-color: blue;
color: white;
}
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.
The first two align perfectly, but the third one just won't. .. Can anyone here tell me what i'm doing wrong? I was stuck at this for hours last night, and this morning, by looking at other similar questions here, I was able to get the first two divs to align, but the third one won't no matter what. There is an entirely different div below it that it keeps going inside of instead of going up to align itself with the other two.
HTML & CSS
.framebox:after {
content: "", ;
clear: both;
display: table;
}
.frame1 {
float: left;
width: 300px;
height: 300px;
background-color: white;
margin-left: 40px;
margin-right: 5px;
}
.frame2 {
margin: 0 auto;
width: 300px;
height: 300px;
background-color: white;
}
.frame3 {
width: 30%;
height: 300px;
background-color: white;
margin-left: 5px;
margin-right: 40px;
float: right;
}
<div class="framebox">
<div class="frame1">
<h2> dfgdfg</h2>
</div>
<div class="frame2">
<h2> dfgdfg </h2>
</div>
<div class="frame3">
<h2> dfgdfg </h2>
</div>
</div>
First. Set them all to float left, so the will try to pack left until they fill the page width.
If you set some of the widths percentually and other in absolute numbers, your design wont work for all screen sizes. You'll have to do a lot of math. I suggest you use all widths percentual, so they will behave in all screens.
In your case, they would align to the top only in screens where the width of all elements together isn't greater than the width of the screen.
I changed the background colors so we could see better.
obs: If you know the min screen size this has to work, you can use absolute numbers. You'll have to make the divs with their margins fit on the smallest considered screen size.
.framebox:after {
content: "", ;
clear: both;
display: table;
}
.frame1 {
background-color: yellow;
float: left;
width: 33%;
height: 300px;
// margin-left: 40px;
// margin-right: 5px;
}
.frame2 {
background-color: blue;
float: left;
margin: 0 auto;
width: 33%;
height: 300px;
}
.frame3 {
background-color: green;
width: 33%;
height: 300px;
// margin-left: 5px;
// margin-right: 40px;
float: left;
}
<div class="framebox">
<div class="frame1">
<h2> dfgdfg</h2>
</div>
<div class="frame2">
<h2> dfgdfg </h2>
</div>
<div class="frame3">
<h2> dfgdfg </h2>
</div>
</div>
I have two divs displayed using display: inline-block, in order to have them next to each other. This works when neither of the divs have text in them. However, when I put text in one div, it moves it down dramatically, while the other keeps the same position. This happens regardless of what div I put the text in; if I put it in both, however, only the left div will go down.
Here's a codepen to show what I mean: http://codepen.io/anon/pen/MwEKPp
#import url(http://fonts.googleapis.com/css?family=Open+Sans:400,700);
#main_container {
width: 1000px;
height: 95%;
margin: 0 auto;
font-size: 0px;
}
#logo {
display: inline-block;
height: 200px;
width: 200px;
background-color: aqua;
font-size: 20px;
}
#title_area {
display: inline-block;
font-size: 20px;
height: 200px;
width: 800px;
background-color: rgb(60,105,123);
font-family: "open sans";
font-size: 60px;
}
<div id="main_container">
<div id="logo_title_area">
<div id="logo">
test
</div>
<div id="title_area">
test
</div>
</div>
</div>
Entering text into the divs labelled logo and title_area will produce the effect I'm talking about.
Does anyone have any idea how to fix this? I need them to remain side-by-side regardless of their contents. Thanks in advance!
It's just missing the vertical alignment, the default value is baseline.
E {
display: inline-block;
vertical-align: top;
}
#logo, #title-area {
vertical-align: top;
}
I just added overflow:auto; for both of the divs and it worked
#logo {
overflow:auto;
display: inline-block;
height: 200px;
width: 200px;
background-color: aqua;
font-size: 20px;
}
#title_area {
overflow:auto;
display: inline-block;
font-size: 20px;
height: 200px;
width: 800px;
background-color: rgb(60,105,123);
font-family: "open sans";
font-size: 60px;
}