.wrapper{
display: inline-block;
position:relative;
}
.big-img{
width: 100%;
max-width: 100%;
border: 1px solid blue;
}
.small-img{
border: 1px solid red;
position:absolute;
right: 10%;
top: 10%;
width: 25%;
max-width:25%;
}
<div class="wrapper">
<img class="big-img" src="https://webhostingvirtualdedicatedservers.com/files/2012/09/Web-server.png" />
<img class="small-img" src="http://loosechange.co.za/wp-content/uploads/2012/06/Personal-Discount-10-lg.jpg" />
</div>
So, I want to place a logo right in the middle of a background image which is full page size (height: 100vh). I can do it in about 10 seconds using Elementor, but I have to do it on a website without any CMS, so that's hard for me. I tried literally any snippet I could find online, but it was always about how to stack an image on top of another image, not an image on top of a full-page background.
Here's an example of what I found: https://jsfiddle.net/uu3pqwpa/
I would literally use a background-imagein the CSS for the large container and apply flex settings to it (details see below) to center the smaller img tag within that div (i.e. no absolute/relative positioning, no two img tags):
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
}
.big-img {
width: 100vw;
height: 100vh;
border: 1px solid blue;
position: relative;
background: url('https://placehold.it/2000x1500/fda?text=Background-Image') center center;
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
}
.small-img {
border: 1px solid red;
width: 30%;
height: 25vh;
}
<div class="big-img">
<img class="small-img" src="https://placehold.it/200x100/ab7?text=centered-Image" />
</div>
You could set the background of the wrapper using
background: url('https://webhostingvirtualdedicatedservers.com/files/2012/09/Web-server.png);
keeping it position relative, but adding:
width: 100%;
height: auto;
And then leave the small image positioned absolutely
Placing the images as background images will make it possible to use background-size: cover and have them fit in any size of the container.
Using pseudo-elements, like ::before, is just a way of clear up HTML code with elements that are mostly there for styling.
Any other kind of clarification down below in the code as comments.
html, body {
padding: 0px; /* remove default spacing */
margin: 0px;
}
.wrapper {
position: relative;
min-width: 100vw;
min-height: 100vh; /* min lets the element span beyond the page, if necessary. */
/* didn't find image */
/* background: url('https://webhostingvirtualdedicatedservers.com/files/2012/09/Web-server.png'); */
background: url('https://picsum.photos/200/300');
}
.wrapper, .wrapper::before {
box-sizing: border-box; /* needed to include border size in width and height */
border: 1px solid blue;
background-repeat: none;
background-size: cover;
}
.wrapper::before {
content: '';
position: absolute;
width: 25vw;
height: 25vw; /* make it squarish */
left: 50%;
top: 50%;
/* move element negative 50% of it's own size on both x and y coordinate */
transform: translate(-50%, -50%);
border-color: red;
/* background: url('http://loosechange.co.za/wp-content/uploads/2012/06/Personal-Discount-10-lg.jpg'); */
background: url('https://picsum.photos/200/200');
}
<div class="wrapper">
</div>
.wrapper{
display: flex;
justify-content:center;
align-items:center;
height:100vh;
}
.big-img{
width: 100%;
max-width: 100%;
border: 1px solid blue;
}
.small-img{
border: 1px solid red;
width: 25%;
height:25%;
}
body {
background-image: url("https://images.freeimages.com/images/small-previews/1c9/maine-at-4-45-am-1370871.jpg");
background-size: cover;
}
<div class="wrapper">
<img class="small-img" src="https://images.freeimages.com/images/small-previews/25d/eagle-1523807.jpg"/>
</div>
Related
I don't have much knowledge about html and css and I couldn't find the answer on the internet so I am here.
Problem:
I am trying to make an image fill top part of the screen but one thing stops me from it and it's the default margin of the <body>. I've managed it by using margin: -10px; But now the image can't fill the screen by 20px, probably because there is no margin, image still thinks screen is that big.
html,body {
width: 100%;
height: 100%;
margin: -10px;
padding: 0;
overflow: hidden;
}
img {
width: 1600px;
height: 300px;
opacity: 70%;
object-fit: cover;
object-position: top 10px;
}
.cont {
position: relative;
text-align: center;
padding: 10px;
}
.main-text {
font-size: 100px;
color: white;
position: absolute;
top: 100px;
left: 70px;
}
<body>
<div class="cont">
<img src="https://i.stack.imgur.com/DWZAk.jpg">
<div class="main-text">Big Ass Title</div>
</div>
</body>
NOTE: If you have any questions or didn't understand anything about the question, please ask because I am ready for any answer. :) Thanks.
If your image is meant to be a decoration(design), then background is fine to use.
.cont can be a flex or grid element, to avoid position absolute and possible unwanted sides effects.
here an example with a background and grid:
body {
margin: 0;
min-height: 100vh; /* optionnal if it does not have a purpose */
}
.cont {
height: 300px; /* guessed from your code */
display: grid; /* default make a single column*/
background: url(https://picsum.photos/id/1015/600/300) 0 0 / cover; /* background covering */
}
.main-text {
margin-block: auto; /* vertical-centering in its row from here */
margin-inline-start:70px;
font-size: 100px; /* from your code */
color: white; /* from your code */
font-weight: normal; /* you looked for this? */
text-shadow: 0 0 1px #000; /*Optionnal increase readability*/
}
<div class="cont">
<h1 class="main-text">Big Ass Title</h1><!-- if that's a title, then make it a title ;) -->
</div>
Generally to eliminate all the margins and paddings you can add:
* {
margin: 0;
padding: 0;
}
By the way I attached a snippet where it's working as you requested. Is better to eliminate the margins than adding a negative margin, if you want to do it that way you must to compensate it in the width to achieve the 100% width intended.
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
img {
width: 100%;
margin: 0;
height: 300px;
opacity: 70%;
object-fit: cover;
}
.cont {
position: relative;
text-align: center;
}
.main-text {
font-size: 100px;
color: white;
position: absolute;
top: 100px;
left: 70px;
}
<html>
<body>
<div class="cont">
<img src="https://images2.alphacoders.com/941/thumb-1920-941898.jpg">
<div class="main-text">Big Ass Title</div>
</div>
</body>
</html>
Page should look like this (picture via link below), but I've encountered few problems:
I cannot fit this image to be exactly at half of the div without increasing that div. Also, for some reason, I can't move text from that picture (it's basically merged with her). I've tried with margin-left but didn't work.
Thanks in advance.
Html code:
<div class="parent">
<img src="" alt="parfum">
<p>text</p>
Css code:
.parent{
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
border: 1px solid blue;
padding: 175px;
border-radius: 5%;
}
img{
float: left;
}Picture
this is best handled with flex.
#container {
width: 100%;
height: 100vh;
padding: 0;
margin: 0;
display: flex; /* use flexbox */
background-color: lightblue;
justify-content: center; /*center x axis */
align-items: center; /*center y axis */
}
body,
html {
padding: 0;
margin: 0;
}
<div id='container'>
<img src='https://via.placeholder.com/150'>
</div>
I have a div that I want to fill the width of the browser window.
Inside the div, I want 2 images, on top of each other and each centered within the div. The images might be of any size. Both images need to maintain their aspect ratio and I don't want either of them as background to the div.
.parent {
position: relative;
border: 1px solid blue;
width: 100%;
}
.child {
border: 1px solid red;
margin-left: auto;
margin-right: auto;
display: block;
max-width: 100%;
}
.ontop{
z-index:1;
}
<div class="parent">
<img class='child ontop' src="https://i.postimg.cc/yNh7V4v1/spaceship.png">
<img class='child' src="https://i.postimg.cc/mgB04zzn/universe.jpg">
</div>
fiddle
The problem here is that they are not on top of each other. I can get them on top of each other, and centered, at least horizontally
.parent {
position: relative;
border: 1px solid blue;
width: 100%;
}
.child {
position: absolute;
border: 1px solid red;
margin-left: auto;
margin-right: auto;
display: block;
max-width: 100%;
left: 50%;
transform: translate(-50%);
}
.ontop{
z-index:1;
}
<div class="parent">
<img class='child ontop' src="https://i.postimg.cc/yNh7V4v1/spaceship.png">
<img class='child' src="https://i.postimg.cc/mgB04zzn/universe.jpg">
</div>
fiddle
but the problem now is that because of my use of absolute positioning, the images are no longer contained within the div.
Could anyone tell me what the technique would be?
I would suggest you move the image(space) to CSS background-image. That is the right approach when you want to the image as a background.
Apply flex to parent and you are good to go.
.parent {
background-image: url(https://i.postimg.cc/mgB04zzn/universe.jpg);
background-repeat: no-repeat;
background-size: cover;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.child{
height: 100px;
width: 100px;
}
<div class="parent">
<img class='child' src="https://i.postimg.cc/yNh7V4v1/spaceship.png">
</div>
Use position:absolute; to place the space ship into the middle center of the parent. This requires the parent to be the same width as the image. I did this in the example using width: fit-content;
Using this method you can position the set of images anywhere by putting these three HTML elements into one container which you move around.
.parent {
width: fit-content;
border: 1px solid blue;
position: relative;
}
.parent img {
border: 1px solid red;
}
.center {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="parent">
<img class="center" src="https://i.postimg.cc/yNh7V4v1/spaceship.png">
<img src="https://i.postimg.cc/mgB04zzn/universe.jpg">
</div>
Want the background image larger, simply change its size? In this example I am going to set it's width to width: 100vw; 100 view width
(I put as an inline style for this example though you should probably make a CSS rule for your desired size)
.parent {
width: fit-content;
border: 1px solid blue;
position: relative;
}
.parent img {
border: 1px solid red;
}
.center {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="parent">
<img class="center" src="https://i.postimg.cc/yNh7V4v1/spaceship.png">
<img style="width:100vw;" src="https://i.postimg.cc/mgB04zzn/universe.jpg">
</div>
I have created a container (red border with line denoting the center) that scrolls horizontally if there is overflow.
The first child (purple block) of this container is always a button.
When you click this button, it adds additional children to the container.
What I am trying to do is figure out a way with pure CSS, so that the first child is always centered when the app is loaded, and the last child element in the container, if there is more than one child, can also be scrolled to the very center of the container.
I am having difficulties figuring this out because I have applied a min-width (i.e. 100px) in addition to a responsive width (i.e. 25vw) to the child elements.
The way I was initially planning on achieving this was by applying a padding-left to the parent container, and then an ::after pseudo element to :not(.first-child).children:last-child, but then I realized the approach is not sufficient if I want it to be completely responsive. However, I know I can manually calculate at which width min-width will be triggered and then use a #media query, but I am hoping there is a better way.
#container {
width: 100%;
height: 100%;
padding-left: ; /* Half the window width minus half the width of the child. */
overflow-x: scroll;
display: flex;
align-items: center;
background: skyblue;
box-sizing: border-box;
border: solid red 2px;
}
#container::-webkit-scrollbar {
display: none;
}
.children {
width: 25vw;
min-width: 100px;
height: 50%;
min-height: 200px;
position: relative;
margin-right: 5%;
display: flex;
justify-content: center;
align-items: center;
background: purple;
}
:not(.first-child).children:last-child::after {
content: '';
width: ; /* Half the window width minus half the width of the child. */
height: 100%;
position: relative; /* relative or absolute */
left: ; /* and offset appropriately. */
transform: ; /* */
}
Does anybody have any ideas of how to do this?
You can apply margin-left to the first child and to deal with the min-width you can use media query. When the screen is less than 400px the 25vw will be less than 100px and you change the value to consider the 100px.
#container {
position: fixed;
top:0;
left:0;
width: 100%;
height: 100%;
overflow-x: scroll;
display: flex;
align-items: center;
background:
linear-gradient(red,red) center/1px 100% no-repeat,
skyblue;
box-sizing: border-box;
border: solid red 2px;
}
#container::-webkit-scrollbar {
display: none;
}
.children {
width: 25vw;
min-width: 100px;
height: 40%;
min-height: 100px;
position: relative;
margin-right: 5px;
display: flex;
justify-content: center;
align-items: center;
background: purple;
flex-shrink:0; /* don't forget this to avoid the shrink */
}
.children:first-child {
margin-left: calc(50% - 12.5vw);
}
#media (max-width:400px) {
.children:first-child {
width: 25vw;
min-width: 100px;
margin-left: calc(50% - 50px);
}
}
<div id="container">
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
</div>
Without media query you can consider a pseudo element where you will have a max-width constraint:
#container {
position: fixed;
top:0;
left:0;
width: 100%;
height: 100%;
overflow-x: scroll;
display: flex;
align-items: center;
background:
linear-gradient(red,red) center/1px 100% no-repeat,
skyblue;
box-sizing: border-box;
border: solid red 2px;
}
#container::-webkit-scrollbar {
display: none;
}
.children {
width: 25vw;
min-width: 100px;
height: 40%;
min-height: 100px;
position: relative;
margin-right: 5px;
display: flex;
justify-content: center;
align-items: center;
background: purple;
flex-shrink:0;
}
#container:before {
content:"";
width: calc(50% - 12.5vw);
max-width:calc(50% - 50px);
flex-shrink:0;
}
<div id="container">
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
</div>
I was able to create a strictly responsive solution using calc, even though I am using vh as the width of the children. CSS is a savior.
#container {
--offset: calc(50vw - ((100vh / 100) * 20);
padding-left: var(--offset);
background: skyblue;
}
.children {
min-width: 40vh; /* vh is used for width to account for the height of window. */
min-height: 60vh;
position: relative;
background: purple;
}
:not(.first-child).children:last-child::after {
content: '';
width: var(--offset);
height: 1px;
position: absolute;
left: 100%;
}
There are a few answers out there about how to skew the single side of a div both empty and with images:
CSS3 Transform Skew One Side
Skew one side only of an element
But using these answers, I cannot figure out the rest of my issue.
I am attempting to create a 2 column row with an image background for the second column and a skewed or angled left side. The problem I have is filling the space with the containers after they have been skewed.
I am using Foundation 6 as the primary framework behind my site.
I have attached an image of how it should look completed
The closest I have got so far is this:
I have posted the code I have so far below.
Codepen
HTML:
<section class="lan_primary">
<div class="container-full">
<div class="row wide">
<div class="columns small-12 medium-6 lan_primary--select">
CONTENT LEFT
</div>
<div class="columns small-12 medium-6 lan_primary--img">
CONTENT
</div>
</div>
</div>
</section>
CSS:
div {
border: 1px red solid;
}
.lan_primary {
width: 100%;
height: 80vh;
margin-top: 10vh;
overflow: hidden;
.row {
flex-flow: row !important;
overflow: hidden;
}
&--select,
&--img {
padding: 100px 0;
overflow: hidden;
position: relative;
}
&--select {
background-color: aqua;
}
&--img {
background-color: blue;
transform-origin: top left;
transform: skew(-20deg);
//margin-left: 80px;
}
}
UPDATE - from first answer
Adding a pseudo element to solve causes problems with variable heights. If I were to set 100vh, it would give a different result to if I were to set height: 700x;.
See image below:
Use the triangle border trick with a pseudo. With viewport units it will scale with the height
To make the skew centered, I sized the right 25px (half of the skewed
area) wider than the left.
html, body {
margin: 0;
}
.wrapper {
display: flex;
}
.left, .right {
height: 100vh;
}
.left {
flex-basis: calc(50% - 25px);
position: relative;
background: lightgray;
display: flex;
align-items: center;
}
.left::after {
content: '';
position: absolute;
top: 0;
left: 100%;
height: 0;
width: 0;
border-top: 100vh solid lightgray;
border-right: 50px solid transparent;
}
.right {
flex-basis: calc(50% + 25px);
background: url(http://lorempixel.com/500/500/people/10/) left center no-repeat;
background-size: cover;
}
<div class="wrapper">
<div class="left">
<h1>Some text</h1>
</div>
<div class="right"></div>
</div>
You can Make use of the pseudo elements to make the look skewed one side
CSS(SCSS)
div {
border: 1px red solid;
}
.lan_primary {
width: 100%;
height: 80vh;
margin-top: 10vh;
overflow: hidden;
.row {
flex-flow: row !important;
overflow: hidden;
}
&--select,
&--img {
padding: 100px 0;
overflow: hidden;
position: relative;
}
&--select {
background-color: aqua;
position: relative;
overflow:visible;
&::after{
content:"";
position: absolute;
z-index:1;
top:0;
bottom:0;
height:100%;
width:20%;
background-color: cyan;
right:-40px;
transform:skew(-20deg);
}
}
&--img {
background-color: blue;
transform-origin: top left;
//margin-left: 80px;
}
}
link for reference
hope this helps