How to randomly place elements on a web page - html

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>

Related

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

HTML/CSS: Relative/Absolute Positioning

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>

Top aligning something with Css

I'm just starting to look at html/css and having some problems getting my head around how the layout engine works.
I'm from a c# background and anything with hardcoded widths/heights feels like something is wrong. One of the first things I discovered is that you can't make divs expand to fill their available height in a sensible way, so I've been using tables.
The UI I want is basically a grid, which is the main part of the page with a side panel. The idea is to navigate the grid with the arrow keys and then select options for a cell using the side panel - its a fairly straightforward master/detail.
I'm happy with using a table to seperate the two columns but the problem I'm running into is in the side panel:
I want to have a search box at the top. When you type into the search box it 'autocompletes' to show you a options relevant to what you just typed.
The first problem I had was that the search box wasn't at the top of the cell in the grid. So I was using:
position: absolute;
top: 0;
for the <input> with a position:relative set in the td.
I was then using another div inside the cell to layout the results. That works fine but the search box obscures the first item.
I then tried changing the search box to have display: block which solves the problem but means the search box isn't top aligned when there are no search results!
It seems like using the display and position attributes are mutually exclusive so how do I achieve this in a sensible way?
One option seems to be to just use tables for everything, is there anything wrong with that?
#mainLayout
{
width: 100%
}
#turnSelectionPanel
{
/*visibility: hidden;*/
width: 30%;
height: 100%;
background-color: saddlebrown;
/*this is to allow positioning within this element using 'absolute'*/
position: relative;
}
#turnSearchBox
{
min-height: 20px;
max-width: 200px;
min-width: 100px;
display: block;
/*or
position: absolute;
top: 0;
*/
}
<body>
<table id="mainLayout">
<tr>
<td>
<table id="roster"></table>
</td>
<td id="turnSelectionPanel">
<input type="text" id="turnSearchBox"/>
<div id="turnArea">
</div>
</td>
</tr>
</table>
</body>
I've ommitted some of the other css for brevity's sake + some of the stuff shown there is basically irrelevant - widths etc.
The table + sidepanel is populated from javascript
This is a basic structure that you can you use.
<body>
<!-- This is a wrapper that controls the total height and width of page elements -->
<div id="mainLayout">
<!-- You can position the elements however you would like. -->
<div id="leftColumn"> ...Code goes here... </div>
<div id="searchBar"> ...Code goes here... </div>
<div id="rightColumn"> ...Code goes here... </div>
</div>
</body>
I am particularly fond of using float:left; to arrange my elements. Something like:
#mainLayout {
width:100%
height:100%;
}
#leftColumn {
width:75%;
height: 100%;
float:left;
}
#searchBar{
width:25%;
height: 10%;
float:left;
}
#rightColumn {
width:25%;
height: 90%;
float:left;
}
This will create a layout that scales with the window, and gives you a left column and a right column with a search bar above it. Obviously if you know what size you want your elements than you can simply set them.
I'm not a fan (at all, really) of using floats, so this is my approach on your layout:
I'm not sure if you want a left column, but I've added one in for your choice anyway:
.left, .nav,.right, .content {
display: inline-block;
position: absolute;
position: absolute;
}
html,
body {
margin: 0;
padding: 0;
}
.nav {
width: 75%;
background: lightgray;
height: 100px;
top: 0;
left: 0;
}
.left {
width: 20%;
top: 100px;
left: 0;
background: darkgray;
height: calc(100% - 100px);
}
.right {
width: 25%;
top: 0;
right: 0;
background: gray;
height: 100%;
}
.content {
width: 55%;
height: calc(100% - 100px);
background: lightblue;
top: 100px;
left: 20%;
}
#search {
position: relative;
width: 90%;
margin: 2%;
}
<div class="nav">I'll go most of the way along the top</div>
<div class="left">I'll be a column on the left</div>
<div class="right">
<input id="search" type="text" placeholder="Search Me" />
<br/>
<div class="furtherContent">
I'll be a column on the right</div>
</div>
<div class="content">I'll be a content</div>

Square DIV with Content in a Fluid Layout

SO,
I've created a four-column fluid-width layout for a site, and I'm working on placing a fluid square DIV within one of my columns. There are a few techniques I've found to achieve this - namely, setting padding-bottom to the same percentage as the width - but none of these seem to work when the DIV contains content.
Is there a way to maintain a 1:1 (square) ratio on a fluid DIV when that DIV contains content?
Here's my HTML:
<div id="leftmostcolumn">
<div id="logo"></div>
</div>
<div id="leftcolumn"></div>
<div id="rightcolumn"></div>
<div id="rightmostcolumn"></div>
And my CSS:
body {
width: 100%;
height: 100%;
background-color: red;
}
#leftmostcolumn {
position: absolute;
top: 0;
left: 0;
width: 25%;
height: 100%;
background-color: blue;
}
#leftcolumn {
position: absolute;
top: 0;
left: 25%;
width: 25%;
height: 100%;
background-color: green;
}
#rightcolumn {
position: absolute;
top: 0;
left: 50%;
width: 25%;
height: 100%;
background-color: yellow;
}
#rightmostcolumn {
position: absolute;
top: 0;
left: 75%;
width: 25%;
height: 100%;
background-color: gray;
}
#logo {
width:100%;
padding-bottom:100%;
background-color: #aa2d2d;
color: white;
}
​​
And here's a JsFiddle.
The DIV "logo" is the one I'm trying to maintain as a square. Right now, I've used the padding-bottom approach but that doesn't do the trick when there's content in the DIV. Any input is greatly appreciated!
Marca
EDIT:
Getting there...I'm adapting a script I found to find the width of the DIV and then apply that value to the height to keep it a square. However, as it stands now the script doesn't constantly resize the DIV, and it won't allow it to shrink below a certain size. Any thoughts on how to correct either of these issues?
HTML:
<div id="box"></div>
CSS:
​ #box { width: 75%; height: 50px; background-color: black; }​
JQUERY:
$("#box").css("height", function() {
return $(this).width();
});
JsFiddle is here.
This is something I've actually been messing around with for a while, and have come up with a quasi (but not entirely) hacky, CSS-only solution that seems to work on most browsers in the past decade. The trick is to use images, and positioning in a tricky fashion. Consider the following (simplification) of your code.
Markup:
<div class="sqr_box">
your content goes here!
</div>
CSS:
.sqr_box
{
width: 50%; /* or 100px, or 20em, or whatever you want */
border: solid 2px pink;
background-color: grey;
color: white;
}
Now, we can't set the height in terms of percent, so we won't; instead, first we'll go into Photoshop, and make an image that is 2x2 px, transparent, or background-colored. Next we'll add the following to your markup:
<div class="sqr_box">
<img src="images/sizers/2x2.png" class="sizer">
<div class="content">your content goes here!</div>
</div>
and THIS to your CSS:
.sqr_box
{
width: 50%; /* or 100px, or 20em, or whatever you want */
position: relative; /* static positioning is less than ideal for this scenario */
}
.sqr_box > img.sizer
{
display: block; /* images default to an inline-block like thing */
width: 100%;
height: auto; /* CLUTCH!!! this ensures that the image's height changes to maintain proportions with it's width */
visibility: hidden;
}
.sqr_box > .content
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%; /* Our parent element now has a dynamically assigned height, this will work */
border: solid 2px pink;
background-color: grey;
color: white;
}
Best of all, this will work for any sized ratio of box you'd want! Just change the proportions of the image!
Hope this is all still relevant to you, 3 months later.
-Sandy
Put all four columns in one div. set that div to 100% width and set the font size to 100em
Have each of your four columns have a width of 25em instead of 25%
Have your logo width and height set to 25em each

CSS: three-column layout with image and middle height

I want to align two sidebars and one centered image in height.
Additionally the sidebars (which contain text) should be vertical aligned in the height from the outer container.
I made a fiddle which describes the problem:
http://jsfiddle.net/SW3DT/
When having this example on a fullscreen you see everything correct but when you look into the fiddle html preview (or any smaller view) it doesn't work anymore.
So there are following problems:
Align aside and center-containers horizontal (position: absolute, left, right)
Align the child object from the asides (in this case the p's) vertically (breaks the absolute)
Do this all dynamically so that it can be used on different screen sizes
How you can see all three points together make it hard to code this with css.
Does anyone have a solution?
Furthermore:
How could I define reusable css-classes for such tasks?
Best Regards,
bsus
If I did understand you question correctly, you kinda want a centered wrapper with three columns, having the left and right column occupying 200px of width. If this is the case:
See this Fiddle Example
HTML:
<div class="content">
<aside class="column right">
<p>
text...
</p>
</aside>
<aside class="column left">
<p>
text...
</p>
</aside>
<div class="column center">
<img class="home_image" src="path_to_image.png" />
</div>
</div>
CSS:
/* the main wrapper */
.content {
position: absolute; top: 50%; left: 50%;
width: 800px; height: 500px;
margin-left: -401px; margin-top: -251px;
border: 1px solid #D9D9D9;
}
/* columns */
.column {
position: absolute; top: 0; bottom: 0;
width: 180px;
padding: 10px;
}
.column.left{
left: 0; background-color: #F2F2F2;
}
.column.right {
right: 0; background-color: #F2F2F2;
}
.column.center {
left: 200px; right: 200px;
text-align: center;
width: auto;
}
.column.center img {
border: 1px solid #111; border-radius: 22px;
width: 128px; height: 128px;
margin: 0 auto;
}
Furthermore: How could I define reusable css-classes for such tasks?
You can see the .column class that is applied to the .left, .right and .center elements, thus being reused on all three.