I want to use a text as the background for another text. Basically, this is quite easy by using two div elements with appropriate position and z-index settings, as described in Is there a way to use use text as the background with CSS?.
My problem is that I want to align both texts, although they are different in size. The background text is bigger, and the foreground text shall be aligned vertically with the background one, so that both texts' vertical center is at the same position.
I can achieve this by manually adjusting the position in pixels, but I have to do this for every font-size I want to use individually. Moreover, I can never be sure that this works in every browser in the same way.
Is there a better alternative to do this?
I got it by using:
<div style="position: relative; font-size: 100pt; height: 100px;">
<div style="font-size: 2em; color: #f00; position: absolute; top: 0; left: -0.37em; bottom: 0; right: 0; z-index: -1; line-height: 0.4em;">
B
</div>
Sample text
</div>
Using left and line-height properties I am able to align both texts, and when I increase the font-size of the container, their relative alignment stays the same.
Vertical align can be done like this:
.parent {
position:relative;
zoom:1;/*has layout for ie*/
}
.verticalcentered1, .verticalcentered2 {
position:absolute;
top:50%;
height:20px;
margin-top:-10px;/*half the height*/
}
.verticalcentered1 {z-index:1;}
.verticalcentered2 {z-index:2;}
If the verticalcentered elements are youre texts they will be vertically centered in the parent and be on top of each other, if thats what youre looking for.
i think you are looking like this :- DEMO
HTML
<div id="container">
<div id="background">Sample Some Text </div>
B
</div>
CSS
#container {
position: relative;
font-size:70px;
color:#00c7ff;
z-index:-1;
}
#background {
left: 20px;
position: absolute;
right: 0;
top: 25%;
z-index: 10;
color:black;
font-size:20px;
}
Yeah ofcourse you can user alternative way - Placeholder text
Text will be automatically removed when you type the new text then default text will be remove
//textbox
<input type="text" defaultvalue="Name" value="Name" name="add_name" id="add_name">
//Javasript Code
//For the placeholder
$('#add-category input[type=text]').focus(function() {
if ($(this).val() == $(this).attr('defaultValue')) {
$(this).val('');
}
});
$('#add-category input[type=text]').blur(function() {
if ($(this).val() == '') {
$(this).val($(this).attr('defaultValue'));
}
});
Try the above option.
Related
I am wanting to, if possible, align a set of text (<p>TextHere</p>) relative to where a image is. I have an image centered horizontally on my page:
.headerlogo {
height: 60px;
width: 70px;
position: fixed;
z-index: 99;
margin-top: 5px;
left: 50%;
margin-left: -35px;
}
Now, I want some text to be 50px to the right/left of that... I thought there may be some sort of way to do so using something similar to margin-left but so far haven't come up with anything.
I know that I can put text a certain distance out from the left using margin-left: 600px; so that it looks good and next to the image, but as you all know, this will move on window resize.Any help would greatly be appreciated as I am still new at positioning...
as you didn't post the relavent code of your problem but still i try to display the text next to the image with margin-left:50px. and it will not gonna move on windows resize .
Here is the jsFiddle link http://jsfiddle.net/datechogeek/qU6Cb/
Keep the position:absolute for your text and add left,right position as per your need . It would be nice if you can post your complete HTML code.
Were you trying something like this?
HTML
<div class='wrapper'>
<p class='leftText'>Left text</p>
<img class='headerlogo' src='http://m.c.lnkd.licdn.com/media/p/2/000/19b/1b2/065601a.png' alt='your_image'/>
<p class='rightText'>Right text </p>
</div>
CSS
.wrapper{
position:relative;
height:60px;
width:280px;
margin:auto;
}
.headerlogo {
position:relative;
float:left;
height: 60px;
width: 70px;
margin-left:50px;
margin-right:50px;
}
.leftText, .rightText {
position:relative;
float:left
}
Fiddle Link
I'm designing a pretty simply website for a friend. In the design, he wants a ribbon to stretch out horizontally in the middle of the page.
Like so:
https://i.imgur.com/Hz4SH4Hh.png
My attempt at doing this was to crop the 'ribbon' parts of the right and left and display those images while floating them to the right and left. Then creating a content div, centering it to fill in the middle of the ribbon. This solution is super sloppy and doesn't work well at all. Here's a picture of it
https://i.imgur.com/66C2kj5h.png
The resolution is kinda off, but you can see that the border of the middle div are off, and when stretching or shrinking the page, the percent width of the middle div messes the whole thing up.
Heres my HTML/CSS
<div class='ribbon-container'>
<div id='ribbon-left'>
</div>
<div id='ribbon-right'>
</div>
<div class='clear'></div>
<div id='ribbon-middle'>
</br>
<center>
<span class='ribbon_header'>Food Around Your School</span>
</center>
</div>
</div>
.ribbon-container { width:100%; height:118px; position:relative;}
#ribbon-left { background-image:url('images/ribbon_left.png'); width:117px; height:119px; position:absolute; bottom:0; left:0;}
#ribbon-right { background-image:url('images/ribbon_right.png'); width:117px; height:119px; position:absolute; bottom:0; right:0;}
#ribbon-middle { width:85%; height:81px; background-color:#b5b5b5; border:7px; border-top-style:solid; border-bottom-style:solid; border-color:#61615f;top:0; margin:0 auto;}
You're just gonna have to size everything in the same units. I'd suggest just using pixels unless you're going with a responsive design, and if you're going with a responsive design, I'd suggest using something like foundation.js.
Edit: and +1 to Michael Peterson's SVG idea. That's a good one too.
Perhaps try setting
.ribbon-middle{
width: auto;
padding: 0 120px;
}
where ribbon's padding is the width of the left/right images. then you will have the text always visible.
Since the height of the banner is not changing, you can accomplish this using by using a horizontally-repeating image as the background for the banner and then using absolute positioning for the left and right portions of the banner.
The html becomes:
<div class='ribbon-container'>
<div id='ribbon-left'></div>
<div id='ribbon-right'></div>
</div>
And the css becomes:
.ribbon-container {
width: 100%;
height: 120px;
position: relative;
background: url('http://i.imgur.com/LVXiQ37.jpg') top left repeat-x;
}
#ribbon-left {
position: absolute;
top: 0;
left: 0;
width: 112px;
height: 120px;
background: url('http://i.imgur.com/2MOcrO9.jpg') top left no-repeat;
}
#ribbon-right {
position: absolute;
top: 0;
right: 0;
width: 97px;
height: 120px;
background: url('http://i.imgur.com/Q6NmXR6.jpg') top left no-repeat;
}
I've done a really crude mock-up using the initial image you posted. The problem is that the image itself is not perfectly horizontal, so it looks like the right side does not line up in my fiddle, but if you are more careful in creating the initial image, this will work. And I haven't added text, but this can be done using absolute positioning as well, or another method I'm sure.
You can see the example at: http://jsfiddle.net/M3GmY/
I am trying to add the word "Invitations" to the top right of the page aligned with the "back to photos" link. However, when I add a new <div> then start a <p> the paragraph does not align to the page properly at all once I apply CSS.
I have uploaded the page where I am having trouble: http://ashliamabile.com/invitations.html
This worked for me:
HTML:
<div id="backtophotos">
<img src="images/backprint.png" border="0">
<p class="title">invitations</p>
</div>
CSS:
/*Drop the width property and set div to position:relative */
#backtophotos {
height: 20px; /* removed width */
background-repeat: no-repeat;
margin: 0 0 0 100px;
position: relative; /* set positon to relative */
}
/* Set title p to position absolute and remove margins: */
.title {
position: absolute;
right: 0;
top: 0;
margin: 0;
}
The above works because the div width is already "set" by the outer div, so you only need to worry about where the top right corner actually is if you change the layout. Otherwise, float-free right-aligned header.
Also, the only reason I explicitly set my margins to 0 for the .title is because p elements have their top and bottom margins set (and I think line-height). If you changed the p to a div (your choice, and a p has some value of being more explicitly meant for text), then your .title rule would just be:
.title {
position: absolute;
right: 0;
top: 0;
}
which is exactly what you are looking for without any additional tricks or tweaks (which is the name of my halloween rap album).
Personally, I would actually go with something more like:
<div id="backtophotos">
<h2>back to print<h2>
<h2 class="title">invitations</h2>
</div>
And deal with clearing all of the default browser css, as the above would be most semantic. I would also advise not using the image for your "back to print" text and explore one of the many CIR methods out there, as a screen reader won't be able to read the image aloud.
<div id="backtophotos">
<img src="images/backprint.png" border="0">
<p>invitations</p>
</div>
CSS
#backtophotos p{
float:right;
}
#backtophotos a{
float:left;
}
#backtophotos{ /*Clear float : any one of this method - micro clearfix, clearfix, overflow method, float, clear both */
overflow:hidden;
width:100%;
}
I have been trying to get this right for days but I just can't.
My scenario is this: I need three columns of equal height. There needs to be borders between them. The left column will have a bit more content than the other two and the other two need to have buttons at the bottom (that are positioned so that their bottom edge is where the left column's content ends).
Here is an image that shows what I mean: http://img217.imageshack.us/img217/6400/49593032.png
I have tried the huge-padding-bottom-and-equally-huge-but-negative-margin-bottom-hack which works great until I try to move the buttons down. At first I tried to use absolute positioning on the button and position:relative on the container but since the container needs overflow: hidden to work the button will be hidden and placed at the bottom of the container (which is about 32767 pixels down due to the huge padding).
I also tried using the above hack while adding a second row which I put the buttons in. Besides the fact that the semantics of that don't make much sense, this method made it so that the content of the left column doesn't go all the way down. Since the hack required overflow: hidden attempts to use negative margins to push the second row up didn't work out either.
So I'm stuck here. Faux columns wouldn't help me and javascript is not an option. What would you do?
Use A List Apart's Holy Grail and position the buttons absolutely.
Don't really like it in this case, but at least one solution would be to use a table. The text height in the first column would force the height for the other cells, and you could use relative positioning inside the cells (with a div) to have the buttons at the bottom.
[removed code --- not 100% sure about your exact requirements]
You can use absolute positioning for your divs and then absolute position the buttons in them. Try this code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
body { height: 100%; margin: 0; }
.col {
width: 33%;
position: absolute;
top: 0;
bottom: 0;
border: 1px solid #000;
background: blue;
position: absolute;
}
.left { left: 0; }
.mid { left: 33.33%; }
.right { left: 66.66%; }
.button { position: absolute; bottom: 0; right: 0; }
</style>
</head>
<body>
<div class="col left">
sdgfiods ajgodsai jngfio nmsadogf nikod sangf sfdsg fdsg
</div>
<div class="col mid">
sdgfiods ajgodsai jngfio nmsadogf nikod sangf sfdsg fdsg
<button class="button">click me</button>
</div>
<div class="col right">
sdgfiods ajgodsai jngfio nmsadogf nikod sangf sfdsg fdsg
<button class="button">click me</button>
</div>
</body>
</html>
I need to create a <div> of height 200px that has some text at the very top and the very bottom. This needs to work in all major browsers. I've tried various combinations of alignment/vertical-alignment with no luck.
Use two spans (or whatever) inside the div:
<div>
<span id="top">Text at top</span>
<span id="bottom">Text at bottom</span>
</div>
Then give the div position: relative; and position the spans absolutely:
div {
position: relative;
height: 200px;
}
span {
position: absolute;
}
span#top {
top: 0;
}
span#bottom {
bottom: 0;
}
Live example:
http://jsbin.com/ucowi3
You can't do this with one single block of text, as you're talking about two separate bits of styling (ie one bit to the top and one bit to the bottom), so you'll need to put the two bits of text into their own separate elements within the main <div>. eg
<div class='maindiv'>
<div class='topofmaindiv'>This goes at the top</div>
<div class='bottomofmaindiv'>This goes at the bottom</div>
</div>
Then you can style them using CSS to position the two inner divs at the top and bottom of the main div:
.maindiv {
height:200px;
}
.topofmaindiv {
position: relative;
top:0px;
}
.bottomofmaindiv {
position: relative;
bottom:0px;
}
Obviously you will probably need to add other styles to that to suit your layout, but that should get you started.