I'm working on a photo gallery that is dynamically generated, photos will be different sizes, and each photo will have a DIV overlapping the image, on the same baseline as the image, that will contain "user menu" items related to that photo. (blue box below)
I'm having trouble getting the "user menu" div to position on top of the image. This div needs to be centered, and on the same baseline as the image, regardless of image size (not sure how to do this with absolute positioning)
I've seen suggestions to use CSS background-image, but I don't see that as a good option with dynamically generated content. I've seen a suggestion to position the image absolutely, and this does work except the parent DIV collapses and makes positioning other items on the page a nightmare. There are quite a few post that I've read through on stackoverflow, but I haven't found one that addresses or solves this particular issue.
This is an example of what I'm trying to accomplish. The blue boxes are the user menus, and should be centered on the baseline of the images.
I've tried 2 things, this doesn't seem to work with parent DIVs since the images are removed from flow and spacing gets screwed up as the parent DIVS collapse. http://jsfiddle.net/3d3m1x9k/
CSS
.box1{
position:relative;
display:inline-block;
background-color:orange;
border: 4px solid black;
}
.box2{
position:relative;
top:80px;
left:80px;
margin: 0 auto;
padding: 5px;
background-color: red;
border: 2px solid red;
}
HTML
<div class="box1">
<img src="http://i60.tinypic.com/33n947o.jpg" style="position:absolute;"/>
<div class="box2">box</div>
</div>
<div class="box1">
<img src="http://i60.tinypic.com/33n947o.jpg" style="position:absolute;"/>
<div class="box2">box</div>
</div>
This works better, but I can't get the box to center properly unless I use javascript to position the "user menu" DIV since the image widths are dynamic. (javascript isn't out of the question, just trying to first find a proper way using CSS)
https://jsfiddle.net/vv5rtkqz/
CSS
.box1{
position:relative;
display:inline-block;
background-color:orange;
border: 2px solid black;
}
.box2{
position:absolute;
bottom: 0px;
margin: 0 auto;
padding: 5px;
background-color: red;
border: 2px solid red;
}
HTML
<div class="box1">
<img src="http://i60.tinypic.com/33n947o.jpg"/>
<div class="box2">Center Me</div>
</div>
<div class="box1">
<img src="http://i60.tinypic.com/33n947o.jpg"/>
<div class="box2">Center Me</div>
</div>
You can get a simple start by using absolute positioning and a 2D transform.
.container {
position: relative;
display: inline-block;
}
.info {
background: black;
color: white;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
white-space: nowarp;
}
<div class="container">
<img src="http://shakacity.com/sites/default/files/dog_0.jpg" alt="" />
<div class="info">Contains the user controls</div>
</div>
Related
I want to make the following element.
Basically an image in top of a page layout, and then a box on top of that image, containing a header and some more text.
But, what is the most optimal way of doing this in regards to responsiveness. I imagine something like the following markup
<div class="header">
<img src="myimage.png" alt="" />
<div class="text">
<h1>Header</h1>
<p>Lorem ipsum</p>
</div>
</div>
And then set .header to position relative, img to max-width of 100%, and .text to position absolute, bottom: -50%, left: 10% etc.
But this does not scale well, and absolute position seems to me, to be a bit invalid in regards to responsiveness. Also, there is content below the element, that should move accordingly to the amount of text, in the text element. If if is positioned absolute, that gets more tricky to manage.
Any ideas to another approach?
Try this:
img {
border: 2px solid black;
}
#container {
position: relative;
}
#example {
position: absolute;
top: 10px;
left: 10px;
padding: 5px;
background-color: white;
border: 2px solid red;
}
<div id="container">
<img src="http://i34.tinypic.com/4tlkci.jpg">
<div id="example">This is my div</div>
</div>
I am making a webpage and learning how to use stylesheets.
I have 3 side by side "div" boxes, each with an image and text underneath the image.
HTML and CSS:
.prod_img_box {
vertical-align: middle;
text-align: center;
width: 300px;
border: 2px solid #black;
margin: 0 auto;
display: inline-block;
position: relative;
}
<div class="prod_img_box">
<img src="direct.png" alt="Storage Vessels" class="prod_img" border=2/>
<h3 class="white_font">Storage Vessels</h3>
</div>
<div class="prod_img_box">
<img src="tran_imm.png" alt="Immersion Heaters" class="prod_img" border=2/>
<h3 class="white_font">Immersion Heaters</h3>
</div>
<div class="prod_img_box">
<img src="at_chrome.png" alt="Towel Rail Heating Elements" class="prod_img " border=2/>
<h3 class="white_font">Towel Rail Heaters</h3>
</div>
This gives me something like the following:
which is almost what I want - but the text underneath each image is currently vertically centered (as it should be as it is inside the "prod_img_box" - but I want the text to be all in a horizontal line at the bottom.
I'm not sure of the best way to do this. Should it be something like:
<div class="image">IMAGE</div>
<div class="text">Storage Vessels</div>
<div class="image">IMAGE</div>
<div class="text">Immersion Heaters</div>
<div class="image">IMAGE</div>
<div class="text">Towel Rail Elements</div>
The webpage can be found here also.
I think you should use
<div class="image">IMAGE</div>
<div class="text">Storage Vessels</div>
and then set height for image class to set the text always stay at the bottom
Check border in this class It should be 2px solid black; or 2px solid #000;
.prod_img_box {
vertical-align: middle;
text-align: center;
width: 300px;
border: 2px solid black;
margin: 0 auto;
display: inline-block;
position: relative;
}
And Remove border=2 from every image tag if not required
Note: If you are using # tag then you need to set color by color code i.e. #000 or if you are using direct color name then you don't need to put # tag
You can set a default height to the div so that however the image is , it will display all the divs to be same and thus same level of texts.
The Div element is most commonly used to keep a group of content together. If you would like to keep the code together, the best way of moving the text is simply manipulating the padding and margin properties in the CSS file.
Hi I am trying to develop a web page. In that I am supposed to place an image at the center of the page. Below that image I placed an div so that by clicking on that div certain items will be displayed.My problem is that when the size of the window reduced the image will become responsive,but not that div. I want to make both of them responsive.Here is my code
index.html
<div class="container">
<div class="row">
<div class="col-lg-12" style="top:-125px;">
<img id="i1" class="img-responsive" src="img/2.png" height="600" width="600" alt="">
<div style="padding-left:45%;top:-250px;">
<div id="triangle-up"></div>
</div>
</div>
</div>
</div>
this is my css
#triangle-up {
position: absolute;
bottom:0;
left: 46%;
padding: 5px;
width: auto;
height: auto;
border-left: 60px solid transparent;
border-right: 60px solid transparent;
border-bottom: 67px solid grey;
}
.container
{
position: relative;
}
Please help me to solve this issue..
Use CSS to set up width of your div to 100% and max-width as pixels amount that you don't want your div to grow any larger than.
I am trying to make these blocks of info the same size regardless of the number of words each one holds. As seen in the example, when one block has less text than the other, one gets a bit smaller and the other remains a different size.
Now my question is, How do I achieve having these blocks the same size regardless of its content or image? I am also going to use another pair right below them.
Here is the CSS code:
/***********All containers**************/
.bottomContainers{
position: absolute;
margin-left: 0%;
display: inline-box;
}
/**********Small Containers*************/
.container{
max-width: 30%;
max-height: 30%;
margin-top:5%;
margin-bottom: 5%;
margin-left: 10%;
padding-left: 2%;
padding-right: 2%;
padding-bottom: 2%;
background-color: #ecf0f1;
color: grey;
display: inline-block;
/*display: inline-block;*/
border-radius: 5px;
border-bottom: 2px solid grey;
}
Here is the HTML code:
<div class="bottomContainers" role="moreInfo">
<!--Small Inner Containers for Information-->
<div class="container" id="firstContainer">
<br />
<center><img src="img/map.png"></center>
<br>
<article>
Some random text is in this block, It doesnt size like the next one
</article>
</div>
<div class="container" id="firstContainer">
<br />
<center><img src="img/money.png"></center>
<br>
this is another block which also doesnt scale to the other block regardless of text inside of it
</div>
What did I possibly do wrong here ?
I am heavily refactoring your original code in this solution. If this is a static width website then having static width cells won't be a problem. If you want this solution to be responsive you will have a lot of issues with it:
http://jsfiddle.net/VET6x/1/
I positioned the image and its corresponding text using absolute. Again that will work with a static layout, once it goes responsive there will be problems.
<div class="bottomContainers">
<div class="container">
<div>
<img src="http://placekitten.com/g/80/80" />
</div>
<div>
Some random text is in this block, It doesnt size like the next one
</div>
</div>
<div class="container">
<div>
<img src="http://placekitten.com/g/80/80" />
</div>
<div>
This is another block which also doesnt scale to the other block regardless of text inside of it
</div>
</div>
</div>
.bottomContainers { overflow:hidden; }
.container {
width:200px;
height:200px;
float:left;
position:relative;
margin:5% 5%;
padding:2%;
background-color: #ecf0f1;
color: grey;
border-radius: 5px;
border-bottom: 2px solid grey;
}
.container > div { position:absolute; bottom:10px; }
.container > div:first-child { position:absolute; top:10px }
If it were me I would find someway to avoid static height cells.
Here is one solution that may work for you:
Demo Fiddle
I changed up your code a bit. Using the center tag is frowned upon, also it looks like the br tags were there for spacing, which could be done with margin. I ended up giving .container a specified height, the main drawback in that being if the window is sized down too far the overflow text will be hidden.
HTML:
<div class="bottomContainers" role="moreInfo">
<div class="container" id="firstContainer">
<img src="http://www.placehold.it/100x100">
<p>
Some random text is in this block, It doesnt size like the next one
</p>
</div>
<div class="container" id="firstContainer">
<img src="http://www.placehold.it/100x100">
<p>
this is another block which also doesnt scale to the other block regardless of text inside of it
</p>
</div>
</div>
CSS:
.container{
// your current styles here
overflow: hidden;
height: 200px;
display: block;
float: left;
}
.container img {
display: block;
margin: 10px auto 0px;
}
This is a quick fix, but setting an explicit height on the objects will have them all be the same height. This requires some playing around with the best size and such but it will fix your problem. I'm curious how a professional would fix this problem.
Some other things with your code. Centering the <img> using HTML is discouraged, use css instead. Also, where are the <br> tags and why are some closed but some aren't?
Maybe you can use display:table;, display:table-row; and display:table-cell;. This way, your div will act like column of a table. They will stay at the same height.
Take a look at this jsfiddle!
I am trying to horizontally center an image that is contained within an anchor tag. I have tried setting it as a block and setting margins to auto with no luck. I have tried wrapping it in a div tag and doing the same, still, no luck. Even tried centering the img within the div within the div. I looked at other posts, and nothing seems to help. The image is still aligned to the left no matter what I do.
###HTML
<div id="slideWrapper">
<h1 id="slideHeading">This is a Header</h1>
<hr/>
<div class="centerMe">
<a href="javascript:slidelink()" class="slideA">
<img src="./images/Slides/cheeseBoards.jpg" name="slide" id="slideImg"/>
</a>
</div>
<hr/>
<div class="centerMe">
Check out our services and other great stuff.
</div>
</div>
###CSS
.centerMe {
margin-left: auto;
margin-right: auto;
border: 1px solid green;
text-align: center;
}
#slideWrapper {
border: 1px solid blue;
}
#slideImg {
display: block;
height:500px;
width:900px;
border:1px solid red;
margin-left: auto;
margin-right: auto;
}
I put borders around everything so that I could see where things were getting placed. It seems like the image isn't being contained within the div for some reason. This seems like it should be so simple. Any help would be greatly appreciated. Thanks.
Make sure there is no float:left inherited from parents. margin:0 auto; does not work when you have float:left; or position:absolute;