I know this may seem like a newby question, but is it possible to set an image element as a parent? If so, how can I do so?
Heres an example of what I'm looking for:
Also, the reason I can't just have the div element as the parent is that I want that text element relative to that image element, not the div element. This way I can center the text relative to the image. Thanks in advance for any help!
Looks like I've figured it out. I started messing with some values and I got it.
HTML:
<div id="main-div">
<img id="image" src="url.com/image.png">
<h1 id="text">Caption</h1>
</div>
CSS:
#main-div {
position: absolute;
top: 30px;
left: 50%;
margin: 0px auto;
transform: translate(-50%, 0);
}
#image {
position: absolute;
top: 130px;
left: 50%;
margin: 0px auto;
transform: translate(-50%, 0);
color: white;
font-family: "Roboto";
text-align: center;
}
#text {
position: absolute;
top: 30px;
left: 15%;
margin: 0px auto;
transform: translate(-50%, 0);
display: block;
border: thin red solid;
}
Here's a JS Fiddle of the code and final result: https://jsfiddle.net/k3b70Lg7/
No you cannot because image elements are replaced.
You can however wrap the image and the text element in another div and position that.
<div class="outer-black">
<div class="inner-image-text-wrapper">
<img src="..">
<div class="text"></div>
</div>
</div>
Now setting the wrapper to be inline-text means that it will expand according to contents (the image) and by setting it also to be position:relative you can position the text (position:absolute) wherever you want inside it.
Full example
.outer-black {
background: black;
padding: 2em;
}
.inner-image-text-wrapper{
position:relative;
display:inline-block;
}
.inner-image-text-wrapper img{display:block;}
.inner-image-text-wrapper .text{
position:absolute;
top:105%;
left:5%;
right:5%;
text-align:center;
background:rgba(255,255,255,0.5);
}
<div class="outer-black">
<div class="inner-image-text-wrapper">
<img src="http://lorempicsum.com/simpsons/300/200/1">
<div class="text">image caption</div>
</div>
</div>
Related
I'm trying to put two images on top of each other in the HTML of an email, but it fails. It displays fine in normal HTML, but when it comes to the email layout, it collapses.
code
<td className="icon">
<img class="block" src='./img/b.png' />
<img src='./img/a.png' />
</td>
<style>
.block {
margin-bottom: -15px;
margin-left: -40px;
}
</style>
margin isn't working totally.
Do you have any ideas in MAIL HTML?
ideal:
issue:
I am not sure, but I think your "td" with the icon class should have a bigger width in your layout. So the margin of -40px does not work right. I guess you can try hardcode the icon width, increase the negative margin value or position your images as absolute within your container.
I also leave this "logo" draw with CSS below. I hope it can help you a little. (You can change the width and height of the container for your needs).
HTML
<div class="circles-container">
<div class="circle circle1"></div>
<div class="circle circle2"></div>
</div>
CSS
.circles-container{
position: relative;
display: flex;
width: 200px;
height: 100px;
background-color: transparent;
}
.circle{
position: absolute;
top:0;
width: 50%;
height: 100%;
border-radius: 50%;
transform: translateX(-50%);
}
.circle1{
left: 33%;
background-color: #3484b9;
}
.circle2{
left: 66%;
background-color: #ffd61e;
}
I was looking for a solution where I have text on top of an image.
I found solutions similar to this:
<div>
<div class="centered" style="position:absolute; top:50%; left:50%; transform:translate(-50%, -50%);">
No Events found
</div>
<img src="https://www.steelmint.com/nw/public/images/events-01.svg" alt="Snow" style="width:100%;
position:relative; text-align: center; opacity:0.4;">
</div>
Now I realized due to the position-style my text will be displaced if I for example
Zoom in/out
Change Window Height/Width
Switch to Smartphone-View
How is this usually solved so my text stays over my image?
My idea was to just create an image where the text is part of the image and then bind that image to my <img>- Tag. Is this a common way to do?
Simply use the class on the wrapping div and give it a position: relative; like in the sample below. Then let the div with the text span the entire parent div by using position: absolute; in combination with top, bottom, left and right: 0;.
For adding a opacity effect, use rgba as background color instead. It will not cause the same issues as opacity which is rendered last. Use a positive z-index to span the text above the image layer wise.
.text-image-wrapper {
width: 100%;
position: relative;
object-fit: contain;
}
.text-image-wrapper div {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
text-align: center;
background-color: rgba(255, 255, 255, 0.5);
z-index: 1;
display: flex;
justify-content: center;
align-items: center;
}
.text-image-wrapper img {
width: 100%;
}
<div class="text-image-wrapper">
<div>Some Exampel Text</div>
<img src="https://www.tacoshy.de/Images/Yoshi/IMAG0735.jpg">
</div>
Add z-index: 20; to the .centered div. Like this:
<div>
<div class="centered" style="position:absolute; top:50%; left:50%; transform:translate(-50%, -50%); z-index: 20;">
No Events found
</div>
<img src="https://www.steelmint.com/nw/public/images/events-01.svg" alt="Snow" style="width:100%;
position:relative; text-align: center; opacity:0.4;">
</div>
Codepen: https://codepen.io/manaskhandelwal1/pen/MWjqpwb
I'd like to be able to circle elements on a web page using only CSS. I have some code that is almost working - it produces a circle around the element but
the width does not match the width of the content (it is always too big), and
I cant seem to get it to center on the child
The following code is what I currently have
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>MWE</title>
</head>
<body>
<style>
div.ccc {
display: run-in;
position: relative;
}
div.ccc:after {
display: inline-block;
content: '';
position: absolute;
top: -50px;
right: -50px;
bottom: -50px;
left: -50px;
opacity: 0.7;
border: 5px solid red;
border-radius: 50%;
padding: 10px;
}
</style>
<div class="ccc">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg" width="10%">
</div>
<div class="ccc">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg">
</div>
</body>
which produces the following. Note that the circles are not centered on the images and the width's are off.
Is it possible to fix that using CSS only?
Set the div to display:inline-block and it will work.
Divs are block-level elements by default, which mean they'll take 100% the width.
edit: problem is that you're using % to size the image, which depends on the parent width... and we are trying to get the parent sized accordingly to the child... So that won't work.
Closest you can get, as far as I can tell, is avoid sizing your image on %, and display the div as inline-block
div.ccc {
position: relative;
display:inline-block;
}
div.ccc:after {
display: inline-block;
content: '';
position: absolute;
top: -50px;
right: -50px;
bottom: -50px;
left: -50px;
opacity: 0.7;
border: 5px solid red;
border-radius: 50%;
padding: 10px;
}
.small{
width:200px;
}
<div class="ccc">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg" class="small">
</div>
<div class="ccc">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg">
</div>
if you really need to size it as %, you'll need to add another container and size that one instead
div.ccc {
position: relative;
display:inline-block;
}
div.ccc:after {
display: inline-block;
content: '';
position: absolute;
top: -50px;
right: -50px;
bottom: -50px;
left: -50px;
opacity: 0.7;
border: 5px solid red;
border-radius: 50%;
padding: 10px;
}
.img-container{
display:inline-block;
}
.img-container img{width:100%}
.small{
width:200px;
}
<div class="ccc">
<div class="img-container small">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg">
</div>
</div>
<div class="ccc">
<div class="img-container">
<img src="https://beautifulenvironments.files.wordpress.com/2017/12/twinkly-lights.jpg">
</div>
</div>
The width of a div is 100% by default, that's the reason all your circles are massive and not centred on the child.
Why not just put the red circle border on the img tag instead of the div? I.e., put the circle on the child element in the first place.
Another option would be to get the div to match size of the content by setting display: inline-block on the .ccc class.
If neither of those is an option, I'm pretty sure there is no pure CSS way of doing it.
I have an image on which I want to overlay some white text. I'm trying to achieve the same thing video-sharing sites such as YouTube do with the video duration in the corner of the thumbnail. How can I do this with CSS/HTML?
Like This:
Try This :
.container {
width: 300px;
position: relative;
}
.container img {
width: 100%;
}
.container h3 {
background-color: rgba(0,0,0,0.3);
color: #fff;
position: absolute;
bottom: 0;
right: 2px;
}
<div class="container">
<img src="https://i.imgur.com/0s8kLb7.png">
<h3>Cute Animal</h3>
</div>
.main {
width:400px;
position:relative;
}
.picture {
width:100%;
}
.main p {
position:absolute;
bottom:0px;
right:0px;
color:#fff;
font-size:14px;
background:#999;
padding:3px;
z-index:99;
}
<div class="main">
<p>
Hello
</p>
<img class="picture" src="http://lorempixel.com/400/200/sports/" alt="">
</div>
You can use CSS for it.
There are two ways of doing it ~
position: absolute;
top: 200px;
left 200px;
So this one sets the position of an element to absolute and then you can specify the location in pixels but it would make it something which can't change it's position in response to other elements. It would make your element like a rectangle in Paint which you can move at freely at any place.
The Second one is recommended by me ~
margin-top: -200px;
This one is a dirty way of doing it but it is useful. You can pull thing upwards using this. If your text is on side of the text you can use margin-left as same. It depends on you which method you want to use and how much pixels do you want to specify.
In your case I could give mathematical expression for doing this ~
margin-top: -text_height;
margin-left: video_width - text_width;
Enjoy :D
Here you go :
HTML :
<div class="image">
<img src="http://lorempixel.com/400/400/sports/2" alt="" />
<h2><span>Some Text</span></h2></div><br/>
</div>
CSS :
.image {
position: relative;
width: 100%; /* for IE 6 */
}
h2 {
position: absolute;
top: 300px;
left: 0;
width: 100%;
}
h2 span {
color: white;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0); /* fallback color */
background: rgba(0, 0, 0, 0.4);
padding: 10px;
}
https://jsfiddle.net/emilvr/f03m3Lks/
You can play with top & left to set your desire location
This is very easy to do. I am sure you do not have enough knowledge of CSS. But any way I will tell you on this.
Your structure should be like below:
<div class="relative">
<img src="" />
<span class="absolute">text</span>
</div>
Then add css for this
.relative{float:left; position;relative;}
.absolute{position:absolute; bottom:0px; right:0px;}
Adjust position as needed.
Everything is explained in the title of this post. I'm trying to put an PNG image in the center bottom border of a div element.
.content_block {
border: ridge;
border-width: 1px;
border-color: #969696;
height: 200px;
width: 200px;
}
.content_block.orange {
background-image: linear-gradient(#FBB03B, #FF9933);
}
<div class="content_block orange"></div>
Here's an image of what I'm trying to do:
I searched the net for a way to that with CSS, and border-image and stuff, but nothing worked.
To achieve the effect of it being exactly in the middle of the border, you will have to include the border with the image by inheriting it, and making it invisible. Like this, you can 'calculate' with it.
See this Fiddle for the effect. In this Fiddle, I've created a pseudo element that has a background-image of a play button.
The CSS that does the trick is this:
div::after{
content: '';
position: absolute;
top: 100%;
left: 50%;
width: 30px;
height: 30px;
background-image: url('http://www.iconsdb.com/icons/preview/gray/video-play-3-xxl.png');
background-size: cover;
background-repeat: no-repeat;
transform: translate(-50%, -50%);
border-top: inherit;
border-top-color: transparent;
}
I've placed it to the absolute bottom and 50% from the left. Then with the transform property, I shifted it to be centered around these points (50% from the left, and 100% from the top);
Then to make it move along with the border, I inherited only at the top, and made it invisible.
.content-block {
position: relative;
width: 200px; height: 100px;
border: 1px solid #f0f;
}
.content-block img{
position: absolute;
left: 50%; bottom: 0;
width: 50px; height: 50px; margin: -25px;
}
<div class="content-block">
<img src="http://placehold.it/50x50" alt="">
</div>
If you have a relative positioned parent, you can manipulate the position of an inner child using position:absolute;
Add an img in html
<div class="content_block orange">
<img class='element' src='https://cdn0.iconfinder.com/data/icons/form-elements-kit/100/checked-green-rounded-01-128.png'/>
</div>
Add this to your css.
.element { width:32px;
height:33px;
display:block;
background-color:grey;
margin-left:auto;
margin-right:auto;
margin-top:185px;
border-radius:100%;
}
Hope that helps!
Put an image inside the orange div and add text-align:center to the div
<div class="content_block orange">
<img src="" height="30" width="30">
</div>
and then set margin-top to the img. Check this Fiddle