Gradient on background using CSS - html

I want to create something like this for the top section of my one-page website.
repeating background image with a gradient
I have figured out how to repeat a background image, but I was wondering if there is a way I can specify opacity for each time the image gets repeated.
This is the CSS code I've used for the section:
section{
width: 100%;
float: left;
height: 100vh;
background-image: url("img/bgflower.jpg");
background-repeat: repeat-x;
background-size: contain;
}
Please suggest any methods I can use to achieve the same, thank you!

If you want to have true gradient instead of visible opacity regions, you can do something like my code below. Unfortunately this does not really apply opacity to your image and works only with one color (like in your example picture you have white).
#background {
/* place at the top of your page */
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
/* set background image */
background: url(https://pyry.info/stackoverflow/flower.png);
background-repeat: repeat-x;
background-size: contain;
}
/* create the white gradient */
#gradientLayer {
width: 100%;
height: 100%;
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
}
<!-- place this below everything else -->
<div id="background">
<div id="gradientLayer"></div>
</div>

I'm not sure if the section you made is responsive or if it sits within another container that has a fixed width. With the codes below, a fixed width will render a better result. However, I made something up in codepen to help you move along. https://codepen.io/jennift/pen/qBRJOYd?editors=1100. I've included some comments in the code below:
<section>
<div class="extended">
<div class="first">first</div>
<div class="second">second</div>
<div class="third">third</div>
<div class="fourth">4th</div>
</div>
</section>
section {
width: 100%;
height: 100vh;
background-image: url("https://placeimg.com/200/480/nature");
background-repeat: repeat-x;
background-size: contain;
}
/* again I'm not sure if you will use the same image bg. However, if you intend to change, remember to change the aspect ratio here as well so that the white layers on top will lay somewhat nicely aligned with the bg */
:root {
--aspectratio: 0.416; /* divide bg image width with bg image height of bg image > 200 / 480 */
}
.extended { /*this extends the container box so the divs stays in a row instead of breaking into a new line as the screen gets resized to something smaller */
width:500%;
height: 100vh;
overflow:hidden;
}
.first, .second, .third, .fourth {
background-color: #fff;
height: 100vh;
float: left;
width: calc(100vh * var(--aspectratio)); /*using the aspect ratio, you can then calculate the width of each white section
}
.first {
opacity:0;
}
.second {
opacity: 0.3;
}
.third {
opacity: 0.6;
}
.fourth {
opacity: 0.9;
}
With the codes above, if your section gets wider than this, you probably need to put in a fifth div, and probably javascript will be easier solution to auto-create divs as the screen gets wider/smaller. But if your width is fixed, this way works well.

Related

How to remove page scrolling to the left because of a photo?

Currently, I am trying to fit (crop) image on the mobile version of the website, but, with no results...
How it looks on a desktop -
How it looks on a mobile -
Image crops on mobile because position: absolute; margin-left: 50%;, but..., this page has a scroll to the left, like this -
I've tried object-fit, different positions, so on, and no success.
How should it be done?
No image is going to be able to cope with all random viewport aspect ratios.
In this particular design it seems important that the figure is looking at/moving towards the text on the left, so the common method of dealing with narrow portrait viewports by putting one half of the 'header' below the other won't make sense in this case.
One suggestion therefore is to separate out the figure from the background and position and size it in relation to the aspect ratio/size of the viewport. Bearing in mind that the text on the left has to have a certain minimal physical size in order to be readable, this snippet sets a minimum absolute width and also the percentage across the viewport that the figure will stand.
Obviously you will want to alter the actual numbers here to get the layout that is required for narrow as well as wide devices so treat the numbers used here as being just for the demo.
Also, the colored background could be a real image as stretching to use cover would work given its design, but in this snippet it is roughly drawn using CSS just to give the idea. The figure has been roughly cut out of the given image in the question and of course would need to be a better done for a published environment.
* {
padding: 0;
margin: 0;
}
.bg {
--minw: 256px;
/* minimum width we allow the left hand side (with the text) to go */
--textw: max(var(--minw), 50vw);
position: fixed;
top: 0;
left: 0;
height: 100vh;
width: 100vw;
display: inline-block;
}
.bg::before,
.bg::after {
display: inline-block;
position: absolute;
top: 0;
left: 0;
content: '';
background-repeat: no-repeat no-repeat;
}
.bg::before {
width: 100vw;
height: 100vh;
background-image: radial-gradient(circle, rgba(160, 32, 240, .8), transparent), linear-gradient(to right, rgba(0, 0, 0, 0), #00B7EB), linear-gradient(to right, rgba(255, 0, 255, .6), rgba(160, 32, 240, .8));
background-position: center center;
}
.bg::after {
background-image: url(https://i.stack.imgur.com/655zI.png);
background-size: contain;
--pc: 80%;
background-position: var(--pc) center;
--w: calc(100vw - var(--textw));
width: var(--w);
left: calc(100vw - var(--w));
margin: 5% 0;
height: calc(100vh - 10%);
}
.text {
height: 200px;
width: var(--textw);
position: relative;
top: 0;
left: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.innertext {
border: solid white 2px;
color: white;
padding: 5%;
}
#media screen and (max-aspect-ratio: 1 /1) {
body::after {
--pc: 90%;
}
}
<div class="bg">
<div class="text">
<div class="innertext">HERE IS SOME TEXT</div>
</div>
</div>
Additional note: background-attachment: fixed is not fully implemented on all browsers at the moment so the backgrounds are added to pseudo elements on a div which is given position fixed.
Use two different versions based upon the screen size in picture element, or use object-fit in the css for mobile media query.
You are getting a horizontal scroll bar because of the absolute position plus the margin, this would push the image to the right of the screen(assuming 100% wide image). If you wanna keep it this way, use overflow-x: hidden on the html and body tags.

Partial background color change of table-cell - gradient issue

In this design I need the red background color of each cell partially changed say the first cell 100% second cell 100% and the third cell 50%.
Update: I have made a change where cell's background property is changed from red to
background: linear-gradient(to right, red 50%, white 51%)
but it has one problem that the edge on the right is not sharp and fades out gently blending into the white background, how to avoid that look?
Note: There are already few questions regarding hard-stop gradient creation which is why I didn't post my earlier comment as an answer but while searching for a duplicate I figured out there might be a better way to tackle your problem and hence posting the alternate approach as an answer.
Why is there a fade out and blend to white?
Let me get this out of the way before explaining the alternate approach (just for completeness sake). The gradient that you have defined would be interpreted by the UA as follows:
Since the first param is to right, the gradient starts at left (that is 0% is at left).
From 0% to 50% (that is, from left edge till half way), the color of the gradient is a solid red.
Red ends at 50% and white starts only at 51% as per gradient definition and so between 50 - 51% the color slowly changes from red to white (and blends with the white on the other side).
From 51% to 100% (that is, from slightly past half way till the right edge), the color is pure white.
This gap between 50% to 51% is generally used for diagonal (or angled) gradients where sharp stops result in jagged (uneven) edges but for normal horizontal or vertical gradients it won't be needed.
Now, I assume that you are trying to change the color stop points like below in order to get partial fill:
background: linear-gradient(to right, red 50%, white 50%); /* for a 50% fill */
background: linear-gradient(to right, red 75%, white 75%); /* for a 75% fill */
But there is a better way to do this than change the color stop points.
What is the better way and why?
A better option would be the one in the below snippet where the color never really changes. Gradient is just a solid red color always but we control it's size/width using background-size property. As you can see in the demo below, this is as effective as changing the color stop points.
This method is more advantageous when you want to animate/transition the background because the background-size is a transitionable property whereas the gradient image's color stop point change is not. You can see what I mean in the below demo. Just hover on each cell and see what happens.
.Row {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column {
display: table-cell;
background: linear-gradient(red,red); /* use the color you need */
background-repeat: no-repeat; /* dont change */
border: 1px solid; /* just to show cell width */
}
.Column:nth-child(1) {
width:20%;
background-size: 100% 100%; /* change first value for width change */
}
.Column:nth-child(2) {
width:50%;
background-size: 75% 100%; /* change first value for width change */
}
.Column:nth-child(3) {
width:30%;
background-size: 50% 100%; /* change first value for width change */
}
/* just for demo */
.Column { transition: all 1s; }
.Column:nth-child(1):hover { background-size: 50% 100%; }
.Column:nth-child(2):hover { background-size: 100% 100%; }
.Column:nth-child(3):hover { background-size: 75% 100%; }
<div class="Row">
<div class="Column">C1</div>
<div class="Column">C2</div>
<div class="Column">C3</div>
</div>
How to change direction of the fill?
We can make the fill start from the right hand side of the cell instead of the left hand side by setting the background-position as right to the cells like in the below snippet:
.Row {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column {
display: table-cell;
background: linear-gradient(red,red); /* use the color you need */
background-repeat: no-repeat; /* dont change */
background-position: right;
border: 1px solid; /* just to show cell width */
}
.Column:nth-child(1) {
width:20%;
background-size: 100% 100%; /* change first value for width change */
}
.Column:nth-child(2) {
width:50%;
background-size: 75% 100%; /* change first value for width change */
}
.Column:nth-child(3) {
width:30%;
background-size: 50% 100%; /* change first value for width change */
}
/* just for demo */
.Column { transition: all 1s; }
.Column:nth-child(1):hover { background-size: 50% 100%; }
.Column:nth-child(2):hover { background-size: 100% 100%; }
.Column:nth-child(3):hover { background-size: 75% 100%; }
<div class="Row">
<div class="Column">C1</div>
<div class="Column">C2</div>
<div class="Column">C3</div>
</div>
.Row {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column {
display: table-cell;
background-color: red;
background: linear-gradient(to right, red, white);
}
.Column:nth-child(1) {
width:20%;
}
.Column:nth-child(2) {
width:50%;
}
.Column:nth-child(3) {
width:30%;
}
<div class="Row">
<div class="Column">C1</div>
<div class="Column">C2</div>
<div class="Column">C3</div>
</div>
Look this

CSS Background position pushes content up when screen is small

I have the following code:
<div class='row front-page-img' style="background-image:url('http://img2-3.timeinc.net/people/i/2014/news/140428/coachella-crowd-600.jpg')">
</div>
<div>Below image</div>
.front-page-img {
background-size: 100%;
width: 100%;
height:400px;
margin: 0;
opacity: 0.4;
background-position: 0px -200px;
background-repeat: no-repeat;
}
I am happy with what I am seeing. That is, I like the portion the of image that I currently have. The two things that I don't like are that are the following:
When I make the screen smaller the img gets pushed up and disappears as the screen gets small. I want it to always have the same section of the image, but just scaled down.
For some reason there is white space below the image that is now allowing other elements to follow the image.
Anyone know what I am doing wrong and how i could fix this? Later, I would like put an input tab over this image incase that changes the approach. Thanks!
jsFiddle
Just use Bootstraps img-responsive class with an inline image. If you do not want the upper part of the image to be shown, just crop the image.
#import url(https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css);
.front-page-img {
opacity: .4;
}
.front-page-img > img {
width: 100%;
}
<div class="front-page-img">
<img class="img-responsive" src="http://img2-3.timeinc.net/people/i/2014/news/140428/coachella-crowd-600.jpg" />
</div>
<div>Below image</div>
You can try this:-
.front-page-img {
background-size:cover;
height:400px;
opacity: 0.4;
background-position: 0px -200px;
background-repeat: no-repeat;
}
This works better for what you want.
background-position: center bottom;
Update your CSS to the following.
.front-page-img {
background-size: 100%;
width: 100%;
height:0;
padding-bottom:40%;
margin: 0;
opacity: 0.4;
/* background-position: 0px -200px; */
background-repeat: no-repeat;
}
By setting the height to 0, we can use padding-bottom to maintain a fixed ratio as when you use percentage based padding it is relevant to the element width.

How to make only background image transparent in responsive square grid?

Using web-tiki's responsive square grid lay-out's I have made some responsive squares with background images and text on it as follows:
HTML:
<div class="square bg imgautumn1">
<div class="content">
<div class="table">
<div class="table-cell months">
VISIBLE TEXT
</div>
</div>
</div>
</div>
CSS:
.square {
float: left;
position: relative;
margin: 0.25%;
width: 50%;
padding-bottom : 50%; /* = width for a 1:1 aspect ratio */
background-color: #1E1E1E;
overflow: hidden;
}
.content {
position: absolute;
height: 90%; /* = 100% - 2*5% padding */
width: 90%; /* = 100% - 2*5% padding */
padding: 5%;
}
.table {
display: table;
width: 100%;
height: 100%;
}
.table-cell {
display: table-cell;
vertical-align: middle;
}
.months {
font-size: 40px;
font-weight: 900;
}
.imgautumn1:before {
background-color: black;
}
/* For responsive images as background */
.bg:before {
background-position: center center;
background-repeat: no-repeat;
background-size: cover; /* you change this to "contain" if you don't want the images to be cropped */
content:'';
position:absolute;
top:0;left:0;
right:0;bottom:0;
}
.bg{color: #fff;}
/*CHANGE OPACITY ON HOVER*/
.bg:hover:before{opacity:0.2;}
Now I am trying to only make the background transparent, not the text.
While using the opacity: 0.3 property on the imgautumn1 CSS-class the image becomes transparent, but also the text in it. Other techniques like the one from this SO-answer with using a separate div for the background, or a technique with using the :after element from here for the background plus opacity make the positioning of the background go wrong (i.e., image not centred) and I find it hard to implement. Another possibility might be to place a transparent div square on top of the image, but I don't think that is possible with the background-image property.
I hope someone here can provide me with some help on how to only make the background transparent and not the text.
JSFiddle: http://jsfiddle.net/L7m5psrm/
Seems to work fine if you use the :after/:before solution (setting the image as the background)
You just need to make sure you apply the same background properties.
.imgautumn1:before {
background-image: url('https://raw.githubusercontent.com/erooijak/zaaikalender/master/Zk/Content/Images/Autumn/1.jpg');
}
/* For responsive images as background */
.bg:before {
background-position: center center;
background-repeat: no-repeat;
background-size: cover; /* you change this to "contain" if you don't want the images to be cropped */
content:'';
position:absolute;
top:0;left:0;
right:0;bottom:0;
}
Demo at http://jsfiddle.net/L7m5psrm/2/

Chrome zoom out breaks linear-gradient background

I am using a gradient background to display an alternating background for absolute positioned rows.
When zooming out in Chrome the layout gets messed up.
The calculation of the gradient background size seems to be different to the calculation of the top margins.
I have created a JSFiddle to illustrate the problem: http://jsfiddle.net/4y3k2/4/. When zooming out to e.g. 75% an offset appears between the foreground and background. The offset sums up more and more so that the layout looks completely broken for the last rows.
Here is my code:
#container {
position: absolute;
height: 2000px;
width: 100px;
background: linear-gradient(red 50%, green 50%, green);
background-size: 40px 40px;
}
.row {
position: absolute;
}
<div id="container">
<div class="row" style="top: 920px;"></div>
</div>
Everything works fine on IE and Firefox.
You can do this without calculating top every single time
for each row.
Instead set the parent div to be a block and use
predefined height and width for each row while floating them to the left:
#container {
position: absolute;
height: 2000px;
width: 100px;
background: linear-gradient(red 50%, green 50%, green);
background-size: 40px 40px;
display: block;
}
.row {
float: left;
width: 100px;
height: 20px;
}
http://jsfiddle.net/4y3k2/11/