How to move image in div with CSS? - html

I have an image located inside a div, I am trying to move it 50 px down and 50 px left in order to have everything complete. But I am not sure how to edit the image in the CSS since I don't know what code to put in to connect the photo to the css.
My code:
#OverviewText4 img:MoneyIcon.png {
width: 150px;
height: 150px;
position: absolute;
top: 50px;
left: 50px;
}
<div id="OverviewText4">
<img src="MoneyIcon.png" />
</div>
Thanks for helping

Remove the image name from your declaration and make sure your container is set to position: relative so that your image is absolutely positioned against the right containing element in this instance #OverviewText4
#OverviewText4 {
position: relative;
}
#OverviewText4 img {
width: 150px;
height: 150px;
position: absolute;
top: 50px;
left: 50px;
}

You have to add position:relative to parent <div> and then add position: absolute; to the <img>. Like this:
#OverviewText4{
position: relative;
}
#OverviewText4 img{
width: 150px;
height: 150px;
position: absolute;
top: 50px;
left: 50px;
}
<div id="OverviewText4">
<img src="MoneyIcon.png" />
</div>

There are many ways to do this in CSS as per the multitude of answers. If I might suggest, since the image name in your example is related to iconography a slightly different approach:
#OverviewText4 {
position: relative;
}
#OverviewText4:before {
content: "";
background: transparent url(MoneyIcon.png) scroll no-repeat 0 0;
background-size: cover;
width: 150px;
height: 150px;
position: absolute;
display: block;
top: 50px;
left: 50px;
}
https://jsfiddle.net/zk8su1qw/
This way you don't even need an img tag in the HTML, which is desirable if its just presentational.
There is also an assumption in this answer that you want the image displayed over the top of any content in the OverviewText4 div, rather than having content flow around the image. If this is not the case you would want to use margins and keep the image position: static or relative.

Right, your CSS is fine but your selector is not. I think this is what you were going for.
#OverviewText4 img[src="MoneyIcon.png"] {
width: 150px;
height: 150px;
position: absolute;
top: 50px;
left: 50px;
}
<div id="OverviewText4">
<img src="MoneyIcon.png" />
</div>
I've changed img:MoneyIcon.png (which doesn't mean anything to CSS) to img[src="MoneyIcon.png"] which means an img tag where the src = MoneyIcon.png
The main problem here is if you change the src you have to change your CSS also, I'd recommend having a class like this:
#OverviewText4 img.money-icon {
width: 150px;
height: 150px;
position: absolute;
top: 50px;
left: 50px;
}
<div id="OverviewText4">
<img class="money-icon" src="http://placehold.it/150x150" />
</div>
I hope you find this helpful.

You can simpy do this with padding
#OverviewText4 img {
padding:50px 0 0 50px;
}

Use the marginattribute for creating a margin around an element. You can also use padding on the div element.
Try it like this:
#OverviewText4 img: MoneyIcon.png{
width: 150px;
height: 150px;
position: absolute;
margin-top: 50px;
margin-left: 50px;
}

You can link an image to a CSS class by adding the class name inside the tag <img>
Working Example:
body {
background: #111
}
.OverviewText4 {
width: 150px;
height: 150px;
position: absolute;
top: 50px;
left: 50px;
}
<body>
<img src="MoneyIcon.png" class="OverviewText4" />
</body>

If I understand your question correctly all you have to do is add this style to your div where the image is located.
div > img {
margin-top: 50px;
margin-left: 50px;
}

Related

Placing many images on top of another image with CSS

Beginner in CSS here.
Basically, what I am trying to do is to place check marks or X-es on top of a country map and I am trying to find the best way to do this.(open to learn JS for this)
So far, I have placed my map in a div and centered it, with HTML code <img src="check mark"> after the map image.
I will do this for every check mark i have to add, but is it there any better solution ?
.container {
margin-left: 10%;
width: 75%;
height: 80%;
display: flex;
justify-content: center;
}
.child {
position: relative;
margin: 0 auto;
}
.check {
position: absolute;
top: 300px;
right: 500px;
}
<div class="container">
<div class="child">
<img src="Map_image.png">
</div>
</div>
This is an example of what i want to achieve:
https://imgur.com/a/mu5WpuN
Short answer is create a wrapper div with position: relative and place the map and the Xes inside it. Then make map fit with the wrapper (i.e. 100% width and height or whatever) then make all Xes position: absolute and position them accordingly using top: left: right: bottom: properties
Here's a working sample. Try to run it.
.wrapper {
position: relative;
width: 100%;
}
img.map {
width: 100%;
}
img.marker {
position: absolute;
width: 20px;
}
.marker.x1 {
top: 20px;
left: 50px;
}
.marker.x2 {
top: 50px;
left: 190px;
}
<div class="wrapper">
<img class="map" src="https://www.onlygfx.com/wp-content/uploads/2015/12/world-map-vector.png" alt="map">
<img class="marker x1" src="https://i.pinimg.com/474x/b1/7e/59/b17e59bc32383f7878c9132081f37c60.jpg" alt="x1">
<img class="marker x2" src="https://i.pinimg.com/474x/b1/7e/59/b17e59bc32383f7878c9132081f37c60.jpg" alt="x1">
</div>

Responsive percentage padding of fixed parent

I'm trying to get a child DIV to have its padding set relative to its fixed parent DIV.
To demonstrate the problem, I've put together a quick JSFiddle - http://jsfiddle.net/mdxsegLt/
.top-fixed {
position: fixed;
top: 0px;
height: 70px;
max-height: 12.5%;
margin-bottom: 20px;
width: 100%;
left: auto;
right: auto;
z-index: 1030;
background-color: green;
}
.padding-percentage {
position: relative;
width: 50px;
max-width: 30%;
/*padding: 14px;*/
padding-top: 20%;
background-color: red;
}
<div class="top-fixed">
<div class="padding-percentage">test</div>
</div>
In that example, I'd like the red DIV to be contained entirely within the green, using 20% of the green DIVs height for the padding, not the entire page.
First things first... lets drop the ALL CAPS element names. It's bad practice these days. I think you were overdoing it a bit with your CSS declarations and by NOT nesting the text properly by wrapping it in a span or p tag. You shouldn't just have floating text that's not wrapped in a p or span tag. Wrapping it in such tags allows you to further customize like I did in my fiddle.
UPDATE
I think I actually get what you're trying to do.
CSS:
.top-fixed {
position: fixed;
top: 0px;
height: 70px;
width: 100%;
background-color: green;
}
.padding-percentage {
height: 100%;
width: 50px;
background-color: red;
}
.padding {
margin-top: 0px;
padding-top: 20px;
}
HTML:
<div class="top-fixed">
<div class="padding-percentage">
<p class="padding">TEST</p>
</div>
</div>
LINK TO FIDDLE
Looks like you needed to adjust the CSS and add a class to the "test" text by tossing it an a p tag.
Check out my fiddle and hope it helps you!

getting auto-width to work with absolute position from left

I'm trying to apply absolute position on an error label elemennt, which is inside an input field that is also positioned absolutely. The problem is that auto-width on the error element won't apply correctly, and will break after the first word. Why is that happening? If I use position right instead of left, it seems to work fine. Here's a jsfiddle link: http://jsfiddle.net/u793ata5/
Here's the HTML code:
<div id="outside">
<div id="inside">
<label class="error">Show this error on the side</label>
</div>
</div>
And CSS:
#outside {
position: relative;
width: 250px;
height: 250px;
}
#inside {
position: absolute;
top: 30%;
height: 30px;
left: 40%;
width: 80%;
}
.error {
width: auto;
position: absolute;
left: 90%;
top: 10%;
background-color: red;
color: white;
}
Why so many absolutely positioned elements? Maybe I'm not understanding what you want the layout to look like--and maybe you could clarify--but this modified fiddle looks more reasonable to me.
http://jsfiddle.net/u793ata5/3/
.error {
background-color: red;
display: block;
margin-left: 50%;
color: white;
}
I try not to use position: absolute unless I...uh absolutely have to.
You're putting it's position at 90% from the left. This means it only has 10% of the parent width to place text before wrapping. Try using
float: right;
instead of
left: 90%;

Center a search bar in a container div that contains many other divs

I have a container div, that contains many other divs, and in one of the divs, a search bar. Hence the html looks like this:
<div id="header-middle">
<div id="header-search">
<form>
<input type="text">
</form>
</div>
<div id="header-middle-left">
</div>
<div id="header-transition">
</div>
<div id="header-middle-right">
</div>
</div>
the css I have for it is:
#header-middle{
width: 400px;
height: 64px;
float: left;
}
#header-search{
position: absolute;
left: 50%;
top: 50%;
margin-top: -32px;
margin-left: -200px;
}
#header-middle-left{
width: 241px;
height: 64px;
float: left;
background-image:url('foo');
background-repeat:repeat-x;
}
#header-transition{
width: 19px;
height: 64px;
float:left;
background-image:url('foo2');
}
#header-middle-right{
width: 140px;
height: 64px;
float:left;
background-image:url('foo3');
background-repeat:repeat-x;
}
the divs mainly just are there for style, they use images as backgrounds, and so theres no harm in the search bar covering them. However, I need it to be centered in the containing div, and cover small parts of the rest of them. Any help would be very appreciated. Also any advice about css in general would be appreciated.
You should edit your CSS to achieve this, change #header-middle and #header-search to use the following code :
#header-middle {
width: 400px;
height: 64px;
position: relative;
}
#header-search {
position: absolute;
left: 50%;
top: 50%;
margin-top: -11px;
margin-left: -75px;
width: 150px;
height: 22px;
}
Here is a DEMO.
You can use position:relative for #header-search to make its position relative to the container div #header-middle.
For centering you can use exact pixels if the width & height of the search box and the container div is static. This is not the best way but it should work. If they are dynamic than you can calculate with javascript.
http://jsfiddle.net/rWNkj/

IE7 - absolute positioning offset different to Firefox?

I'm tidying up another developer's work who seems to have done a shoddy job with the CSS.
There is the main "wrapper" div on the page, and inside this is a logo and images for the navigation. The images are using "position: absolute" and using the CSS "top" property to offset them. However, Firefox and IE seem to start their offset from a different point, meaning the logo is about 100px above where it should be in IE.
Is this an IE CSS bug or known thing?
Example in question: http://barry.cityjoin.com/mccamb/
If you want to position elements absolutely within a wrapper using top, right, bottom and/or left, the position of the wrapper has to be set as relative explicitly. Otherwise the absolute elements will get positioned within the view port instead.
A little working example:
<style>
.wrapper
{
position: relative;
height: 100px;
width: 800px;
}
.absoluteLogo
{
position: absolute;
top: 10px;
left: 10px;
height: 60px;
width: 80px;
}
.absoluteElement
{
position: absolute;
top: 80px;
left: 320px;
height: 20px;
width: 80px;
}
</style>
<div class="wrapper">
<div class="absoluteLogo">Logo</div>
<div class="absoluteElement">Element</div>
</div>
Another possibility would be to position the absolute elements using margins:
<style>
.wrapper
{
height: 100px;
width: 800px;
}
.absoluteLogo
{
position: absolute;
margin: 10px 0 0 10px;
height: 60px;
width: 80px;
}
.absoluteElement
{
position: absolute;
margin: 80px 0 0 320px;
height: 20px;
width: 80px;
}
</style>
<div class="wrapper">
<div class="absoluteLogo">Logo</div>
<div class="absoluteElement">Element</div>
</div>
The result is the same and should be working across all browsers.