Rotate and crop a svg background with CSS - css

I have a weird shape svg that I cannot edit, is there a way to rotate and crop a piece of it and use it as a background image with CSS? It doesn't necessarily have to be a background image as long as its location stays intact on mobile.
This is my code:
<section id="alert">
<div class="container">
<div class="row">
<div class="col-sm-12">
<h4 class="text-center">Get scholarship alerts by providing your info:</h4>
<form class="" action="index.html" method="post">
</form>
</div>
</div>
</div>
and this is my CSS:
#alert .container {
padding-top: 4em;
padding-bottom: 4em;
}
#alert {
background-color: #004976;
color: #fff;
min-height: 400px;
position: relative;
}
#alert::after {
position: absolute;
content: '';
background-image: url(https://svgshare.com/i/SN2.svg);
width: 60px;
height: 100%;
right: 0;
top: 0;
}
With this Pseudo element it looks okay but it doesn't look like the mockup.
This is how it looks with my code:
And this is the mockup and how it should look:
This is the actual SVG file: https://svgshare.com/i/SN2.svg

Hello this is what I got, I think this is what you want to achieve?
No change was made in the HTML, the CSS changed like this:
#alert .container {
padding-top: 4em;
padding-bottom: 4em;
}
#alert {
background-color: #004976;
color: #fff;
min-height: 400px;
position: relative;
overflow: hidden;
}
#alert::after {
position: absolute;
content: '';
background-image: url(https://svgshare.com/i/SN2.svg);
background-repeat: no-repeat;
background-position: bottom;
background-size: cover;
width: 100vh;
height: 60px;
right: 0;
bottom: 0;
transform-origin: bottom right;
transform: scaleY(-1) rotate(-90deg);
}
Basically I rotated and flipped the after pseudo-element and played around with the background (I would imagine a shorthand can be used there)
I also added overflow: hidden; to make sure the SVG doesn't go outside the container
You can check out my solution here: https://jsfiddle.net/h7k2eosx/5/
Note that this can present issues depending on the screen size but this should be enough to get you going I hope :)
If your design allows it you could position the after pseudo-element with a fixed position, that would work nicely :)
( like here: https://jsfiddle.net/Ltamj8r6/ )

Related

Image overlay on hover css issue

Trying to add an image overlay on hover but it returns the background under the image and the image stays on top how would I fix this
<div class="slider-inner pop parentSlider-cell content_overlay">
<?php echo wp_get_attachment_image($img['image'], 'carousel-image', '', ['class' => 'img-responsive myImg', 'data-track-content' => '']); ?>
</div>
.content_overlay{
position: relative;
top: 0;
left: 0;
width: 100%;
height: 100%;
color: #FFF;
&:hover{
display: block;
background: rgba(0, 0, 0, .6);
color: #1f8dd6;
z-index: 999;
}
}
Why isn't it working?
The question asks what is the problem with the overlay not overlaying the img.
The basic problem is that the img is within a div which is being used as the overlay, so when the overlay z-index is increased on hover the whole lot 'moves forward' on the z-access so their relative positions on that axis are not changed.
If we separate out the img from the overlay and make sure the overlay stacks over the img then the hover will work.
Here's a simple example, maintaining all the CSS given in the question but separating the overlay element from the containing element. Obviously in the real version the php takes the place of the img element here. img and overlay are given position absolute so they sit in the same place.
<!DOCTYPE html>
<html>
<head>
<style>
.content_overlay{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
color: #FFF;
rbackground-color:transparent;
}
.content_overlay:hover{/* taken out the & and written as pure CSS rather than SCSS/SASS */
display: block;
background: rgba(0, 0, 0, .6);
color: #1f8dd6;
z-index: 999; /* kept this but not strictly necessary */
}
</style>
</head>
<body>
<div <div class="slider-inner pop parentSlider-cell" style="width: 100px; height: 100px; position: relative;"> <!-- given style just for this demo -->
<img src="" style="width:100%;height:100%;background-color:blue;position:absolute;"/> <!-- using a blue square img element just for this demo -->
<div class="content_overlay"></div>
</div>
</body>
</html>
You can hide an image with an overlay like so:
.container {
position: relative;
width: 220px;
height: 220px;
background-color: gray;
}
#image, #overlay {
position: absolute;
width: 200px;
height: 200px;
top: 10px;
left: 10px;
}
#image {
background-image: url('https://www.licg.nl/media/1287/duitse-dog740x433.jpg');
background-size: cover;
background-position: center;
z-index: 1;
}
#overlay {
visibility: hidden;
background-color: red;
z-index: 2;
}
.container:hover #overlay {
visibility: visible;
}
<div class="container">
<div id="image"></div>
<div id="overlay">This image is now hidden</div>
</div>
I'm not sure if I understood the question properly, but if you want to hide/show an image on hover it would be something like so:
<div class="content_overlay">
hover to show image
<img src="https://placeimg.com/640/480/any" />
</div>
.content_overlay img {
display: none;
}
.content_overlay:hover img {
display: block;
}

Can I add a ::before element to the top of it's parent responsively without knowing the height

I want do add a diagonal stripe to the top of my div. I'll use an SVG to create it (or I could use CSS). I could do this with media queries but I wanted to know if there was a way to automatically work this out using CSS.
I can do this if I know the height, and I can manually add the height to the media queries (it's not a very long job). But surely there is a cleverer way?
I tried looking at calc, but again it relies on us knowing the width or a percentage or similar.
I don't want to use JavaScript.
Here's my code:
.itemTitle--padder {
position: absolute;
bottom: 0;
width: 100%;
}
.itemTitle--padder::before {
background: url('../../../../images/joomla-london-brand-assets/videos-diagonal-background.svg?5d167918') no-repeat;
background-size: cover;
width: 100%;
height: 100%;
position: absolute;
top:0;
left:0;
display: block;
content: "";
margin-top: -56px;
}
.itemTitle--holder {
position: relative;
width: 100%;
height: 100%;
background: $dark-blue;
padding: 0.25rem 0.5rem;
}
<div class="itemTitle--padder">
<div class="itemTitle--holder">
<div class="itemTitle">
The Title of thing I'm creating
</div>
</div>
</div>
At the moment it relies entirely on my negative margin. Is there another way to achieve this?
The main reason for trying to achieve this is because content within the .itemTitle div can be a variable length
After Gregs suggestion I realised that using ::before wasn't really a great way to acheive this as it would always inherit something from it's parent.
I ended up doing this by using two divs.
HTML
<div class="itemTitle--padder">
<div class="itemTitle--image">
<img src="../../../../images/joomla-london-brand-assets/videos-diagonal-background.svg" alt="" width="100%" height="auto">
</div>
<div class="itemTitle--holder">
<div class="itemTitle">
The Title
</div>
</div>
</div>
SCSS
.itemTitle--padder {
position: absolute;
bottom: 0;
width: 100%;
}
.itemTitle--holder {
position: relative;
width: 100%;
height: 100%;
background: $dark-blue;
padding: 0.25rem 0.5rem;
}
.itemTitle--image {
width: 100%;
img {
position: absolute;
bottom: calc(100% - 1px);
}
}

How to center crop a full screen background image

Stack Overflow seems to be full of similar questions but I haven't found a satisfying answer for my use case. Basically, I need a responsive, full screen background image for the top part of my front page. Means, narrowing the viewport leads to cropping, not stretching; the image should be centered. I'd like to avoid JS for this.
Aaron created a fiddle that almost looks like what I'm searching for. Two problems:
Strange behaviour when narrowing viewport (below 500px width)
position: fixed;
I was able to reproduce the solution of Bryce Hanscomb and Gabriela Gabriel for a container (see my fiddle):
But I failed to extend this to full screen. This is my code (see fiddle):
HTML:
<div id="background">
<img src="//dummyimage.com/600x200/0099cc/fff.png" />
</div>
CSS:
div#background {
background-color: orange;
min-height: 100%;
min-width: 100%;
overflow: hidden;
position: absolute;
top: 0;
z-index: -1;
}
img {
left: 50%;
position: relative;
transform: translate(-50%, 0);
min-height: 100%;
min-width: 100%;
}
Problem #1: The image doesn't take up the full height of its parent div (with min-height set to 100%).
Problem #2 + #3: In a narrow viewport, the image is cut off on the right (not centered), and a horizontal scrollbar is shown.
As a side note, can somebody tell me where those 4 pixels come from?
Your image will fill the entire space and also not have the problem of not being centered if you use position:absolute on your image
div#background {
background-color: orange;
min-height: 100%;
min-width: 100%;
overflow: hidden;
position: absolute;
top: 0;
z-index: -1;
}
img {
left: 50%;
position: absolute;
transform: translate(-50%, 0);
min-height: 100%;
min-width: 100%;
}
The issue with the 4px at the bottom is because images always align to the top just like text, this also adds a bit of padding to the bottom to make the baseline for the text so that letters can hang down under the rest.
If you set vertical-align: bottom it should fix it like so:
h1 {
text-align: center;
}
div#container {
background-color: black;
height: 200px;
width: 100%;
margin: 50px auto;
}
div#content {
background-color: orange;
min-width: 100%;
overflow: hidden;
}
img {
left: 50%;
position: relative;
transform: translate(-50%, 0);
vertical-align: bottom;
}
<div id="container">
<div id="content">
<img src="//dummyimage.com/600x200/0099cc/fff.png" />
</div>
</div>
For the centre aligning of the image, I would personally recommend actually using css background-image and then setting the background-position like so:
div#background {
background-color: orange;
min-height: 100%;
min-width: 100%;
position: absolute;
top: 0;
z-index: -1;
background: url(//dummyimage.com/600x200/0099cc/fff.png) center center no-repeat;
background-size: cover;
}
<div id="background">
</div>

Can't Insert Background Image

I have been trying to update my site and have been having extreme difficulty with implementing an image into the site background.
I tried to create two divs, one for each side (as I want the output to be image1 - lets call it leftbg.png then content in the middle, followed by image2 - lets call it rightbg.png on the right).
So I have put the divs in the body as so:
<body>
<div id="leftbg">
<div id="rightbg">
content
</div>
</div>
</body>
And in the css file I have:
#leftbg {
float: left;
width: 22.5% (body is 55%);
background: url(images/leftbg.png) no-repeat;
z-index: 999
}
However this is not producing anything. I think it might be because my body code includes a background already, and I have tried to put z-indexes, such that my leftbg class would be dominant over the body class, however I am aware z-indexes have many problems.
This is the body css code:
body {
font:11px "Trebuchet MS", Arial, sans-serif;
color:#666;
background:maroon url(image/backgrd.png);
z-index: 0;
}
Any help would be greatly appreciated!
Try to set a height to #leftbg. Example:
#leftbg {
float: left;
height: 80px;
width: 22.5%;
background: url(images/leftbg.png) no-repeat;
z-index: 999
}
I suggest restructuring your HTML like this:
<div class="bgs">
<div class="bg left"></div>
<div class="bg right"></div>
</div>
<div class="fake-body">
<h1>Hello World!!!!!!!!!!!!!</h1>
</div>
Then you can do something like this with your CSS:
.bgs{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: green;
}
.bg{
position: absolute;
width: 50%;
height: 100%;
top: 0;
}
.bg.left{
left: 0;
background-color: red;
}
.bg.right{
right: 0;
background-color: blue;
}
.fake-body{
position: absolute;
top: 0;
left: 0;
}
I think that's pretty clean. Feel free to replace the red and blue background colors with your desired background images.
JSFiddle

How to overlay images without using postion:absolute?

I'm looking for the proper CSS method to overlay div of images on top of another div of images (not background image) without using position:absolute. Anyone know how to do this?
http://jsfiddle.net/HUUQ6/2/
You can overlay/overlap elements on top of one another using a negative margin.
Example:
#b{
margin-left:-10px;
}
This will move the element b to the left 10px, overlaying whatever is to the left of it (assuming this is a display:block type element, not an inline, like a span).
position: absolute isn't "improper" - it's part of the CSS spec! There isn't another way to put elements over other elements, unless you faff about with position: relative or maybe some float properties.
position: absolute is the easiest way to do it. What makes you think it's a bad idea?
The only other solution is to use an image inside a div with a background, like this:
<div>
<img src="...">
</div>
Then give the div a background-image:
div
{
background: url(/images/foo.png) no-repeat;
}
However, for multiple images I'd definitely stick to position: absolute.
There's a very glitchy demo here demonstrating the effect.
Here's how to layer 2 images and center the second image.
NOTE: "Position: absolute" in "image2" is only used so the images can overlap. It will still center your image responsively / without needing to use "left: " or "right: " or margin, and is independent of the image size.
HTML:
<div class="page-container">
<div class="images-container">
<img class="image1" src="YOURIMAGEURLHERE" />
<div class="image2container">
<img class="image2" src="SECONDIMAGEURLGOESHERE" />
</div>
</div>
</div>
CSS:
body, html {
height: 100%;
width: 100%;
margin: 0;
}
.images-container {
position: relative;
top: 0;
left: 0;
background-position: center;
background-repeat: no-repeat;
}
.image1 {
position: relative;
top: 0;
left: 0;
//if you want to blur the outer image
//transform: scale(1.1);
//filter: blur(20px);
//-webkit-filter: blur(20px);
height: 100vh;
width: 100vw;
}
.image2container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.image2 {
position: absolute;
top: 0px;
}
.page-container{
}
SCSS (if you prefer it, which I do - makes it much easier to copy styles from 1 file to another)
body, html {
height: 100%;
width: 100%;
margin: 0;
}
.page-container {
.images-container{
position: relative;
top: 0;
left: 0;
background-position: center;
background-repeat: no-repeat;
.image1 {
position: relative;
top: 0;
left: 0;
//if you want to blur the outer image
//transform: scale(1.1);
//filter: blur(20px);
//-webkit-filter: blur(20px);
height: 100vh;
width: 100vw;
}
.image2container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
.image2 {
position: absolute;
top: 0px;
}
}
}
}

Resources