I'm trying to achieve the following drop shadow effect using CSS:
I have tried styling an <hr> element with a linear gradient as the background image as an alternative to using the box-shadow rule on an image however it does not produce the desired curved shadow effect.
Is what I am trying to do possible via CSS only ?
Here is my current code:
HTML
<section class="section-categories section-fruits">
<div class="row">
<figure class="categories">
<img src="res/img/category/fruits.png" alt="Offers" class="categories__fruits">
</figure>
<div class="categories__explore">
<p>Fruits & Vegetables</p>
<p>A variety of fresh fresh and vegetables</p>
<button>Explore fruit and veg</button>
</div>
</div>
</section>
CSS
/* Using Box Shadow, but didn't get the desired effect */
.section-categories{
height: 250px;
margin: 20px 0px;
box-shadow: 0px 1px 1px rgba(0,0,0,0.3);
}
One pure CSS approach might be to use the radial-gradient function on a pseudo element as follows:
/* The shadow CSS class element */
.shadow {
position:relative;
}
/* The CSS peseudo element that achieves the shadow effect */
.shadow:after {
content:'';
display:block;
position:absolute;
bottom:-1rem;
/* The main idea with this technique is to use a radial gradient to simulate the
desired effect */
background:radial-gradient(farthest-corner at 50% 0px, grey 0%, transparent 50%);
width:100%;
height:1rem;
}
/* The styling below is not part of the technique and is included to support the snippet */
div {
display:flex;
flex-direction:row;
justify-content:center;
align-items:center;
}
div button {
background:red;
}
<div class="shadow">
<img src="https://i.pinimg.com/originals/2b/1c/f5/2b1cf5525873467315eaa0c07394d302.jpg" height="100px" />
<button>Explore</button>
</div>
The idea here is to define a pseudo element :after the actual element casting the shadow (ie the <div> in the snippet above), which contains a radial-gradient. To simulate the desired effect, the radial-gradient is colored with a dark inner and transparent outer, with the center of that gradient being offset to the upper edge of the pseudo element via the farthest-corner at 50% 0px parameters.
Hope that helps
Related
is it possible to create a background with 3 colors and 3 directions in CSS?
I'm a beginner with CSS gradients.
Here is a solution with only one div and multiple gradient and without the need to complicate with transform or negative margin:
.box {
width:200px;
height:200px;
background:
linear-gradient(150deg,#0c13a2 35%,transparent 35.5%),
linear-gradient(-120deg, #000000 50%, #c70535 50.5%);
}
<div class="box">
</div>
How can one add opacitiy to just the left 100px of a 600px wide image in css? Is there a css property for that?
I have tried to add an overlapping div and add opacity to this div, but that is a pain in the back and does not look as a good solution.
Well i found that overlapping div with position:absolute is the only solution for this because their is no property in css to catch half image.
HTML
<div class="parent">
<div id="opacity_div"></div>
<img class="half_fade" src="http://i.stack.imgur.com/W7mPR.jpg?s=32&g=1">
</div>
CSS
.parent{
position:relative;
}
#opacity_div{
background:#fff;
height:20px;
width:20px;
position:absolute;
top:18px;
left:6px;
opacity:0.5 /* manipulate to desired opacity */
}
img.half_fade {
position:absolute;
top:0;left:0;
z-index:-1000;
}
Example : http://jsfiddle.net/JMBFS/81/
Checkout this question to understand better : https://drupal.stackexchange.com/questions/70025/how-to-apply-opacity-to-just-a-portion-of-the-image/70029
The solution is to use overlay the image element with another image element, using position absolute and clipping (http://codepen.io/anon/pen/jqpwgV).
HTML:
<img src="img.jpg" id="image-overlay" />
<img src="img.jpg" id="image" />
CSS:
#image-overlay{position:absolute;clip: rect(0px,498px,374px,100px);}
#image{opacity:0.5;}
If you want to be future ready. Use clip-path with graceful degradation in your CSS. See code below (or http://codepen.io/anon/pen/zqLdxW).
#image-overlay{position:absolute;
clip: rect(0px,498px,374px,100px);
-webkit-clip-path: inset(0px 0px 0px 100px);
clip-path: inset(0px 0px 0px 100px);
}
#image{opacity:0.5;}
This question already has answers here:
Opacity of div's background without affecting contained element in IE 8?
(8 answers)
Closed 7 years ago.
I'm trying to set an opacity to my background div, but all the content inside gets an opacity too. I don't want this.
I tried to fix it with pseudo elements but it didn't work out, I can fix this problem by adding a second background div and setting a height and position to that div, but I don't want to set a height for a div.
How can I fix this without adding a second div and height?
You can see my demo here
You could always use an RGBA value:
html {
background-color: red;
}
#login {
width: 365px;
background-color: rgba(255, 255, 255, 0.3);
padding: 37px;
}
https://jsfiddle.net/d2shse4c/2/
What i usually make to do this is sibling divs with position absolute:
<div id="page">
<div id="content">
TEXT here
</div>
<div id="back" style="position:absolute; opacity:0.5; left:0; top:0; width:100%; height: 100%; background-color:#000000;">
</div>
<div id="anotherText" style="position:absolute; width:100px; height: 100px">
TEXT
</div>
</div>
and so on....
OR:
Set a png background image on the parent div!
I have the following html:
<div class="main_container">
<div class="sub_container">
<div class="floating">wookie1</div>
...
<div class="floating">wookie5</div>
</div>
</div>
Consider it's like an image gallery.
main_container has an unfixed size, it's set as a percentage of the user resolution.
I want sub_container to have the exact width of the sum of the floating div.
If I use "display:table;" for sub_container and "display: inline-block;" for floating divs, it works fine:
until I have enough div in the list, so that the sum of width is larger than main_container and they break on the next line:
But still, I want subcontainer (yellow background) to to be ALWAYS the EXACT WIDTH of the sum of the divs, even when they go on several lines, like this:
I've googled for hours now, and wasn't able to find an elegant solution (css only, if possible.)
Here's the jsfiddle, to play with this.
Pure CSS Solution
The problem is that for the items to "know" to wrap to the next line, the container has to have "filled" its available horizontal space, which is your .main_container width. Yet you want the background to not go beyond what is needed for the actual elements themselves. So I've used the elements themselves to create the background with the help of cobbled together pseudo-elements.
Here's the fiddle (tested in IE9, Firefox 18, Chrome 24 on a Win 7 machine)
What I am doing is piecing together the background with each individual .floating element, which makes the right most element to be the right border control for the size of the background (note the two different width examples in the fiddle).
The explanation of each part is given in the comments below. There are a two key limitations to note:
The .floating cannot have a position set, so that is a potential limitation based on particular application.
The background needs to be either a solid color or purely vertical oriented "motion" (i.e. a gradient fading from top to bottom, or horizontal lines would work).
KEY CSS (with explanatory comments)
.sub_container {
border-left: 1px solid #333; /* forms its own left border */
overflow: hidden; /* needed for background and border to work */
position: relative; /* positioning background off this */
z-index: 1; /* needs a stacking context */
}
.floating {
font-size:20px;
line-height:30px;
padding:0 5px 0 5px;
border: 1px solid black;
margin: 3px;
display: inline-block;
/* NOTE: CANNOT be given positioning */
}
.floating:after {
content: '';
position: absolute; /* will position off subcontainer */
border-top: 1px solid black; /* makes the top/bottom border itself */
border-bottom: 1px solid black;
z-index: -1; /* push it to the background */
top: 0; /* set it to top of sub_subcontainer */
bottom: 0; /* set it to bottom of sub_container */
margin-left: -100%; /* shove it past the far left edge of sub_container */
/* next, use padding to bring it back to its position at the end
of the text string of .floating */
padding-left: 100%;
/* next, add enough padding on the right to compensate for the right
padding, right margin, and right border of .floating */
padding-right: 9px;
background-color: yellow; /* set your background color */
/* NOTE: this will not work with a background image that
has any horizonal differences to it (top to bottom
gradient would probably be okay) */
}
.floating:before { /* make right border */
content: '';
padding-top: 10000px; /* give it some obscene height that will not be exceeded */
margin: -5000px 0; /* counter the padding height by half size margins top/bottom */
/* next, push this behind the background with an even lower z-index
to hide it if it is not the right most element beign used to
form the right border */
z-index: -2;
border-right: 1px solid black; /* make the right border */
float: right; /* get the before element to the right */
position: relative; /* needs to be adjusted in position */
right: -9px; /* move it same as padding-right of the after element */
display: block; /* give it a display */
}
I got bored trying this and created a JS script based on jQuery to solve it.
var t = $(".sub_container").width()/$(".floating").outerWidth();
$(".sub_container").width(parseInt(t) * $(".floating").outerWidth());
Demo
Reread your question...since you won't commit to anything (max-width:80%, 500px, etc) I broke everything on the 4th child - per your example.
CSS
.main_container, .sub_container, .floating { outline:1px solid black }
.sub_container { background-color:yellow; display:table; padding-left:5px }
.floating {
float:left;
padding:5px;
margin:5px 5px 5px 0;
}
.floating:nth-child(4n+5) {
clear:left;
float:left;
}
HTML
<div class="main_container">
<div class="sub_container">
<div class="floating">wookie1</div>
<div class="floating">wookie2</div>
<div class="floating">wookie3</div>
<div class="floating">wookie4</div>
<div class="floating">wookie5</div>
<div class="floating">wookie6</div>
<div class="floating">wookie7</div>
<div class="floating">wookie8</div>
<div class="floating">wookie9</div>
</div>
</div>
EDIT (option 2)
I don't believe what you're trying to do can be accomplished by HTML/CSS alone. I offer another solution based on a hunch...and your Image Gallery comment.
CSS
.main_container, .sub_container, .floating { outline:1px solid black }
.main_container {
text-align:center
}
.sub_container { background-color:yellow; display:inline-block; max-width:80%; }
.floating {
display:inline-block;
padding:5px;
margin:5px;
}
HTML
<div class="main_container">
<div class="sub_container">
<div class="floating">wookie1wookie1wookie1</div>
<div class="floating">wookie2</div>
<div class="floating">wookie3</div>
<div class="floating">wookie4</div>
<div class="floating">wookie5</div>
<div class="floating">wookie6wookie6</div>
<div class="floating">wookie7wookie7wookie7</div>
<div class="floating">wookie8</div>
<div class="floating">wookie9</div>
</div>
</div>
It does not make .subcontainer snap to the contents; however, (using max-width) it does allow you to give it some breathing room from the main container and its children can be of various sizes.
Delete main container width and .sub_container position absolute and you should be good to go.
.main_container {
#width:380px;
}
.sub_container {
#position: absolute;
}
Use span element instead of div for "sub_container"
.sub_container{background-color:yellow;}
I would like to create from this image a styled DIV box for my website :
How can I do that using CSS and HTML.
I cut the image in three different parts :
However I don't know how to use them with Divs to create my vertically expendable box.
Thanks for your help!
I assume that you want your content to start inside the top one, but expand to the second one as well. If that is the case then you will need some overlap on the background-images.
HTML
<div class="expandable">
<div class="content top">content goes here</div>
<div class="bottom"></div>
</div>
CSS
.expandable{
background:url('middle-image.jpg') 0 0 repeat-x;
}
.top{
background:url('top-image.jpg') 0 0 no-repeat;
height:auto!important;
height:100px;/* whatever the height of the top (big) area is */
min-height:100px; /* whatever the height of the top (big) area is */
}
.bottom{
background:url('bottom-image.jpg') 0 0 no-repeat;
height:10px; /* whatever the height of the bottom image is. */
}
Example at http://www.jsfiddle.net/gaby/s8XZQ/
This could be done with CSS3 features like border-radius,box-shadow and gradient. Here's an example. Should work in Opera, Firefox, Chrome and Safari.
Also, you can do this with :before and :after CSS pseudo-elements, like in other two answers.
Edit: For Internet Explorer all those features are possible with behavior file, like PIE.
Try this:
HTML:
<div class="container">
<div class="top"></div>
<div class="middle">content here content here content here</div>
<div class="bottom"></div>
</div>
CSS:
.container { width: 300px; }
.top { padding-top: 15px; background: url(topimage.png); }
.middle { background: url(middleimage.png); }
.bottom { padding-bottom: 15px; background: url(bottomimage.png); }
Adjust the paddings in the CSS so that they match the height of your topimage and bottomimage, and the container width so that it matches the image's widths.
Use three separate divs, and set the top padding of the middle one to the minus height of the top one. So:
#top-div {
height: 25px;
background-image: url(bg-top.jpg);
}
#middle-div {
background-image: url(bg-middle.jpg);
padding-top: -25px;
}
#bottom-div {
background-image: url(bg-bottom.jpg);
}