I want to put watermark-text at the center of page. but it's not work it always go to the left of page. I try to use top and left with the #background element but the font-size of #watermark-text get smaller how can I put #watermark-text in the center without change the font-size.
#background {
position: absolute;
background: white;
z-index: 0;
}
#content {
z-index: 1;
}
#watermark-text {
position: absolute;
color: #eae9e9;
font-weight: bold;
font-size: 800px;
}
<div id="background">
<p id="watermark-text">WaterMark</p>
</div>
<div id="content" </div>
I've used display: flex; align-items: center; justify-content: center; on the parent to center the child horizontally and vertically and in order to achieve that we need to set a height and a width to the parent.
#background {
height: 100vh;
width: 100%;
background: white;
display: flex;
align-items: center;
justify-content: center;
}
#content {
z-index: 1;
}
#watermark-text {
color: #eae9e9;
font-weight: bold;
font-size: 800px;
}
<div id="background">
<p id="watermark-text">WaterMark</p>
</div>
<div id="content"> </div>
If by watermark you mean text that overlays the screen with text then you can do it very simply by setting the body to position: relative. This means that when we set the background div with position: absolute and inset:0, the watermark is positioned relative to the body element. This makes the background div cover the whole page.
Use grid and place-items center to put the text in the center of the screen. I've coloured the background and set opacity on the text so you can see that it's overlaid the content.
Note: I've set the font size a percentage of the viewport width using the vw unit so as you make the screen bigger, the watermark increases in size to suit. You can set this to a pixel value or, even better, rem or em.
If you want the watermark not to move with the screen scroll, change position: absolute to position: fixed.
Any questions, just pop a comment in and I'll respond.
body {
position: relative;
margin: 0;
}
#background {
position: absolute;
inset: 0;
display: grid;
place-items: center;
color: #eae9e9;
background-color: rgba(0, 192, 0, 0.5);
opacity: 0.5;
font-weight: bold;
font-size: 15vw;
}
<div id="background">WaterMark</div>
<div id="content">
<img src='https://picsum.photos/id/237/400/900'>
</div>
#background {
position: fixed;
background: white;
width: 100vw;
height: 100vh;
top: 0px;
left: 0px;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
z-index: -10;
}
#content {
z-index: 10;
width: 100vw;
height: 100vh;
color: black;
}
#watermark-text {
color: #eae9e9;
font-weight: bold;
font-size: 800px;
}
<div id="background">
<p id="watermark-text">WaterMark</p>
</div>
<div id="content">jlsgdfjlgdsfjodfgjoifdgjasfddddddddddds<br>joiasjoidsajoasfds </div>
This is how I would do it looks strange in the editor but should work perfectly on the page, alternative you can just set a background to the div itself where to content is, be aware that this won't be secure as anyone can just change the HTML and CSS clientside anyway.
.watermark {
/* Used to position the watermark */
position: relative;
}
.watermark__inner {
/* Center the content */
align-items: center;
display: flex;
justify-content: center;
/* Absolute position */
left: 0px;
position: absolute;
top: 0px;
/* Take full size */
height: 100%;
width: 100%;
}
.watermark__body {
/* Text color */
color: rgba(0, 0, 0, 0.2);
/* Text styles */
font-size: 3rem;
font-weight: bold;
text-transform: uppercase;
/* Rotate the text */
transform: rotate(-45deg);
/* Disable the selection */
user-select: none;
}
Related
I have been trying to get the header image to be shorter, however, I cannot figure out how to. Here is the HTML:
<div class="header">
<img src="images/header_sea(3).jpg" width="99%" class="header_image" alt="sea_sky">
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</div>
Here is the CSS:
html, body {
height: 100%;
width: 99%;
text-align: center;
}
.title{
font-family: 'Poppins', sans-serif;
font-size: 45px;
font-weight: bold;
color: #FB79E1;
text-align: center;
text-shadow: 3px 3px white;
}
.header{
position: relative;
}
.header_image{
opacity: 0.55;
height: 40%;
width: 99%;
}
.header_title{
position:absolute;
top: 0;
left: 0;
height: 100%;
width: 99%;
display: flex;
justify-content: center;
align-items: center;
}
I tried adjusting the height percentage in .header_image, but the image doesn't get shorter when I change the value.
The header needs a size associated with it. Otherwise the image has nothing to be "40%" of since the header is just using auto sizing.
Relevant code
.header {
position: relative;
/* Give the header (containing element) a size, can be %, px, etc.
Also keep in mind to use a percentage as a size the body needs a percentage size as well */
height: 20%;
}
Another good practice is to use semantic elements when possible, so consider using <header> instead of a div with a class of header.
html,
body {
height: 100%;
width: 99%;
text-align: center;
}
.title {
font-family: 'Poppins', sans-serif;
font-size: 45px;
font-weight: bold;
color: #FB79E1;
text-align: center;
text-shadow: 3px 3px white;
}
.header {
position: relative;
/* Give the header (containing element) a size, can be %, px, etc.
Also keep in mind to use a percentage as a size the body needs a percentage size as well */
height: 20%;
}
.header_image {
opacity: 0.55;
height: 100%;
width: 99%;
}
.header_title {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 99%;
display: flex;
justify-content: center;
align-items: center;
}
<header class="header">
<img src="https://98e50e5e-ef9b-4f10-9bb4-65acdcdf4429.id.repl.co/images/header_sea(3).jpg" class="header_image" alt="sea_sky">
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</header>
try removing the width attribute from the image (inline) and change the width in .header_image
I did it for you below
<div class="header">
<img src="https://98e50e5e-ef9b-4f10-9bb4-65acdcdf4429.id.repl.co/images/header_sea(3).jpg"
class="header_image"
alt="sea_sky"
/>
<div class="header_title title"> *.• ʚ welcome to my ocean! ɞ •.* </div>
</div>
.header_image{
opacity: 0.55;
height: 40%;
width: 70%;
}
what i'm trying to make it look like
I'm trying to put a div with a background over an img, I've looked over a lot of resources and when I make a background color the background color covers the whole image.
<div class="containerBox">
<div class="text-box">
<h4> Handpicked, Artisanally Curated, Free Range, Sustainable, Small Batch, Fair Trade, Oragnic Tea</h4>
</div>
<img class="img-responsive" src="https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg">
.containerBox {
position: relative;
display: inline-block;
}
.text-box {
position: absolute;
height: 100%;
text-align: center;
width: 100%;
}
.text-box:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
h4 {
display: inline-block;
font-size: 20px; /*or whatever you want*/
}
img {
display: block;
}
There are a lot of ways to do that. Depends on what you specifically need.
If you can let go img tag and use background image (which is in general considered better), i recommend on using the following code snippet. (This may have extra lines of code but it solves the purpose, please delete these extra lines.)
<div class="containerBox">
<div class="text-box">
<h4> Handpicked, Artisanally Curated, Free Range, Sustainable, Small Batch, Fair Trade, Oragnic Tea</h4>
</div>
</div>
<style>
.text-box {
display: grid;
/* Or Flex */
place-items: center;
/* Or For Flex use align-items and justify-content */
position: absolute;
height: 100%;
width: 100%;
text-align: center;
}
.text-box::before {
content: '';
background: url('https://content.codecademy.com/courses/freelance-1/unit-4/img-mission-background.jpg') no-repeat center center/cover;
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
z-index: -1;
opacity: 1;
/* Opacity: as you like */
}
h4 {
display: grid;
place-items: center;
height: 20%;
width: 1000px;
color: white;
background-color: rgba(0, 0, 0, 0.541);
/* Or whatever you want */
}
</style>
If you want img tag:
.containerBox {
position: relative;
display: inline-block;
}
.text-box {
position: absolute;
height: 100%;
width: 100%;
text-align: center;
z-index: 1;
}
h4 {
display: grid;
place-items: center;
height: 30%;
width: 80%;
font-size: 20px;
background-color: rgba(0, 0, 0, 0.555);
/*or whatever you want*/
}
img {
display: block;
}
If by background color you meant background color of text-box, then you need to reduce its heigth and width else use ::before pseudo selector and set its height and width.
I want to make my text overlap over this image in relative position, while also staying in the 100vmin boundaries. Here is my HTML code. I know the image won't show on Stack Overflow, but the image is an SVG file. The SVG is an exact sqaure, being 332 in width and height in the SVG file(as a txt file).
<!DOCTYPE html>
<!-- Created and Copyright of MrEthanVlogsandGames -->
<html lang="en">
<head>
<title>Test</title>
<link rel="icon" href="/images/tab_icon.svg">
<style>
/******
FONTS
******/
#import url('https://fonts.googleapis.com/css?family=Roboto');
/******
EVERYTHING ELSE
******/
body {
font-family: Roboto;
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
}
ul {
display: inline-block;
}
.app {
width: 100vmin;
height: 100vmin;
}
.starting_page > img {
display: block;
width: 100%;
z-index: 1;
}
p.text {
display: block;
position: relative;
text-align: center;
font-weight: bold;
color: #fff;
text-align: center;
z-index: 2;
}
</style>
</head>
<body>
<div class="app">
<div class="starting_page">
<img src="/images/starting_page.svg">
<p class="text">Overlapping Text</p>
</div>
</div>
</body>
</html>
The position: relative needs to be on parent element, then you use position: absolute on the element you want to overlap and position them within the relative parent using top left etc.
Technically you can move an element around with position: relative and top left etc but the problem is that it'll still take up space in it's original position. Above method doesn't have this problem.
body {
font-family: Roboto;
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
}
.app {
position: relative;
width: 100vmin;
height: 100vmin;
}
.starting_page>img {
display: block;
width: 100%;
z-index: 1;
}
p.text {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%);
text-align: center;
font-weight: bold;
color: #fff;
z-index: 2;
}
<div class="app">
<div class="starting_page">
<img src="https://picsum.photos/332">
<p class="text">Overlapping Text</p>
</div>
</div>
I have two two containers, a navigation bar and the hero below it. I only set background-image for the hero and I have no background-image set for the navigation bar. However, the background for the navigation bar seems to be set automatically the same as the hero.
Part of my css looks like below:
.Nav {
height: 55px;
display: flex;
justify-content: space-between;
padding: 1rem 2rem;
z-index: 100;
position: fixed;
width: 100%;
opacity: 0.8;
}
.HeroWrapper{
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
position: relative;
background-image: url("images/pencil.jpg");
background-size: 100px;
}
and my html is like:
<div class = "Nav"></div>
<div class = "HeroWrapper"></div>
This is happening because of the position values of both of your divs (fixed and relative).
It looks like .Nav has a transparent background by default. If you do not want .Nav to have this background you could simply specify another such as background: white;
.Nav {
height: 55px;
display: flex;
justify-content: space-between;
padding: 1rem 2rem;
z-index: 100;
position: fixed;
width: 100%;
opacity: 0.8;
background: white
}
I want to create a very simple image viewer. When you click a thumbnail, a large version of that thumbnail should open on a black background with 0.8 opacity. That image...
may not exceed 80% of the window height.
may not exceed 80% of the window width.
should be centerd horizontally and vertically.
should always keeps it aspect ratio.
may not be a background image.
should not be changed with javascript.
My biggest problem is that the height of the image inside the div exceed that 80% window height (80wh) when you check it on mobile devices (see Chrome's device toolbar). The strangest thing is that my div is working fine: it's maximum height is 80vh... Here's my code, perhaps somebody sees the mistake I'm making:
#gallery {
overflow: hidden;
}
.Gallery {
align-items: center;
background: rgba(0, 0, 0, 0.85);
display: flex;
justify-content: center;
height: 100%;
position: fixed;
width: 100%;
z-index: 10;
}
.Gallery-imageWrapper {
max-height: 80%;
max-width: 80%;
position: relative;
}
.Gallery-close {
background: rgb(21, 21, 21);
color: rgb(255, 255, 255);
display: block;
font-weight: 700;
height: 20px;
line-height: 20px;
position: absolute;
right: 0;
text-align: center;
text-decoration: none;
top: 0;
width: 20px;
}
.Gallery-image {
max-height: 100%;
max-width: 100%;
}
<div class="Gallery">
<div class="Gallery-imageWrapper">
<a class="Gallery-close" href="#" title="Sluiten">x</a>
<img class="Gallery-image" src="https://placehold.it/600x600" alt="Screenshot website Optiek Cardoen" title="Website Optiek Cardoen">
</div>
</div>
I tried other CSS properties like object fit, width, height, position, ... on the image element, but nothing seems to fix it. Anyone who can help me please? :)
Thx!
If you can use viewport units, try with vmin - equal to the smaller of vw and vh.
Key style:
.Gallery-image {
max-width: 80vmin;
max-height: 80vmin;
}
Full demo:
.Gallery {
background: rgba(0, 0, 0, 0.85);
display: flex;
justify-content: center;
align-items: center;
position: fixed;
left: 0;
top: 0;
height: 100%;
width: 100%;
z-index: 10;
}
.Gallery-imageWrapper {
position: relative;
}
.Gallery-close {
background: rgb(21, 21, 21);
color: rgb(255, 255, 255);
position: absolute;
font-weight: 700;
right: 0;
top: 0;
width: 20px;
height: 20px;
line-height: 20px;
text-align: center;
text-decoration: none;
}
.Gallery-image {
max-width: 80vmin;
max-height: 80vmin;
}
<div class="Gallery">
<div class="Gallery-imageWrapper">
<a class="Gallery-close" href="#" title="Sluiten">x</a>
<img class="Gallery-image" src="https://placehold.it/600x600" alt="Screenshot website Optiek Cardoen" title="Website Optiek Cardoen">
</div>
</div>
In addition:
CSS selectors are generally case-insensitive; this includes class and ID selectors. Make sure you keep it consistent.