I know this question has been asked to death but nothing through searching has worked for me.
You know the deal, I have a div element that I need to vertically align text in but nothing has worked (position:absolute;top:50%;margin-top:-x;display:table-cell;vertical-align:middle;etc., etc.)
Here is what I am working with (sorry for the inline CSS). Anyway, the I would use line-height but the text can be one or two lines. It should vertical align with the image which is always max-height of 30px (30x50).
<div style="margin:0 0 10px 0;padding:10px;border:2px solid #606060;background-color:#2b2b2b;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px;">
<div style="float:left;width:55px;height:40px;">
<img style="max-width:50px;border:1px solid #ffb92c;" src="image.jpg" alt="" />
</div>
<div style="float:right;width:155px;font-size:0.7em;height:40px;">
This is the text to vertically align
</div>
</div>
One other thing you can do. If it's only one line of text in the div you can use line-height
example
div {
line-height:40px;
}
The idea is from here and should work for all browsers.
<div style="margin: 0 0 10px 0; padding: 10px; border: 2px solid #606060; background-color: #2b2b2b;
-webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px;">
<div style="float: left; width: 55px; height: 40px;">
<a href="link">
<img style="max-width: 50px; border: 1px solid #ffb92c;" src="image.jpg"
alt="" /></a>
</div>
<div style="float: right; width: 155px; font-size: 0.7em; height: 40px; display: table; #position: relative; overflow: hidden;">
<div style="#position: absolute; #top: 50%; display: table-cell; vertical-align: middle;">
<div style="#position: relative; #top: -50%;">
This is the text to vertically align
</div>
</div>
</div>
<div style="clear: both"></div>
</div>
You need to do like this:
http://jsfiddle.net/rathoreahsan/5u9HY/
Use fixed height instead of padding in main div. and use line height for left & right Divs
Here is a clean version of the solution
<div style="background: yellow">
<div style="width: 155px; font-size: 0.7em; height: 40px; display: table; overflow: hidden;">
<div style="display: table-cell; vertical-align: middle;">
<div style="">
This is the text to vertically align
</div>
</div>
</div>
<div style="clear: both"></div>
http://jsfiddle.net/5y4Nb/
Seems like a common float issue which can be fixed with a clearfix or, like i did in the following code snippet, with a fixed height of the wrapper.
I also sat an line-height of the floating divs and made it a little wider.
Take a look at this:
<div style="margin:0 0 10px 0;padding:10px;border:2px solid #606060;background-color:#2b2b2b;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px;height:40px"> <div style="float:left;width:55px;height:40px;"> <img style="max-width:50px;border:1px solid #ffb92c;" src="image.jpg" alt="" /> </div> <div style="float:right;width:185px;font-size:0.7em;height:40px;line-height:40px"> This is the text to vertically align </div> </div>
http://jsfiddle.net/YqxPZ/3/
Is you need to show only a few lines of a very long text, here is the Fiddle. Adjust height according as needed.
.container-text {
height:40px;
width:180px;
overflow-y:hidden;
display:table-cell;
vertical-align:middle;
text-align: center;
}
Related
I want to make a "About me" page that would contain a box with a thin border, pin to the center of the page.
I need some text on the left half of the box and an image on the right side of the box.
I have a hard time doing that with <div> and it just doesn't work the way I want it.
How do I solve this problem.
Below you can see my code so far:
<div style="background-color: white;
border: 5px solid #3D65A9;
padding: 50px;
margin: 20px;">
<h4 style="text-align: left;
font-family:'Cinzel';
white-space:pre-line;
margin-left: 10px;
display: inline-block">Hi everyone!!Random text.
</h4>
<img src="Moja-slika.jpg" alt="Moja slika" style="width:30%">
</div>
This is what I get, and I want my text to be on top, not on the bottom.
And I want to make image and whole smaller.
You can float:left; the image, if it's a child of the <h4></h4> element, like so:
<div style="background-color: white;
border: 5px solid #3D65A9;
padding: 50px;
margin: 20px;">
<h4 style="text-align: left; font-family:'Cinzel'; white-space:pre-line; margine-left: 10px;display: inline-block">
<img src="Moja-slika.jpg" alt="Moja slika" style="width:30%; float:left;"> Hi everyone!!
Random text.</h4>
</div>
However, it would be good practise to clear the float, see this link for more information.
Just add vertical-align
<div style="background-color: white;
border: 5px solid #3D65A9;
padding: 50px;
margin: 20px;">
<img src="http://2.bp.blogspot.com/-itYtAL5lBeE/TkFdlIhhacI/AAAAAAAABHI/HYGHzdl85UI/s1600/111433_aerogenerador.jpg" alt="Moja slika" style="width:30%">
<h4 style="vertical-align: top; text-align: left; font-family:'Cinzel'; white-space:pre-line; margine-left: 10px;display: inline-block">Hi everyone!!
Random text.</h4>
</div>
<div style="background-color: white; border: 5px solid #3D65A9; padding: 50px;
margin: 20px;">
<h4 style="text-align: left; font-family:'Cinzel'; white-space:pre-line;
margine-left: 10px;display: inline-block; float:right">
Hi everyone!! Random text.
</h4>
<img src="Moja-slika.jpg" alt="Moja slika" style="float:left" width="100" height="100">
</div>
This should get the paragraph to be at the top. You could then use margins to center it. To make the image smaller, add the height and width properties and set the values to the size you want.
You can use position absolute in your h4 tag:
<div
style="background-color: white;
border: 5px solid #3D65A9;
padding: 50px;
margin: 20px;position: relative;"
>
<img src="img/amorgos.webp" alt="Moja slika" style="width:30%" />
<h4
style="text-align: left; font-family:'Cinzel'; white-space:pre-line;display: inline-block;position: absolute;
top: 29px;
margin: 0;"
>
Hi everyone!! Random text.
</h4>
</div>
In this snippet you can see an example of the layout you want.
The img should have a div that wraps it.
Do not put inline styles. Instead, use classes.
I recommend you this interesting web about layout
Next, it is very useful to learn flexbox
.about__wrapper {
background-color: white;
border: 5px solid #3D65A9;
width: 400px;
height: 150px;
display: flex;
align-items: center;
justify-content: space-around;
flex-wrap: wrap;
padding: 10px;
}
.about__image--wrapper {
width: 25%;
display: flex;
align-items: center;
justify-content: center;
}
.about__image {
width: 100%;
height: auto;
}
.about__text {
width: 65%;
}
<div class="about__wrapper">
<div class="about__image--wrapper">
<img
class="about__image"
src="https://place-hold.it/100x100.jpg"
alt="image-name" />
</div>
<div class="about__text">
Random text Random text Random text Random text Random text Random text Random text
</div>
</div>
Need help getting two divs to align beside each other, one contains 2 pictures, the other a video.
HTML
<img src="example3.png" alt="CopeLogo" height="200" width="200" class="exlogo" >
</div>
</div>
CSS
#pics{
text-align: center;
background-color: #194b6f;
height: 200px;
width: 500px;
float: left;
border-radius: 5px;
margin-left: 50px;
border: 2px dashed black;}
#vid{
text-align: center;
background-color: #194b6f;
height: 490px;
width: 660px;
float: right;
border-radius: 5px;
border: 2px dashed black;
margin-right: 50px;}
just use float:left; for both, also you can use display:inline-block; for both as well if you don't want to use float.
I get your point but please do add your working codes to be more clear in your question.
You can actually do this:
HTML
<div class="header">
<div class="pic">
<img src="http://placehold.it/150x150" alt="">
<img src="http://placehold.it/150x150" alt="">
</div>
<div class="vid">
<img src="http://placehold.it/400x150" alt="">
</div>
</div>
CSS:
.header {
display: inline-block;
width: 100%;
border: 1px solid #ccc;
}
.vid {
float: right;
}
.pic {
float: left;
}
Working fiddle
Think of it this way: use DIVs to group each entity that you want to float:left.
You want to align the two images, so put each into a div, and float those divs left.
You want to align the box containing the two images with the box containing the video. Float each of those left.
When you use float:left (or right), the floated DIV is removed from the HTML "flow" and therefore has zero height. Therefore, the DIV containing a floated DIV (or other element) has zero height. Not good. To fix that, apply the clearfix hack to the parent div.
Here is a better explanation (although it uses a different method to clear the floats -- both methods work fine): Customising Insightly HTML contact form (aligned, spaced fields)
/* CSS: */
body{min-width:650px;min-height:650px;}
#pics{float:left;border-radius:5px;XXXmargin-left:50px;border:2px dashed black;}
#picLeft {float:left;height:200px;width:200px;}
#picRight{float:left;height:200px;width:200px;}
#vid{float:left;height:200px;width:200px;border-radius: 5px;border:2px dashed black;}
.clearfix:after{content:"";clear:both;display:block;}
<!-- HTML: -->
<div id="container" class="clearfix">
<div id="pics" class="clearfix">
<div id="picLeft">
<img src="http://placekitten.com/200/200" alt="MyLogo" height="200" width="200" class="mainlogo">
</div>
<div id="picRight">
<img src="http://placekitten.com/195/195" alt="CopeLogo" height="200" width="200" class="exlogo" >
</div>
</div>
<div id="vid">
<iframe src="https://www.youtube.com/watch?v=8E8xMcXmI9E" width="195"></iframe>
</div>
</div>
Additional References:
CSS-Tricks: Clearfix hack
Another example
Ahmed Alaa's answer also has a good tip: display:inline-block is also useful - +1
float both elements left and adjust there widths accordingly.
Overview
I have an image with variable width aligned to the left, with text (having variable length) to the right of it.
I am trying to make the width of the right-most div element (containing the text) to fill the remaining width, centering the text both horizontally and vertically.
However, I cannot find a way to center align the text horizontally.
Code
As you can see by the code below, the text will only be centered in the rightmost div when the length of text flows over one line - however, in many instances in my project, the code is much smaller (this example is just "Centered Text") and the text is left-aligned:
<hr>
<div style="float:left; height: 75px;">
<img src="//i.imgur.com/rJD1cRy.png" style="margin:auto;display:block;">
</div>
<div style="width: 100%;">
<div style="display: table; height: 75px; overflow: hidden;">
<div style="font-style: italic; color: #777; display: table-cell; vertical-align: middle; text-align: center;">
Centered Text
</div>
</div>
</div>
<div style="clear: both;"></div>
<hr>
for centering the right-most div horizontally ,
adding margin:0 auto to the div should fix the issue:
<hr>
<div style="float:left; height: 75px;">
<img src="//i.imgur.com/rJD1cRy.png" style="margin:auto;display:block;">
</div>
<div style="width: 100%;">
<div style="display: table; height: 75px; overflow: hidden; margin:0 auto;">
<div style="font-style: italic; color: #777; display: table-cell; vertical-align: middle; text-align: center;">
Centered Text
</div>
</div>
</div>
<div style="clear: both;"></div>
<hr>
<hr>
<div style="float:left; height: 75px;">
<img src="//i.imgur.com/rJD1cRy.png" style="margin:auto;display:block;">
</div>
<div style="width: 100%;">
<div style="display: table; height: 75px; overflow: hidden; width: 60%;">
<div style="font-style: italic; color: rgb(119, 119, 119); display: table-cell; vertical-align: middle; text-align: center;">
Centered Text
</div>
</div>
</div>
<div style="clear: both;"></div>
<hr>
As you will see (very quickly), I am somewhat new to CSS. Ultimately I am trying to develop a "timeline", sort of like what facebook had years ago, where each post would be displayed in a timeline. Each post would ultimately alternate between the left side and the right side. For example, Post 1 - left side, post 2 - right side, post 3 - left side, etc...
I am having trouble with the layout of my timeline. I've found a way to make the posts alternate sides, however each "post" is essentially stacked on top of each other, whereas I'd like to put at least 35px spacing between each post. What this means is the second post (on the right side), would be 35px lower than the bottom of the first post (on the left side) and the third post would be 35px lower than the bottom of the second post (on the right side)...so on and so forth.
Any thoughts on how I might accomplish this?
<div id="timeline" style="background-color: #fff; float: left; padding: 10px; clear: left; width: 900px; margin-top: 25px;">
<div id="middle" style="width: 900px; height: 133px; background: transparent url('/timeline.png') repeat-y scroll 0px 0px;">
<div id="leftblock" >
<img src="/dot1.png">
<div style="float: left; margin-right: 23px;">
<div style="border: 1px solid #ccc; width: 400px; float: left;padding: 5px;">adfasdfsdaf</div>
<img style="float: left;" src="/right_arrow.png">
</div>
</div>
<div id="rightblock">
<img style="float: left;" src="/dot1.png">
<div style="float: left; margin-left: 23px;">
<img style="float: left;" src="h/left_arrow.png">
<div style="border: 1px solid #ccc; width: 400px; float: left; padding: 5px;">adfasdfsdaf</div>
</div>
</div>
<div id="leftblock" >
<img src="/dot1.png">
<div style="float: left; margin-right: 23px;">
<div style="border: 1px solid #ccc; width: 400px; float: left;padding: 5px;">adfasdfsdaf</div>
<img style="float: left;" src="right_arrow.png">
</div>
</div>
<div id="rightblock">
<img style="float: left;" src="dot1.png">
<div style="float: left; margin-left: 23px;">
<img style="float: left;" src="/left_arrow.png">
<div style="border: 1px solid #ccc; width: 400px; float: left; padding: 5px;">adfasdfsdaf</div>
</div>
</div>
</div>
</div>
You need to use a separate stylesheet for your css. This way you only need to define 2 sets of rules and you avoid code duplication. Anyways, once you have that set up, simply apply this code.
#leftblock, #rightblock{
margin-bottom: 35px;
}
This gives a 35px margin to the bottom of every div that has an id of leftblock or rightblock.
EDIT: My mistake, as per #Skullclutter's answer, please use classes instead of ids.
As a direct answer to your question, you can use margin-bottom: 35px; to add spacing below each box.
There's a few things you can do to make this code work well for future use, and to follow standard practices.
First off, id attributes must be unique throughout the page - an id of #content can only appear one time, so they're not ideal for repeating elements. Use class instead, which can be used as often as needed.
Next, use an external style sheet instead of inline styles. They're much easier to manage and the industry standard.
It's also generally a good idea to use as few elements as possible for your requirements (combining styling onto one element when possible).
I've marked up your code to show how a fixed up snippet may look:
#timeline{
background-color:#fff;
float: left;
padding: 10px;
clear: left;
width: 900px;
margin-top: 25px;
}
#middle{
background: transparent url('/timeline.png') repeat-y scroll 0px 0px;
}
.block{
float: left;
width: 400px;
display: block;
margin-right: 23px;
margin-bottom: 35px;
}
.block .content{
border: 1px solid #ccc;
padding: 5px;
}
.icon{
float: left;
}
.block:nth-of-type(odd) .icon{
float: right;
}
<div id="timeline">
<div id="middle">
<div class="block" >
<img class="icon" src="/dot1.png" />
<img class="icon" src="/right_arrow.png" />
<div class="content">adfasdfsdaf</div>
</div>
<div class="block">
<img class="icon" src="/dot1.png" />
<img class="icon" src="h/left_arrow.png" />
<div class="content">adfasdfsdafadfasdfsdaf<br />adfasdfsdaf<br /></div>
</div>
<div class="block" >
<img class="icon" src="/dot1.png" />
<img class="icon" src="/right_arrow.png" />
<div class="content">adfasdfsdaf<br />adfasdfsdaf<br /></div>
</div>
<div class="block">
<img class="icon" src="/dot1.png" />
<img class="icon" src="h/left_arrow.png" />
<div class="content">adfasdfsdaf</div>
</div>
</div>
</div>
http://jsfiddle.net/daCrosby/g2w6dzo3/1/
One thing to add: you can only have one of each ID name on the page. For what you're doing, I'd suggest using class=leftblock and class=rightblock instead.
Then Eric's code becomes:
.leftblock, .rightblock{
margin-bottom: 35px;
}
I'm trying to format an image so it aligns to the bottom of a div. I put what I think is the relevant code in a fiddle: http://jsfiddle.net/5KEyD/
<div id="main">
<div style="display: inline-block; margin-left: 15px">
<div id="imageWrapper">
<img id="bottomImage" class="aligncenter" src="Bottom-Text.png" alt="Some Image" width="800" height="152" /></div> </div></div>
#main{width:800px; height: 200px; padding-right:40px; overflow:hidden; background- color: #c0c0c0;}
#imageWrapper { position: relative; height: 152px; } /* height is for display purposes only */
#bottomImage { position: absolute; bottom: 0px; }
What I need is for the image to align (centered) all the way at the bottom, with no extra grey space under it.
Thanks for any help!
<div style="width: 800px; margin: 0 auto;">
<div id="imageWrapper">
<img id="bottomImage" class="aligncenter" src="/wp-content/uploads/2014/05/Bottom-Text.png" alt="Some Image" width="800" height="152">
</div>
</div>
I think that happens because of using display: inline-block for div element. Inline-block rule always adds 5px margins to element. So if you remove it the issue will be fixed. If you need that rule you can add -5px bottom margin.