See following fiddle:
HTML:
<div class='header'>Header</div>
<div class='main'>
<div class='row'>
<div class='cell lg-4'>
content
</div>
<div class='cell lg-4'>
content
</div>
<div class='cell lg-4'>
content
</div>
</div>
</div>
SASS:
.header {
display: none;
}
.main {
.row {
width: 100%;
.cell {
width: 100%;
background-color: red;
margin-bottom: 10px;
}
}
}
#media (min-width: 600px) {
.header {
display: block;
}
.cell {
margin-bottom: 0;
margin-left: 1px;
float: left;
&.lg-4 {
width: 33%
}
}
}
By looking at this code you would think it does the following:
Show by default a mobile design: i.e. cells are stacked vertically and take all screen space and the header is hidden
Above 600px: Become a row of 3 cells taking up 1/3 of the horizontal space each and show the header.
If you try it in the fiddle, you will see that in both viewports (above and below 600px) the cells are shown stacked vertically however the header does get hidden or shown as specified in the media query.
After searching for quite some time, I realised the query becomes effective for cells only if the media query adopts the exact same nested structure as the normal sass code, i.e.:
#media (min-width: 600px) {
.header {
display: block;
}
.main {
.row {
.cell {
margin-bottom: 0;
margin-left: 1px;
float: left;
&.lg-4 {
width: 33%
}
}
}
}
}
Why does this happen, and more importantly, how to avoid having to reuse the same structure in media queries? (the workaround for this fiddle is simple, but my actual code has more than 10 to 20 nested variables so adding a media query for the 20th element would force me to add 19 useless lines of nested variables, quickly overloading the code and making it difficult to read)
Maybe I am doing this wrong, as I am rather new to making my own responsive design, so am I missing some best practices that avoid this situation?
It's because .main .row .cell is more specific than .cell in a #media query.
It's good practice to reduce nesting as much as possible in order to prevent this exact situation happening as it's a pain to get around. Some other problems that come out of a lot of nesting is that it makes styles non-modular and difficult to reuse, as they rely on the exact structure, it can also bad for performance.
I recommend splitting up the top section like this:
Demo
.header {
display: none;
}
.main {
/* ... */
}
.row {
width: 100%;
}
.cell {
width: 100%;
background-color: red;
margin-bottom: 10px;
}
#media (min-width: 600px) {
.header {
display: block;
}
.cell {
margin-bottom: 0;
margin-left: 1px;
float: left;
&.lg-4 {
width: 33%
}
}
}
Related
My site: https://www.gameron.pl/
I need to make the theme like in the screenshot for the resolution 719px and below:
I mean posts list of course. I tried to achieve it with various codes, but each works only on one resolution and is not adjusting to other resolutions.
Here's what works:
#media (max-width:719px){
/* Thumbnail Image */
.main a .wp-post-image{
width:50%;
}
}
Now I just need to move and adjust post title properly. Does anyone have an idea how can I do it? I feel like I tried everything.
The problem is your .post-thumbnail has float:none, and your .post-content has float:left.
I changed it in the console so both float:left and width:50% and it works fine:
https://i.imgur.com/Q8BFev0.png
#media only screen and (max-width: 719px) {
.post-list .post-thumbnail {
float: left;
max-width: 50%;
}
.post-list .post-content {
float:left;
width: 50%;
}
}
You can do it as follows:
#media only screen and (max-width: 719px) {
.main .post-list .post-thumbnail,
.main .post-list .post-content {
float: left;
width: 49%;
}
.main .post-thumbnail {
margin-right: 0.5em;
}
}
Result
here my website I´m working on: http://www.whatsahoy.com/
My problem: I want the input field next to the button. If I add a float left, it goes next to each other but with a weird <br>. And then the whole thing is also on the left. I want it to be in the middle.
Can somebody help me please?! Thank you very much!
Barbara
To provide you with the best solution, it would be helpful to see the source code of your form. The <br> is probably in there somewhere. However, here is my reply based on what I can see. If it does not help, I suggest you update your question.
Adding the following styles to your custom CSS will put the field and button next to eachother until screen width of 767px.
.wpcf7-form p {
display: inline-block;
width: 470px;
max-width: 95%;
}
.wpcf7-form p br {
display: none;
}
If you would like them next to eachother on smaller screens, you could change the width of the frame. This changes on 767px as set by your theme. I'm not sure this will not have an undesired effect elsewhere on your site, but you could try.
#media only screen and ( max-width: 767px ) {
.et_pb_row {
width: 600px;
}
}
However, it then will be crippled on screens as from 600px. It may be better to make the text field smaller then...
#media only screen and ( max-width: 600px ) {
.wpcf7 input {
max-width: 50%;
}
}
You might want to fine tune this a bit, but I hope you get the idea.
GL!
.wpcf7-form-control-wrap {
display: inline-block !important;
margin: -8px;
}
form.wpcf7-form.init.mailchimp-ext-0\.5\.55 {
display: flex !important;
justify-content: center !important;
}
input.wpcf7-form-control.has-spinner.wpcf7-submit {
border-radius:0px !important;
width: 30%;
}
#media only screen and (min-width: 50px) and (max-width: 600px) {
div#wpcf7-f698-p10-o1 {
width: 130%;
}
#media only screen and (min-width: 50px) and (max-width: 600px) {
input.wpcf7-form-control.wpcf7-text.wpcf7-email.wpcf7-validates-as-required.wpcf7-validates-as-email {
width: 235px;
}
form.wpcf7-form.init.mailchimp-ext-0\.5\.55 {
display: flex !important;
padding-right: 70px;
}}
So I have a page for displaying products, and I'm trying to convert that page from a table-based layout where each spot is a table cell, three to a row, into a dynamic grid of some sort.
Unfortunately, using inline-block is a pain due to the "keep whitespace like it matters between inline-block" issue, and using floats is... ok, but tends to result in gaps in the listings (see attached image).
The tiles have a max and min width, so it seems like waterfall or pinterest type tiling shouldn't necessary, since I'm not really dealing with variable-height and width rectangles.
So what techniques are best for making this kind of grid listing fill available space regularly, but still allow rows to be shorter for shorter screens?
There's an in-development example of the problem page here: http://www.shermanbrothers.com/catalog.php
the technique is called Liquid design or if you have to support smart phones and tablets, then it will be "Responsive design".
In your scenario, first you need to turn the fixed-width table to a liquid grid. The code snippets are:
JsFiddle
CSS:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.container {
width: auto;
}
.container:after {
content: " ";
clear: both;
display: table;
}
.tabletContainer {
/*The total width for the first two column*/
width: 67%;
float: left;
display: block;
}
.left {
background-color: red;
float: left;
/*Each column takes have of the container size, so their width =67/2 = 33.5%*/
width: 50%;
}
.middle {
background-color: yellow;
float: right;
/*Each column takes have of the container size, so their width =67/2 = 33.5%*/
width: 50%;
}
.right {
float: right;
width: 33%;
background-color: blue;
}
/*For tablet devices, show only the two columns in a row and a column underneath*/
#media (min-width: 481px) and (max-width: 768px) {
.tabletContainer, .right {
float: none;
width: auto;
}
.right
{
clear: both;
width: 50%;
}
}
/*For mobile phones, show only one column*/
#media (max-width: 480px) {
.tabletContainer, .left, .right, .middle {
float: none;
width: auto;
display: block;
}
}
HTML
<div class="container">
<div class="tabletContainer">
<div class="left">It is well enough that people of the nation do not understand our banking and monetary system, for if they did, I believe there would be a revolution before tomorrow morning.
</div>
<div class="middle">Under capitalism, man exploits man. Under communism, it's just the opposite.
</div>
</div>
<div class="right">One of the funny things about the stock market is that every time one person buys, another sells, and both think they are astute
</div>
</div>
You also need to make your images liquid as well. One trick is to remove the fixed-widths and heights of your images.
CSS
/*Make images not resize outside of the column that they are in*/
img {
max-width: 100%;
}
HTML
<img src="imgs/clouds.jpg" alt="Clouds" class="half right"/>
You can also change the image size by using %. For instance, the width of the image in the example above will be set to 50% of the container width by using the following CSS.
img.half {
max-width: 50%;
}
If you want to float it to the left of the container:
img.left {
float: left;
margin: 0 10px 10px 0;
}
By applying padding/margins you can achieve the effect you want.
Hope this help.
First of all, here's the jsfiddle for the particular markup/styling in question.
Main question is why the img and text box (dark_block) do not have the same margin. Both are set to 100% width of the container div, so I'm not sure what's up. Mind taking a look?
Other things I'm still trying to figure out and googling (thus far) has not helped me:
When the text box is in-line (to the left) of the photo container, how do I get it to be the same height as the photo container
If the image's width is smaller than the photo container, how do I get it to center horizontally and vertically?
For accessibility sake, can I just create a non-responsive version of the css before the #media tag stuff?
Sorry, I'm sort of new to web development, and any help would definitely be appreciated. Also if anything in the code fragment seems awfully done, call me out! I'd love to learn some best-practices in addition to solving the issue at hand. Especially display types, having a hard time wrapping my head around 'em.
Appreciate you taking the time to look at this!
John
CODE:
<div id="home_top_container">
<div id="photo_slider">
<img src="redacted">
</div>
<div id="dark_block"></div>
</div>
#home_top_contianer {
width: 100%;
margin-left: 10px;
margin-right: 10px;
margin-bottom: 20px;
}
#media screen and (min-width: 800px){
#photo_slider{
float:right;
background-color: #cccccc;
padding: 0px;
width: 69%;
min-width: 500px;
display: inline-block;
}
}
#media screen and (max-width: 799px){
#photo_slider{
float:none;
background-color: #cccccc;
padding: 0px;
width: 100%;
min-width: 500px;
display: inline-block;
}
}
#media screen and (min-width: 800px){
#dark_block {
float:left;
background-color: #383838;
padding: 10px;
width: 28%;
display: inline-block;
}
}
#media screen and (max-width: 799px){
#dark_block {
float:left;
background-color: #383838;
margin-top: 20px;
padding: 10px;
width: 100%;
min-height: 200px;
display: inline-block;
}
}
img {
max-width: 100%;
margin: 0px;
}
You need to read up on the CSS box model. The width of an element refers to its content. The padding, border and margin are then added it to it. That means your #dark_block is actually 100% + 2*10px wide.
The proper solution would be to set #dark_block to display: block and remove both floatand width. The default value for width is auto, which automatically makes the block as wide s possible without overflowing. Rule of thumb in web development: If you give a display: block element width: 100%, then you are doing something wrong.
Another simple solution would be to set box-sizing: border-box; on #dark_block, however box-sizing is a relatively new property, so it won't work if you need to support older browsers.
Getting them to the same height, is not a trivial thing. You could use the display: table-* properties, and give them height: 100% but that requires you to put #dark_block first in the HTML.
Quick example:
<div id="home_top_container">
<div>
<div id="dark_block"></div>
<div id="photo_slider">
<img src="http://caldwellfellows.ncsu.edu/wp/wp-content/uploads/2013/06/Justin-sews.jpg">
</div>
</div>
</div>
#home_top_container > div > div {
display: table-cell;
width: 50%;
border: 1px solid red;
}
img {
width: 100%;
}
Again centering vertically is not a trivial thing in CSS. Your best bet would be to use display: table-cell with vertical-align: middle.
Most certainly. Especially you should move all properties that are common to all media-variants to outside the media rules, so that you don't repeat them.
Also it's no need to repeat the media rules around each rule. Just have one media rule:
#media screen and (min-width: 800px) {
#photo_slider {
/* ... */
}
#dark_block {
/* ... */
}
}
#media screen and (max-width: 799px) {
#photo_slider {
/* ... */
}
#dark_block {
/* ... */
}
}
I've been having some issues with my CSS3 media queries...
Here's a small snippet of one I'm currently working on:
#media only screen
and (max-width : 420px) {
.page { min-width: 300px; max-width: 480px; width: 100%; }
.page .alpha { font-size: 2em; }
/* Set-up the column */
.page .column { margin: 0 auto 2%; width: auto; }
.page .gallery .column { min-height: 470px; height: auto; padding: 2%; }
}
/* Increase the main title for slightly larger screens! */
#media only screen
and (max-width : 480px) {
.page .alpha { font-size: 3em; }
}
I'm working from a 'mobile first' standpoint and given the normal behaviour of CSS regarding the 'cascading' aspect I would expect the second #media statement to inherit all of the styles from the previous statement, whilst overriding any for which it has a matching or 'heavier' selector.
(Plus CSS's order of precedence would mean any matching style definitions would use the last defined rule-set unless 'trumped' with an !important statement!)
From what I've seen though, through testing and some Google / SE searches this is not the case.
Is it possible for #media style rules to inherit from applicable earlier statements or am I stuck with having to repeat all the rules I need for each statement? (not very DRY)
I'd really appreciate any help and clarifications / explanations for this.
Firstly thanks #BoltClock (for both comments), and to the other comments and answers for all your help.
I think I made a mistake in my media queries and/or was miss-understanding how they worked and interacted together. I was going to edit my question with the following but decided it would make more sense as an answer (since it's the solution I used). I apologise if this has wasted anyone else's time.
Here's my fixed snippet of code:
#media only screen
and (max-width : 480px) {
.page { min-width: 300px; max-width: 480px; width: 100%; }
.page .alpha { font-size: 2em; }
/* Set-up the column */
.page .column { margin: 0 auto 2%; width: auto; }
.page .gallery .column { min-height: 470px; height: auto; padding: 2%; }
}
/* Increase the main title for slightly larger screens! */
#media only screen
and (min-width : 421px)
and (max-width : 480px) {
.page .alpha { font-size: 3em; }
}
I realised from your comments that if I increased the max-width in my first block to cover the necessary range/limit I could then either nest or add the second block after it (I tried both and they both worked for me -- using chromium browser [18.0.1025.151]). This successfully gave me the desired result, in that the page .alpha element's font size increased at the required stepping/interval.
Thanks again for all SO'ers who helped!
(and to SE for the awesome communities they've helped build)
Knowledge > OpenSource > Freedom
If you want to work from mobile up, you will need to set the mobile layout as the default layout. (Remove the query). From there the queries will inherit from above.
.page { min-width: 300px; max-width: 480px; width: 100%; }
.page .alpha { font-size: 2em; }
/* Set-up the column */
.page .column { margin: 0 auto 2%; width: auto; }
.page .gallery .column { min-height: 470px; height: auto; padding: 2%; }
/* Increase the main title for slightly larger screens! */
#media only screen
and (max-width : 480px) {
.page .alpha { font-size: 3em; }
}