I have three divs that are side-by-side using display:inline-block.
If the divs are empty, they are at the same horizontal level.
As soon as I add <p> tags and some line breaks (<br/>) to the leftmost/first div, the rest are moved down.
If I put enough content in the second box, the third is moved down even more.
My HTML for the boxes:
<div class="main-box" id="about">
<h1>About</h1>
<p>This box has one paragraph of text, with line breaks</p>
</div>
<div id="login-container">
<div class="main-box" id="login">
<h1>Login</h1>
<p>Already a member? Sign in and see your stuff!</p>
</div>
<div class="main-box" id="signup">
<h1>Signup</h1>
<p>Create an account by filling out this form.</p>
</div>
</div>
The last two boxes are grouped in a div so that they "float" together.
My CSS:
div.main-box {
text-align: left;
display: inline-block;
border: 10px solid red;
margin: 20px;
padding: 25px;
border-radius: 50px;
width: 400px;
height: 400px;
}
div#login-container {
display: inline-block;
}
Just add:
vertical-align: top;
You can read about inline-block and some more details like IE7 fix and spacing in html code here.
Are you willing to use float: left instead of display: inline-block for the inside divs?
Another solution that I can think of is to create a class with float: left or display: table and applying it to the paragraph tags.
Related
Basically I have this long bar div and 2 pieces of text inside the innerHTML of that div.
How would I make one of the elements hug the left side of the div and one hug the right side?
I thought float:left; and float: right; would have worked but only one of them can work at a time.
Any tips are appreciated.
You haven't shared any code, so it's hard to know what you've attempted so far..
You mention you've set the text via innerHTML, but if you're instead able to actually edit the markup itself, then this left/right text split can easily be achieved by using flexbox. Here, I've sepearted the items with the justify-content property:
.longDiv {
background: orange;
width: 300px;
display: flex;
justify-content: space-between;
}
<div class="longDiv">
<p>left text</p>
<p>right text</p>
</div>
You can have multiple floats in one container. See this example. I generally make sure the floats are defined "first", before the other content of the container. The order of the elements does make a difference on how they render.
.long {
width: 200px;
height: 2em;
text-align: center;
}
.box {
border: solid thin black;
}
.left {
float: left;
}
.right {
float: right;
}
<div class="long box">
<span class="left box">L</span>
<span class="right box">R</span>
Sample text.
</div>
I'm pretty new to the front-end materials so bear with me
while I try to explain the question.
I'm having an issue trying to align three boxes together side by side.
However, when I try to add the <p> tag within the box, a top margin
gets added. Here is what I mean.
<div class="work-box">
<div class="box">
<div class="idea">
</div>
<p>Hello World</p>
</div>
<div class="box">
<div class="idea">
</div>
</div>
<div class="box">
<div class="idea">
</div>
</div>
</div>
As you can see, the boxes are wrapped within the "work-box" class. Here is the CSS code.
.work-box {
text-align: center;
}
.box {
margin-top: 30px;
display: inline-block;
width: 30%;
height: 300px;
background-color: #495159;
border: solid #A1E8CC thick;
}
.idea {
height: 50%;
background: url('img/idea.svg') center no-repeat;
background-size: contain;
}
I have been struggling with this issue for this whole entire day and I just cannot figure it out. Please help!
Thank you so much.
Solution: Add vertical-align: top to your .box class.
Explanation: The effect that you saw was because for all inline elements, the vertical-align is defaulted to baseline.
This behaves funny when you have inline elements inside of your .box. Because it will try to align the last inline element to the baseline of all your inline elements.
Try adding vertical-align:middle; to the box class.
.box {
margin-top: 30px;
display: inline-block;
vertical-align:middle;
width: 30%;
height: 300px;
background-color: #495159;
border: solid #A1E8CC thick;
}
See Codepen Example here .
You try using <span>Hello world</span> instead of <p> tag. Moreover, <p> tag is out of .idea div. Is it ok? I think it should be inside of <div class="idea">
There are some answers to a similar question already, but this one has a twist.
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-3 grey">
<div class="wrapper">
<div class="info">(i)</div>
<div class="text"><div class="labeled">This is a long text</div></div>
<div class="icon">[$]</div>
</div>
</div>
<div class="col-xs-12 col-sm-9 green">
Content
</div>
</div>
So I need three divs, aligned in one line at all conditions - info, text, icon - with two divs on the sides having fixed h/w, and one in the middle taking only as much space, as
either it needs, and not more
or is available for it, cutting the context with overflow:hidden
Here is the fiddle http://jsfiddle.net/L7tmt5w1/3/
Here are my mad skills in sketching ideas http://imgur.com/tF0HkD2
For those, who want to feel my pain, you may also try re-ordering the divs - text, icon, info - when the screen size goes mobile (bootstrap's col-xs-)
You can use the display: table-cell; method for this situation:
.wrapper {
display: table;
text-align: right;
width: 100%;
}
.info {
width: 20px;
height: 20px;
display: table-cell;
background-color: #005ea8;
color: #fff;
}
.icon {
width: 20px;
height: 20px;
display: table-cell;
background-color: #eb690b;
color: #fff;
}
.text {
display: table-cell;
background-color: #ccc;
width: auto;
}
This mimics the table display properties and keeps all the children of .wrapper inline and the middle one "elastic" as it has no defined width. You can also remove the floats.
http://jsfiddle.net/L7tmt5w1/7/
maybe this solution will help you DEMO
<aside class="panel">
...
</aside>
<div class="content">
...
</div>
.content {
overflow: hidden;
border: 1px solid;
}
.panel {
float: right;
width: 200px;
border: 1px solid;
}
You can try this http://jsfiddle.net/L7tmt5w1/3/
Remember: If you want to float an element to the right, it must be the first element. For example:
<div style="float:right"></div>
<div style="float:left"></div>
AND DIV's are already block elements, so you don't have to add display:block to a DIV-element
I don't know if this is what you want: jsfiddle
if not content on "text" no div... if too much content it's hidden
(but you can add
overflow:auto
to the text div for scroll bars
I am trying to make these blocks of info the same size regardless of the number of words each one holds. As seen in the example, when one block has less text than the other, one gets a bit smaller and the other remains a different size.
Now my question is, How do I achieve having these blocks the same size regardless of its content or image? I am also going to use another pair right below them.
Here is the CSS code:
/***********All containers**************/
.bottomContainers{
position: absolute;
margin-left: 0%;
display: inline-box;
}
/**********Small Containers*************/
.container{
max-width: 30%;
max-height: 30%;
margin-top:5%;
margin-bottom: 5%;
margin-left: 10%;
padding-left: 2%;
padding-right: 2%;
padding-bottom: 2%;
background-color: #ecf0f1;
color: grey;
display: inline-block;
/*display: inline-block;*/
border-radius: 5px;
border-bottom: 2px solid grey;
}
Here is the HTML code:
<div class="bottomContainers" role="moreInfo">
<!--Small Inner Containers for Information-->
<div class="container" id="firstContainer">
<br />
<center><img src="img/map.png"></center>
<br>
<article>
Some random text is in this block, It doesnt size like the next one
</article>
</div>
<div class="container" id="firstContainer">
<br />
<center><img src="img/money.png"></center>
<br>
this is another block which also doesnt scale to the other block regardless of text inside of it
</div>
What did I possibly do wrong here ?
I am heavily refactoring your original code in this solution. If this is a static width website then having static width cells won't be a problem. If you want this solution to be responsive you will have a lot of issues with it:
http://jsfiddle.net/VET6x/1/
I positioned the image and its corresponding text using absolute. Again that will work with a static layout, once it goes responsive there will be problems.
<div class="bottomContainers">
<div class="container">
<div>
<img src="http://placekitten.com/g/80/80" />
</div>
<div>
Some random text is in this block, It doesnt size like the next one
</div>
</div>
<div class="container">
<div>
<img src="http://placekitten.com/g/80/80" />
</div>
<div>
This is another block which also doesnt scale to the other block regardless of text inside of it
</div>
</div>
</div>
.bottomContainers { overflow:hidden; }
.container {
width:200px;
height:200px;
float:left;
position:relative;
margin:5% 5%;
padding:2%;
background-color: #ecf0f1;
color: grey;
border-radius: 5px;
border-bottom: 2px solid grey;
}
.container > div { position:absolute; bottom:10px; }
.container > div:first-child { position:absolute; top:10px }
If it were me I would find someway to avoid static height cells.
Here is one solution that may work for you:
Demo Fiddle
I changed up your code a bit. Using the center tag is frowned upon, also it looks like the br tags were there for spacing, which could be done with margin. I ended up giving .container a specified height, the main drawback in that being if the window is sized down too far the overflow text will be hidden.
HTML:
<div class="bottomContainers" role="moreInfo">
<div class="container" id="firstContainer">
<img src="http://www.placehold.it/100x100">
<p>
Some random text is in this block, It doesnt size like the next one
</p>
</div>
<div class="container" id="firstContainer">
<img src="http://www.placehold.it/100x100">
<p>
this is another block which also doesnt scale to the other block regardless of text inside of it
</p>
</div>
</div>
CSS:
.container{
// your current styles here
overflow: hidden;
height: 200px;
display: block;
float: left;
}
.container img {
display: block;
margin: 10px auto 0px;
}
This is a quick fix, but setting an explicit height on the objects will have them all be the same height. This requires some playing around with the best size and such but it will fix your problem. I'm curious how a professional would fix this problem.
Some other things with your code. Centering the <img> using HTML is discouraged, use css instead. Also, where are the <br> tags and why are some closed but some aren't?
Maybe you can use display:table;, display:table-row; and display:table-cell;. This way, your div will act like column of a table. They will stay at the same height.
Take a look at this jsfiddle!
I'm trying to have a H1 header and regular text on the same line, with a line under it, like so:
I have tried below code, but have been unsuccessful. What am I doing wrong?
<div style="border-bottom:1px;">
<div align="left"><h1>Header</h1></div>
<div align="right">Regular Text Goes Here</div>
</div>
Original answer (still working just fine)
See the snippet below. The idea is to make the <h1> inline to allow the second text to be at the same line.
header { border-bottom: 1px solid #000; }
header > h1 { display: inline-block; }
header span { margin-left: 100px; }
<header>
<h1>Text</h1>
<span>text2</span>
</header>
2020 Update
See the snippet the snippet below that makes use of Flexbox. So instead of setting the h1 to an inline-block, you can make the header a flex container. A flex container will (by default) layout its children on a horizontal axis.
Note that you also need align-items: center to keep the h1 and span on the same vertical axis. Also, note that you might want align-items: baseline if you want the texts to appear on the same baseline (like my original answer).
header {
display: flex;
align-items: center;
/* Remove the next line if you want the span to appear next to the h1 */
justify-content: space-between;
border-bottom: 1px solid #000;
padding: 10px 30px;
}
<header>
<h1>Text</h1>
<span>at the end</span>
</header>
I came up with a simple solution. My requirements are slightly different in that I want my status right aligned.
.my-header h2 {
display: inline;
}
.my-header span {
float: right;
}
<div class="my-header">
<h2>Title</h2>
<span>Status</span>
</div>
<div style="clear:both"></div>
Add this line border-bottom:1px solid #000
<div style="border-bottom:1px solid #000;">
<div align="left"><h1>Header</h1></div>
<div align="right">Regular Text Goes Here</div>
</div>
DEMO
Use class name instead of inline-style.
Try
<div style="float:left;"><h1>Header</h1></div>
<div style="float:right;">Regular Text Goes Here</div>
instead?
There are two methods to accomplish H1 and TEXT inline. To clarify, TXT is in an element container. You suggest DIV, but any symantic element will do. Below, h1 and p illustrate a common use, while showing that you need not hide from element blocking, using DIV's (though divs are pandemic for many javascript coders).
Method 1
.inline { display: inline; float: left; padding-right: 2rem; }
<h5 class="inline">Element a's link family...</h5>
<p class="inline">
Method 2
h5 { display: inline-block; float: left; font-size: 1rem; line-height: 1rem; margin-right: 2rem; }
h5>p { display: inline-block; float: right; }
<h5>Title</h5>
<p>Paragraph</p>
I think you should write like this :-
HTML
<div style="border-bottom:1px solid black; overflow:hidden;">
<h1>Header</h1>
<div class="right">Regular Text Goes Here</div>
</div>
CSS
h1 {
float:left;
margin:0px;
padding:0;
}
.right {
float:right;
margin:0px;
padding:0px;
}
DEMO
EVEN YOU CAN USE THIS METHOD ALSO WITH MINIMIZED MARKUP :- DEMO