Allow inline-block elements to wrap before stacking - html

I have two divs next to each other that are displayed using inline-block. As the viewport shrinks, I'd like the text in the leftmost div to wrap before the divs collapse vertically, but I can't seem to make that happen. JSFiddle here.
In the demo, when the viewport shrinks "Should stay in block" is pushed below the title block, whereas I'd like the "Lots of text I want to wrap" to start wrapping to keep the two blocks on the same line.

Use display: table-cell; Instead of display:inline-block will solve your issue.
.title {
display: table-cell;
vertical-align: top;
}
.box {
display: table-cell;
vertical-align: top;
}
<div class="title">
<h1>Hi</h1>
<h2>Lots of text I want to wrap</h2>
</div>
<div class="box">
Should stay in a block
</div>
Check your updated Fiddle Here.

Can't you make them to fill the body or the container giving them a 50% width?
JSfiddle
EDIT: JSfiddle with a wrapper
.title {
display: inline-block;
vertical-align: top;
background-color:red;
width:50%;
}
.box {
display: inline-block;
vertical-align: top;
background-color:blue;
width:50%;
}
<div class="title">
<h1>Hi</h1>
<h2>Lots of text I want to wrap</h2>
</div><div class="box">
Should stay in a block
</div>
Edit: remember to not wrap after the first div, and make sure that there are not spaces </div><div class="box"> so you can use 50% preserving the inline-block

Related

SVG img on the same line of text in inline-block container

How can i have my SVG image on the same line of my text in an inline-block container ?
In the above example, I want my picture let at the right of my text, on the same line but browser automaticaly break line.
.container{
display: inline-block;
background:orange;
}
<div class=container>
text<img src="https://res.cloudinary.com/aquis/image/upload/v1521481254/site-2018/megaphone-picto.svg">
</div>
Simply specify a width and/or a height to the image:
.container{
display: inline-block;
background:orange;
}
<div class=container>
text<img src="https://res.cloudinary.com/aquis/image/upload/v1521481254/site-2018/megaphone-picto.svg" width="20">
</div>
Remove display: inline-block from your .container class and set the inline-block on your image. Since it is an svg, you will also want to give the svg an explicit height and width.
It's recommended to wrap the img in a span tag, instead of setting it to the img html element, which would set the same small size on all image items on your site. I gave the span the classname of svg-image, but you could name it anything else.
.container{
background:orange;
}
.svg-image {
display: inline-block;
height:20px;
width: 20px;
}
<div class=container>
text<span class="svg-image"><img src="https://res.cloudinary.com/aquis/image/upload/v1521481254/site-2018/megaphone-picto.svg"></span>
</div>

set button width to 100% of parent div without a line break

I have a div called text, and next to it I want a button which should take up the remaining width of the screen so I set the width of the button to 100%, but it takes that as 100% of the screen and appears below the text div. How can I make it so the button is 100% the width of its parent div and not the whole screen so it all appears on the same line, without setting a width for the text div? Here is a demo. Thank you.
css
#text{
float:left;
}
div{
border:1px solid red;
}
button{
width:100%;
}
html
<div id = "text">text</div>
<div>
<button>button</button>
</div>
You should be able to achieve this using display: table-cell css:
Try something like this: DEMO
<div class="container">
<div class="cell" id="text">text</div>
<div class="cell">
<button>button</button>
</div>
</div>
div.container {
width: 100%;
display: table;
}
div.cell {
border:1px solid red;
display: table-cell;
}
button {
width:100%;
}
/* use this style to set width of the #text cell to exactly the width of it's cotnents */
#text {
width:1%;
white-space: nowrap;
}
You can place them in a div together and display them as flex.
http://jsfiddle.net/1amvwzuw/2/
<div class="flex">
<div id="text">text</div>
<button>button</button>
</div>
You need to give your parent container a width. So text could be like, 80% then the div that contains button could be 20%. then button will be 100% of the parent div and both divs will be beside each other.
So div element is a block element. That means it will start on a new line and the next element after it will be on a new line as well. What you can do here is to set display: inline-block to both divs you have.
If you don't need a block element just use an inline one like span
http://jsfiddle.net/1amvwzuw/3/
If your browser-matrix allows it this is the perfekt opportunity to try out flexbox:
html
<div class="wrapper">
<div id="text">text</div>
<div class="button-wrapper"><button>button</button></div>
</div>
css
.wrapper{
display: flex;
}
.button-wrapper{
flex: 1;
}
button {
width: 100%;
}
http://jsfiddle.net/1amvwzuw/5/
if not look at this question to see how to stretch a div using display:table-cell:
Auto-stretching table-cell div css

Inline-blocks not in one like exactly

I have 3 divs on my page, that css: display: inline-block.
.utilitiesContent div
{
display: inline-block;
width:33%;
}
Here is fiddle (don't pay attention that it has cyrillic letters). As you can see it is like stairs, not exactly in one line. How can I make it to has the same height level?
.utilitiesContent div
{
display: inline-block;
width:33%;
vertical-align: top;
}
you are looking for vertical-align property, I used top , you can play around and see what you like best.
updated fiddle here
I strongly recommend using width: 32%; 33% is just cutting it pretty close , and can jump down to the next row in certain cases
.utilitiesContent div
{
display: inline-block;
width:32.5%;
}
<div class="utilitiesContent">
<div>div 1</div>
<div>div 2</div>
<div>div 2</div>
</div>
DEMO just decrease width to 0.5% FROM 33% to 32.5%
.utilitiesContent div
{
display: inline-block;
width:32.5%;
}
Inline block will by default put space between the elements based on the font size. If you reduce the font size to zero you can remove the spacing.
.utilitiesContent div
{
display: inline-block;
width:33.33%;
font-size:0;
vertical-align: top;
}
Then just set the font size to the elements inside the inline-block element.
See my fiddle: http://jsfiddle.net/4ear5n8n/6/

vertical-align is not working

I know it's quite annoying, seems that vertical-align in CSS is common pitfall but I really need your help.
I want the text to be always be in "middle" vertical position left to the image (image test.gif has different dimensions)
I have such structure of html elements (unfortunately I can't change it due to system restrictions)
<div class="wrapper">
<div class="logo">
<img alt="some text" src="http://localhost/test.gif">
</div>
<div class="source">some text</div>
</div>
CSS:
.wrapper {clear: both;}
.logo {float: left;}
.source { float: left;line-height: 16px;}
Spent 1.5 hours and no luck. Tried to apply vertical align to different elements with different positioning, etc...Please help!
If you don't need to support IE7, try this:
.wrapper { display: table; }
.logo { display: table-cell; }
.source { display: table-cell; vertical-align: middle; }
Basically, tables give you the option to vertically align to the middle, and by making your divs act like a table, we can mimic that option.
A common issue but is explained well here: http://phrogz.net/css/vertical-align/
Key Points
Specify the parent container as position:relative or position:absolute
Specify a fixed height on the child container.
Set position:absolute and top:50% on the child container to move the top down to the middle of the parent.
Set margin-top:-yy where yy is half the height of the child container to offset the item up.
<style type="text/css">
#myoutercontainer { position:relative }
#myinnercontainer { position:absolute; top:50%; height:10em; margin-top:-5em }
</style>
...
<div id="myoutercontainer">
<div id="myinnercontainer">
<p>Hey look! I'm vertically centered!</p>
<p>How sweet is this?!</p>
</div>
</div>

HTML & CSS: Vertical centering of inline elements inside an inline-block

I've read about 20 questions and articles on vertical alignment/centering in HTML/CSS, but I'm still unable to get my specific case working a expected.
Please have a look at http://jsfiddle.net/pf29r/3/
<div class="outer">
<div>Left1</div>
<div>Left2</div>
</div>
<div class="inner">
Before
<span>Middle</span>
After
</div>
<div class="outer">
<div>Right1</div>
<div>Right2</div>
</div>
.outer {
display: inline-block;
}
.inner {
display: inline-block;
}
I want to vertically center the content of the inner block. The best I've been able to accomplish was by using display: table and display: table-cell, which almost works, but the content isn't quite in the middle.
What am I missing?
Add vertical-align: middle to .outer. This is counter-intuitive because you would expect to have to add it to .inner, but it works.
http://jsfiddle.net/pf29r/5/