I am trying to make my div boxes work out.
I want the orange boxes to have the same size by expanding the smaller ones to the same height as the biggest one.
I also want to make the one on the second row fit properly, to the left perfectly aligned.
HTML
<div class="picture">
<div class="image"></div>
<div class="description">A description</div>
</div>
<div class="picture">
<div class="image"></div>
<div class="description">A description <br /> Extra line</div>
</div>
<div class="picture">
<div class="image"></div>
<div class="description">A description</div>
</div>
<div class="picture">
<div class="image"></div>
<div class="description">A description</div>
</div>
<div class="picture">
<div class="image"></div>
<div class="description">A description</div>
</div>
CSS:
.picture {
width: 20%;
margin: 5px;
float: left;
}
.image {
width: 100%;
height: 200px;
background-color: chartreuse;
}
.description {
background-color: orange;
}
JSFiddle with the problem: http://jsfiddle.net/PW3GV/1/
Edit
The orange boxes can have a varying height. Sometimes it may contain 1 line, 2 lines or 3 lines - so a fixed height is not going to work out
check this fiddle: http://jsfiddle.net/VYZEx/
i've just added a single line of code to the css, it will work as long as you don't mind that the orange boxes have a fixed size, in that case change this and you will be done:
.description {
background-color: orange;
height: 40px;
}
Good luck!
There you are JSFiddle hope it's what you were aiming for;
.picture {
width: 18.5%;
margin: 5px;
float: left;
}
.image {
width: 100%;
height: 200px;
background-color: chartreuse;
}
.description {
background-color: orange;
height: 20px;
overflow: auto;
cursor: pointer;
text-align: center;
}
.description::-webkit-scrollbar{
display: none;
}
I kept the description size as it is and instead made it scrollable.
Change the .picture width to 18.484% as below:
.picture {
width: 18.484%;
margin: 5px;
float: left;
}
.description {
background-color: orange;
}
And add the jQuery script as below:
function equalHeight(group) {
var tallest = 0;
group.each(function() {
var thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.height(tallest);
}
$(document).ready(function() {
equalHeight($(".description"));
});
See the updated jfiddle.
Give the .picture frame a relative positioning, make the description position relative and place it at the bottom of the .picture frame then give it a 100% width to take up the entire container.
.picture {
width: 20%;
margin: 5px;
float: left;
position: relative;
}
.image {
width: 100%;
height: 200px;
background-color: chartreuse;
}
.description {
background-color: orange;
position:absolute;
bottom:0;
width:100%;
}
http://jsfiddle.net/PW3GV/8/
Here is how I would attempt this using relative positioning for .picture and absolute positioning for .description.
See my jsfiddle:
http://jsfiddle.net/PW3GV/9/
.picture {
width: 20%;
margin: 5px;
float: left;
position:relative;
}
.description {
background-color: orange;
position:absolute;
bottom:0;
width:100%;
}
Related
How Google made the color bar located at their footer of this page.
Is it possible to make this in CSS and have it expand 100% of width ?
This is quite easy to do in CSS. Make a div(in this case .googlebar) that has 100% width, and however high you want(I chose 5px).
Then, place 4 spans inside, display them inline-block, set the width to 25%(since there is 4 of them), and the height to 100%.
All that is left to do is change the colors, and you are done :
.googlebar{
width:100%;
height:5px;
}
.googlebar span{
display:inline-block;
width:25%;
height:100%;
}
.googlebar span.blue{
background:#0089FA;
}
.googlebar span.red{
background:#FF002B;
}
.googlebar span.orange{
background:#FFA900;
}
.googlebar span.green{
background:#00A753;
}
<div class="googlebar">
<span class="blue"></span><span class="red"></span><span class="orange"></span><span class="green"></span>
</div>
This will do it for you: https://jsfiddle.net/xtat0oa6/1/
HTML
<div class="footer">
<div class="div1">
</div>
<div class="div2">
</div>
<div class="div3">
</div>
<div class="div4">
</div>
</div>
CSS
.footer {
margin-top: calc(100vh - 5px);
}
.div1 {
width: 25%;
height: 5px;
background-color: blue;
float: left;
}
.div2 {
width: 25%;
height: 5px;
background-color: red;
float: left;
}
.div3 {
width: 25%;
height: 5px;
background-color: orange;
float: left;
}
.div4 {
width: 25%;
height: 5px;
background-color: green;
float: left;
}
The Obective here: Get column 2(skyblue) and column3(salmon) to float inside it's wrapper(green). The first column(lightgreen) is floated to the left, the second column(skyblue) is float left, and the third column(salmon) is floated right. What am I doing wrong here? Why are they sitting underneath my wrapper? I tried clear fixes and expanding the wrapper and can't get these columns to sit inside the container. Suggestions?
Demo - http://codepen.io/Chris-Brennan/pen/pJORJY
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#wrapperGreen {
margin: 0 auto;
width: 960px;
height: 700px;
background: green;
}
#mainContentLightgreen {
width: 520px;
height: 700px;
background: lightgreen;
}
#contentleftSkyblue {
width: 200px;
height: 600px;
background: skyblue;
float: left;
}
#contentrightSalmon {
width: 200px;
height: 600px;
background: salmon;
float: left;
}
#footer {
height: 100px;
background: black;
clear: both;
}
<div id="wrapperGreen">
<div id="mainContentLightgreen">
</div>
<div id="contentleftSkyblue">
</div>
<div id="contentrightSalmon">
</div>
<div id="footer">
</div>
</div>
I dont know if I've misunderstood but I think its the order in which you are writing them?
Does this solve your problem?
<div id ="wrapperGreen">
<div id="contentleftSkyblue">
</div>
<div id="contentrightSalmon">
</div>
<div id="mainContentLightgreen">
</div>
<div id="footer">
</div>
</div>
You don't set the float left in
#mainContentLightgreen{
width:520px;
height:700px;
background:lightgreen;
float: left;
}
look at this sample
Instead of float use display:inline-block; or display:table-row;
If CSS3 is an option and you are not to worried about compatibility you could use column-count.
Float is used literally for floating element's so that it break's the document flow.
Like for instance you wanted to float an image to the left and have text wrap around it.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.align {
display: inline-block;
vertical-align:top;
}
#wrapperGreen {
margin: 0 auto;
width: 960px;
height: 700px;
background: green;
}
#mainContentLightgreen {
width: 520px;
height: 700px;
background: lightgreen;
}
#contentleftSkyblue {
width: 200px;
height: 600px;
background: skyblue;
}
#contentrightSalmon {
width: 200px;
height: 600px;
background: salmon;
}
#footer {
height: 100px;
background: black;
clear: both;
}
<div id="wrapperGreen">
<div id="mainContentLightgreen" class="align">
</div>
<div id="contentleftSkyblue" class="align">
</div>
<div id="contentrightSalmon" class="align">
</div>
<div id="footer">
</div>
</div>
I got this to work, if I'm understanding you correctly by nesting the div's inside of one another. For example the salmon div inside the green wrapper
<div id ="wrapperGreen"><div id="contentrightSalmon"></div>
</div>
I want to achieve a flexible container with three images. One large one the left, two smaller ones (roughly one quarter of the size) aligned to the right of it:
When resizing the browser window, I want the three images to adjust accordingly while keeping the original proportions so the large image's baseline keeps aligned with the lower small image's baseline.
So far, I've tried the following code:
<div id="space">
<div id="large">
<img src="http://placehold.it/640x420" />
</div>
<div class="small">
<img src="http://placehold.it/320x200" />
</div>
<div class="small">
<img src="http://placehold.it/320x200" />
</div>
</div>
#space {
width:100%;
}
#large {
width:60%;
float:left;
margin:1% 1%;
padding:0px;
}
.small {
width:30%;
float:left;
margin:1% 1%;
padding:0px;
}
img {
width:100%;
height:auto;
padding:0px;
margin:0px;
}
In case the images are slightly higher than the proportions allow, the images should be vertically centered in the respective container, the overflow should be hidden.
You can find this code on JSFIDDLE: https://jsfiddle.net/u8kksgbq/12/
Please help - I've been trying and trying and don't find a solution.
Thanks for your answers. This my final solution:
<section id="contact-pics">
<div class="pic-large">
<div class="dummy"></div>
<div class="pic-content">
<img src="http://192.168.178.20"/>
</div>
</div>
<div class="v-spacer">
<div class="dummy"></div>
<div class="pic-content">
</div>
</div>
<div class="pic-small">
<div class="dummy"></div>
<div class="pic-content">
<img src="http://192.168.178.20"/>
</div>
</div>
<div class="h-spacer">
<div class="dummy"></div>
<div class="pic-content">
</div>
</div>
<div class="pic-small">
<div class="dummy"></div>
<div class="pic-content">
<img src="http://192.168.178.20"/>
</div>
</div>
</section>
And the CSS:
#contact-pics {
img {
width: 100%;
height: auto;
}
overflow: auto;
.pic-large {
display: inline-block;
position: relative;
width: 65.99%;
float:left;
.dummy {
padding-top: 62%;
}
}
.pic-small {
display: inline-block;
position: relative;
width: 32.8%;
float:left;
.dummy {
padding-top: 62%;
}
}
.v-spacer {
display: inline-block;
position: relative;
width: 1.2%;
float:left;
.dummy {
padding-top: 2535%;
}
}
.h-spacer {
display: inline-block;
position: relative;
width: 32.333333%;
float:left;
.dummy {
padding-top: 2.4%;
}
}
.pic-content {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
}
Guess there are easier solutions, but this one definitely works :-)
Fiddle
I don't know how much of an answer it is, but I gave it a try.
You have to put a vertical separator between small and large images, that way you can specify the "fake" margin between them in width: xx%;
As for horizontal separator, I tried it, but couldn't get it. The only solution that I can see is to create a transparent image and put between them. Set its width: 30% , just like the photos and that way it will keep the height: auto too.
Thanks for your question, I have tested some solutions and found the answer for # is the correct one, but the code isn't placed correctly. I have edited CSS as follow:
#contact-pics {
overflow: auto;
}
#contact-pics img {
width: 100%;
height: auto;
}
.pic-large {
display: inline-block;
position: relative;
width: 65.99%;
float:right;
}
.pic-large .dummy {
padding-top: 62%;
}
.pic-small {
display: inline-block;
position: relative;
width: 32.8%;
float:right;
}
.pic-small .dummy {
padding-top: 62%;
}
.v-spacer {
display: inline-block;
position: relative;
width: 1.2%;
float:right;
}
.v-spacer .dummy {
padding-top: 2535%;
}
.h-spacer {
display: inline-block;
position: relative;
width: 32.333333%;
float:right;
}
.h-spacer .dummy {
padding-top: 2.4%;
}
.pic-content {
position: absolute;
/*top: 0;*/
bottom: 0;
left: 0;
right: 0;
}
Right now Im trying to put an image on the top of a div. The divs are in horizontal, and I donĀ“t know why, but when I put the image its position affects all external divs... I mean, the image should only affect the div in which I put it.
I know this can be a little bit difficult to undestand, I took a capture of my divs: Capture. As you can see, the height of my image affects the external divs.
Here is the HTML code:
<div class="hoteles">
<div class="head-hoteles">Los mejores hoteles</div>
<div class="hotel"><img src="images/hotels/hotel-bellevue.jpg" alt="Hotel Bellevue"></div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
</div>
And the CSS:
.hoteles{
background-color: pink;
height: 100%;
width: 65%;
float: left;
padding-left: 2%;
}
.head-hoteles{
width: 100%;
height: 100px;
background-color: yellow;
padding: 5%;
font-size: 1.5em;
}
.hotel{
height: 12.5em;
min-width: 23%;
display: inline-block;
background-color: brown;
margin-bottom: 2%;
}
.hotel img{
width: 100px;
}
Other question is... when I put "width 100%" its does not do it, I just can resize the image with pixels... Thanks !
You need to float the divs, currently your divs are positioned as inline-block which is causing disorder. Additionally you can use vertical-align: top to order the inline-block.
Working example:
JSFiddle
.hoteles {
background-color: pink;
height: 100%;
width: 65%;
float: left;
padding-left: 2%;
}
.head-hoteles {
width: 100%;
height: 100px;
background-color: yellow;
padding: 5%;
font-size: 1.5em;
}
.hotel {
height: 12.5em;
min-width: 23%;
background-color: brown;
float: left;
margin:2% 5px 2% 0;
}
.hotel img {
width: 100px;
float:left;
}
<div class="hoteles">
<div class="head-hoteles">Los mejores hoteles</div>
<div class="hotel">
<img src="images/hotels/hotel-bellevue.jpg" alt="Hotel Bellevue" />
</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
<div class="hotel">Hotel1</div>
</div>
As for your second question, you need to have a width for the parent of img. Currently it uses min-width, change it to width and give your img the width of 100% and it will expand to the percentage of the parent. Like the following:
.hotel {
width: 23%;
}
.hotel img {
width: 100%;
}
Try adding the following CSS rule:
.hotel { vertical-align: top; }
You are seeing the result of inline elements being positioned along the baseline.
Is there a possibility to put an image on the middle of a page?
BUT: On the right side and the left side of the image, there are 2 areas that can grow according to the screen resolution.
These areas are "1 pixel repeat-x" images.
Please note: the image on the right side and the left side aren't the same picture!
Below a picture with a sketch that (I hope) will explain my problem:
You can do it like this
Set the image in .container where for now I have added sample text.
CSS
.rightArea,
.leftArea {
position: absolute;
width: 50%;
background: yellow;
top:0;
left:0;
height: 100%;
}
.rightArea {
background: red;
right: 0;
left: auto;
}
Here is one way of building this layout using CSS table cells.
Start with this HTML:
<div class="wrapper">
<div class="left"></div>
<div class="center">
<img src="http://placekitten.com/400/200">
</div>
<div class="right"></div>
</div>
and apply the following CSS:
.wrapper {
display: table;
width: 100%;
border: 1px solid blue;
}
.left, .center, .right {
display: table-cell;
border: 1px dotted blue;
vertical-align: top;
}
.center {
width: 1%;
}
.left, .right {
width: 50%;
}
.left {
background-image: url(http://placekitten.com/4/100);
background-repeat: repeat-x;
background-position: left center;
}
.right {
background-image: url(http://placekitten.com/10/100);
background-repeat: repeat-x;
background-position: left center;
}
.center img {
display: block;
}
The .wrapper has a width of 100%, so it fills the width of the page.
The child elements .left, .center and .right are table cells.
.center is forced to shrink-to-fit the image by setting the width to some small value, for example, 1%.
The .left and .right elements are set to the same width, 50%, which forces them to take up the remaining space equally.
You can apply background images as needed to any of the child elements.
See demo at: http://jsfiddle.net/audetwebdesign/pG2v3/
Note: Most modern browsers support CSS table cells.
You can relay on a single container and pseudo-elements.DEMO
display:table/table-cell -properties will make this easy to manage: (update test with your image name/path)
HTML
<div>
<img src="middle.jpg" />
</div>
CSS
img {
display:block;/* avoid gap underneath*/
margin:auto;/*optionnal*/
}
div {
display:table;
width:100%;
background:#7E858F;
}
div:before, div:after {
content:' ';
display:table-cell;
width:50%;/* will shrink to leave room for image */
background-repeat:repeat-x;
}
div:before {
background-image:url(left.jpg);/* slice of 1px */
}
div:after {
background-image:url(right.jpg);/* slice of 1px */
}
DEMO
To freely grow sided placed divs, keeping a content in the middle, use a wrapper display: table; element, and set the inner divs to display:table-cell, so the center element will adjust according to the width of the right and left divs:
http://jsfiddle.net/4uHm8/
HTML:
<div class='wrapper'>
<div class='left'></div>
<div class='center'>test</div>
<div class='right'></div>
</div>
CSS:
.wrapper {
display: table;
width: 100%;
height: 200px;
}
.wrapper > div {
display: table-cell;
}
.left {
width: 30px;
background: red;
}
.right {
width: 50px;
background: blue;
}
EDIT:
In the same line of thought, if you set only the central div's width, the left and right divs will adjust themselves equally using the remaining width... even zero.
http://jsfiddle.net/4uHm8/1/
.wrapper {
display: table;
width: 100%;
height: 200px;
}
.wrapper > div {
display: table-cell;
}
.center {
background: blue;
width: 100px;
}
.left, .right {
background: red;
}
HTML
<div class="main">
<div class="left"> </div>
<div class="image">
<img src="http://www.ricoh.com/r_dc/r/r8/img/sample_10.jpg" height="150" width="120" alt="image" />
</div>
<div class="right"> </div>
</div>
CSS
.main {
width: 100%;
margin: 0 auto;
position: relative;
}
.left {
float: left;
width: 50%;
background: #FF0000;
}
.right {
float: right;
width: 50%;
background: #FFFF00;
}
.image {
position: absolute;
}
jQuery
$(document).ready(function () {
$(".image").css('right', ($(".main").width() / 2) - ($(".image").width() / 2));
$(window).resize(function() {
$(".image").css('right', ($(".main").width() / 2) - ($(".image").width() / 2));
});
});
Example
http://jsfiddle.net/wphn9/