Make a grid of inline-block elements respond to resize - html

I'm currently working on the following website:
http://hmdesign.alpheca.uberspace.de/
As you can see, I already managed to create a list of div.project-item elements. Due to the use of inline-blocks, it also reduces the number of columns when you resize the window. What I want to accomplish now is, when you resize the window, that the elements scale up/down in a certain range (between min-width and max-width) until it reaches the maximum/minimum and that it THEN removes/creates a column. The problem now is that there is a huge empty gap after removing a column. It would be much smarter to still show lets say 3 smaller columns in that situation instead of 2 big ones.
I already tried to use a flexbox which didn't really help and also to use block elements instead of inline-block and float them to the left. Then the resizing works but I also want the whole thing to be centered (like now), which I didn't found a way yet to do with floated elements.
Relevant code below:
HTML:
<div class="content-wrapper">
<div class="project-list">
<div class="project-item">
<a href="#">
<img class="project-image" src="res/img/Placeholder.jpg">
<div class="project-overlay">
<div class="project-desc">
<span class="project-title"></span>
<span class="project-text"></span>
</div>
</div>
</a>
</div>
<div class="project-item"...
CSS:
/* Wrapper */
div.nav-wrapper, div.content-wrapper {
max-width: 1280px;
padding: 0 25px;
position: relative;
margin: 0 auto;
height: 100%;
}
/* Portfolio Projektliste */
div.project-list {
padding-top: 150px;
max-width: 1300px;
margin: 0 10px;
text-align: center;
font-size: 0;
}
/* Projekt Item */
div.project-item {
display: inline-block;
position: relative;
width: 400px;
height: auto;
margin: 10px;
overflow: hidden;
}
div.project-item:after {
padding-top: 56.25%;
/* 16:9 ratio */
display: block;
content: '';
}
img.project-image {
position: absolute;
left: 50%;
top: 50%;
transform: translateY(-50%) translateX(-50%);
-webkit-transform: translateY(-50%) translateX(-50%);
-moz-transform: translateY(-50%) translateX(-50%);
max-width: 100%;
}
div.project-overlay {
position: absolute;
width: 100%;
height: 100%;
z-index: 10;
background-color: black;
opacity: 0;
}

You can set the width of div.project-item in % instead of a fixed width (px)
e.g
div.project-item{
width: 30%;
}
So when you resize the window it will adjust.
And if in some point you want to show only two, you can do it using medias
e.g
#media (max-width: 920px){
div.project-item{
width: 45%;
}
}

I agree with Yandy.
Try changing your div.project-item width to like 40% (this is for when it has two divs showing). Like so:
div.project-item {
display: inline-block;
position: relative;
width: 40%;
height: auto;
margin: 10px;
overflow: hidden;
}
Then add this code:
#media only screen and (min-width: 1347px) {
div.project-item {
width:31%
}
}
#media only screen and (max-width: 926px) {
div.project-item {
width:75%
}
}
All of the width percentages can be picked to your choosing for your project.
The one with the 926px is for the single div, and the 1347px is for the triple div.

Related

contain image in 60%-height-div, while keeping aspect ratio

What I am trying to accomplish:
- create a pop-up div (fixed), centered in view
- this pop-up should be 60% height of the browser window
- the contents of the pop-up should be an image and a 'x' above the upper right corner of the image
- the height of the image should be maximal, considering it should be contained in the div together with the 'x'
- the aspect ratio of the image should be maintained
I tried the following code
<div class="pop-up">
<p class="exit-button">x</p>
<img class="image" src="safari.png" width="1200" height="630" alt="" title="" />
</div>
With CSS:
body {
background: #333;
}
.pop-up {
position: fixed;
height: 60%;
width: auto;
left:50%;
top:50%;
-webkit-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
background:yellow;
object-fit: contain;
}
.exit-button {
text-align: right;
margin: 0;
font-size: 300%;
}
.image {
height: 100%;
width: auto;
opacity:0.7;
}
This code is not solving the problem, the image is not contained in the (yellow) div, as can be seen in the following screen shot:
http://www.michielvisser.nl/tmp/screenshot.jpg
How to contain the image in the div with maximal height for the image in the div and maintain aspect ratio?
SOLUTION 1: Remove the height and width from .pop-up and change height:100% in .image to height:60vh. That works perfectly. Apparently the child (img) will not adjust to the parent (div), but the parent (div) will adjust to the child (img). Sounds like real life.
SOLUTION 2: Essentially the problem arises when the window is resized (except in firefox). The solution can be to redraw the image after a resize, this solves the problem:
$(window).resize(function(){
$('img').hide();
setTimeout(function(){ $('img').show(); }, 1);
});
Your problems are:
You have an inline width and height set on your image, which is overriding the CSS styles for width and height on that image
The margin from your X is pushing the image down since the X is wrapped in a <p> tag.
You don't need object-fit at all.
The simple way to solve #1 is to delete the inline width and height from the image tag and leave it to the stylesheet.
Number 2 can be solved by wrapping the X in a div instead of a p, or you can use a pseudo element for it. I have taken the latter approach in the snippet below.
To solve #3, just delete the style from the stylesheet. (Having this property set in Safari actually messed things up for me.)
This snippet is tested in Safari 10.1.1. Note how the placeholder image is quite large by default (1000x800), but it only displays as big as it can per the parent div.
Edit: Based on your comments, let's revise this further so that we dictate the size on the image, and just let the wrapper take up the size of the image.
So on our image, in order to get it to be 60% as tall as the screen, we can do:
img {
height: 60vh;
width: auto;
}
Then, in our parent, we won't specify a width or height at all, but we can do display: flex just to make sure it is big enough to fit its contents.
body {
background: #333;
}
.pop-up {
display: flex;
position: fixed;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background: yellow;
}
.exit {
color: black;
text-decoration: none;
text-align: center;
font-size: 300%;
display: block;
position: absolute;
top: -50px;
right: -40px;
width: 40px;
height: 50px;
}
.image {
height: 60vh;
width: auto;
opacity: 0.7;
}
<div class="pop-up">
X
<img class="image" src="http://placehold.it/1000x800" alt="" title="">
</div>
I put the image above the P tag and added some CSS to .exit-button and .image
From here you can adjust padding and sizing of the elements.
body {
background: #333;
}
.pop-up {
position: fixed;
height: 60%;
width: auto;
left:50%;
top:50%;
-webkit-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
background:yellow;
object-fit: contain;
}
.exit-button {
position: absolute;
text-align: right;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: 0;
font-size: 300%;
}
.image {
height: 100%;
width: auto;
opacity:0.7;
}
<div class="pop-up">
<img class="image" src="http://icons.iconarchive.com/icons/johanchalibert/mac-osx-yosemite/1024/safari-icon.png" width="1200" height="630" alt="" title="" />
<p class="exit-button">x</p>
</div>
I copied your code and edited it. Please tell me whether this is the output you wanted or not.
body {
background: #333;
}
.pop-up {
position: fixed;
height: 60%;
width: auto;
left:50%;
top:50%;
padding-top: 30px;
-webkit-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
background:yellow;
object-fit: contain;
}
.exit-button {
margin-top: -50px;
text-align: right;
margin-right: 0;
font-size: 300%;
}
.image {
margin-top: -20px;
height: 100%;
width: auto;
opacity:0.7;
}
<div class="pop-up">
<p class="exit-button">x</p>
<img class="image" src="safari.png" alt="" title="" />
</div>
Because of either needing to hardcode in the alignment of the image given the size or deal with weird convolution, I believe this is the best way:
Create a fixed overlay occupying the entirety of the screen, create a container of 60% height, align it in the center with flexbox and stick the image inside making it occupy the entire height. The aspect ratio will update automatically (only happens with height).
As for the button – give it absolute positioning and a right position of 0, and manually give the parent relative positioning (this is necessary).
<div id="popup">
<div id="container">
X
<img src="https://i.redd.it/gelilvo30mgz.jpg">
</div>
</div>
html,
body {
height: 100%;
}
#popup {
position: fixed;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#container {
position: relative; !important // has to be specified for the children (anchor) to find the bound
height: 60%;
background: #333;
}
a {
right: 0;
position: absolute;
}
img {
height: 100%;
}
https://jsfiddle.net/L2nLjjxc/1/
I believe that's the least amount of convolution if you want it to be dynamic.

Hidden text element within centered absolute div

I'm having trouble getting a h2 header to appaer within an absolutely positioned element. I've ran through the list of possible errors, but still can't seem to find the answer. The h2 element, after inspecting the screen, has a height of 0 for some reason.
Here's a codepen link
HTML:
<div class="gallery">
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<img class='gallery-image' src='https://dummyimage.com/200x400/000000/ffffff'>
<a class='call-action' href='#'>
<h2>Shop Now.</h2>
</a>
</div>
CSS:
.gallery {
position: absolute;
z-index: 2;
width: 100%;
display: flex;
flex-wrap: wrap;
font-size: 0;
img {
display: block;
width: 25%;
height: 50%;
#media only screen and (max-width: 500px) {
width: 50%;
height: 100%;
}
}
}
$call-action-width: 150px;
$call-action-height: 50px;
.call-action {
width: $call-action-width;
height: $call-action-height;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 0;
background-color: white;
opacity: 0.5;
border-radius: 5%;
h2 {
position: relative;
color: black;
font-size: 2em;
}
}
Sorry if the answer is obvious - I've just returned to practicing coding and I'm extremely rough.
Thanks for your help!
The problem lies in your font-size. You can't have an em font within a position: absolute <div>, as em is relative to the parent element.
Simply swapping to a fixed-size font (such as px) fixes the problem. I've created a new pen showcasing this here.
Unfortunately this means that your font can't be responsive. If you want responsive font, you'll either have to use a few media queries, or restructure your HTML so that the <h2> element has a position: relative parent (so you can use em).
Hope this helps! :)

How to center the title based on the viewport width (css3)

I have this situation in which I have a header with on the left a logo and on the right some user info (like an avatar). Important to understand is that the width of the logo and the user element are not the same!
So, in the middle of the header I have a title, something like this:
<header>
<img class="logo" src="http://file.png">
<div class="user"> </div>
<h1>This is the title</h1>
</header>
I have create a DEMO
Although this header works great if you resize the area (with ellipses), however, the title is not in the middle of the screen/viewport.
So the question is, considering the behaviour when resizing, is it possible to center the title based on the viewport width ?
UPDATE: Based on the work done by Hassan Ahmad (see below) I've updated the DEMO. This is exactly what I need, but as mentioned already, the media query makes this solution a bit hacky. Any suggestions how to achieve the same without a media query ?
How about this css:
header {
position: relative;
height: 50px;
background-color: lightgrey;
text-align: center;
}
.logo {
position: absolute; top: 0; left: 0;
height: 50px;
width: 300px;
}
.user {
position: absolute; top: 0; right: 0;
width: 50px;
height: 50px;
background-color: red;
}
edit:
h1 {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin-right: 50px;
}
#media only screen and (max-width: 820px){
.logo { position: static; float: left; }
}

Responsive CSS circles that can hold centered content

I am searching for a way to create a responsive CSS3 circle that can hold centered content.
Regarding the circle, I found some good info in this question. Too bad it seems that one can't center the content in this one.
This question is also pretty similar to mine, despite the fact it's an image that should be centered. Using a background image in not an option in my case, so this option isn't working for me as well.
Do you have any ideas how I could approach this?
Of course I could use an image, but CSS would be much more elegant!
Pure CSS requiring many extra wrappers
UPDATED: Original posting (which I deleted) missed the fact that you were seeking a responsive design. Building upon my answer for the responsive circles question you reference seems to work in all CSS3 browsers, see fiddle.
HTML (requiring five levels of wrapper, I only show one circle in this html)
<div class="circles">
<div>
<div>
<div>
<div>
<!-- BEG Content -->
All types of content (see fiddle)
<!-- END Content -->
</div>
</div>
</div>
</div>
<!-- ditto the above 3 more times -->
</div>
CSS
.circles{
margin:0px auto;
}
.circles > div {
overflow:hidden;
float:left;
width:auto;
height:auto;
position: relative;
border-radius:50%;
-moz-border-radius:50%;
-webkit-border-radius:50%;
-khtml-border-radius: 50%;
background:#eee;
}
.circles > div > div {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.circles > div > div > div {
display: table;
width: 100%;
height: 100%;
}
.circles > div > div > div > div {
display: table-cell;
text-align: center;
vertical-align: middle;
}
#media (max-width: 320px)
{
.circles > div {padding: 50%;}
}
#media (min-width: 321px) and (max-width: 800px)
{
.circles > div {padding: 25%;}
}
#media (min-width: 801px)
{
.circles{width:800px}
.circles > div {padding: 12.5%;}
}
If the height of the content is fixed and you want a CSS method, use the following properties to apply to what's inside the cicle
margin: auto; /*will center the element*/
position: relative;
top: 50%;
margin-top: - [here insert the height of the element if you know in advance / 2]px
The accepted answer did not work for me, since I wanted to rotate the circle.
This does:
HTML
<div class="mycircle">
<div class="mycontent">
<span>TEXT</span>
</div>
</div>
CSS
.mycircle {
width: 30%;
height: 0;
padding: 15% 0; //padding top & bottom must equal width
border-radius: 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
background: #dedede;
}
.mycontent {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
text-align: center;
}
.mycontent:before {
content: '';
vertical-align: middle;
display: inline-block;
width: 0;
height: 100%;
}
.mycontent span {
vertical-align: middle;
display: inline-block;
}

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