How to remove extra space from position relative? - css

I have used position:relative to move the priceholder div above its initial position, now when I put another div below it which is the description div (you can check it on my jsfiddle), you can see that the priceholder div still occupies the area of its initial position.
My question is how do I remove this extra space? I do not want to use another position relative on my description div to fix it because it messes up my layout.
<div class="frame">
<div class="imageholder"><img src="http://i.imgur.com/daiSXVG.jpg"/></div>
<div class="priceholder"><p>$ 456</p></div>
<div class="description">This is a test description.</div>
</div>
Here is my jsfiddle: http://jsfiddle.net/MarkTe/g6zs8qhx/2/

I redid your structure a bit, removing some (unnecessary elements)
body {
margin-top: 50px;
}
.frame {
margin: 0 auto;
padding: 6px;
background: #fedd02;
border-radius: 6px;
width: 200px;
height: 150px;
}
.frame img {
max-width: 100%;
}
.priceholder {
border: 1px solid black;
border-radius: 50%;
width: 74px;
height: 74px;
position: absolute;
box-sizing: border-box;
margin-left: -37px;
margin-top: -43px;
padding: 10px 0 0 10px;
}
<div class="frame">
<p class="priceholder">$ 456</p>
<img src="http://i.imgur.com/daiSXVG.jpg" />
<p>This is a test description.</p>
</div>
Or see the updated Fiddle.
EDIT
To add this in a loop you could use something like this:
<?php
while( ...statement... ) {
echo '<div class="frame">';
echo ' <p class="priceholder">$ 456</p>';
echo ' <img src="http://i.imgur.com/daiSXVG.jpg" />';
echo ' <p>This is a test description.</p>';
echo '</div>';
}
?>

Related

How do I make a dynamic background image opaque in React?

I have blog posts each with a cover image. I want to use these images in my post listing page as an experiment to see if it will liven up the page a little bit.
The images are too bright. I either want the opacity lowered or an overlay must be added.
Changing the opacity will change the opacity of the text. Adding an overlay has positioned itself on top of the anchors rendering them useless.
I am using the Gatsby Casper Starter Kit in the event that you are interested.
PostListing.jsx
...
<PostFormatting className={className} key={title} cover={cover}>
<PostHeader>
<h2 className="post-title">
<Link to={path}>{title}</Link>
</h2>
...
PostFormatting.jsx
...
const style = cover ? { backgroundImage: `url(${cover})` } : {};
return <article className={className} style={style}>{children}</article>;
...
Generated HTML
<article class="post" style="background-image: url("https://picsum.photos/1280/500/?image=800");">
<header class="post-header">
<h2 class="post-title">
Test Post
</h2>
</header>
<section class="post-meta">
<span>
<span class="tag">Mindset</span>
<span class="tag">Productivity</span>
</span>
<time class="post-date" datetime="2017-06-27">27 June 2017</time>
</section>
<section class="post-excerpt"><p>...</p></section>
</article>
CSS
All the styles I have for the post element.
<element.style> {
background-image: url(https://picsum.photos/1280/500/?image=800);
}
.home-template .content .post,
.tag-template .content .post {
background-color: rgba(0, 0, 0, .1);
padding: 30px 50px 50px 50px;
}
.post {
position: relative;
width: 80%;
max-width: 710px;
margin: 4rem auto 0em auto;
padding-bottom: 4rem;
border-bottom: #1a232c 3px solid;
word-wrap: break-word;
}
I know about this method but I don't know how to get the image into the after pseudo element.
div {
width: 200px;
height: 200px;
display: block;
position: relative;
}
div::after {
content: "";
background: url(image.jpg);
opacity: 0.5;
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
Note: Try Radium for pseudo classes
U will have to put the overlay as a sibling of the content, and make it absolute.And also u will have to increase the z-index of the the content, so that it can be interactive.
In your case, all the elements inside the article should be grouped and put inside the content class.
Try this
.parent{
height:300px;
padding:50px;
position:relative;
}
.overlay{
position:absolute;
top:0;right:0;left:0;bottom:0;
background-color:rgba(0,0,0,0.5);
z-index:0;
}
.content{
position:relative;
z-index:1;
font-size:25px;
color:white;
}
<div class="parent" style="background-image:url(http://via.placeholder.com/350x150)">
<div class="overlay"></div>
<div class="content">Test Test</div>
</div>

CSS img insert on a:hover:after breaks if two lines

My goal is to have an image show to the right of an h3 when a user hovers over it. My code works if the h3 is one line, but it breaks if h3 becomes two or more lines because the image shows still shows immediately after the text instead of after the whole block.
I am working on a WordPress Template.
I added the absolute positioning to a:hover:after because otherwise the insert of the image shifted the whole h3 element.
<div class="col-sm-10 offset-sm-1">
<div class="blog-entry">
<?php
the_title( '<h3 class="blog-entry-title">🥑 <a href="' . esc_url(
get_permalink() ) . '" rel="bookmark">', '</a></h3>' );
?>
</div>
</div>
Here is my CSS
.blog-entry-title {
min-height: 75px;
}
.blog-entry-title a {
color: #232323;
padding: 15px 90px 15px 15px;
}
.blog-entry-title a:hover {
color: #66ccff;
}
.blog-entry-title a:hover:after {
content:url('https://example/color-burst.png');
position: absolute;
top: -10px;
}
Try with background-image instead of content:url. You can adjust the background-position instead of moving top the img element generated.
I'm using position:absolute as well, but only to set the width and height, so elements stays at the end of the line.
.wrapper {
width: 250px;
}
.blog-entry-title {
min-height: 75px;
}
.blog-entry-title a {
color: #232323;
padding: 15px 90px 15px 15px;
}
.blog-entry-title a:hover {
color: #66ccff;
}
.blog-entry-title a:hover:after {
content: '';
background: url("https://upload.wikimedia.org/wikipedia/commons/f/fe/Farm-Fresh_bullet_error.png");
background-repeat: no-repeat;
background-position: 50% 40%;
position: absolute;
width: 16px;
height: 16px;
}
<div class="wrapper">
<div class="blog-entry">
<h3 class="blog-entry-title">Hello world</h3>
</div>
<div class="blog-entry">
<h3 class="blog-entry-title">The Best Guacamole Recipe in the World</h3>
</div>
<div class="blog-entry">
<h3 class="blog-entry-title">Goodbye world</h3>
</div>
</div>
</div>

CSS Positioning with Text and Images on Same Line

How to I align my text and image on the same line?
Whenever I used padding or margins it crashes into the circle image I'm using.
#alignPhoto {
padding-right: 50px;
padding-left: 400px;
}
#alignCompany {
margin-left: 240px
}
#alignImage {
position: relative;
bottom: -250px;
}
.wrapper {
background: #C3C3C3;
padding: 20px;
font-size: 40px;
font-family: 'Helvetica';
text-align: center;
position: relative;
width: 600px;
}
.wrapper:after {
content: "";
width: 200px;
height: 0;
border-top: 42px solid transparent;
border-bottom: 42px solid transparent;
border-right: 40px solid white;
position: absolute;
right: 0;
top: 0;
}
<div id="alignPhoto">
<div class="circle" id=image role="image">
<img src="http://placehold.it/42x42">
</div>
</div>
<div id=alignPhoto class="titleBoldText">Mary Smith</div>
<div id=alignCompany class="titleText">Morris Realty and Investments</div>
<br>
Currently It does this:
My desired effect is this:
Any help or suggestions would be greatly appreciated.
You're making it a little more complicated than it needs to be. Just put two elements as wrappers (one you already have in alignImage, set them to display as inline-block and then put the vertical-align to middle, top, or whatever you like. I got rid of all the bizarre padding, which was messing with the display as well. Looks like that was a holdover from your vertically stacked layout.
Edit – You've also got two elements with the ID alignPhoto. You really, really shouldn't do that. If you need to style two different elements with one rule, please use classes instead.
#alignPhoto {
display: inline-block;
vertical-align: middle;
}
#alignPhoto img {
border-radius: 100%;
}
#alignImage {
position: relative;
}
.alignText {
display: inline-block;
vertical-align: middle;
}
.titleBoldText { text-align: right; }
<div class="alignText">
<div class="titleBoldText">Mary Smith</div>
<div id=alignCompany class="titleText">Morris Realty and Investments</div>
</div>
<div id="alignPhoto">
<div class="circle" id=image role="image">
<img src="http://placehold.it/42x42">
</div>
</div>
<br>
One quick and dirty way to wrap it in a table, as to get your vertical align working without any problems as well.
<table>
<tr>
<td>
<div id="alignPhoto" class="titleBoldText">Mary Smith</div>
<div id="alignCompany" class="titleText">Morris Realty and Investments</div>
</td>
<td>
<img src="image/url" alt=""/>
</td>
</tr>
</table>
http://jsfiddle.net/7m5s6gd7/
What about slightly simpler version:
HTML:
<div id="alignPhoto">
<div class="content-wrapper">
<p>Mary Smith</p>
<p>Morris Realty and Investments</p>
</div>
<div class="image-wrapper" id="image" role="image">
<img src="http://placehold.it/250x200" />
</div>
</div>
CSS:
.content-wrapper { float:left; }
.image-wrapper img { border-radius:50%; }
#alignPhoto {
display: flex;
flex-direction: row;
align-items: center;
}
JSFiddle for that
Basically you keep both paragraphs of text in one holding div and float it to left. This alone should do the job.
EDIT:
To make it even simpler, you can use flexbox for vertical alignment.
I've updated the answer.
One of the more effective and scalable solutions to ensuring elements are placed correctly from left to right are to employ wrapper divs with clear:both;. Inside of these wrapper divs you can use float:left or float:right. The wrapper divs allow you to generate a new "row".
#alignPhoto {
float: left;
margin-left: 10px;
}
#profileCompany, #profileName {
display:block;
width:100%;
}
#alignImage {
float: left;
}
.profileWrapper {
float:left;
}
/* Below creates a circle for the image passed from the backend */
.wrapper {
padding: 20px;
font-family: 'Helvetica';
text-align: center;
position: relative;
width: 600px;
clear: both;
}
.profileWrapper:after {
content: "";
width: 200px;
height: 0;
border-top: 42px solid transparent;
border-bottom: 42px solid transparent;
border-right: 40px solid white;
/* Tweak this to increase triangles height */
position: absolute;
right: 0;
top: 0;
}
.circle {
display: block;
height: 50px;
width: 50px;
background-color: #cfcfcf;
-moz-border-radius: 25px;
-webkit-border-radius: 25px;
-khtml-border-radius: 25px;
border-radius: 25px;
}
<div class="wrapper">
<div class="profileWrapper">
<div id=profileName class="titleBoldText">Mary Smith</div>
<div id=profileCompany class="titleText">Morris Realty and Investments</div>
</div>
<div id="alignPhoto">
<div class="circle" id=image role="image">
</div>
</div>
</div>

Position link relative to image inside a div with fixed dimensions, keeping that image fit and vertically or horizontally aligned inside that div

I have a div with an image and a hidden button that shows up when the image is being hovered.
However, I don't know how to position the button relative to the top and right of the image instead of the div...
And here is the fiddle of what I have so far: http://jsfiddle.net/ab3vg97t/
HTML:
<div class="placeholder-container">
<a href=<?= "index.php?page=viewPoll&id=".$currentPoll['idPoll']."&previous=My+polls"; ?>>
<img class="resize-to-fit-and-center placeholder-containter-img" src="<?= $currentPoll['image'] != '' ? UPLOADS_URL . "/" . $currentPoll['image'] : 'assets/img/default-poll.png' ?>" alt="Default poll image">
</a>
<a id="my-poll-delete-btn" href=<?="actions.php?action=deletePoll&id=".$currentPoll['idPoll']?>>
<i class="glyphicon glyphicon-remove"></i>
</a>
</div>
CSS:
.placeholder-container {
width: 200px;
height: 200px;
line-height: 200px;
text-align: center;
display: inline-block;
position: relative;
}
.placeholder-container:hover #my-poll-delete-btn {
display: flex;
}
.placeholder-containter-img {
border-radius: 10px;
}
.placeholder-containter-img:hover {
box-shadow: 0 0 1em gray;
}
#my-poll-delete-btn {
display: none;
text-decoration: none;
position: absolute;
top: 10px;
right: 10px;
}
#my-poll-delete-btn:hover {
color: red;
}
.resize-to-fit-and-center {
max-width:100%;
max-height:100%;
vertical-align: middle;
}

How can I center an image between two other images in this example?

I have a div 'imgcontainer' and a div 'commentcontainer'. Representing a large image and a white box filled with comments below. I want to add a third image that sits on top of the two, vertically centered.
So basically what I'm trying to do is this: https://d13yacurqjgara.cloudfront.net/users/488816/screenshots/1726057/1_1x.jpg
My problem is that I can't figure out how to integrate the CSS to do this into my code below. What can I add to the 'profilepic' div in order to set that image to sit midway between the imgcontainer and commentcontainer ?
HTML
<div id="timeline">
<div class="block2x3 block">
<div class="imgcontainer">
<img src="top-picture.jpg" />
<div class="profilepic">
<img src="facepic.png" />
</div>
</div>
<div class="commentcontainer">
<div class="peoples">
<strong class="people name">Joe Schmoe</strong> and 42 other people bought this
<p>Have commented on your wall post</p>
</div>
</div>
</div>
CSS3
#media (max-width:1860px) {
#timeline .block .imgcontainer img {width:100%;}
#timeline div[class*="block"][class*="x1"] {height:150px;}
#timeline div[class*="block"][class*="x2"] {height:300px;}
#timeline div[class*="block"][class*="x3"] {height:450px;}
#timeline div[class*="block2x3"] .imgcontainer {height:60%; position:relative; z-index:1;}
#timeline div[class*="block2x3"] .commentcontainer {height:40%; z-index:2;}
.profilepic {
margin: 40px 0px 0px 0px;
border: 7px solid white;
border-radius: 70px;
}
On your CSS to center the 'profile' pic you can add this to you CSS:
.profilepic {
/*margin: 40px 0px 0px 0px; INSTED OF THIS */
margin-left: auto; /* USE */
margin-right: auto; /* THIS */
border: 7px solid white;
border-radius: 70px;
}
but first you will have to add this to you css file:
html { width: 100%; height: 100%; margin: 0; padding: 0; }
body { width: 100%; height: 100%; margin: 0; padding: 0; }
Hope it works :')

Resources