Horizontal align two adjacent divs with left oriented text - html

I want to horizontal align a div that consists of two other divs:
One from the left, that will contain an image.
Other from the right, that div will contain text, aligned to the left.
The alignment will be relative to a container, and the centered div should expand to a maximum width (and not take the entire container's width).
This pen describes what i tried to do using table layout
This is the HTML
<div class="container">
<div class="centered">
<div class="left">
left text
</div>
<div class="right">
very very very long right text
</div>
</div>
</diV>
And the CSS
.container {
width: 200px;
background-color: red;
}
.centered {
display: table;
margin: 0 auto;
max-width: 100px;
}
.left {
background-color: green;
display: table-cell;
vertical-align: middle;
}
.right {
background-color: blue;
display: table-cell;
vertical-align: middle;
}
As you can see, the space on the right of the blue area is part of the centered div (the green+blue area), but it makes the div's content not to be centered. What i would like is the blue area to take the width of the longest line in it

If you're wanting the blue area to dynamically grow with the text inside of it while retaining a centered area of content would the following be what you're looking for?
.container {
width: 200px;
background-color: red;
padding:0 20px;
}
.centered {
display: table;
margin: 0 auto;
max-width: 100px;
}
.left {
background-color: green;
display: table-cell;
vertical-align: middle;
color:#fff;
}
.right {
background-color: blue;
display: block;
vertical-align: middle;
width:inherit;
color:#fff;
}
<div class="container">
<div class="centered">
<div class="left">
left text
</div>
<div class="right">
very very very long right text very very very long right textvery very very long right textvery very very long right textvery very very long right textvery very very long right text
</div>
</div>
</div>

Related

Why does this text move my div?

I have a grid of white divs with a green div behind
(like this).
I am trying to put text in each div, but for some reason, when I give a div text, that div is shifted down (like this). What css property/ HTML technique can I use to make the divs stay in place while still holding text?
The HTML consists of a div with this CSS:
#main {
top: 50px;
height: 280px;
width: 100%;
background-color: green;
position: fixed;
}
which contains divs with this CSS:
.small {
position: relative;
background-color: white;
border: 4px solid black;
width: 5%;
height: 80px;
display: inline-block;
margin-top: 2px;
}
and looks like this:
<div id="main">
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<!-- etc -->
</div>
They are aligned at their baseline. If there is text, that's the baseline of the text (i.e. of the last line of text), if not, it's near the bottom of the container.
Add
.small {
vertical-align: top;
}
to change this.
Here's a codepen: http://codepen.io/anon/pen/oxVajG

center image next to two lines of text responsively

I am trying to display an image next to two lines of text, which are centered. I have attached an example, and you will see from it that the image is to the left of the text, whereas I am trying to center the image to be on the left side of the text, and have a perfectly centered image/text.
CSS:
.center-class{
text-align:center;
}
.righty img{
max-width: 100px;
float:left;
}
.vid-open{
}
HMTL:
<section class="">
<div class="row pull-down">
<div class="center-class">
<div class="righty">
<img src="http://www.psdgraphics.com/file/white-egg.jpg" >
<h2>This is a header.</h2>
<h5 class="vid-open">some text some text some text<span class="icon-right-left-01-011" ></span></h5>
</div>
</div>
</div>
</section>
SEE DEMO
Simply wrap the text in a div and display it inline-block:
.center-class {
text-align: center;
}
.righty > * {
display: inline-block;
vertical-align: middle;
}
.righty img {
max-width: 100px;
}
<section class="power-of-egg">
<div class="row pull-down">
<div class="center-class">
<div class="righty">
<img src="http://www.psdgraphics.com/file/white-egg.jpg">
<div class="con">
<h2>This is an egg.</h2>
<h5 class="vid-open">eggs are very nutritious<span class="icon-right-left-01-011" ></span></h5>
</div>
</div>
</div>
</div>
</section>
Updated Codepen
Well, this will center the entire block:
.center-class{
text-align:center;
}
.righty img{
max-width: 100px;
float:left;
}
.vid-open{
}
.righty {
width: 300px;
margin: 0 auto;
}
The problem is that you've got your image inside of a div and div is a block-level element, which means it will expand to be the full width of its parent element.
If you take the image out of the div and make the div that contains the text have:
display:inline-block;
That div will shrink down to be only as wide as its content.
Here's your updated code: http://codepen.io/anon/pen/LNNJRQ
To horizontally center an element you can use display: block; and margin: auto;. There may be a better approach but this is the css I used to have the image in the center and the text to the right of it:
.righty > .con {
position: absolute;
top:0;
left: 55%;
}
.righty img {
display: block;
vertical-align: middle;
margin: auto;
max-width: 100px;
}
Note: the position of the class .con will vary based on screen size.
Here is the updated codepen.

HTML paragraph new line if no place in inline-block div

I have in div two divs with floats (left and right). In right div there are paragraphs. All that two divs have inline-block display. If paragraphs in right div too long, then right div jump over the left, and set to display block.
I'm want to paraghraps do new line if it too long.
Code:
.left {
margin: 30px;
float: left;
display: inline-block;
}
.right {
float: left;
margin-top: 30px;
}
.right p {
margin: 10px;
font-weight: 900;
}
<div class="box_container">
<div class="left">
<img src="{url}">
</div>
<div class="right">
<p>p1</p>
<p>p2</p>
<p>p3</p>
<p>p4</p>
<p>p5</p>
</div>
</div>
When text in paragraph too long:
.left {
margin: 30px;
float: left;
display: inline-block;
}
.right {
float: left;
margin-top: 30px;
}
.right p {
margin: 10px;
font-weight: 900;
}
.left img {
border: 5px solid white;
}
<div class="box_container">
<div class="left">
<img src="http://monitorgame.com/m/games/001.jpg">
</div>
<div class="right">
<p>p1</p>
<p>p2</p>
<p>p3</p>
<p>p4</p>
<p>p5 text text text text text text lalalalalalalalalalalallalalallalalalala</p>
</div>
</div>
You should allocate space for them. I like using floats in these instances, so for example you could add float:left width: 50% to each one, something like that.
.left {
margin: 30px;
float: left;
width: 50%;
}
.right {
float: left;
width: 50%
margin-top: 30px;
}
You already had the float, you just needed to specify the width. They could be static too not % if you want, but if the static sizes don't fit in the screen they will break like your example.
see working here : https://jsfiddle.net/3LtLuxbc/3/
Just a note on the fiddle - I changed your img size to with 100% and removed the border so it would scale , you can change that to suit your design.
Add a width to the right div. This will force the text to wrap. Without a specified width, div will increase in size until reaching max size of wrapper div or page

Vertically aligned image and text div

UPDATE: The answers have got me close, but they still don't align vertically as the text div is larger, how can I make them both the same height and therefore align?
I would like to have two DIVs next to each other, one containing an image and one containing text, both sitting in a container DIV.
The image should be 15% of the width of the container div, with the text using the remaining 85%
The image and text should be aligned vertically within their respective DIVs, so it looks like they are aligned with each other.
I've tried to work this out but can't seem to do it! Can anyone help?
#picture {
float: left;
width: 15%;
line-height: auto;
}
#text {
width: auto;
padding-left: 16%;
line-height: auto;
vertical-align: middle;
margin-top: auto;
margin-bottom: auto;
}
#text p {
display: inline-block;
vertical-align: middle;
line-height: normal;
}
and
<div id="quotes">
<div id="picture">
<img style="width: 100%; vertical-align: middle" src="tom.jpg" >
</div>
<div id="text">
<p>"Christiaan was one of the stand out candidates throughout, therefore there was no hesitation in offering him a place on this highly sort after scheme..."</p>
</div>
</div>
Here's a fiddle with your code in it: http://jsfiddle.net/hQ6Vw/1/
The only changes I made was to assign matching top/bottom margins to the img and p tags. I think that will give you the effect you're looking for.
If you use float and verticl-align, those two won'nt work together.
Float extract itself from regular flow and go slide on one side or the other on top of next line right after any content within the regular flow.
Vertical-align works:
in betweem inline-boxes (inline-block-level element or displayed so with display:inline-block;)
inside td or it's CSS default display : display:table-cell;
here jsfiddle #TXChetG updated
Using display:inline-block; http://jsfiddle.net/GCyrillus/hQ6Vw/2/
Using display:table/* table-cell*/;
http://jsfiddle.net/GCyrillus/hQ6Vw/3/
This should get you close:
<div>
<div style="background: grey; width: 15%; float:left"></div>
<div style="background: blue; width: 85%; float:left"></div>
</div>
Replace the grey background div with your image and the blue with your text.
Check this out
HTML:
<section>
<div id="one"></div>
<div id="two"></div>
</section>
CSS:
section {
width: 80%;
height: 200px;
background: aqua;
margin: auto;
padding: 10px;
}
div#one {
width: 15%;
height: 200px;
background: red;
float: left;
}
div#two {
margin-left: 15%;
height: 200px;
background: black;
}
Is this what you mean?
html
<div class="container">
<div class="images">
<img src="http://jsfiddle.net/img/logo.png" style="background-color:black">
</div>
<div class="text">
Example
</div>
</div>
<div class="container">
<div class="images">
<img src="http://jsfiddle.net/img/logo.png" style="background-color:black">
</div>
<div class="text">
Example
</div>
</div>
css
.container {
clear: both;
}
.images {
width: 15%;
float: left;
vertical-align: text-top;
}
.text {
width: 85%;
float: right;
vertical-align:text-top;
}
Why not just set the #text p display to display: inline or display:block; or use margins to align them?
<div id="quotes">
<div id="picture">
<img src="tom.jpg" />
</div>
<div id="text">
<p>"Christiaan was one of the stand out candidates throughout, therefore there was no hesitation in offering him a place on this highly sort after scheme..."</p>
</div>
</div>
Display the container div as table and the text and image divs as table-cell to make them the same heights. You can then centre the image vertically through vertical-align:middle.
#quotes {
display:table;
}
#picture {
width: 15%;
display:table-cell;
vertical-align: middle;
}
#text {
display:table-cell;
width:85%;
padding-left: 16%;
}
#picture img {
width: 100%;
}
http://jsfiddle.net/X3WsV/1/

CSS: Having 3 div on the same line with the middle one taking the remaining space

I'm building a toolbar, I'd like the yellow part in the following example to take the whole space left (in white):
http://jsfiddle.net/MWjGH/1/
<div class="left"> Some content </div>
<span class="middle"> This should fill the space left </span>
<div class="right"> Some other content </div>
with css:
.left {
float: left;
background-color: #ddd;
display: inline-block;
}
.middle {
background-color: yellow;
display: inline-block;
}
.right {
float: right;
background-color: #ddd;
display: inline-block;
}
Edit: the content of left and right is dynamic, it can change, so I don't want to set width on them
I don't know if that suits you because of a slight HTML change:
<div class="left"> Some content </div>
<div class="right"> Some other content </div>
<span class="middle"> This should fill the space </span>
But I believe it is what you want,
CSS:
.left {
float: left;
background-color: #ddd;
}
.middle {
background-color: yellow;
display: block;
overflow:hidden;
}
.right {
float: right;
background-color: #ddd;
}
DEMO :http://jsfiddle.net/pavloschris/MWjGH/12/
Put the middle div after the floated divs:
<div class="left"> Some content </div>
<div class="right"> Some other content </div>
<div class="middle"> This should fill the space left </div>
Then, don't change any of the display properties so they stay on block (the default for div)
.left {
float: left;
background-color: #ddd;
}
.middle {
background-color: yellow;
}
.right {
float: right;
background-color: #ddd;
}
Fiddle: http://jsfiddle.net/hLmj7/
If you do't have a fixed width for the two side columns, you can always display:table-cell.
.left {
background-color: #ddd;
display: table-cell;
}
.middle {
background-color: yellow;
display: table-cell;
width:100%;
}
.right {
background-color: #ddd;
display: table-cell;
}
JSFiddle example.
With this you're then able to add min-width to the outer columns without having to keep changing the width of the middle element.
JSFiddle example with min-width applied.
I would wrap your divs in a wrapper, and assign the background-color to the wrapper div
Then, you don't need to specify width at all.
jsfiddle
Html:
<div class="toolbar">
<div class="left"> Some content </div>
<div class="middle"> This should fill the space left </div>
<div class="right"> Some other content </div>
</div>
CSS:
.toolbar {
background-color: yellow;
}
.left {
float: left;
background-color: #ddd;
display: inline-block;
}
.middle {
display: inline-block;
}
.right {
float: right;
background-color: #ddd;
display: inline-block;
}
Consider adding clearfix to the wrapper div as the divs inside are floating :)
try this:
don't right align your last div
make all your containers float:left
and give percentage width to each of your containers, so that their total sum should be 100%;
working fiddle
if you don't want to enforce a static width do this:
give each of your containers a width:auto, but be notified, that,
if the total sum of the width of each of the containers turns out to be more than that of parent( body in your case) contaienr, then line-break would occur,
a div will slide down to the next row.
see this fiddle