Why does it not work properly media query? - css

When I write max-width(1220px) it works when width is 1300px and when I write second breakpoint 1150px now from 1300px works only 1150px and ignored 1220px
HTML CODE
<div class="container">
<div class="inner">
<div class="left">arrow left</div>
<div class="right">arrow right</div>
</div>
</div>
STYLE CODE
.container {
width: 1600px;
margin: auto;
}
.inner {
position: relative;
}
.left,
.right {
position: absolute;
}
.left {
left: -50px
}
.right {
left: 300px
}
#media (max-width: 1220px) {
.right {
left: 250px
}
}
#media (max-width: 1150px) {
.right {
left: 210px
}
}
you can see the image description here
https://i.stack.imgur.com/7viiX.jpg

max-width: 1220px media query applies on devices with the width of 1220px or smaller, same thing happens for max-width: 1150.
This is what should happen in your code with both breakpoints:
When device width greater than 1220px --> .right { left: 300px; }
When device width smaller than 1220px and greater than 1150px --> .right { left: 250px; }
when device width is smaller than 1150px --> .right { left: 210px; }

Related

Responsive design - mantain the aligment of a div to the left when the width becomes smaller

I have the following layout:
a wrapper that get all the width of the browser
a wrapper-right(menu) that is always close to the browser right
header that has a max width and is centered
content align with header
When the resolution is below a specific step(32rem) and wrapper_right get close to the header I want the header to get a smaller width, so the wrapper_right doesn't go over it.
The problem is that the header doesn't remain align to the left to the content, being set to left,right auto.
I try to use margin-left:80px, but doesn't work properlly.
If the resolution goes below 27rem I want the wrapper_right to be hidden, and header back to normal.
OBS. 27rem, 32rem are just for example, to be visible in the code box. I can modify the html code if is necessary.
.wrapper {
height: 6rem;
position: relative;
width: 100%;
background-color: red;
display: inline-block;
}
.header {
margin: 1.5rem auto 0;
max-width: 30rem;
background-color: blue;
}
#media (max-width: 32em) {
.header {
max-width: calc(30rem - 80px);
}
}
.wrapper-right {
background: green;
height: 100%;
position: absolute;
right: 0;
top: 0;
width: 80px;
}
.content {
max-width: 30rem;
margin: 0 auto;
background-color: orange;
}
<div class="wrapper">
<div class="header">
<div> lorem ipsum></div>
<div> lorem ipsum></div>
<div> lorem ipsum></div>
</div>
<div class="wrapper-right">menu</div>
</div>
<div class="content">content</div>
Is this what you're after?
https://codepen.io/panchroma/pen/PxKBBV
The important bits in the CSS are:
lines 36-40 where we're scaling the width of .header at viewports below 40rem
lines 42 - 50 where we're hiding .wrapper-right, and restoring .header to full-width
As an FYI, your CSS has a class titled .l-header but I couldn't see where you were going with this.
Hope this helps!
HTML
As originally posted
CSS
.wrapper {
height: 6rem;
position: relative;
width: 100%;
background-color: red;
display: inline-block;
}
.header {
margin: 1.5rem auto 0;
max-width: 30rem;
background-color: blue;
}
#media (max-width: 32em) {
.l-header {
max-width: calc(30rem - 80px);
}
}
.wrapper-right {
background: green;
height: 100%;
position: absolute;
right: 0;
top: 0;
width: 80px;
}
.content {
max-width: 30rem;
margin: 0 auto;
background-color: orange;
}
#media (max-width: 40rem) {
.header{
width:calc(75vw - 80px);
}
}
#media (max-width: 27rem) {
.wrapper-right{
display:none;
}
.header{
width:100%;
}
}
Version 2
Based on your comments, here's version 2
https://codepen.io/panchroma/pen/VVMJxM
The important bits are that I added the .header-wrapper. Then we're changing the left and right padding on .header-wrapper at various viewports to keep the header and content divs aligned.
Good luck!
HTML
<div class="wrapper">
<div class="header-wrapper">
<div class="header">
<div> lorem ipsum></div>
<div> lorem ipsum></div>
<div> lorem ipsum></div>
</div>
</div>
<div class="wrapper-right">menu</div>
</div>
<div class="content">content</div>
CSS
/* note that I'm using normalize.css in the CSS setings */
/* https://necolas.github.io/normalize.css/ */
.wrapper {
height: 6rem;
position: relative;
width: 100%;
background-color: red;
display: inline-block;
margin-right:80px;
}
.header {
margin: 1.5rem auto 0;
max-width: 30rem;
background-color: blue;
position:relative;
z-index:5;
}
.wrapper-right {
background: green;
height: 100%;
position: absolute;
right: 0;
top: 0;
width: 80px;
}
.content {
max-width: 30rem;
margin: 0 auto;
background-color: orange;
}
#media (max-width: 40rem) {
.header-wrapper {
/* add padding-right to make room for .wrapper-right */
/* have used 81px instead of 80px so we can be certain */
/* that the div isn't extending under .wrapper-right */
padding-right:81px;
/* add padding-left to keep .header and .content aligned */
/* logic is that the space to the left of .content */
/* is the half of the window width - width of .content */
padding-left:calc(50vw - 15rem);
}
}
/* at viewports below 27rem, hide .wrapper-right and return .header to full width */
#media (max-width: 27rem) {
.wrapper-right{
display:none;
}
.header-wrapper{
padding:0;
}
}

Make a absolute positioned div same height as parent (not relative)

I want to have a div in the background (100% width of browser) by mouseover on a content-div. Everything works great but i dont know is there a solution to make the background div as height as the parent?
Without using Jquery? Pure CSS would be great!
Thanks!
.content {
/* position: relative; >> will make the absoluted positioned div 100%width of the browser*/
z-index: 1;
}
.background {
position: absolute;
background: aqua;
width: 100%;
right: 0px;
left: 0px;
z-index: -1;
display: none;
}
.post:hover .background {
display: block;
height: 10%;
/* WHAT TO DO? */
}
<div class="post">
<div class="content">
<div class="background"></div>
…content…
</div>
</div>
part of the problem is: the "post" div is in a centerd "main" div with a given width and i want to have a background-div that has 100% width of the viewport
Sorry, it is a bit hard to explain what i need, therefore I make a small sketch here:
This is posible with vw units, look:
* {
margin: 0;
padding: 0;
}
.main {
background: #29D;
margin: 0 auto;
max-width: 800px;
}
.post {
background: #E31;
margin: 3vw 3vw 3vw 12vw;
}
.content {
background: #8D5;
margin: 2vw;
}
.content img {
width: 100vw;
margin-left: -14vw;
display: block;
}
#media(min-width: 800px) {
.content img {
margin-left: calc(800px / 2 - 50vw - 14vw);
}
}
<div class="main">
.main
<div class="post">
.post
<div class="content">
.content
<img src="http://s3.postimg.org/3k8v5p0v7/dragon.jpg">
</div>
<div class="content">
.content
</div>
<div class="content">
.content
</div>
</div>
</div>
First thing you need to know is the max width of your wrapper. In my case it is 800px. You have to substitute all the 800's with your size. Then notice that all your left margins should be in vw units or pixels but not 100%. The trick here is that calc() function and the media query!

Media Queries content adaptation

I cannot handle correctly my #main-wrapper behaviour for #media only screen and (min-width: 1400px)
What I am trying to get is that #main-wrapper has 100% width for resolution <1400px and >1400 its width is set to 1336px
Now #menu-section overflows #content-section
Live example:
http://solutionsmvo.nazwa.pl/nell-ogrody/o-nas/
Code:
#main-wrapper {
width: 100%;
overflow:hidden;
}
#menu-section {
width: 25%;
float: left;
height:100vh;
position: fixed;
}
#content-section {
width: 75%;
float:right;
min-height: 100vh;
}
#media only screen and (min-width: 1400px) {
#main-wrapper {width: 1336px; margin: 0 auto;}
}
<div id="main-wrapper">
<div id="menu-section">
</div>
<div id="content-section">
</div>
</div>
because your #menu-section in <1400px has position: fixed; property, it's okay in this case, but when width > 1400px, then this problem occur. remove position: fixed; and test it.
The problem is position:fixed on your sidebar element.
You need to add position relative to main-wrapper and change position from fixed to absolute in your menu-section
#main-wrapper {
position: relative
}
#menu-section {
position: absolute; //instead of fixed
}

#media query and "left:x%" is applying % of #width define in #media not the browser/parent element width

I am using bootstrap to implement the responsive web design. I am facing issue to apply "left" property as %. What I found is, instead of taking the % of total browser width, it takes the % of #media width define which really breaking the responsive nature of application.
.image-container {
width: 173px;
top: -69px;
left: 50%;
margin-left: -86px;
max-width: 336px;
overflow: hidden;
position: absolute;
}
#media (min-width: 660px) {
.image-container {
left: 63%;
}
}
#media (min-width: 831px) {
.image-container {
top: -91px;
left: 80%;
width: 30%;
}
}
#media (min-width: 1280px) {
.image-container {
left: 85%;
}
}
I found following
1. At >1280 width, left=1280*.85 is used
2. At > 831, left=831*.80 us used
3. At > 660, left=660*.63 is used
Following is HTML markup snippet
<div class="bottom-section">
<div class="parent-container">
<div class="image-container">
<img class="card-art" src="/img/application/cardarts/thumbnails/img.png">
</div>
</div>
</div>
Following is parent container css
.parent-container {
padding-left: 0;
width: 100%;
padding-top: 68px;
max-width: 970px;
margin: 0;
position: relative;
}
#media (min-width: 660px) {
.parent-container {
padding-top: 20px;
}
}
This is a surprising behavior for me. My understanding is, left=x% should alwasy look for parent element and apply the % of that. I am new to media query and using bootstrap to implement the responsive web design.
Here is a fiddle to play with.

Bootstrap 3 - 100% height of custom div inside column

Let's say I have two columns in a row.
One contains an image using class="img-responsive" for fluid image and the other column next to has a custom div block.
My question is how to get this custom div's height 100%. Below didn't work for me.
height: auto;
max-width: 100%;
If I set fixed value in height, it won't play nicely with the
column containing image as the image resizes height when the viewport size changes.
I can get the columns heights equal by doing something like the following as suggested by previous questions.
class*="col-"]{
margin-bottom: -99999px;
padding-bottom: 99999px;
}
.row {
overflow: hidden;
}
But my case is slightly different that I need to use custom div inside the column that
sets equal height with the image height, not the parent divs (columns).
Here is JS Fiddle
I was just looking for a smiliar issue and I found this:
.div{
height : 100vh;
}
more info
vw: 1/100th viewport width
vh: 1/100th viewport height
vmin: 1/100th of the smallest side
vmax: 1/100th of the largest side
The original question is about Bootstrap 3 and that supports IE8 and 9 so Flexbox would be the best option but it's not part of my answer due the lack of support, see http://caniuse.com/#feat=flexbox and toggle the IE box. Pretty bad, eh?
2 ways:
1. Display-table:
You can muck around with turning the row into a display:table and the col- into display:table-cell. It works buuuut the limitations of tables are there, among those limitations are the push and pull and offsets won't work. Plus, I don't know where you're using this -- at what breakpoint. You should make the image full width and wrap it inside another container to put the padding on there. Also, you need to figure out the design on mobile, this is for 768px and up. When I use this, I redeclare the sizes and sometimes I stick importants on them because tables take on the width of the content inside them so having the widths declared again helps this. You will need to play around. I also use a script but you have to change the less files to use it or it won't work responsively.
DEMO: http://jsbin.com/EtUBujI/2
.row.table-row > [class*="col-"].custom {
background-color: lightgrey;
text-align: center;
}
#media (min-width: 768px) {
img.img-fluid {width:100%;}
.row.table-row {display:table;width:100%;margin:0 auto;}
.row.table-row > [class*="col-"] {
float:none;
float:none;
display:table-cell;
vertical-align:top;
}
.row.table-row > .col-sm-11 {
width: 91.66666666666666%;
}
.row.table-row > .col-sm-10 {
width: 83.33333333333334%;
}
.row.table-row > .col-sm-9 {
width: 75%;
}
.row.table-row > .col-sm-8 {
width: 66.66666666666666%;
}
.row.table-row > .col-sm-7 {
width: 58.333333333333336%;
}
.row.table-row > .col-sm-6 {
width: 50%;
}
.col-sm-5 {
width: 41.66666666666667%;
}
.col-sm-4 {
width: 33.33333333333333%;
}
.row.table-row > .col-sm-3 {
width: 25%;
}
.row.table-row > .col-sm-2 {
width: 16.666666666666664%;
}
.row.table-row > .col-sm-1 {
width: 8.333333333333332%;
}
}
HTML
<div class="container">
<div class="row table-row">
<div class="col-sm-4 custom">
100% height to make equal to ->
</div>
<div class="col-sm-8 image-col">
<img src="http://placehold.it/600x400/B7AF90/FFFFFF&text=image+1" class="img-fluid">
</div>
</div>
</div>
2. Absolute bg div
DEMO: http://jsbin.com/aVEsUmig/2/edit
DEMO with content above and below: http://jsbin.com/aVEsUmig/3
.content {
text-align: center;
padding: 10px;
background: #ccc;
}
#media (min-width:768px) {
.my-row {
position: relative;
height: 100%;
border: 1px solid red;
overflow: hidden;
}
.img-fluid {
width: 100%
}
.row.my-row > [class*="col-"] {
position: relative
}
.background {
position: absolute;
padding-top: 200%;
left: 0;
top: 0;
width: 100%;
background: #ccc;
}
.content {
position: relative;
z-index: 1;
width: 100%;
text-align: center;
padding: 10px;
}
}
HTML
<div class="container">
<div class="row my-row">
<div class="col-sm-6">
<div class="content">
This is inside a relative positioned z-index: 1 div
</div>
<div class="background"><!--empty bg-div--></div>
</div>
<div class="col-sm-6 image-col">
<img src="http://placehold.it/200x400/777777/FFFFFF&text=image+1" class="img-fluid">
</div>
</div>
</div>
You need to set the height of every parent element of the one you want the height defined.
<html style="height: 100%;">
<body style="height: 100%;">
<div style="height: 100%;">
<p>
Make this division 100% height.
</p>
</div>
</body>
</html>
Article.
JsFiddle example
My solution was to make all the parents 100% and set a specific percentage for each row:
html, body,div[class^="container"] ,.column {
height: 100%;
}
.row0 {height: 10%;}
.row1 {height: 40%;}
.row2 {height: 50%;}

Resources