So I have a class, let's call cont that will hold a lot of things, including an image.
Now I want to style the image. We can style an image inside a div with .cont using:
.cont>img {
...
}
However, in my cont div, I want another box to occupy 150px*150px, where the image is 'inside'. To clarify, I want to have a 150px*150px box, that holds an image that may be smaller:
[ ]
[ [ ] ]
[ [img] ]
[ [ ] ]
[ ]
Basically, in common css, this can be done like so:
.cont-image {
width:150px;
height:150px;
}
But that has the disadvantage of having to add the cont-image class to every image.
Is this possible to be inherited and styled automagically™? Somehow Like:
.cont>img {
width:150px;
height:150px;
}
(The above obviously doesn't work, because it stretches the image to the dimension, instead of having a 'wrapper' container.
Basically, I want to have an image have a preset size that it gets dropped into. This can be done like so:
<div style="width:150px;height:150px">
<img src=""></img>
</div>
So that I have a div that is 150*150 and the image with variable size is inside of it. However, I'm looking to do this through styling the image itself only (no outside div):
<img style="???" src=""></img>
Have you tried changing to max-width/height?
.cont>img {
max-width:150px;
max-height:150px;
}
There will be no stretching for image.
Are you looking for something like this: http://jsfiddle.net/abhitalks/pULZA/1/ ?
Using the same markup:
<div class="cont">
<img src="http://lorempixel.com/50/50" />
</div>
Try changing the size of the image in the fiddle. Try making it http://lorempixel.com/350/350. Hope that suits your purpose of capping the image size in the inner container.
Now for the CSS:
Using :after pseudo-class to generate a faux-container. It is actually not a container, but looks like one. This will help you to not change your existing markup.
.cont {
border: 1px solid gray;
width: 250px; height: 250px;
line-height: 250px;
position: relative;
text-align: center;
}
.cont > img {
border: 1px solid black;
display: inline-block;
max-width: 150px; max-height: 150px;
vertical-align: middle;
text-align: center;
}
.cont:after {
content: "";
display: block;
position: absolute;
top: 50px; left: 50px;
width: 150px; height: 150px;
border: 1px solid red;
background-color: #ddd;
z-index: -10;
}
Making the z-index negative, makes it go behind the image and thus appearing as if a container.
Otherwise, the only option you have is to change the markup to have nested divs.
Update:
Check this: http://jsfiddle.net/abhitalks/pULZA/2/
As you have said that you want the container to be fixed at 150px, you can use the half of that figure (75px) to calc and position the :after content accordingly. Thus even if the .cont dimensions change, the image and its frame will always remain in center.
Using CSS calc:
.cont:after {
...
/* to calculate center,
we halve the width to 75px and subtract it from 50% of parent
*/
top: calc(50% - 75px); left: calc(50% - 75px);
width: 150px; height: 150px;
...
}
HTML:
<div class="cont">
<div>
<img src="http://www.famfamfam.com/lab/icons/silk/icons/application_form.png" />
</div>
</div>
CSS:
.cont {
width: 150px;
height: 150px;
background-color: green;
padding: 25px;
}
.cont > div {
width: inherit;
height: inherit;
background-color: grey;
display:table-cell;
vertical-align:middle;
text-align: center;
}
.cont img {
max-width: 150px;
max-height: 150px;
}
If I understood you correctly, this should work for you.
Related
I have the following
div {
border: 1px solid red;
}
input {
font-size: 10vw;
width:50%;
}
button {
height: 100%;
width: 40%;
}
<div>
<input/><button>button</button>
</div>
The border is just to show the div. The input has a variable size. I've demonstrated this by using 10vw as the font size.
I would like the button to be the same height as input.
I would like the div to stay in the main flow of the page (i think that may rule out using absolute positioning tricks?) and I want the solution to be CSS only.
I'd prefer not to have to work out the size of the input - is there anyway I can get the button to fill the height of the containing div?
Sure, just add position:relative to the div and position:absolute to the button:
div {
border: 1px solid red;
position: relative;
}
input {
font-size: 10vw;
width: 50%;
}
button {
height: 100%;
width: 40%;
position: absolute;
}
<div>
<input/>
<button>button</button>
</div>
I have a div of fixed width and height.
I want to put and image and caption to it (using img and figurecaption) such that they both never exceeds the dimensions of the parent.
I tried this :
`
parent->
height: 100px;
width: 100px;
margin: 0;
img->
display: block;
height: 100%;
width: 100%;
figurecaption->
text-align: center
`
When the image is of greater size than the specified height and width, the caption goes outside. How to deal with this. Thanks.
If you are trying to avoid both the image and the caption going outside the parent container, you have several options. The nicest might be to set the image max-height and max-width to 100% and then to overlay the caption on the bottom. If you want to keep them completely separate, you can do something like this:
#container {
height: 400px;
width: 300px;
border: 1px solid #000000;
text-align:center;
}
#image {
max-height: calc(100% - 50px);
max-width: 100%;
}
#caption {
background: #282828;
max-height: 50px;
width: 100%;
text-align: center;
color: #ffffff;
}
<div id="container">
<img id="image" src="http://www.fixedstars.com.au/images/runBack.jpg">
<div id="caption">This is the caption</div>
</div>
This sets the maximum height of the image at100% less the height of the caption. If you prefer for the caption to be stuck to the bottom of the container, even if the image is shorter, st the container to position: relative and give the caption position: absolute; bottom:0; for the container and the caption.
To Fix this issue you have to set width and height to 100% for Image
Below is the complete demo.
Hope this will helpful to you.
<style>
.mydiv {
border-color: red;
border-style: solid;
height: 50px;
width: 80px;
}
.imgStyle {
height: 100%;
width: 100%;
}
</style>
<html>
<div class="mydiv">
<img src="http://www.boltoday.com/wp-content/uploads/2014/10/only-3d-natural-1024x768.jpg" class="imgStyle" />
Your Text Goes Here
</div>
</html>
For example this div is displayed:
<div class="circle"></div
.circle {
width: 300px;
height: 300px;
background: red;
border-radius: 50%;
}
but when width and height are in % it collapses:
.circle {
width: 30%;
height: 30%;
background: red;
border-radius: 50%;
}
Is there a way to get it displayed?
This is because the div has no height. width: 30%; will always make the div 30% width of the parent (<body>, in this case), which is what you want. However, height behaves a little differently.
You need to specify a 100% height for body and html like so:
html,
body {
height: 100%;
}
Working Fiddle
You can read why height: 100%; behaves like that here
You need to declare a hard width/height somewhere, in this example I put a hard width/height on a container div:
http://jsfiddle.net/exrNm/1/
<div class="con">
<div class="circle"></div
</div>
.con{
width:300px;
height:300px
}
.circle {
width: 30%;
height: 30%;
background: red;
border-radius: 50%;
}
You could easily set a hard width somewhere up the parent chain. The % needs a hard value to calculate against.
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
I have a problem where a div tag that is supposed to show on hover is hidden behind an image. This is how it looks:
I tried to remake it with jsfiddle: http://jsfiddle.net/Gwxyk/21/
I tried position relative also on '.image-options' but did not turn out right. Also how do i float the small orange box to the right side? I tried float: right; but it did not respond.
Help would be appritiated.
Some arbitrary code since stackoverflow asks for it (its in jsfiddle):
.image-options {
float: right;
}
I'm struggling to understand exactly what you require to happen. However have you tried using the z-index property? Both the div and the image will need to be positioned relatively or absolutely, then apply a higher z-index to the element that you want to appear in front. So you could apply z-index: 1 to the image and z-index: 100 to the div.
Is this what you are expecting?
Add top:0 to .image-options and interchange the place of image and inner div.
DEMO
Here you go, i think this will help you out.
http://jsfiddle.net/dmP2x/
You dont have to do this with jQuery, use CSS as much as you can to tidy up your code.
css:
.testclass {
width: 105px;
height: 80px;
overflow: hidden;
display: inline-block;
border: 2px solid rgba(140,140,140,1);
}
.image-options {
width: 10px;
height: 10px;
border: 2px solid rgba(255,128,64,1);
position: absolute;
top: 10px;
left: 25px;
overflow: none;
display: none;
}
.image {
background-image: url('http://www.placehold.it/105X80');
width: 105px;
height: 80px;
position: relative;
}
.image:hover .image-options {
display: block;
}
html:
<div class="testclass">
<div class="image">
<div class="image-options"></div>
</div>
</div>