Everytime i work on a responsive design I wonder why all media queries are fixed on the screens width/height. Why not the width/height of an element? Is this possible?
When making reusable html elements in a somewhat modular way, it seems logical to 'scale/design' it relative to it's own width and not the screens width. This way you only have to design the HTML/CSS once and you can use it in different sections of your page without needing to add extra media queries.
Obviously there are workarounds, but is there a css-only way to add css rules depending on it's own or parents width/height?
Regards,
Pedro
ContainerQueries A.K.A. ElementQueries are still waiting to be implemented natively in CSS. I think this is very sad, but it doesn't look like that it does any progress for years now.
Related
I made a Navigation bar in the header and have added media queries for mobile devices and table but any thing in middle of this makes the page layout look bad so do i need to add more media queries for all measurements or is there an other way of making page more responsive.
I tried doing it with media queries but was wondering if their is another way around.
You can use libraries like Bootstrap or Tailwind and its easy to learn and use,
or you can just use flexbox or grids in CSS.
Learn more about Flexbox on MDN, using this link.
Learn more about the CSS Grid Layout system on MDN, using this link.
you can use your same media queries but make sure that your sizes are in
em
or
%
in this case everything will go smaller and bigger according to the user's device width.
that's in case you don't want to use a css library which is also recommended.
First, lets keep all simple, is not required to use libraries to build a responsive views
You need to implement mobile first, implement mobile then you can adjust only required elements to other break points (tablet, desktop, ...)
Here you can use break points, flex, grid and positions
Manage all spaces (margins, paddings) with parent container to make less changes, here you can take advantage of flex and grid to centered and spaces between elements the trick is property gap, in this days is not required to use tables to adjust elements
If you are building components dont forget, all components should be reusable, configurable and mantenible
Here is some interesting content:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
https://css-tricks.com/snippets/css/complete-guide-grid/
https://developer.mozilla.org/en-US/docs/Web/CSS/position
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
Do you still need media queries to make website responsive when you have already used flex box? Are there any instances where media queries give us better control?
Yes there are a lot of things where you would still use media-queries.
You would probably want to switch from flex-basis: 200px; to a more flexible flex-basis: 50%; width on mobiles.
A combination of both is probably best.
If you are working with percentages and wan't a different column count on smaller devices there is imho no way around media queries.
Flex-box and Grid Layout, along with Multi-column layout are responsive by default. These specifications were written in a world where responsive design and supporting multiple devices was already a thing. This means that they include a lot of common sense functionality, which enables responsive design without us needing to do much.
If we want to change the size or proportions of some columns, we have to add a break-point using a Media Query and redefine them. We have no other options when working in percentages, as they will always be the same percentage of the container they are in, whether that container is wide or narrow.
FlexBox is one thing.
Responsive Web Desing (RWD) is something different.
FlexBox is a native CSS set of properties allowing you to apply some specific layout-related styles to your parent and child elements.
RWD is an approach of adjusting the site to fit every screen size. In order to achieve this, you use #MediaQueries.
Flexbox is not a replacement for mediaQueries. Magic happens when you use them together.
Imagine a situation where you want to replace an img scr in the "background" css property when user's screen size goes below 768px. How would You do that with Flexbox but without #Media rules ?
MediaQueries are really powerful and give you a lot of possibilities to control different layouts on your site based on the screen width.
I have to make a checkerboard homepage with several div's containing pictures and text.
I made something like this :
In the CSS, all blocks are placed with an absolute position and some top, left properties (in px).
The design is correct, but I want to make all this responsive, but in 2 different ways :
1 - Adapt all the body to the screen and eventually increase the font-size property.
2 - Simply align all the blocks in two or one column(s) (for smartphones for example)
I don't want an answer with a ready-made code, just help to realize the responsive version of my homepage.
You should really look into using a CSS framework rather than relying on absolute positioning, which is difficult to adapt for use with multiple screen sizes and make responsive.
Although there is a learning curve when first attempting to use a CSS framework, it quickly pays dividends, especially when taking into account cross-browser and responsive-design considerations.
Probably the most popular CSS framework is Bootstrap, which is responsive by default, and would make producing your layout relatively simple.
Another good option for a CSS framework is Foundation.
There are countless others. Here's a list: http://usablica.github.io/front-end-frameworks/compare.html
I used to realize the responsive with the float columns and change the columns' width through media query. But in your job, you should change the hope wrapper's width and reset the columns' left and top through js. You can consult the plugin 'Masonry', which may help you.
I mobilizing a website and would like to know if I can use one or more rather simple css rules to shrink a main div that has many nested divs and still have everything look proportonate in the smaller screen. The page is... http://www.junkyardzombiez.com/choose-a-zombie/ and the container is Any help is appreciated.
There is no magical way to to do as it's something that needs to be built into the site initially. If you had written the CSS to use % rather than PX then you would have more scope to work with but hard coded PX values mean it's built to stay at that size.
As it stands at the moment, you will need to change a number of classes to scale in width based on screen size.
It's worth looking at some frameworks like bootstrap that can help with this.
I make use of percentages by specifying the width of elements I want to stretch and percentages when specifying "left" "left:40%;" for example and a relative position. This allows elements to move with the page when it's resized and stretch but at certain resolutions sizes the elements don't entirely stay in place. Am I stuck using media queries for this issue or is there another way? I'm still using media queries but I also don't want my design breaking so early before reaching breakpoints, I want it to be as flexible/elastic as possible before having to have media queries kick in.
I think instead of worrying about those wacky sizes where your page looks funky, you should think about the most common screen sizes that are actually used.
I know exactly what you mean about those breakpoints. That's why I use screenfly to test my site on the most common screen sizes and work from there.
And if your site elements aren't maintaining your desired positions, then you are probably calculating the percentages wrongly. Here is a great article that helped me with fluid layouts and how to set up the percentages and what-not: http://www.creativebloq.com/css3/create-fluid-layouts-html5-and-css3-9122768
Use float: left with a percentage width instead, and that should accomplish what you need. If you're using left: 40% for an offset (gap or spacing), try using margin-left.