How to stack HTML elements on top of each other - html

I would like to make the background an image. All my other HTML elements will go on top of it. The problem is that it won't stack. Every time I try, the text just goes to the bottom. Also, I want to use vanilla HTML and CSS and do not want to use canvas. This is my code.
.navBar {
display: flex;
position: sticky;
top: 0;
padding: 20px;
border: 2px solid gainsboro;
width: 100%;
justify-content: center;
background-color: gainsboro;
z-index: 2;
}
#Title {
color: black;
font-family: monospace;
}
.navBar:hover {
border: 2px solid black;
}
h3 {
z-index: 2;
}
<div class="navBar" onclick="toHomePage()">
<div>
<h1 id="Title">A Random Website</h1>
</div>
</div>
<img src="..." width="100%" height="100%">
<h3>Blablabla</h3>
<br>
<h3>01234567890</h3>

I don't know if there is a reason as to why the image has to be in the html, but why not use it in the css directly?
background: gainsboro url(...) left / cover no-repeat;

By doing some code change this will works.
HTML
<div class="main">
<img src="https://rajeshdoot.com/niwax-demos/html/images/about/about-dg-agency.jpg" width="100%" height="100%">
<div class="title">
<h3>Blablabla</h3>
<br>
<h3>01234567890</h3>
</div>
css
.main {
position: relative;
}
.main .title {
z-index: 2;
position: absolute;
top: 50%;
}

Like the previous contributor suggested, use the css background-image property instead of embedding an image.
See this jsfiddle here: https://jsfiddle.net/6zo9h08m/
I used your example code, removed the img element and added a css selector for the body that sets a background-image instead.
.navBar{
display: flex;
position: sticky;
top:0;
padding: 20px;
border: 2px solid gainsboro;
width: 100%;
justify-content: center;
background-color: gainsboro;
z-index: 2;
}
#Title{
color: black;
font-family: monospace;
}
.navBar:hover{
border: 2px solid black;
}
h3{
z-index: 2;
}
body{
background-image: url('https://via.placeholder.com/150/FF0000/FFFFFF')
}

You can change background image position to 'fixed' and z-index can be less than navbar.
style ="position:fixed; z-index:1; top:0px; left:0px;"
Instead of inline, you can also give css cass to image tag and css above.
.navBar {
display: flex;
position: sticky;
top: 0;
padding: 20px;
border: 2px solid gainsboro;
width: 100%;
justify-content: center;
background-color: gainsboro;
z-index: 2;
}
#Title {
color: black;
font-family: monospace;
}
.navBar:hover {
border: 2px solid black;
}
h3 {
z-index: 2;
}
<div class="navBar" onclick="toHomePage()">
<div>
<h1 id="Title">A Random Website</h1>
</div>
</div>
<img src="..." style ="position:fixed; z-index:1; top:0px; left:0px;" width="100%" height="100%">
<h3>Blablabla</h3>
<br>
<h3>01234567890</h3>

Related

How can I add a footer at the end of this website?

I am trying to create a footer at the end of this website but for some reason it appears above the products :
And when I change the browser size :
But I want a footer like this :
Here is my code :
HTML :
{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="{% static 'main.css' %}">
</head>
<body style="background-color: #36454F;">
{% for i in p%}
<div class='card'>
<div class="number">{{i.Number}}</div>
<img src="{{i.image}}"></img>
<p id="id">{{i.description}}</p>
<a href="{{i.buy}}" target='_blank' rel='noopener noreferrer'>
<button><span class="price"> ${{i.price}}</span> buy</button>
</a>
</div>
{%endfor%}
<div class="footer">
<h3>hello</h3>
</div>
</body>
</html>
CSS :
.card {
max-width: 400px;
margin: 0auto;
text-align: center;
font-family: arial;
border-style: solid;
border-width: 6px;
position: relative;
top: 611px;
margin-bottom: 33px;
margin-right: 33px;
justify-content: center;
float: left;
}
.footer {
position: relative;
height: 130px;
clear: both;
background-color: red;
}
.card img {
height: 400px;
width: 400px;
vertical-align: middle;
}
.price {
background-color: #f44336;
font-size:22px;
border-radius: 3px;
position: absolute;
bottom: 0px;
right: 0px;
padding: 3px;
}
.card button {
border: none;
color: white;
background-color: #000;
position: relative ;
cursor: pointer;
width: 100%;
height: 100px;
font-size: 44px;
align-items: center;
}
.card button:hover {
opacity: .5;
background-color: #330;
}
#id {
background-color: palevioletred;
color: white;
margin: 0;
font-size: 17px;
}
.number {
width: 50px;
height: 50px;
background-color: #330;
color: yellow;
border-radius: 50%;
position: absolute;
top: -22px;
right: -22px;
justify-content: center;
align-items: center;
display: flex;
font-size: 22px;
}
#media (max-width: 1864px) {
.card {
max-width: 300px;
}
.price {
font-size:20px;
}
.card img {
height: 300px;
width: 300px;
}
}
I tried to set a negative bottom property to push it to the end :
.footer {
position: relative;
bottom: -674px;
height: 130px;
clear: both;
background-color: red;
}
But it didn't help. How can i solve the problem?
To set the footer to the bottom of the page, you need to use this CSS:
.footer {
position:absolute;
bottom:0;
width:100%;
height:60px; /* Height of the footer */
background:#6cf; /* Set your own background */
}
If you want it to stay at the bottom of the page and stretch along the bottom, I'd do something like this with the CSS
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: (whatever you want);
color: (color of the text, whatever you want);
text-align: center; /*unless you want the text aligned differently*/
}
Also look up how to use the grid-container if you want the items of the footer in rows like the example you gave.
You need the footer tag to do its job!
Read more about the footer tag here:
https://www.geeksforgeeks.org/html5-footer-tag/
Reference: https://code-boxx.com/keep-html-footers-at-bottom/
The easy ways to keep a footer at the bottom with modern CSS are:
Use footer { position: fixed} or footer { position: sticky } to
keep the at the bottom.
Use a flexbox layout that "stretches" the body section, keep the
footer at the bottom.
body{ display: flex; flex-direction: column; }
main{ flex-grow: 1; }
Lastly, use a grid layout to achieve the same "stretch body
section".
<header>HEAD</header> <main>MAIN</main> <footer>FOOT</footer>
html, body { height: 100%;}
body { disply: grid; grid-template-rows: auto 1fr auto; }

How to overlay text centre-bottom on an already centred image?

I've played around quite a bit with margins, positions, etc. but can't manage to centre the text on my image where I want it to without roughly manually inputting the position, i.e. left: 10px;. It is probably simple but I cant figure it out as a learner
.container {
position: relative;
}
.text-block-main {
position: absolute;
bottom: 5%;
right: 44%;
background-color: black;
opacity: 75%;
color: white;
padding-left: 20px;
padding-right: 20px;
border-radius: 15px;
border: 2px solid white;
padding: 10px;
}
<div class="container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1200px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg" alt="vvg" style="height:100%;" class="center">
<div class="text-block-main">
<h2> The Starry Night </h2>
<h3> Vincent van Gogh </h3>
</div>
</div>
Its kind of centred (manually) but not really and depends on the image im using itself, so i'd have to adjust it for every image i want to use. Appreciate some help with this
Solution with flex. Specify display: inline-flex on the container class to take the form of an img tag image.
And also remove bottom and right from the text-block-main class.
I marked all the edits in css.
.container {
position: relative;
display: inline-flex; /*add this it*/
align-items: center; /*add this it*/
justify-content: center; /*add this it*/
}
.text-block-main {
position: absolute;
/*bottom: 5%;*/ /*remove this it*/
/*right: 44%;*/ /*remove this it*/
background-color: black;
opacity: 75%;
color: white;
padding-left: 20px;
padding-right: 20px;
border-radius: 15px;
border: 2px solid white;
padding: 10px;
}
<div class="container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1200px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg" alt="vvg" style="height:100%;" class="center">
<div class="text-block-main">
<h2> The Starry Night </h2>
<h3> Vincent van Gogh </h3>
</div>
</div>
You can use flexbox for this functionality. The text-block-main is getting the full cover of the image and is centering the inner div in the center with Flexbox.
.container {
position: relative;
}
.container img {
height: 100%;
width: 100%;
}
.text-block-main {
position: absolute;
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
top: 0;
left: 0;
right: 0;
bottom: 0
}
.text-block-main .inner {
background-color: black;
opacity: 75%;
color: white;
padding: 25px;;
border-radius: 15px;
border: 2px solid white;
text-align: center;
}
<div class="container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1200px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg" alt="vvg" style="height:100%;" class="center">
<div class="text-block-main">
<div class="inner">
<h2> The Starry Night </h2>
<h3> Vincent van Gogh </h3>
</div>
</div>
</div>
You can try to do something like this with the flex attribute
.container {
position: relative;
height:200px;
display: flex;
justify-content: center;
align-items: center;
}
.container img {
width:100%;
position:absolute;
}
.text-block-main {
background-color: black;
opacity: 75%;
color: white;
padding-left: 20px;
padding-right: 20px;
border-radius: 15px;
border: 2px solid white;
padding: 10px;
}
<div class="container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1200px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg" alt="vvg" style="height:100%;" class="center">
<div class="text-block-main">
<h2> The Starry Night </h2>
<h3> Vincent van Gogh </h3>
</div>
</div>
( Here below i add other solutions by changing only some piece of codes, what you don't see remains as it was )
solution with the image without having the container full width
.container img {
/*width:100%;*/
position:absolute;
}
solution without adding style to the image
/*
.container img {
width:100%;
position:absolute;
}
*/
.text-block-main {
position:absolute; /* add this with the other styles */
}

How to set header background to fit appropriately

Need help setting the header size so that accommodate the image and text.
I've tried changing the background size using the "background-size" property but it doesn't change.
header {
background-color: red;
background-size: 200px 100px;
}
header h1 {
font-size: 50px;
text-align: center;
text-decoration-line: underline overline;
text-decoration-style: double;
position: relative;
margin-top: -100px;
margin-bottom: 100px;
}
header .pic {
border: 5px #000 outset;
border-radius: 50px;
}
<header>
<img class="pic" src="" alt="" width="200px">
<h1>Placeholder Text</h1>
</header>
The header background appears until the middle of the text and image when I want it to be on the bottom of the text and image (whichever is lower).
try this :
header {
background: url("img/index.jpg") ;
background-color: red;
background-size: 200px 100px;
}
header h1 {
font-size: 50px;
text-align: center;
text-decoration-line: underline overline;
text-decoration-style: double;
position: relative;
margin-top: -100px;
margin-bottom: 100px;
}
header .pic {
border: 5px #000000 outset;
border-radius: 50px;
}
<header>
<h1>Placeholder Text</h1>
</header>
When you want a background image in a container you do something like this:
header {
background-image: url('https://picsum.photos/800/400');
background-size: cover;
background-repeat: no-repeat;
height:100vh;
width: 100vw;
}
h1 {
color: black;
font-size: 50px;
text-align: center;
}
Instead of using a normal image tag, you declare you background image in the CSS.
See a working example here: https://codepen.io/Angel-SG/pen/dwOEvy
I think you might be looking for the display: flex
Try this
/* Reset
*******************/
* { margin: 0; padding: 0; box-sizing: border-box; }
img { max-width: 100%; }
/* Styles
*******************/
header { background-color: red; display: flex; align-items: center; justify-content: space-between; padding: 1rem; }
header .pic { width: 200px; }
header h1 {
font-size: 50px;
text-align: center;
text-decoration-line: underline overline;
text-decoration-style: double;
position: relative;
}
<header>
<img class="pic" src="https://sg.fiverrcdn.com/photos/105746482/original/3e2a4ad867ed23117cfda223391c35ab42bb99fc.png">
<h1>Placeholder Text</h1>
</header>

Creating a title overlaying a line (CSS)

For my web app's landing page, I'm trying to create a title that appears overlaid on a dotted line (similar to this effect). This is what I currently have:
How do I create this such that the dotted line does not run through the title? I prefer to use the simplest CSS/HTML I possibly can and support the max number of browsers.
My code is pretty rudimentary. So far it is:
<h2>New Account:</h2><br>
<h2 style="margin-top:-0.5em;border:2px dashed #ffffff;border-radius:4px;color:white;display: inline-block;padding:10px 5px 5px 5px;">Choose Nickname:<br>Password:<br></h2>
With the example below you don't need to know the background color, is perfectly scalable, the dots extend to the remaining space of the title.
Actually, the title can wrap on multiple lines.
Feel free to tweak it to your needs and don't forget to prefix.
dotted-container {
border: 2px dotted red;
border-top-width: 0;
margin: 2rem 1rem;
display: block;
}
dotted-container>.content {
padding: 1rem;
}
dotted-title {
display: flex;
align-items: center;
height: 2px;
margin: 0 2px;
}
dotted-title > span {
padding: 0 1rem;
}
dotted-title:after,
dotted-title:before {
border-top: 2px dotted red;
content: '';
display: inline-block;
height: 0;
flex:1;
}
<dotted-container>
<dotted-title>
<span>title</span>
</dotted-title>
<div class="content">
Actual content
</div>
</dotted-container>
<dotted-container>
<dotted-title>
<span>some other title</span>
</dotted-title>
<div class="content">
Some other actual content
</div>
</dotted-container>
<dotted-container>
<dotted-title>
<span>and here's a title<br /> on two lines</span>
</dotted-title>
<div class="content">
Some content for a title on two lines.
</div>
</dotted-container>
Of course, you might want to adjust the margin/padding to your own liking and to accommodate any title wrapping on more than one line.
If you want to replace the "crappy" dotted line with a true dotted one, here's an example. Read the blog post to understand it.
Another good write-up on border-image property here.
Also note you don't have to use custom tags, as I did. It's an example. You may use classes or any other selectors that work for your specific case.
And here's an SCSS script I made you can use to pass in your selectors and desired margin/padding values. Far from perfect, but seems to do the trick:
$border-width: 2px;
$border-style: dotted;
$border-color: red;
$container: 'dotted-container';
$title: 'dotted-title';
$content:'.content';
$padding: 2rem;
$margin: 1rem;
$title-padding-value: 3;
$title-padding-unit:rem;
#{$container} {
border: $border-width $border-style $border-color;
border-top-width: 0;
margin: #{$title-padding-value/2}#{$title-padding-unit} $margin $margin $margin;
display: block;
> #{$content} {
padding: #{$title-padding-value/2}#{$title-padding-unit} $padding $padding $padding;
}
#{$title} {
display: flex;
align-items: center;
height: $border-width;
margin: 0 $border-width;
> span {
padding: 0 $padding;
}
&:after,
&:before {
border-top: $border-width $border-style $border-color;
content: '';
display: inline-block;
height: 0;
flex: 1;
}
}
}
Here's a solution with a combination of pseudo elements, flexbox, and absolute positioning.
* {
margin:0;padding:0;
box-sizing:border-box;
}
body {
background: url('https://s-media-cache-ak0.pinimg.com/originals/33/3b/4f/333b4f22ae39d1aaf8c23d77e759d8e1.jpg') 0 0 no-repeat / cover;
}
h2:before,h2:after {
content: '';
bottom: 50%;
border-top: 3px dotted black;
flex: 1 0 0;
}
h2:before {
margin-right: 1em;
}
h2:after {
margin-left: 1em;
}
h2 {
display: flex;
align-items: center;
font-size: 3em;
position: absolute;
top: 0; left: 0; right: 0;
transform: translateY(calc(-50% + 1px));
text-shadow: 0 3px 0 #fff;
}
section {
border: dotted black;
border-width: 0 3px 3px;
position: relative;
width: 80%;
margin: 3em auto;
padding-top: 3em;
background: rgba(255,255,255,0.5);
}
<section>
<h2>New Account:</h2>
<p>foo</p>
<p>foo</p>
<p>foo</p>
</section>
You can use a combination of z-index and background-color, as shown in the snippet below:
z-index pulls the New Account: title in front, then the background-color hides the border behind the it
body {
background: green;
}
#one {
position: absolute;
left: 35px;
z-index: 1;
background: green;
display: inline-block;
color: white;
}
#two {
position: absolute;
z-index: -1;
top: 55px;
margin-top: -0.5em;
border: 2px dashed white;
border-radius: 4px;
color: white;
display: inline-block;
padding: 10px 5px 5px 5px;
}
<h2 id="one">New Account:</h2><br>
<h2 id="two">Choose Nickname:<br>Password:<br></h2>
Perhaps you can do something like this:
* {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.box {
background: green;
padding: 15px;
width: 250px;
height: 250px;
}
.inner-box {
border: 1px dotted #ffffff;
height: 100%;
position: relative;
-webkit-border-radius: 7px;
border-radius: 7px;
}
.inner-box p {
position: absolute;
width:70%;
text-align: center;
top: -25px;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
color: #ffffff;
display: block;
background: green; /* Make it the same as background color */
}
<div class="box">
<div class="inner-box">
<p>My Awesome Title</p>
</div>
</div>
I would suggest having a background colour on the "New Account" if the background is only one colour, that way the dotted line will not be seen as it is covered by the background colour.
The code snippet shows how this can be adjusted to show more or less of the dotted border either side of the title.
.parent{
background-color: green;
position: relative;
font-size: 14px;
}
.parent h2:first-child{
color: #fff;
text-align: center;
z-index: 1;
background-color: green;
position: absolute;
left: 20px;
padding: 0 5px;
}
.parent h2:last-child{
margin-top: 15px;
z-index: 0;
}
.parent_two h2:first-child{
left: 12px;
padding: 0 17px;
}
<div class='parent'>
<h2>New Account:</h2><br>
<h2 style="border:2px dashed #ffffff;border-radius:4px;color:white;display: inline-block;padding:10px 5px 5px 5px;">Choose Nickname:<br>Password:<br></h2>
</div>
<div class='parent_two parent'>
<h2>New Account:</h2><br>
<h2 style="border:2px dashed #ffffff;border-radius:4px;color:white;display: inline-block;padding:10px 5px 5px 5px;">Choose Nickname:<br>Password:<br></h2>
</div>
I would move the title up with absolute positioning (just make sure the parent is relative positioned), wrap the title text in a <span> and then add padding and matching background color to that <span>.
body {
margin: 0;
background-color: green;
}
.box {
position: relative;
margin: 1rem;
padding: 1rem;
color: white;
box-sizing: border-box;
border: 1px dashed white;
}
.box-title {
position: absolute;
top: -1rem;
left: 0;
margin: 0;
width: 100%;
text-align: center;
font-size: 1.5rem;
}
.box-title>span {
padding: 0 1rem;
background-color: green;
}
<div class="box">
<h2 class="box-title"><span>New Account:</span></h2>
<p>Username:</p>
<p>Password:</p>
</div>
FWIW, I don't typically care for extra markup but if I have to work extra hard to make it work some other way then I find it acceptable. Especially when it's super simple.
body{
background: url('https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=750&h=350') center top 0 no-repeat / cover;
}
.wrapper {
max-width: 400px;
margin: 30px auto 0;
border: 2px dotted red;
height: 200px;
border-top: 0;
text-align: center;
}
.heading {
display: table;
width: 100%;
position: relative;
}
.heading:before {
content: '';
display: table-cell;
border-top: 2px dotted red;
}
.heading:after {
content: '';
display: table-cell;
border-top: 2px dotted red;
}
.txt-wrapper {
display: table-cell;
width: 1%;
white-space: nowrap;
line-height: 0;
padding: 0 10px;
}
<div class="wrapper">
<div class="heading">
<h2 class="txt-wrapper">
This is heading
</h2>
</div>
<P>
This is paragraph.
</P>
<P>
This is another paragraph.
</P>
</div>

Using opacity to blur a Div's background colour instead of mentioning rgba colour of a Div

I have an Image on the page. I have put a Div on footer with a heading and a paragraph inside the Div. I have made the Div's background transparent like this way , background-color: rgba(0,0,0,.6);.
But I need to implement this like this way , background-color:black; opacity:0.6".
The problem is, if I am doing it using opacity then the heading and paragraph is also getting blur with the Div's colour. How can I solve it?
Below is my full code.
CSS
<style type="text/css">
.div1 {
max-width: 100%;
height: auto;
text-align: center;
display: block;
border: none;
}
.feature-text-overlay {
background-color: rgba(0,0,0,.3);
bottom: 0;
padding: .6em 0 .8em;
position: absolute;
text-align: center;
width: 100%;
}
.feature-title {
font-size: .875em;
margin: 0;
text-transform: uppercase;
color:white;
}
.feature-description {
line-height: 1.2;
margin: 0 2em;
color:white;
}
</style>
Html
<div class="div1">
<img src="~/Content/Image/rendering-graphics-in-resolution-222734.jpg" />
<div class="feature-text-overlay" style="height:52.599999277954px; min-height:1px;">
<h4 class="feature-title">Enterprise Mobility Suite</h4>
<p class="feature-description">Manages Users, Devices and Data</p>
</div>
</div>
css
.feature-text-overlay {
bottom: 0;
padding: .6em 0 .8em;
position: absolute;
text-align: center;
width: 100%;
position:relative; /* add this to specify position */
}
/* create pseudo css element */
.feature-text-overlay:before {
content:"";
background-color: #000;
opacity:0.3;
bottom: 0;
top:0;
right:0;
left:0;
position: absolute;
}
Demo
edit as per comment demo 2