Positioning text with the :after pseudo selector - css

I have this:
div#myImg{
background: url('myimage.png') left top no-repeat;
}
div#myImg:after{
content: 'TEXT UNDER IMAGE';
margin:0 auto;
vertical-align:text-bottom;
font-size: 14px;
}
.dm_lp_imgs{
width: 100px;
float:left;
height: 115px;
margin-right: 15px;
}
<div id="myImg" class="lp_imgs"></div>
I know you can add text with the :after pseudo selector, but I want to position that text, centered, just below my image.
Can I do that with the :after pseudo selector?
At the moment, the text seems stuck to the top of the div#myImg DIV.
UPDATE:
display:block
Doesn't do the trick...

Usually I use CSS position to do that sort of thing. Make the parent position:relative, add position:absolute to the ::after element, and give it a top:100%
http://jsfiddle.net/s22Af/
div#myImg:after{
content: 'TEXT UNDER IMAGE';
margin:0 auto;
vertical-align:text-bottom;
font-size: 14px;
position:absolute;
top: 100%;
text-align: center;
}
.dm_lp_imgs{
width: 100px;
float:left;
height: 115px;
margin-right: 15px;
position: relative;
}

You can position pseudo elements absolutely relative to the parent. Add position: relative to the div and position: absolute to the :after CSS, then position using usual methods. I've done a quick fiddle of it here: http://jsfiddle.net/5s3Fr/

I don't know your requirements but if you're coding for modern browsers/html5 you should look into the <figure> and <figcaption> html tags
For your consideration: FIDDLE
Markup:
<figure id="myFigure">
<div id="myImg" class="dm_lp_imgs"></div>
<figcaption>text under image</figcaption>
</figure>
CSS
.dm_lp_imgs {
width: 100px;
height: 115px;
}
#myFigure {
float:left;
margin-right: 15px;
width: 100px;
}
#myImg { background: url('myimage.png') left top no-repeat; }
#myImg + figcaption {
/* Style caption here */
text-align:center;
text-transform:capitalize;
}

Related

How can I use CSS to center text in DIV

I need to create upload button that centered at an image.
I'm a beginner, please help and explain how to use. Many thanks.
Here is my code:
https://jsfiddle.net/marco83/ydgnkfw3/
HTML
<div class="media-left text-center container22">
<img src="http://images.buddytv.com/btv_2_505531673_0_1200_10000_-1_/the-pacific-james-ba.jpg" style="width: 150px; height: 150px;" class="image1"></img>
<a href="#" id="upfile1">
<span style="font-size: 28px;" id="img-upload-bt" class="glyphicon glyphicon-camera"></span>
</a>
</div>
<input type="file" id="file1" name="file1" style="display:none" />
CSS
.container22 {
height: 10em;
display: table-cell;
vertical-align: middle;
text-align: center;
}
#upfile1 {
margin-left: auto;
margin-right: auto;
}
Here is what i want
One approach is to make the parent container relative and use position absolute for the inner element you're trying to center:
.image-container {
position: relative;
}
#img-upload-bt {
position: absolute;
left: 50%;
top: 50%;
margin: -14px 0 0 -14px;
}
JSFiddle Demo
The easiest way to handle this would be to just set the image as a background-image in the css, if that's something your particular situation allows for -
.container22 {
height: 150px;
width: 150px;
background: url("http://images.buddytv.com/btv_2_505531673_0_1200_10000_-1_/the-pacific-james-ba.jpg") center center no-repeat;
}
That + removing the image tag for the markup will create the look you want.
If you have to use the image inline, you can put position:relative on the container element, and position either the image or the upload glyph span with position: absolute - note that you may have to force a width and height on the container if you set that on the image. If you set it on the glyph span, you'll also need left:0; right:0; margin: auto; to center it.
The image pushes the icon out of the way
Use background image instead
.container22{
width:150px;
height:150px;
background-image:url('http://images.buddytv.com/btv_2_505531673_0_1200_10000_-1_/the-pacific-james-ba.jpg');
background-size:150px 150px;
background-position:center top;
display: table-cell;
vertical-align: middle;
text-align: center;
padding-right:0
}
https://jsfiddle.net/marco83/ydgnkfw3/
You need to set the css position property of the parent element to relative and img to position:absolute. Then you use css properties of top, bottom, left and right to position the image.
.container22 {
height: 10em;
display: table-cell;
vertical-align: middle;
text-align: center;
position:relative;
}
#upfile1 {
position: absolute;
left:55px;
top:55px;
}
See an example here.

How to put some divs in a row?

I'm trying to put two divs without a linebreak between them.
this is the html:
<div id="header">
<div id="logo"></div>
<div id="left">
<div id="slideshow"></div>
</div>
</div>
and this is the CSS:
#header {
background-color: #13768a;
width: 962px;
height: 207px;
margin-right: auto;
margin-left: auto;
clear: both;
}
#logo {
background-image:url('logo.png');
height: 207px;
width: 250px;
margin-right: 0px;
padding: 0px;
}
#left {
width:712px;
height: 207px;
}
#slideshow {
background-color: #137387;
width: 686px;
height: 144px;
margin-right: auto;
margin-left: auto;
}
the problem is that I want it to look like this:
How I want it to look like
But it looks like this:
How it looks like
This is controlled by the display style property. Normally, div elements use display: block. You can use display: inline or display: inline-block instead if you want them on the same horizontal line.
Example using inline-block (live copy | source):
CSS:
.ib {
display: inline-block;
border: 1px solid #ccc;
}
HTML:
<div class="ib">Div #1</div>
<div class="ib">Div #2</div>
Introduce a float CSS property. Change CSS as below, for #logo and #left.
#logo {
background-image:url('logo.png');
height: 207px;
width: 250px;
margin-right: 0px;
padding: 0px;
float:right;
}
#left {
width:712px;
height: 207px;
float:left;
}
From the MDN Documentation,
The float CSS property specifies that an element should be taken from
the normal flow and placed along the left or right side of its
container, where text and inline elements will wrap around it.
Div elements normally use display:block which forces a line break before and after the element.If you want to remove the line breaks , you can use display:inline which will display elements horizontally.Make the div's display property to display:inline or display:inline-block you want to appear horizontally .
Try this way:
#logo {
background-image:url('logo.png');
height: 207px;
width: 250px;
margin-right: 0px;
padding: 0px;
float:right;}
#left {
position:relative;
width:712px;
height: 207px;
}
#slideshow {
position:absolute;
top:20px;
left:20px;
background-color: #137387;
width: 686px;
height: 144px;
margin-right: auto;
margin-left: auto;
}​
Basically I put a float:right; on the logo to position it right, then added position:relative to the #left div and position:absolute to the #slideshow div. This way you can adjust the top and left attributes to position the slideshow anywhere you want it.
display:inline is the css style that you need to use.

Vertical align in CSS, no heights known

Here's my markup:
<div id="container">
<img id="image" src="image.jpg" />
<div id="contents">Contents</div>
</div>
The height of #container equals the height of #image.
All the heights are dynamic (they change on window resize).
The image can not be set via background property.
How can I have Contents over the image and vertically centered in #container?
Is the height of #contents known? In that case this should do it (jsfiddle demo):
#container{
position:relative;
/* For demo only */
height:500px;
border: 5px solid red;
}
#image{
position:absolute;
/* For demo only */
height:500px;
}
#contents{
position: absolute;
top:50%;
height:100px;
margin-top: -50px; /* Half of #contents height */
/* For demo only */
background-color: blue;
}
​This ought to do what you are looking for. I have just set the height of the container and image in css, but if they are the same set in html or using javascript, the result should be the same. The background colour is just there for clarity.
#container {
background-color: #333;
height: 200px;
}
#image{
height: 200px;
float: left;
}
#contents{
line-height: 200px;
float: left;
position: fixed;
}
​
EDIT: Here is a fiddle of a solution using the old classic margin auto trick. The only thing that may cause problems here is that the parent needs to be position: fixed; which may cause issues for you elsewhere. The main thing is it works, and no heights are set using pixels.
link
Here is the code from the fiddle for a pure css solution with no fixed heights.
<div id="container">
<img id="image" src="https://www.google.co.uk/logos/2012/juan_gris-2012-hp.jpg" />
<div id="contents">Contents</div>
</div>
#container {
position: fixed;
}
#contents{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 50%;
height: 10%;
margin: auto;
}
If you know the height of #contents you can set up
#container{position:relative;}
#contents{
position:absolute;
top:50%; /*50% of parent*/
margin-top:/*negative one-half of container height i.e. if contaner is 4 em -2em*
}
You say you don't know the height of #contents, though.
Another option is to set the display: to table-cell and use vertical-align: middle
#container{
display: table-cell;
vertical-align: middle;
}
But depending on what browsers you are targetting for support, that may cause display issues as well.
The more sure fire way is to combine the first option with some jquery or javascript to find the element's height and set its margin-top:
content= document.getElementById('content')
content.style.marginTop = '-' + content.offsetHeight + 'px';
http://jsfiddle.net/h76sy/
EDIT: Sorry, had a bug in my javascript, try it now

position fixed margin divs in scrollable content

Hello
please have a look at my jsfiddle.
The content of the inner div-element is scrollable.
Each grey symbol has a margin-left. When I scroll the content the symbols shouldn't be fixed to the background.
It should be scrollable with the position.
Have you got an idea how I achieve that effect?
Keep in mind that positioning is relative to the closest positioned parent.
When you are assigning an absolute position to the "symb" class you are positioning them relative to the document rather than their parent.
Simply adding "position: relative;" to your div.tl element will set the parent div as positioned without moving it and the "symb" elements will act the way I think you expect them to.
Your new .tl definition should be:
.tl {
width: 500x;
height: 80px;
background-color:grey;
position: relative;
}
Furthermore, I'm assuming that you have some need to position these absolutely. You could achieve similar results by simply removing the "position: absolute" portion of your .symb definition.
You are setting a margin, not a position, so you don't need to bother with positioning at all in your example case.
I am not sure what do you need. You had an error in your last "symb" - you missed 'p' in 'px'. Try this?
<div class ="outer">
<div class="inner">
<div class="tl">
<div class="box" style="width: 315px;">
<div class="symb" style="margin-left: 0px;"></div>
<div class="symb" style="margin-left: 15px;"></div>
<div class="symb" style="margin-left: 20px;"></div>
</div>
</div>
</div>
</div>
.outer {
width:50%;
}
.inner {
overflow-x:scroll;
}
.tl {
width: 500x;
height: 80px;
background-color:grey;
}
.box {
float: left;
height: 61px;
}
.box .symb {
float:left;
width: 5px;
height: 5px;
background-color: #cccccc;
z-index: 999;
margin-top: 10px;
}
Use
position: relative;
Not
position: absolute;
Just try with the following CSS:
.box .symb {
position: relative;
float: left;
position: inline-block;
width: 5px;
height: 5px;
background-color: #cccccc;
z-index: 999;
margin-top: 10px;
}

How do I avoid gap when using z-index in CSS?

I have the following HTML code:
<div class="outer_container">
<div id="imgContainer">
<img src="/some/image" />
</div>
<div id="slogan">
<span class="quote">Some text here</span>
</div>
<div id="footer" class="gray_top_border">
Some text here
</div>
</div>
And this is my CSS:
.outer_container {
background-color:#FFFFFF;
margin:0 auto;
width:960px;
}
#slogan {
font-size: 3em;
position: relative;
z-index: 999;
bottom: 50px;
left: 50px;
}
#footer {
border-top:1px solid #B5B5B5;
min-height:50px;
padding:10px;
}
Using this code, I get a 3em gap between image and footer.
If I change position from relative to absolute, the gap problem is gone. But then the top / left position is relative to the browser window, and not within the DIV container.
How can I float text over the image without creating this gap?
This does it:
#slogan {
font-size: 3em;
position: relative;
height: 0;
overflow: visible;
z-index: 999;
bottom: 50px;
left: 50px;
}
can you try the below css.
#slogan {
font-size: 3em;
position: relative;
z-index: 999;
margin-top:-20px;
}
#footer {
position:absolute;
bottom:10px
border-top:1px solid #B5B5B5;
min-height:50px;
padding:10px;
}
"Position: relative" still reserves the area the text would have been in. This means it can make some weird padding/margin issues once in a while.
"position: absolute" does not reserve the area. I recommend just using that instead of hacking around with the relative one.
http://www.w3schools.com/css/css_positioning.asp
You can wrap the whole thing in a new div that's position: relative then your absolute pixels will go out from that instead of screen 0,0.
"An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is ."

Resources