I'm having trouble with creating a transparent box on top of a background image, like this. My code is almost exactly the same, but for some reason, I can't get the transparent box to move away from the top of the image (I'd like the box to be centered with some margin above and below). Adding padding:20px to .background does nothing; adding margin:20px to #transbox only increases the left and right margins by 20px, but does nothing to the top and bottom margins.
HTML & CSS:
.background {
background-image: url("https://source.unsplash.com/5OMTuqOM7bI");
}
#transbox {
background-color: rgba(255, 255, 255, 0.8);
padding: 20px;
margin: 60px;
}
h3 {
text-align: center;
font-weight: 300;
color: #3C5FA3;
}
.container {
width: 80%;
margin: 0 auto;
padding: 0 30px;
}
.clearfix:after {
visibility: hidden;
display: block;
content: "";
clear: both;
height: 0;
}
<div class="container clearfix background">
<div id="transbox">
<h3>Some text here</h3>
</div>
</div>
Several options to get this. One of them is to set the padding-top/bottom of the container.
padding: 30px;
(instead of padding: 0 30px).
Here is the fix to your code:
.background {
background-image:url("https://source.unsplash.com/5OMTuqOM7bI");
}
#transbox {
background-color:rgba(255,255,255,0.8);
padding:20px;
margin:60px;
}
h3 {
text-align:center;
font-weight:300;
color:#3C5FA3;
}
.container {
width: 80%;
margin: 0 auto;
padding: 30px;
}
.clearfix:after {
visibility:hidden;
display:block;
content:"";
clear:both;
height:0;
}
<div class="container clearfix background">
<div id="transbox">
<h3>Some text here</h3>
</div>
</div>
You don't need a clearfix or anything for this case. All you need is the background opacity. The key here is to give the container:
position: relative;
height: <some>px;
And the transbox:
position: absolute;
top and right values.
If you want it to be centered, it's very easy. Use a dead centre trick like this:
.background {
background-image: url("https://source.unsplash.com/5OMTuqOM7bI");
}
#transbox {
background-color: rgba(255, 255, 255, 0.8);
padding: 20px;
margin: 60px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
h3 {
text-align: center;
font-weight: 300;
color: #3C5FA3;
}
.container {
width: 80%;
margin: 0 auto;
padding: 0 30px;
position: relative;
height: 500px;
}
<div class="container background">
<div id="transbox">
<h3>Some text here</h3>
</div>
</div>
Initial Version
Centered Version
padding: 0 30px; is identical to padding-top: 0; padding-right: 30px; padding-bottom: 0; padding-left: 30px;
You say:
Adding padding: 20px; to .background does nothing
That's because later in your CSS you overwrite this padding in .container.
If two or more rules have the same CSS specificity, the one that occurs last wins. Since both selectors have identical specificity, .container definitions win over .background definitions.
Related
What is the best way to have an image on top of a card with content underneath it?
I have tried negative margins but I have had no luck on with this approach.
I attached an image of what the look and feel I am going for:
Here is my attempt on code
.card {
text-decoration: none;
margin-right: 20px;
text-align: center;
}
.card__section {
overflow: hidden;
padding-bottom: 50px;
display: block;
position: relative;
}
.card__inner {
background: black;
padding: calc(35px + 30%) 35px 35px;
color: white;
width: 100%;
display: block;
position: relative;
}
.card__image {
width: 80%;
height: auto;
margin: 0 auto -30%;
position: relative;
z-index: 1;
}
<a href="#" class="card">
<section class="card__section">
<img
class="card__image"
src="http://via.placeholder.com/340x220"
alt=""
/>
<div class="card__inner">
<h1>
This is a static template, there is no bundler or bundling
involved!
</h1>
</div>
</section>
</a>
you need to add below css
.card__section {
overflow: visible;
}
.card__image {
margin-bottom: 20px;
margin-top: -100px;
}
section.card__section {
margin-top: 60px;
}
and put your image in card__inner div
see https://codesandbox.io/s/card-hover-5n0yf?file=/index.html:450-461
hope this helps
You're almost there! There are two things that need to be updated:
1. Remove extra % in .card__image
.card__image {
width: 80%;
height: auto;
margin: 0 auto -30%%; // <-- Remove extra % at the end.
position: relative;
z-index: 1;
}
2. Adjust padding in .card__inner due to negative margin on previous element.
.card__inner {
background: black;
// Update padding to below. Negative margin on previous element needs to be added to the top padding.
padding: calc(35px + 30%) 35px 35px;
color: white;
width: 100%;
display: block;
position: relative;
}
Sandbox: https://codesandbox.io/s/card-hover-wiilf
I'm a newbie and trying to figure out how to stack these two boxes on top of each other when the browser gets resized. I don't want to use float, I'd rather stick to inline-block unless anyone else has strong suggestion against it. I was thinking I guess in using inline-block that the boxes would stack when the div got resized with the browser but it isn't happening. The boxes are just getting skinner and skinner and the text just wraps and exceeds the box. `
.cp2_maincontainer {
width: 100%;
height: 300px;
font-size: 0;
display: flex;
justify-content: space-between;
padding: 10px 50px 20px 50px;
}
.cp2_container {
width: 47%;
height: 100%;
background: no-repeat center;
display: inline-block;
position: relative;
}
.cp2_subcontainer {
background-color: rgba(211, 211, 211, .8);
height: 100%;
width: 100%;
padding: 10px 15px;
font-size: 16px;
font-family: playfair display;
position: absolute;
outline: solid 2px darkgrey;
outline-offset: -10px;
}
.cp2_subcontainer ul {
margin-left: 20px;
}
.cp2_subcontainer p {
margin: 10px;
}
.cp2_subcontainer h3 {
padding: 10px 0;
}
.cp2_container2 {
background-color: darkgrey;
background: no-repeat center;
width: 47%;
height: 100%;
display: inline-block;
position: relative;
}
.cp2_subcontainer2 {
background-color: rgba(211, 211, 211, 0.8);
height: 100%;
width: 100%;
padding: 10px 15px;
font-size: 16px;
font-family: playfair display;
position: absolute;
outline: solid 2px darkgrey;
outline-offset: -10px;
}
.cp2_subcontainer2 ul {
margin-left: 20px;
}
.cp2_subcontainer2 p {
margin: 10px;
}
.cp2_subcontainer2 h3 {
padding: 10px 0;
}
.addtextarea {
color: black;
padding: 10px;
width: 100%;
font-size: 16px;
text-align: justify;
}
<div class="cp2_maincontainer">
<div class="cp2_container" style="background-image:URL(<?php the_field('imageleft'); ?>)">
<div class="cp2_subcontainer">
<h3 style="text-align:center;">Title for Text Box 1</h3>
<p>Text in box # 1</p>
</div>
</div>
<div class="cp2_container2" style="background-image:URL(<?php the_field('imageright'); ?>)">
<div class="cp2_subcontainer2">
<h3 style="text-align:center;">Title for Text Box 2</h3>
<p>Text in box #2</p>
</div>
</div>
</div>
<div class="sectionbreak" align="center"></div>
You gave the divs a width in percentage. That means that as their container grows smaller, so do they, and they never have a reason to break. The obvious solution is to give them a fixed width (px, em).
If for some reason you need the percentages to work up to a certain point (for the purpose of bigger screens for example), two options come to mind:
Give the divs a min-width, so that once they hit that width the percentages will be ignored and the line will break.
Using media queries, define a different width for them based on screen size.
.cp2_maincontainer {
width:100%;
height:300px;
font-size: 0px;
display:flex;
justify-content: space-between;
padding: 10px 50px 20px 50px;
}
.cp2_container {
width:47%;
height:100%;
background: no-repeat center;
display:inline-block;
position: relative;
}
.cp2_subcontainer {
background-color: rgba(211, 211, 211, 0.8);
height:100%;
width:100%;
padding:10px 15px;
font-size:16px;
font-family: playfair display;
position: absolute;
outline:solid 2px darkgrey;
outline-offset: -10px;
}
.cp2_subcontainer ul{
margin-left:20px;
}
.cp2_subcontainer p{
margin:10px;
}
.cp2_subcontainer h3{
padding:10px 0px;
}
.cp2_container2 {
background-color: darkgrey;
background: no-repeat center;
width:47%;
display:inline-block;
position: relative;
min-width: 300px;
position: absolute;
right: 0;
height:300px;
}
.cp2_subcontainer2 {
background-color: rgba(211, 211, 211, 0.8);
height:100%;
width:100%;
padding:10px 15px;
font-size:16px;
font-family: playfair display;
position: absolute;
outline:solid 2px darkgrey;
outline-offset: -10px;
}
.cp2_subcontainer2 ul{
margin-left:20px;
}
.cp2_subcontainer2 p{
margin:10px;
}
.cp2_subcontainer2 h3{
padding:10px 0px;
}
.addtextarea {
color: black;
padding: 10px;
width: 100%;
font-size: 16px;
text-align: justify;
}
<div class="cp2_maincontainer">
<div class="cp2_container" style="background-image:URL(<?php the_field('imageleft'); ?>)">
<div class="cp2_subcontainer">
<h3 style="text-align:center;">Title for Text Box 1</h3>
<p>Text in box # 1</p>
</div>
</div>
<div class="cp2_container2" style="background-image:URL(<?php the_field('imageright'); ?>)">
<div class="cp2_subcontainer2">
<h3 style="text-align:center;">Title for Text Box 2</h3>
<p>Text in box #2</p>
</div>
</div>
</div>
<div class="sectionbreak" align="center">
</div>
I think this is what you are trying to accomplish.
I have added/edited the following on .cp2_container2
min-width: 300px;
position: absolute;
right: 0;
height:300px;
You need a min-width in order to have the 2 boxes overlap, otherwise they will always be half the width of the page and never overlap.
Positioning absolutely allows the div to freely pass over the statically positioned one.
Right just tells the div to position at the right edge of whatever it is relative to, in this case the body.
By positioning absolutely the height 100% becomes relative to the entire window, I have solved by using a pixel height, although you could also position the cp2_maincontainer relative and given it a height, making the height 100% of cp2_container2 relative the height of cp2_maincontainer.
Good luck.
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
http://samnorris.co.nz/backup/test/
In the section with the Pythagoras quote "There is geometry in the humming of the strings,
there is music in the spacing of the spheres" is a little experiment from Codepen I've tweaked a bit and have been playing around with (the rotating animated circles)
...not sure if my mind is just working a bit slow tonight or what but I'm having a great amount of difficulty positioning and centering the div that contains these elements underneath the div's that contain the quote and without overlaying the background...
any help would be appreciated :)
edit: updated code, I figured out why it was being pushed down (padding on the wrapper was causing it, so I've changed the background div to fixed height instead) now I just need to figure out why I am having to use left: 32% (roughly) to try and center it. Margin: 0 auto is failing me, and setting #spheres-block to width 100% messes up the bg.
Relevant CSS:
.spacesuit-bg {
background: transparent url("../img/spacesuit-bg.jpg") no-repeat 0 0;
color: #fff;
padding: 15% 0;
text-align: center;
text-transform: uppercase;
-webkit-transform: translate3d(0,0,0);
}
.quote-wrapper {
width: 100%;
display: block;
padding-top: 300px;
}
.quote-one {
font: 18px/1em "proxima_nova_rgbold", sans-serif;
font-weight: 700;
text-shadow: 0px 3px 3px rgba(0,0,0,0.3);
letter-spacing: 1px;
word-spacing: .06em;
width: 100%;
line-height: 1.25em;
}
.quote-two {
font: 36px/1em "proxima_nova_rgbold", sans-serif;
font-weight: 700;
text-shadow: 0px 3px 3px rgba(0,0,0,0.3);
letter-spacing: 1px;
word-spacing: .06em;
width: 90%;
line-height: 1.25em;
padding-top: 25px;
margin: 0 auto;
}
.spacesuit-wrapper:before, .missiongo, .golaunch {
display: inline-block;
vertical-align: middle;
}
#spheres-block {
overflow: hidden;
position: absolute;
display: block;
width: 600px;
height: 600px;
left: 32%;
}
.spheres {
width: 600px;
height: 600px;
margin: 0 auto;
position:absolute;
}
.rings {
border-radius: 50%;
border: 2px solid rgba(255,255,255, .5);
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
opacity: 0.6
}
Relevant HTML:
<div id="spacesuit-bg" class="section spacesuit-bg">
<div id="spheres-block">
<div class="spheres">
<div class="rings"></div>
<div class="rings"></div>
<div class="rings"></div>
<div class="rings"></div>
<div class="rings"></div>
<div class="rings"></div>
</div>
</div>
<div class="quote-wrapper">
<div class="quote-one wow flipInX">"There is geometry in the humming of the strings,</div>
<div class="quote-two wow flipInX">there is music in the spacing of the spheres."</div>
</div>
</div>
I am not a CSS expert (trying to learn, that's why I spend time here :)), so I may have missed other issues with my suggestion, but anyway I was able to achieve the desired effect by positioning the speres-block relatively, and positioning the quote-wrapper absolutely with a top offset of 0:
#spheres-block {
position: relative;
width: 650px;
height: 650px;
margin: 0 auto;
margin-top: 15px;
}
.quote-wrapper {
width: 100%;
display: block;
padding-top: 300px;
position: absolute;
top: 0px;
}
I hope I understood correctly.
I'm trying to show off the best hairstyles that I've done on a web site.
When you hover over the images, it gives a little information about each of the styles. The hover works fine and shows up, but even though I have the height set as 100% on the information div, it only takes up enough space needed for the text. I'm using display: table to get the text centered vertically "on the image". When I change it to display block, it works fine, but the vertical centering is a must. This is extremely annoying & after 2 hours of fiddling with it I simply cannot get it to work. Please help! http://jarahairandmakeup.tumblr.com/
div.post {
width: 50%;
height: auto;
position: relative;
float: left;
}
div.information {
display: table;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.65);
text-align: center;
z-index: 2;
opacity: 0;
}
div.post:hover .information { opacity: 1 !important; }
div.info {
color: #fff;
display: table-cell;
vertical-align: middle;
font: 11px/1.4 'Gudea';
letter-spacing: .3em;
text-transform: uppercase;
padding: 0 5%;
}
div.info h3 {
font: 100 20px/1 'Gudea';
margin: 0; padding: 0;
letter-spacing: 6px;
}
div.info hr { color: #fff; background: #fff; margin: 20px auto;}
div.image {
float: left;
position: relative;
z-index: 1;
}
div.image img.hairstyle {
width: 100%;
height: 100%;
display: block;
}
<div class="post">
<a href="/ciara">
<div class="information">
<div class="info">
<h3>CURLED UPDO</h3>
<hr />
A romantic style updo with braided accents for prom
</div>
</div>
<div class="image">
<img src="http://static.tumblr.com/ww0u3rz/BRjn5dotq/img_0919.jpg" class="hairstyle">
</div>
</a>
</div>
I'd like to make it so the black background of the information div takes up the entire length of the photo.
use display:block instead of table.
Edit:
Just wrap the info div with a wrapper and have the following css:
display: table;
position: absolute;
height: 100%;
width: 100%;
it works fine: http://cloud.kerim.me/image/2p3W1x011m0I