HTML/CSS: Relative/Absolute Positioning - html

How do I lay an image (such as a circle) over a different image so that it always stays in the right place, regardless of image or browser resizing? Is there a way I can do this with divs?
**Update: Thank you for your help. I have attached some images of relevant html and css to illustrate what I'm trying to do. I'm hoping to get the circles to surround one person's face, even though the image itself resizes with the browser. Thanks!
Webpage Image HTML CSS

One way I would recommend would be to use position:relative on your circle image, add it after the image you want in on top of, and set the left value to -outerWidth of 1st image. You should also put both of these in a span and add css to prevent line break as well

The code below gives examples of circles that remain within their parent square, and can be positioned in proportion to their parent squares. I've created some CSS so that you can add a class to various divs to change their size, etc.
N.B. You cannot apply .large or .small to the circles, their size is proportional to their parent div, although this does not have to be the case.
.square {
width: 100px;
height: 100px;
background: blue;
text-align: center;
margin: 5px;
float: left;
}
.large {
height: 200px;
width: 200px;
}
.small {
height: 50px;
width: 50px;
}
.circle {
position: relative;
border-radius: 50%;
background: red;
color: white;
}
.center {
left: 25%;
top: 25%;
width: 50%;
height: 50%;
}
.left-center {
left: 0;
top: 25%;
width: 50%;
height: 50%;
}
.right-center {
left: 50%;
top: 25%;
width: 50%;
height: 50%;
}
<div class="square small">
<div class="circle left-center">
</div>
</div>
<div class="square">
<div class="circle center">
</div>
</div>
<div class="square large">
<div class="circle right-center">
</div>
</div>

Related

How to randomly place elements on a web page

I need to make an art gallery for my website and I want to take an asymmetric approach by placing the artwork and descriptions randomly across the page with no symmetry. It should look a bit like this, with the colored squares being the divs containing each image and description:
Keep in mind that I also want to include text in each div, so it's not just the images. The max-width of my container is 1504 pixels. Resizing isn't necessary because I have the min-width set to 1504 as well. I don't know much about CSS or HTML as I'm doing this for a school thing. How do I go about making this?
If you don't mind manually coding the positions, you can simply use absolute positioning. Essentially, you'll need to:
Give your container (which might just be the body) a constant width and height. I arbitrarily chose 400x400 in the example.
Add position: relative to your container to allow the content boxes to be positioned relative to the container. This is not necessary if your container is the body.
Add position: absolute to all your content boxes (the .box elements in my example).
Use the top, bottom, left, and/or right properties to position your content boxes as desired.
Here's a (miniature) example:
.container {
/* You can set this to whatever you wish. */
width: 400px;
/* You'll need to set a height. */
height: 400px;
/* Allows positioning relative to this container. */
position: relative;
/* For clarity. */
border: 1px solid black;
}
.box {
position: absolute;
}
.box#one {
width: 90px;
height: 60px;
background: red;
top: 20px;
left: 60px;
}
.box#two {
width: 90px;
height: 100px;
background: blue;
top: 50px;
right: 100px;
}
.box#three {
width: 67px;
height: 76px;
background: purple;
bottom: 100px;
left: 278px;
}
.box#four {
width: 150px;
height: 160px;
background: yellow;
top: 203px;
left: 100px;
}
.box#five {
width: 70px;
height: 50px;
background: orange;
top: 110px;
left: 45px;
}
<div class="container">
<div class="box" id="one">
<!-- You can put your content in here. -->
</div>
<div class="box" id="two">
<!-- You can put your content in here. -->
</div>
<div class="box" id="three">
<!-- You can put your content in here. -->
</div>
<div class="box" id="four">
<!-- You can put your content in here. -->
</div>
<div class="box" id="five">
<!-- You can put your content in here. -->
</div>
</div>

Wouldn't work to overlay two images in Mail HTML

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;
}

div positioning: absolute, relative, etc

I have pure CSS image slider which I want to have positioned (margin:auto) with text underneath. Slider images are absolutely positioned as they are stacked. I can't figure out how to position divs around it all. I have content and wrapper divs with relative position. Image size should be responsive (therefore max-width:100%) but wrapper or content divs can be exact size. Or maybe they don't need to either?
This is what I am after:
And this is what I managed so far: www.jsfiddle.net/1qxxnxbf/1/
If your image slider is a carousel, you can't make it responsive without js. If you give your slider a height in the css, you can adjust it in the js to make it responsive.
The only other thing you can do is maintain an aspect ratio. So in your example you have 350x220 images. so If you get your padding-bottom on your .slider class to 62.857% (roughly 220/350) you get a variable height based on the width. If your width grows/shrinks, the height will grow/shrink as well.
http://jsfiddle.net/1qxxnxbf/2/
Edit: I just noticed that none of your code around the slider is responsive. Why are you trying to make the slider responsive?
Checkout this design
https://jsfiddle.net/jalayoza/zvy87dcv/9/
HTML code
<div class="content">content
<div class="wrapper">wrapper
<div class="slider">
<img src="https://placeimg.com/350/220/any" class="slide" alt="slide1">
<img src="https://placeimg.com/350/220/nature" class="slide" alt="slide2">
<img src="https://placeimg.com/350/220/abstract" class="slide" alt="slide3">
</div>
<!-- text should go underneath the image -->
<div class="text">
<div class="text_left">
left text
</div>
<div class="text_right">
right text
</div>
</div>
</div>
</div>
CSS code
.content {
width: 500px;
background: #fff;
margin: auto;
}
.wrapper {
max-width: 400px;
position: relative;
background: purple;
margin: auto;
padding:10px;
}
.slider {
margin: auto;
left: 0;
right: 0;
max-width: 100%;
position: relative;
padding-bottom: 62.857%;
}
.slide {
max-width: 400px;
margin: auto;
position: absolute;
opacity: 0.5;
width: 100%;
}
.text {
max-width: 100%;
position: absolute;
background: transperant;
opacity: 0.9;
bottom:10px;
width: 95%;
}
.text_left {
max-width: 50%;
background: #fff;
float: left;
text-align: left;
padding:5px;
}
.text_right {
max-width: 50%;
background: #fff;
float: right;
text-align: right;
padding:5px;
}
Hope you will like this design

Child div height determines parent height?

I have a parent div that contains two children, side by side. The first child is an image that must be height 100% and 58% width, margin auto and overflow hidden. The second child contains text, and the length of the text determines the height of the parent. This is a template for several pages, with different length of text, and therefore different parent height. Is it possible to do what I'm trying to do without using JS? Thanks for your input! Code below.
HTML:
<div id="product-summary">
<div class="product-image-container">
<img />
</div>
<div id="product-details">
<h3 class="product-title"></h3>
<div class="product-description"></div>
</div>
</div>
CSS:
.product-image-container {
position: absolute;
top: 0;
left: 0;
width: 58%;
height: 100%;
overflow: hidden;
img {
position: absolute;
top: 0;
left: 50%;
margin: auto;
transform: translateX(-50%);
min-width: 100%;
height: 100%;
}
}
#product-details {
float: right;
border: solid thin #777;
height: ~"calc(100% - 2px)";
width: 41%;
text-align: center;
}
The problem is your #product-details is floated, which creates a new BFM (block formatting context), and the parent gets collapsed.
I suggest you read more about BFMs here: http://yuiblog.com/blog/2010/05/19/css-101-block-formatting-contexts/
There are several ways to fix this:
You could clear the parent, a way to do that is by adding overflow: hidden; to the #product-summary element.
You could remove the float: right from #product-details, and use flexbox to align it instead.
I don't know any preprocessor wizardry, but using inline-block works good, as well as keeping positioned absolute elements wrapped in a relative parent for control. It wasn't mentioned how the image is displayed, so I assume aspect ratio unchanged and no cropping.
SNIPPET
.product-image-container {
display: inline-block;
position: relative;
top: 0;
left: 0;
right: 0;
width: 58%;
height: 100vh;
overflow: hidden;
}
img {
display: inline-block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: auto;
}
#product-details {
float: right;
border: 1px solid #777;
height: 100%;
width: 41%;
text-align: center;
}
a {
margin-left: 50%;
}
<div id="product-summary">
<div class="product-image-container">
<img src='https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png'>
</div>
<div id="product-details">
<h3 class="product-title">Lena Söderberg</h3>
<div class="product-description">
<blockquote>Lenna or Lena is the name given to a standard test image widely used in the field of image processing since 1973. It is a picture of Lena Söderberg, shot by photographer Dwight Hooker, cropped from the centerfold of the November 1972 issue of Playboy
magazine.
</blockquote>
<a href='https://en.wikipedia.org/wiki/Lenna'>Wikipedia</a>
</div>
</div>
</div>

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/