center text in zero width element - html

Lets say i have a code:
#container {
position: fixed;
top: 50%;
left: 50%;
width: 0;
height: 0;
text-align: center;
font-size: 300px;
}
<div id = "container">
text
</div>
I need the text to be centered. I need the text to be centered, even if the text is bigger than the window. It is necessary, that the text is position fixed, because the whole text sometimes goes BELOW the screen. I don't want it generating any scrollbars. It is very necessary in my situation. I need the container to be as small as possible, because huge scrolling divs generating lag, and i am doing parallax, with about 40 of them.
How do you center that text?

You could try with flexbox (display: flex)
#container {
display: flex;
position: fixed;
font-size: 300px;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
<div id="container">
text
</div>

If you use position:fixed, it should not generate scrollbars on html. No need to set size to 0, but transform translate can help:
possible example (out of your context, may be weird if you use transform3D around your page)
#container {
position: fixed;
top: 50%;
left: 50%;
font-size: 600px;/* even bigger for demo purpose*/
transform: translate(-50%, -50%);
}
/* show center */
html {
min-height: 100%;
background: linear-gradient(to left, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0.2) 50%), linear-gradient(to top, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0.2) 50%);
}
<div id="container">
text
</div>

Related

How to hide an element as page is scrolled?

I'm trying to hide an element as I'm scrolling up in the page. I'm using clip-path and position:fixed to try achieve this. This is what I have so far. The text is hidden as I'm scrolling as intended. Wondering if it would be possible to start clipping it from the top as I'm scrolling vs the bottom as it is working right now.
I want the element to be clipped from the top as I'm scrolling down.
One idea I had was to rotate the text 180 degrees, and then rotate the clipped element back 180 degrees but I'm not too sure if that would work/how to implement it. Since transform creates a containing block I don't know how to get the scrolling to work.
#main-container {
position: relative;
height: 100px;
width: 100vw;
}
#clip-container {
position: absolute;
height: 80px;
width: 100vw;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
overflow: hidden;
background-color: rgba(255, 165, 0, 0.5);
margin-top: 10px;
}
.filler-div {
background-color: blue;
width: 100vw;
height: 100px;
}
#text {
position: fixed;
}
<div class="filler-div"></div>
<div id="main-container">
<div id="clip-container">
<div id="text">
<h1>TEXT GOES HERE</h1>
</div>
</div>
</div>
<div class="filler-div"></div>
Learn any of the javascript libraries below
scrollrevealjs.org
scrollmagic.io

how to overlap a top <div> and a bottom <div> into an image without generating ugly/messy white spaces?

I have a top div, an image, and a bottom div in my html and I want to overlap the 2 divs into my image, the first div should cover some area in the top part of the image, and the bottom div should cover some area in the bottom part of the image, the div should be the one covering the image (on front of display). how would I do that in a clean way (without leaving white spaces) and make it responsive in respect to the width of the view or parent div?
here is my html
* {
padding: 0px;
margin: 0px;
}
#image {
max-width: 100%;
}
.rects {
height: 100px;
}
#rect-top {
background-image: linear-gradient(45deg, rgba(0, 194, 228, 0.75), rgba(214, 0, 203, 0.479));
}
#rect-bottom {
background-image: linear-gradient(45deg, rgba(214, 0, 203, 0.479), rgba(0, 194, 228, 0.75));
}
<h1>Start</h1>
<div id="rect-top" class="rects"></div>
<img id="image" src="image-with-gradient.jpg">
<div id="rect-bottom" class="rects"></div>
<h1>End</h1>
this is what I want to achieve:
as you can see there is no white space before and after the h1 tags
Wrap your image and overlapping divs in a div. Inside that, position your overlap div elements absolute.
As a sidenote, don't use id for styling purposes. Also note that any id in your page can only live on a single element; id must be unique per-document.
Instead of creating meaningless markup for the gradients, you can use the pseudo elements ::before and ::after here.
* {
padding: 0;
margin: 0;
}
.image-container {
padding: 50px 0;
position: relative;
}
.image {
max-width: 100%;
}
.image-container::before,
.image-container::after {
content: '';
height: 100px;
position: absolute;
width: 100%;
}
.image-container::before {
background-image: linear-gradient(45deg, rgba(0, 194, 228, 0.75), rgba(214, 0, 203, 0.479));
top: 0;
}
.image-container::after {
background-image: linear-gradient(45deg, rgba(214, 0, 203, 0.479), rgba(0, 194, 228, 0.75));
bottom: 0;
}
<h1>Start</h1>
<div class="image-container">
<img class="image" src="https://placekitten.com/g/1920/1080">
</div>
<h1>End</h1>
This also makes the use of z-index obsolete because elements with position: absolute are automatically on top of unpositioned elements.

Make horizontal line spanning to width of container using pseudo css elment

I want to make a a horizontal line spanning to the width of the container in which it is placed. The lined element should look like this:
//////////////////////////////////////////////////////////////////
Very much like a horizontal rule. I have tried it but 100% width is taken only when I put enough slashes in the content property of pseudo element. Here is my HTML code:
<div style='width: 100%;>
<p class='horizontal-line'></p>
</div>
Here is my CSS code:
.horizontal-line:before
{
content: '///////////////////////////////////////////////////////////////////////////////////';
margin: 0;
padding: 0;
color: purple;
width: 100%;
font-size: 10px;
}
The result is:
///////////////////////////////////////////
But it does not span to 100% width of outer div. In order to do so I have to put more slashes in content property. I know there is some alternate and better way to achieve this.
P.S: I am not very good at working with pseudo element and might be doing something wrong. Can anyone point out?
Edit: And if I place many slashes in content property then the horizontal line goes to two lines when placed in smaller container.
Here is fiddle link
I think you should try linear-gradient. Please find the below code.
.horizontal-line:before
{
content: '';
margin: 0;
padding: 0;
color: purple;
width: 100%;
height: 10px;
font-size: 10px;
display:block;
background: repeating-linear-gradient(135deg,purple,purple .25em,transparent 0,transparent .75em );
}
<div style='width: 100%;'>
<p class='horizontal-line'></p>
</div>
You can achieve this with css background property like this:
.horizontal-line {
margin: 0;
padding: 0;
width: 100%;
height: 10px;
background: purple linear-gradient(-45deg, rgba(255, 255, 255, 0.15) 25%, rgba(0, 0, 0, 0) 25%, rgba(0, 0, 0, 0) 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, 0)) repeat scroll 0 0 / 40px 40px
}
<div style='width: 100%;'>
<p class='horizontal-line'></p>
</div>
You have 3 options here
Use very long content and set it's parent overflow to hidden .
Use css linear-gradient to draw your stripes. Problem here is, the gradient may look poor (looks like aliasing issue). But here is a great explanation and suggestion how to overcome this.
Draw your pattern as image and use background repeat, you can draw your pattern online (i.e. http://www.patternify.com/ ) and use just base64 version of image
Here is are examples of all three options:
p { width: 80%; margin: 0px auto; margin-top: 30px; padding: 0; }
.container { width: 80%; border: 2px solid #888; margin: 10px auto; padding: 10px 0; }
.horizontal-line { width: 100%; height: 10px; }
.horizontal-line-v1 { overflow: hidden; }
.horizontal-line-v1:before
{
content: '//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////';
color: purple;
font-size: 20px;
}
.horizontal-line-v2 {
background-image: linear-gradient(-45deg, purple 25%, transparent 25%, transparent 50%, purple 50%, purple 75%, transparent 75%, transparent);
background-size: 4px 4px;
}
.horizontal-line-v3 {
background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAKElEQVQYV2NkQAMNDA3/GZHFQAINDA2McEGYAEgRWBBZACyILgASBACrXQ4FrzarHwAAAABJRU5ErkJggg==");
}
<p>Stripes using :before and content</p>
<div class="container">
<div class='horizontal-line horizontal-line-v1'></div>
</div>
<p>Stripes using css linear-gradient</p>
<div class="container">
<div class='horizontal-line horizontal-line-v2'></div>
</div>
<p>Stripes using base64 image</p>
<div class="container">
<div class='horizontal-line horizontal-line-v3'></div>
</div>
Here is jsFiddle
If it actually needs to be a ///-line, then you can trick it a bit using an image. ;)
.my-line
{
width: 100%;
height: 11px;
background: url("//i.imgur.com/OMxDsnu.png");
background-repeat: repeat-x;
}
<p>Before Line</p>
<div class="my-line"></div>
<p>After Line</p>

Background attachment fixed to the wrapper div

I have a such a situation: http://jsfiddle.net/5axmtw9g/3/
<div class="content inner clearfix1 has-left-sidebar">
<div class="sidebar-left-menu prepended"></div>
<div class="content-middle">
<section id="about-stat" class="clearfix1 about-stat-section">
<h1>Some title</h1>
</section>
</div>
</div>
.inner {
margin: 0 auto;
width: 600px;
}
.content.inner {
position: relative;
}
.content .sidebar-left-menu {
height: 100%;
left: 0;
overflow-x: hidden;
overflow-y: auto;
position: absolute;
top: 0;
float: left;
width: 160px;
}
.sidebar-left-menu {
background: #3b86c4 none repeat scroll 0 0;
color: #fff;
}
.content-middle {
margin: 0 0 10px 170px;
}
#about-stat {
background: rgba(0, 0, 0, 0) url("http://quotesnhumor.com/wp-content/uploads/2014/12/Funny-Minions-Pictures-and-Quotes.jpg") no-repeat fixed 0 0;
height: 590px;
overflow: hidden;
position: relative;
transform-style: preserve-3d;
}
As you can see in the fiddle the fixed background image is positioned to the window not to the wrapper. I would like the image to be positioned at the start of "content-middle" div, as expected. Using any solution with background-size:cover is not working for me, as I shall avoid of image stretching.
Would be really grateful for help as I stacked on this and can't find a working solution.
Thanks in advance!enter code here
Try jquery
posBg();
$(window).resize(function() {
posBg();
})
function posBg(){
var posLeft=$('#about-stat').offset().left;
var posTop=$('#about-stat').offset().top;
$('#about-stat').css("background-position", posLeft+"px "+posTop+"px");
}
Fiddle demo http://jsfiddle.net/5axmtw9g/9/
I think you can achieve what you want by adjusting your background to the following:
#about-stat {
background: rgba(0, 0, 0, 0) url("http://quotesnhumor.com/wp-content/uploads/2014/12/Funny-Minions-Pictures-and-Quotes.jpg") no-repeat center top ;
background-size: 100% auto;
The center-top will position the background to the right place. the size will display it at 100% width without adjusting the aspect-ratio. Now you just have to go for a bigger height div to show it full size (or a different background-image).
Demo
Background image X AXIS is 50% plus half the width of it's sibling container
https://codepen.io/AliKlein/pen/dVrmVO
section {
height: 100vh;
background-image:
url(https://source.unsplash.com/collection/194676/3);
background-size: cover;
background-attachment: fixed;
background-position-y: center;
background-position-x: calc(50% + 12.5vw);
}

How to create an invisible square inside a div with text? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to reproduce the image below.
The image in the center is my background. In my first div I've got some text. Over this div I need maybe an other div that hides a part of the div with the text so I can see the background of my page. Is there anyway I can have this invisible square in the middle of my div in CSS ?
The reason I need this behaviour is because I'm using the parallax scrolling effect.
Only in webkit, you can use a mask:
#background, #overlay {
position: absolute;
width: 200px;
height: 400px;
}
#background {
background: url("yourimage");
}
#overlay {
top: 0px;
left: 0px;
-webkit-mask-position: 0px 0px;
-webkit-mask-size: 100% 100%;
-webkit-mask-image: linear-gradient(180deg, rgba(0, 0, 0, 1) 33%, rgba(0, 0, 0, 0) 33%, rgba(0, 0, 0, 0) 66%, rgba(0, 0, 0, 1) 66%),
linear-gradient(90deg, rgba(0, 0, 0, 1) 33%, rgba(0, 0, 0, 0) 34%, rgba(0, 0, 0, 0) 66%, rgba(0, 0, 0, 1) 66%);
background-color: rgba(255, 255, 255, 0.8);
}
fiddle
I have set the background of the overlay with a little alpha to show that the background div is stiil there, just set it to white in real code.
position:absolute; left: something; top: something; z-index: 2;
I would suggest you use a transparent image for this. This will create a 'window' to expose your background. I will also enable your text to wrap around it as apposed to cover it.
Like this:
#text-container {
width: 500px;
height: 500px;
position: relative;
z-index: 0;
overflow: hidden;
}
#image-container {
width: 200px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -50px;
background-color: #aaa;
z-index: 5;
}
And the HTML:
<div id="text-container">
<p>Background text here</p>
<div id="image-container">
<img src="picture.jpg" />
</div>
</div>
The reason for specifying the top and left at 50% is so that the image box will be 50% from the top and left of the parent div. This applies to the top and left edges of the image box though, so it ends up not being quite centered. Setting the margins to - 1/2 the width and hight offsets them so that the box is properly centered.
Here is a link to a working fiddle
I think the best you can do in pure CSS is to place an image on top of your text which exactly matches your background image. It would not be a true hole, just an illusion.