Centering texted div with absolute position - html

I need few lines of text to be centered horizontaly and verticaly on image, i read a lot in internet but i didn't helped me any of solution.
HTML:
<div id="textContainer">
Preparing for important meeting? <br />
<b>Drink some coffeee!</b> <br />
<img src="http://i.imgur.com/me2octq.jpg" />
</div>
<div id="baner">
<div class="imgBaner"></div>
</div>
CSS:
.imgBaner {
margin: 0 auto;
width: 100%;
height: 460px;
background: url(http://i.imgur.com/5pijCrS.jpg) no-repeat center;
position: relative;
}
#textContainer {
position: absolute;
font-size: 32px;
text-align: center;
z-index: 1;
color: yellow;
}
Also i'm enclosing fiddle: http://jsfiddle.net/vXzKc/
I appreciate every help!

To achieve this, I'm quite confident you would at least have to know the height of your textcontainer. Then you could use a top/margin combination to solve this.
For what I know a table-cell is the only element that allows its content to be vertically aligned. I have no idea why, but this would allow you to make a little cheat by using display: table-cell
Here's my workaround:
HTML:
<div class="imgBaner">
<div id="textContainer">
Preparing for important meeting? <br />
<b>Drink some coffeee!</b> <br />
<img src="http://i.imgur.com/me2octq.jpg" />
</div>
</div>
CSS:
.imgBaner {
margin: 0 auto;
height: 460px;
background: url(http://i.imgur.com/5pijCrS.jpg) no-repeat center;
display: table-cell;
vertical-align: middle;
}
#textContainer {
font-size: 32px;
text-align: center;
color: yellow;
}
http://jsfiddle.net/vXzKc/1/
Note that display: table-cell may not work in old browsers or could cause different behaviour. I have tested it in the newest browers (IE, Firefox and Chrome) and it seemed to work in all of them.

Related

Css and html alignment issue with divs and images in a div

Im building my first responsive type website, So far doing the index page. Im having an issue when that i cannot align the footer divs together. i have three divs spread out and the last div on the right has social 4 icons. but im unable to get these to align with the other two divs texts. Ive tried a number of different things to fix it in the css and flex though id rather stick to css right now on this site.
Here is the site on test host to see the actual icons in the footer.
https://hireahottub2.netlify.com/
i feel the problem may lie in my code somewhere but i cannot see it for the life of me.
align-items: center
display:inline block is in the parent
<html>
<footer>
<div id="footerwrap">
<div class="fdiv1">
<h5>Hire A Hot Tub, Goole, DN14 6QT</h5>
</div>
<div class="fdiv2">
<h5>Web Design by DM DESIGN</h5>
</div>
<div class="fdiv3">
<a href="https://www.facebook.com/hireahottub2000" target="_blank"
><img src="./img/fb2.png"
/></a>
<a href="https://www.instagram.com/hireahottub2000" target="_blank"
><img src="./img/insta2.png"
/></a>
<a href="https://twitter.com/HireahottubUK" target="_blank"
><img src="./img/twitter2.png"
/></a>
<a href="mailto:hireahottub2000#hotmail.com" target="_blank"
><img src="./img/email2.png"
/></a>
</div>
</div>
</footer>
</html>
/* FOOTER CSS */
footer{
padding: 5px;
margin-top:;
color:#ffffff;
background-color: #354243;
text-align: center;
font: bold;
border-top: #e8491d 3px solid;
}
#footerwrap{
width: 80%;
text-align: center;
}
.fdiv1{
float: center;
display: inline-block;
width: 20%;
}
.fdiv2{
float: left;
width: 20%;
}
.fdiv3{
float: right;
width: 20%;
min-width: 75px;
}
.fdiv3 img{
width: 30px;
}
For your issue specifically, I'm seeing that your divs fdiv1 and fdiv2 only align in the center because of browser-set margins on the heading tags within them. Furthermore, they have zero concept of the height of any other div, because they are floated (removed from document flow). To fix this, you will need to set them all an equal height. Then vertical-align will actually work.
h5 {
margin: 0;
}
.fdiv1, .fdiv2, .fdiv3 {
height: 50px;
}
a {
display: inline-block;
vertical-align: middle;
}
It may be beneficial for you to learn Flexbox. It makes these types of tasks easy, but it's not supported in older browsers.
My recommendations are:
Get rid of all of the float stuff.
Get rid of the width: 20% stuff on the footer items. (Maybe bring it back after you see the results of the rest of this.)
Get rid of the single inner <div> that's a child to the <footer> element (I guess you said you already did that somewhere else, just not on the current demo website).
Use the flex justify-content (space-between) and align-items (center) CSS attributes on your <footer> to spread your footer items out in the proper fashion.
Follow up...
I tried the above, ended up keeping the width: 20%, and got this as a result:
I guess you might want to switch the order of those first two footer items around, but that's not something I could do easily just playing with CSS attributes in my web console.
Use a css grid layout to achieve this.
footer {
padding: 5px;
color: #ffffff;
background-color: #354243;
text-align: center;
border-top: #e8491d 3px solid;
display: grid;
grid-template-columns: repeat(3, 1fr);
align-items: center;
}
footer div img {
width: 30px;
}
<footer>
<div class="fdiv1">
<h5>Hire A Hot Tub, Goole, DN14 6QT</h5>
</div>
<div class="fdiv2">
<h5>Web Design by DM DESIGN</h5>
</div>
<div class="fdiv3">
<img src="./img/fb2.png" />
<img src="./img/insta2.png" />
<img src="./img/twitter2.png" />
<img src="./img/email2.png" />
</div>
</footer>
Hello this is a solution if you want to stick with only CSS ( without flex ) :
footer{
padding: 5px;
position: relative;
margin-top:;
color:#ffffff;
background-color: #354243;
text-align: center;
font: bold;
border-top: #e8491d 3px solid;
}
.fdiv3{
width: 20%;
min-width: 75px;
position: absolute;
top: 50%;
right: 0;
transform: translate(0,-50%);
}
.fdiv2{
width: 20%;
width: 20%;
min-width: 75px;
position: absolute;
top: 50%;
left: 0;
transform: translate(0,-50%);
}

How do I create a right aligned logout button?

My top div acts as a logo and has a title. I would like a logout button to be on the right-hand side of the div and text above also right-aligned.
I left out the button/ link as i did not know where to place it.
I'm looking for something like this:
My goal is a logo and, on the right, the logout button with text on the top.
How can I achieve that?
.logo {
overflow: hidden;
text-align: left;
position: relative;
margin: 0px 100px;
height: 60px;
background-color: pink;
color: blue;
font-family: Arial;
}
<div class="logo">
<h1>LOGO</h1>
</div>
You can use flexbox here. Try this out:
.wrap {
display: flex;
justify-content: space-between;
}
.logo {
overflow: hidden;
text-align: left;
position: relative;
height: 60px;
background-color: white;
color: #1F6C8B;
font-family: Arial;
}
<div class='wrap'>
<div class="logo">
<h1>LOGO</h1>
</div>
<div>
<p>abcdefg</p>
<button>Click It</button>
</div>
</div>
jsfiddle: https://jsfiddle.net/dm198kpx/2/
There are various ways to achieve what you want. I believe the simplest one is with Flexbox:
.flex {
display: flex;
}
.justify-between {
justify-content: space-between;
}
<div class="flex justify-between">
LOGO
<div>
BLABLABLA<br>
<button>Logout</button>
</div>
</div>
Here, flex is a display property that is usually used in container-type elements (like div). It helps to align content. It allows the use of various other properties like justify-content, align-items* and others. In this case, we are using only justify-content, which align direct children on the main axis (the horizontal one by default), with the space-between value, which distributes the content as far as possible - and since we have only two direct children of <div class="flex justify-between">, LOGO and <div>, put the first on the far left and the last on the far right.
*: you can learn more about Flexbox properties and use cases in this game: https://flexboxfroggy.com/
I've added the button to what I think is your header? I used the native header tag, but if it isn't your header, you can always replace this with a div with a unique id of your choice. I included position:fixed; in the css, otherwise the button wouldn't stay to the right (you could use float, but it can be problematic imho). Height/colour etc are adjustable of course.
Hope this helps
h1.logo {
display: inline-block;
position: relative;
text-align: left;
margin: 10px 100px;
height: 60px;
background-color: white;
color: #1F6C8B;
font-family: Arial;
}
#logout {
background-color: lightblue;
height: 30px;
text-align: right;
right: 40px;
position: fixed;
}
.logo,
#logout {
vertical-align: top;
}
<header>
<h1 class="logo">LOGO</h1>
<button id="logout">Logout</button>
</header>
EDIT: Just saw the text-above edit to your question. See fiddle

Align image and text within same cell vertically?

I have a small 2x2 HTML table. Each cell contains one image and one piece of text.
Example image:
As you can see, the text is vertically below the vertically middle level of the image. How can I make the text vertically in the center, relative to the image on the left?
I have tried a several different display values, which I can't fix this.
JSFiddle: http://jsfiddle.net/ahmadka/rbJNQ/
this all div behave like a
table: it is a rectangular block that participates in a block formatting context
.main-container is TABLE
.container is ROWS
.inner-container is CELLS
now all div behave like a table I just vertical align middle image so all content align in middle
.main-container{
display:table;
background:#bcbbbb;
width:100%;
height:100vh;
}
.container{
display:table-row;
}
.inner-container {
vertical-align: middle;
display: table-cell;
text-align: center;
}
.inner-container figure{
background-color:#807c7c;
vertical-align: middle;
display: inline-block;
margin: 0;
}
.inner-container p {
display: inline-block;
}
<div class="main-container">
<div class="container">
<div class="inner-container">
<figure>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66" alt="stack" width="100" height="100">
</figure>
<p>Example</p>
</div>
<div class="inner-container">
<figure>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66" alt="stack" width="100" height="100">
</figure>
<p>Example</p>
</div>
</div>
<div class="container">
<div class="inner-container">
<figure>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66" alt="stack" width="100" height="100">
</figure>
<p>Example</p>
</div>
<div class="inner-container">
<figure>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.svg?v=6e4af45f4d66" alt="stack" width="100" height="100">
</figure>
<p>Example</p>
</div>
</div>
</div>
Well. Try this:
<td>
<a id="social_twitter" onclick="window.open(this.href,'external'); return false;" href="http://twitter.com/JenierTeas">
<span>icon</span>
</a>
<span style="vertical-align: middle;">Twitter</span>
</td>
Your code looks way to complicated for this. It is also not really tabular data, so you should not use a <table>-element.
You can solve it as easy as this. I included only the important CSS rules in this answer.
HTML
<div class="social_body">
<a class="facebook">Facebook</a>
<a class="twitter">Twitter</a>
<a class="google">Google+</a>
<a class="share">Share This</a>
</div>
CSS
.social_body {
width: 280px;
overflow: hidden;
margin: 0 auto;
border: 1px dashed black;
}
.social_body a {
float: left;
width: 50%;
font-size: 16px;
line-height: 24px;
}
.social_body a:before {
content: '';
float: left;
width: 24px;
height: 24px;
margin: 0 10px 0 0;
background: transparent 0 0 no-repeat;
}
.social_body a.facebook:before {
background-image: url(http://i.imgur.com/QglLT5Q.png);
}
.social_body a.twitter:before {
background-image: url(http://i.imgur.com/QglLT5Q.png);
}
/* […] You see where this is going. */
Demo
Try before buy
Use This Css
//CSS
#nst_block #social_body table {
background: none repeat scroll 0 center transparent;
border: 1px dashed black;
height: 75px;
margin: 0 auto;
width: 280px;
}
#nst_block #social_body table a {
border: 0 none;
font-family: inherit;
font-size: 16px;
font-style: inherit;
font-weight: inherit;
line-height: 24px;
margin: 0;
outline: 0 none;
padding: 0;
vertical-align: baseline;
}
Updated Link :http://jsfiddle.net/rbJNQ/16/
I realize that this question is 4 years old, but hey, why not answer it - maybe it helps someone else, since the OP probably solved it one way or another in the meantime.
In 2017 your problem, Ahmad, is solved fairly easy with the help of a flexbox (just Google it, you'll surely like it - if you haven't heard of it already, that is) set for the direct 'children' of the table's td-s. What you have to do is add the following to the CSS in your fiddle:
#nst_block #social_body table td > *
{
display: flex;
justify-content: flex-start;
align-items: center;
}
The main axis positioning is done by justify-content and the cross axis positioning (the vertical one, in your case) is done by align-items.
That's all.
You can design it using tables. Keep icon in one cell and text in other cell. Make them vertical-align as middle. So how what ever height the icon may be, text will be aligned vertically middle. Or If you are targeting only new browsers, then you can design this using flex box model.

Overflow several inline vertical lists horizontally

I have an information bar at the bottom of my site. It has several inline-block elements that are displayed horizontally. As you can see in the fiddle, when the information in the inline-block elements gets too large vertically, it overflows below the bottom edge of the information bar. I would like this stuff to overflow to the right, similar to a newspaper column.
jsfiddle: link
<div class="information-bar">
<div class="information">
<div class="title">Section Title</div>
information <br />
information2 <br />
information3 <br />
information4 <br />
information5 <br />
</div>
<div class="information">
<div class="title">Section Title</div>
information <br />
information2 <br />
information3 <br />
information4 <br />
information5 <br />
</div>
</div>
The goal is to transform this:
to this:
I have tried the css3 columns method: jsfiddle.
Unfortunately it is only designed to work with one column, I believe, because this happens:
I think I got it. There's one issue, however: when the windows is too small, the second information set is displayed below. But I think this isn't a big issue, since it's not that wide.
.information-bar {
width:100%;
height: 100px;
background: #999;
position: absolute;
bottom: 0;
}
.information-bar .information {
display: block;
height: 95px;
-webkit-column-width:144px;
-webkit-column-gap:16px;
-moz-column-width:144px;
-moz-column-gap:16px;
column-width:144px;
column-gap:16px;
-moz-column-count:2;
-webkit-column-count:2;
column-count:2;
}
div.information
{
float:left;
width: 304px; /*quick math: 144*2+16=304*/
}
Using the insight from this gist, we can organize the information in columns, but we must know the maximum column width beforehand:
.information-bar {
width:100%;
height: 100px;
background: #999;
position: absolute;
bottom: 0;
}
.information-bar .information {
display: inline-block;
vertical-align: top;
height: 95px;
}
p.info-element {
line-height: 1.2em;
}
p.info-element:nth-child(n+1):nth-child(-n+5) {
margin-left: 0em;
}
p.info-element:nth-child(6) {
margin-top: -6em;
}
p.info-element:nth-child(n+6):nth-child(-n+10) {
margin-left: 100px;
}
The nth-child(n+1):nth-child(-n+5) selector allows us to select a range of elements (as discussed in the gist). An interesting css hack!
jsfiddle

centering image in div

I have a square <div> (70px x 70px) which will contain an image of a variable dimensions(Square, landscape or potrait). I want this image to be symmetrically centered inside the <div>. how do I get it..?
<div class="img-polaroid" style="width: 70px; height: 70px; background-color: black; text-align:center;">
<image src='.base_url("images/store/images/".$image->image).' />
</div>
The actual size of the image can be greater than 70px x 70px. But it should fit symmetrically in the center.
I also have to make it cross-browser compatible..
Help Appreciated...
Must it be an <img> element?
You can set the image as background of the <div> with background-position:center center - this is very easy to do, not using javascript and cross-browser..
Use it like this:
<div class="img-polaroid" style="width: 70px; height: 70px; background:url('.base_url("images/store/images/".$image->image).') center center no-repeat black; text-align:center;"></div>
Check this Working jsFiddle. In terms of browser support I've never come across a browser that doesn't support this.
It is sure to work on:
Netscape 6+
Mozilla 1+
Firefox 1+
Internet Explorer 4+
Opera 3+
Safari 1+
This will do the trick for u
.img-polaroid img{display:block; margin:0 auto;}
<div class="img-polaroid" style="width: 70px; height: 70px; background-color: black; text-align:center;">
<image src='.base_url("images/store/images/".$image->image).' />
</div>
should be
<div class="img-polaroid" style="width: 70px; height: 70px; background-color: black;display:table-cell;vertical-align:middle;text-align:center;">
<image src='.base_url("images/store/images/".$image->image).' />
</div>
add display:table-cell;vertical-align:middle;
Use display: table-cell; for your div with your text-align: center; and vertical-align: middle;
Below is the code:
<div class="img-polaroid" style="width: 70px; height: 70px; background-color: black; border: 1px solid yellow; text-align:center; vertical-align: middle; display: table-cell;">
<image src="http://www.gelifesciences.com/gehcls_images/GELS/Link%20Images/Product%20Link%20Images/ImageScanner%20III%2050x50.jpg" />
</div>
and here is the example: http://jsfiddle.net/eUGb6/
Here you can find more about compatibility issues:
vertical-align text-align display
It's very simple to do it vertically/horizontally alignment.
First build a holder div:
.holder {
display: table;
}
Then build a container div:
.container {
display: table-cell;
vertical-align: middle;
text-align: center
}
And in your container, it would be shown everything centered.
I didn't test it yet.. but should work ;p
<div class="holder">
<div class="container">
<img src="YOURIMAGE" />
</div>
</div>
Its easy :)
.img-polaroid {
display:table-cell;
vertical-align: middle
}
check fiddle http://jsfiddle.net/9hppL/2/
You can use the display:inline-block and :after for div, like this:
div {
width: 70px;
height: 70px;
text-align: center;
border: 1px solid green;
}
div:after {
content:"";
display: inline-block;
width:0;
height: 100%;
vertical-align: middle;
}
img {
vertical-align: middle;
}
Please view the demo.Click here, you can look at other solutions.
This is your ultimate guide to centering things in CSS> http://www.w3.org/Style/Examples/007/center
Here is a working jsfiddle: http://jsfiddle.net/9hppL/
.img-polaroid {
display: table-cell;
vertical-align: middle
}
#image {
background: blue;
width: 50px;
height: 50px;
margin-left: auto;
margin-right: auto;
}