I thought this would be simple but it's proving to be a bit of a headache. I'm trying to get a grid of images to re-center when the user resizes the browser and causes one (or more) of them to wrap onto the next line.
I've tried giving the grid-wrapper display:inline-block; and it's parent a value of text-align: center; but this doesn't re-center the elements when they wrap to a new line. Help appreciated.
For a visual of what I'm trying to achieve view
(source: ianclarke.ca)
.
HTML:
<div class="parent-wrapper">
<div class="child-wrapper">
<!-- Worpress loop to load many thumnails -->
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class="project-thumbnail">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<?php endwhile; ?>
</div>
</div>
CSS:
.parent-wrapper
{
width:100%;
text-align: center;
}
.child-wrapper
{
display:inline-block;
}
.project-thumbnail{
float:left;
border:2px solid black;
min-width: 269px;
max-width: 269px;
}
This is the best solution I can think of with CSS only, the magic part is the #media queries. Obviously you'll have to do the math to fit your case.
JsFiddle Demo
body {
margin: 0;
}
.parent-wrapper {
margin: auto;
width: 500px;
padding: 5px 0;
font-size: 0;
}
.child-wrapper {
display: inline-block;
width: 100px;
font-size: 16px;
}
.child-wrapper img {
max-width: 100%;
height: auto;
padding: 5px;
vertical-align: top;
box-sizing: border-box;
}
#media screen and (max-width: 499px) {
.parent-wrapper { width: 400px; }
}
#media screen and (max-width: 399px) {
.parent-wrapper { width: 300px; }
}
#media screen and (max-width: 299px) {
.parent-wrapper { width: 200px; }
}
#media screen and (max-width: 199px) {
.parent-wrapper { width: 100px; }
}
<div class="parent-wrapper">
<div class="child-wrapper">
<img src="//dummyimage.com/100" />
</div>
<div class="child-wrapper">
<img src="//dummyimage.com/100" />
</div>
<div class="child-wrapper">
<img src="//dummyimage.com/100" />
</div>
<div class="child-wrapper">
<img src="//dummyimage.com/100" />
</div>
<div class="child-wrapper">
<img src="//dummyimage.com/100" />
</div>
<div class="child-wrapper">
<img src="//dummyimage.com/100" />
</div>
</div>
I found a very similar question with two functional answers. One uses JS and the other uses placeholder elements. Neither are very pretty, but both appear to work around the inline-block whitespace wrap problem here.
Shrink-wrap and center a container for inline-block elements
Have you tried:
.child-wrapper{margin:0 auto;}
So it stays centered? It usually works.
Please add float:left to class .project-thumbnail for the browser breakpoints specific to different browser / screen/device sizes.
.project-thumbnail{
float:left;
border:2px solid black;
min-width: 269px;
max-width: 269px;
}
Add margin: 0, auto; to following class.
.child-wrapper
{
margin: 0, auto;
}
Going through the PHP code I understood that the DIV with the class name .project-thumbnail gets repeated through WHILE loop iterations.
Related
So I am in the process of mobilizing my website and I want to be able to change the way my page looks when on an iPhone.
I have two divs, one that floats left, and one that floats right so they are horizontally next to each other.
However, I am trying to figure out how can I change these two "blocks" so that they are on top of each other when looking on a phone? Here is my code for the desktop version:
<header class="intro-about">
<div class="container-fluid">
<div class="left" style="background-color: #282828;">
<h1 class="text-center">Get to know my work</h1>
</div>
<div class="right" style="background-color: #282828;">
<h1 class="text-center">Get to know me</h1>
</div>
</div>
</header>
And it looks like this:
However, I am trying to get it to look like this on a mobile phone:
I'm looking to use media queries too in my css.
This is very easy to do using display:flex and #media(max-width):
* {
box-sizing: border-box;
}
body, html {
height: 100%;
margin: 0;
}
.container {
width: 100%;
height: 100%;
display: flex;
/*Do not forget next line, or items won't wrap*/
flex-wrap: wrap;
}
.item {
padding: 1rem;
flex: 1 0 50%;
}
.left {
background-color: blue;
}
.right {
background-color: grey;
}
/*Layout changes on screen width 700px*/
#media(max-width: 700px) {
.item {
flex: 1 0 100%;
}
}
<div class="container">
<div class="item left">Some left content</div>
<div class="item right">Some right content</div>
</div>
Figure out at which width you want to make them stack, then add the following to your CSS with the width after max-width:
#media {max-width: 700px) {
.left, .right {
float: none;
width: 100%;
}
}
There are quite a lot of questions on this subject, but no answer resolved my problem.
I want that for small screens there is no floating around some image. So I have
#media (max-device-width: 639px), (max-width: 639px) {
div.enimage { display: block; clear:both; }
div.enimage img { max-width: 100%; height: auto; border: 0; display: block; clear:both; }
}
for this
<div class="enimage">
<img align="left" border="0" height="192" width="264" src="pic/img2.jpg" />
</div>
<p>Some text.</p>
It does not work, text is still floated right of the image! What am I doing wrong?
Don't use the align attribute. It has been deprecated for 20 years now.
Add float: left to the image's CSS rules and float: none when you want to remove it.
div.enimage img {
float: left;
}
#media (max-device-width: 639px), (max-width: 639px) {
div.enimage img {
float: none;
}
}
<div class="enimage">
<img height="192" width="264" src="pic/img2.jpg">
</div>
<p>Some text.</p>
use display: inline-block; instead of display:block;
div.enimage {
display: inline-block;
clear: both;
}
I would like to place to DIVS (grey & red) inside a DIV (black) under the first DIV (black) when you resize the window and the screen is less than 1024 px. Take a look at the example under. You can also see the image attached.
I would really like som advice here, im totally lost here at the moment.
This is how I want it to be on screens more than 1024px:
<div id="black">
<div id="grey"></div>
<div id="red"></div>
</div>
This is how I want it to be on screens less than 1024 px:
<div id="black"></div>
<div id="grey"></div>
<div id="red"></div>
There is no need to duplicate the content.
#black{background:black;overflow:hidden;}
#grey, #red{
min-height:100px;
margin:2%;
float:left;
width:47%;
}
#grey{background:gray;margin-right:1%}
#red{background:red;margin-left:1%}
#media (min-width:1024px){
#black{padding-top:100px;}
#grey, #red{
float:none;
width:auto;
margin:0;
}
}
<div id="black">
<div id="grey"></div>
<div id="red"></div>
</div>
Sorry, but that is not possible as written.
You cannot move items outside their containing structures using CSS. You can only reformat them within their present structure.
Optionally, you can duplicate the existing black div and show/hide one or the other based on media queries (screen size).
Or, you can use jQuery/javascript to move the DOM items. But CSS alone cannot do this.
Using just CSS (with media queries) and two container <div>s to separate logic:
#media (max-width: 1024px) {
.large { display: none; }
.small { display: block; }
.black { height: 100px; }
}
#media (min-width: 1025px) {
.large { display: block; }
.small { display: none; }
.red, .grey { float: left; }
.black:after { content: ""; display: table; clear: both; }
.red { width: calc(50% - 5px); margin-left: 10px; }
.grey { width: calc(50% - 5px); }
}
.large {
height: 200px;
}
.small {
height: 200px;
}
.black {
background-color: black;=
height: 100px;
padding: 10px;
box-sizing: border-box;
}
.red {
background-color: red;
height: 100px;
}
.grey {
background-color: grey;
height: 100px;
}
<div class="large">
<div class="black">
<div class="grey"></div>
<div class="red"></div>
</div>
</div>
<div class="small">
<div class="black"></div>
<div class="grey"></div>
<div class="red"></div>
</div>
Above snippet better viewed in full page or this codepen
I have simple structure with container and inside boxes:
<div id="container">
<div class="block"></div>
// more blocks
<div class="block"></div>
</div>
What I would like to achieve is to center boxes inside this container but to pack them as much as possible in a one line. The same I can do using JS: http://jsfiddle.net/JhxSd/ but I would like to avoid that, and use only CSS. Is that possible?
#media queries
Use a set of #media queries to define different layouts for the grid based on the current screen size. The only part of the layout that needs to vary is the width of the grid wrapper.
For all practical purposes, this is the only CSS solution available at present. For an explanation of why #media queries are appropriate, and why other available CSS options won't work, see this answer.
JSFiddle Demo
The above demo has #media queries for screen sizes up to 1200px wide (more can be added as needed), and does not use JavaScript. The rendered width of #container is always 75% (not counting the border), and the grid is centered within #container.
Note: This solution requires adding a wrapper div around the blocks. In each #media query, the width of the wrapper is just enough to fit the number of columns appropriate for the current screen size. The fixed wrapper width is what allows the grid as a whole to be centered within #container. If editing the static HTML isn't an option, the wrapper div can be added when the page loads using jQuery.
HTML
<div id="container">
<div class="grid-wrapper">
<div class="block"></div>
...
</div>
</div>
CSS
#container {
width: 75%;
...
}
.grid-wrapper {
margin: 0 auto;
width: 70px; /* Default: 1 column */
}
#media (min-width: 200px) {
.grid-wrapper {width: 140px;} /* 2 columns */
}
#media (min-width: 290px) {
.grid-wrapper {width: 210px;} /* 3 columns */
}
...
I hope this will do the trick:
http://jsfiddle.net/CnjZR/1/
<div id="container">
<div id="wrap">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
CSS:
#container {
width: 100%;
overflow: hidden;
background: red;
text-align: center;
}
.block {
width: 20px;
height: 20px;
background: blue;
float: left;
margin: 5px;
}
#wrap {
background: green;
overflow: hidden;
display: inline-block;
}
Not too sure if you where looking for something like 'flex-justify' , I added in the demo a turn around based on inline-boxes behavior and text-align values.
edit : point cleared: text-align:center ; is it.
http://jsfiddle.net/JhxSd/10/
The point is you should not use float, but display.
Float is not friendly with centering , nor vertical nor horizontal, since it is not standing in the natural flow of the document.
#container {
width: 75%;
border: 1px solid;
text-align:center;
overflow:hidden;
padding:1em 1em 0;
box-sizing:border-box;
float:left;
}
#container .block {
width: 50px;
height: 50px;
background-color: blue;
display:inline-block;
margin: 10px;
}
I think, everything you have almost done already.
#container {
width: 75%;
border: 1px solid;
float: left;
}
#container .block {
width: 50px;
height: 50px;
background-color: blue;
float: left;
margin: 10px;
overflow: hidden;
}
http://jsfiddle.net/JhxSd/3/
Try this:
#container {
width: 75%;
border: 1px solid;
float: left;
overflow: hidden;
text-align: center;
}
#container .block {
display: inline-block;
width: 50px;
height: 50px;
background-color: blue;
margin: 10px;
}
If you truly need everything left-aligned then I think you're out of luck with just CSS.
You can use the text-align:justify for the container and use the display:inline-block for the div.block. but you need add some placeholder tag at the last.Like this:
HTML
<div class="wrapper">
<div class="block">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block">4</div>
<div class="block">5</div>
<div class="block">6</div>
<div class="block">7</div>
<div class="block">8</div>
<div class="block">9</div>
<div class="block">10</div>
<div class="block">11</div>
<div class="block">12</div>
<div class="block">13</div>
<div class="block">14</div>
<div class="block">15</div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
CSS
.wrapper {
width: 75%;
border: 1px solid;
font-size: 0.1px;
text-align: justify;
}
.wrapper:after {
content:"";
display:inline-block;
width: 100%;
}
.wrapper div{
font-size: 16px;
display:inline-block;
*display: inline;
zoom:1;
color: #fff;
background-color:blue;
width: 50px;
height: 50px;
margin: 10px;
}
.wrapper .placeholder {
width: 50px;
height: 0px;
background:none;
}
Please view the demo. A detailed tutorial, please click here.
I have a mostly fixed 2-column layout:
1st column that takes as much space as it needs
and the 2nd column that has a fixed width and is floated to the right
I want to make it a responsive design, so in smaller screen sizes, the 2nd column ends below the 1st column. This means that the HTML for the 2nd column has to be after the 1st column.
My problem is that I can't seem to float the right column properly when using that mark-up. It ends up floated, but is below the main content.
Here's a sample code:
HTML:
<div class='content'>
sdaohdosahdaf
<br />adfafhaskldjs sgdafadkfjas
<br />saodfhdpaofa]sdoas [fdf asfasfjasfdkasfad;sdlafa dfasds dad gad</div>
<div class='sidebar'>
daobfhaohfasod agsdjfa
<br />sidjaofhad
<br />sojghfadpfjas
</div>
CSS:
.content {
background-color: lightblue;
margin-right: 120px;
}
.sidebar {
float: right;
width: 120px;
background-color: lightgreen;
}
JS fiddle here: http://jsfiddle.net/zinkkrysty/U3fA8/
I tried also using negative margins, but can't seem to do it.
If you have any other approaches to my problem (except setting a width on the first column), please give it a go
Put them into a parent and set the position of the side bar as absolute, then use a different set of styles for the responsive display
html
<div class="parent">
<div class='content'>
sdaohdosahdaf
<br />adfafhaskldjs sgdafadkfjas
<br />saodfhdpaofa]sdoas [fdf asfasfjasfdkasfad;sdlafa dfasds dad gad
</div>
<div class='sidebar'>
daobf haohf asod agsdjfa
<br />sidja ofhad
<br />sojgh fadpfjas
</div>
</div>
css
.parent {
position: relative;
}
.content {
background-color: lightblue;
margin-right: 120px;
}
.sidebar {
position: absolute;
top: 0px;
right: 0px;
width: 120px;
background-color: lightgreen;
}
/* Responsive bit */
#media only screen and (max-width: 320px) {
.content {
background-color: orange;
margin-right: 0px;
}
.sidebar {
background-color: red;
position: relative;
width: 100%;
}
}
Example
Let the first div have a position:absolute; and with left:0px; and the desired right you can have a fixed floated right div with a dynamic floated left div.
DEMO
HTML
<div id="parentDiv">
<div id="leftDiv">
</div>
<div id="rightDiv">
</div>
</div>
CSS
#parentDiv
{
position:relative;
overflow:hidden;
}
#leftDiv
{
position:absolute;
left:0px;
top:0px;
right:200px; //Increase it if you want a little space between the 2 divs.
min-height:200px; // or bottom:0px; if you want a dynamic height
}
#rightDiv
{
width:200px;
float:right;
min-height:200px;
}
DEMO
Try negative margins in %
E.g. margin-top: -10%;
Try like below... it will help you...
.content {
background-color: lightblue;
float: left;
}
.sidebar {
float: left;
width: 120px;
background-color: lightgreen;
}
I think you needed like this... Try the updated Fiddle : http://jsfiddle.net/U3fA8/1/
Looking at your fiddle JUST ADD margin-top:-60px; in your .sidebar css and it will move up. Your css will look like this:
.content {
background-color: lightblue;
margin-right: 120px;
}
.sidebar {
float: right;
width: 120px;
margin-top:-60px;
background-color: lightgreen;
}
Building on #9205892's insight that you need another wrapper div, I've found that you avoid using absolute positioning (which can create problems with any content after the two columns), and still getting a responsive design. Just set the two sections to "float" and specify their widths (I just gave them percentages). Then in the responsive #media section, set the width of each section to 100% and the browser will put them one after the other.
HTML
<div class="parent">
<div class='content'>
sdaohdosahdaf
<br />adfafhaskldjs sgdafadkfjas
<br />saodfhdpaofa]sdoas [fdf asfasfjasfdkasfad;sdlafa dfasds dad gad
</div>
<div class='sidebar'>
daobf haohf asod agsdjfa
<br />sidja ofhad
<br />sojgh fadpfjas
</div>
</div>
CSS
.parent {
position: relative;
}
.content {
background-color: lightblue;
width: 60%;
float: left;
}
.sidebar {
top: 0px;
right: 0px;
width: 40%; /* 100% - .content.width */
float: left;
background-color: lightgreen;
}
/* Responsive bit*/
#media only screen and (max-width: 320px) {
.content {
background-color: orange;
width: 100%; /* Leave no room for sidebar */
}
.sidebar {
background-color: red;
width: 100%; /* Sidebar is puffed up */
}
}
Demo (based on the demos from the other answers)
The simplest solution would be to swap your content and sidebar div.
Instead of
<div class='content'>
sdaohdosahdaf
<br />adfafhaskldjs sgdafadkfjas
<br />saodfhdpaofa]sdoas [fdf asfasfjasfdkasfad;sdlafa dfasds dad gad</div>
<div class='sidebar'>
daobfhaohfasod agsdjfa
<br />sidjaofhad
<br />sojghfadpfjas
</div>
You do:
<div class='sidebar'>
daobfhaohfasod agsdjfa
<br />sidjaofhad
<br />sojghfadpfjas
</div>
<div class='content'>
sdaohdosahdaf
<br />adfafhaskldjs sgdafadkfjas
<br />saodfhdpaofa]sdoas [fdf asfasfjasfdkasfad;sdlafa dfasds dad gad</div>
Your css stays the same. Here's how it looks like, exactly what you're looking for :)
http://jsfiddle.net/j1wu/vg08f4ep/