CSS - responsivly center div, position fixed - css

I have a jsfiddle here https://jsfiddle.net/ajvvjnq3/
Really simple, I have a div with fixed width, position fixed and centered.
Below 600px I want the div to be 100% width with 20px margin left and right.
I can't get margin-right: 20px;
.block{
background: red;
height: 100px;
position: fixed;
top: 50px;
left: 50%;
margin-left: -200px;
width: 400px;
}
#media only screen and (max-width: 600px){
.block{
left: 0;
margin-left: 20px;
margin-right: -20px;
width: 100%;
}
}
<div class="block"></div>

JSfiddle https://jsfiddle.net/ajvvjnq3/2/
You could try using calc
width: calc(100% - 40px); seems to work fine
All its doing here is just negating the values from your margins.
Edit
Alternative would be to use #CBroe answer as it supports more browsers than calc, but whatever floats your boat:)
#media only screen and (max-width: 600px){
.block{
left: 20px;
right: 20px;
width: auto;
margin: auto;
}
}

I’d simply used this:
#media only screen and (max-width: 600px){
.block{
left: 20px;
right: 20px;
width: auto;
margin: auto;
}
}
Positioning the element 20px from left and right, and setting width to auto will make it get the right width while respecting the “gap” you want it to have on either side. And margin: auto (0 would work as well) simply undoes the margin you used earlier to center the element.
https://jsfiddle.net/ajvvjnq3/4/

Try this, it will work..
#media only screen and (max-width: 600px){
.block{
left: 0 !important;
margin: 0 !important;
position: relative !important;
right: 0 !important;
width: 100% !important;
}
}

Try this replace width:100%; with width:initial; add right:0;
#media only screen and (max-width: 600px){
.block{
position: fixed;
top: 50px;
left: 0;
margin-left: 20px;
margin-right: 20px;
right:0;
width:initial;
}
}
Demo Here

calc() makes it easy to position an object with a set margin. In this example, the CSS creates a banner that stretches across the window, with a 20-pixel gap between both sides of the banner and the edges of the window
JSFIDDLE
http://jsfiddle.net/DhwaniSanghvi/okvoLuu5/
<div class="block"></div>
.block{
background: red;
height: 100px;
position: fixed;
top: 50px;
left: 50%;
margin-left: -200px;
width: 400px;
}
#media only screen and (max-width: 600px){
.block{
position: fixed;
top: 50px;
margin-left: 20px;
margin-right:20px;
left:0;
width: calc(100% - 40px);
}
}

Related

Min-width and max-width not working on div and image

I am trying to add a min and max width to the className "tile" however what I am using at the moment in my CSS isn't working. Once my screen size goes below 480px it jumps from this:
To this:
The height stays the same however it doesn't gradually change width when resizing the screen.
#media screen and (max-width: 480px) {
.tile {
position: relative;
height: 192px;
padding-bottom: 12px;
width: 100%;
}
.tile-image {
height: 192px;
margin-left: 24px;
object-fit: cover;
position: absolute;
border-radius: 4px;
max-width: 432px;
min-width: 272px;
z-index: 1;
}
.tile-gradient {
background-blend-mode: darken;
background-image: linear-gradient(228deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.36));
height: 192px;
object-fit: cover;
border-radius: 4px;
z-index: 2;
position: absolute;
margin-left: 24px;
margin-right: 24px;
max-width: 432px;
min-width: 272px;
}
}
#media screen and (min-width: 481px) {
.tile {
position: relative;
width: 432px;
height: 192px;
padding-bottom: 12px;
}
.tile-image {
width: 432px;
height: 192px;
margin-left: 24px;
object-fit: cover;
position: absolute;
border-radius: 4px;
z-index: 1;
}
.tile-gradient {
background-blend-mode: darken;
background-image: linear-gradient(228deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.36));
width: 432px;
height: 192px;
object-fit: cover;
border-radius: 4px;
z-index: 2;
position: absolute;
margin-left: 24px;
margin-right: 24px;
}
}
<div className="tile">
<div className="image">
<div className="tile-gradient"></div>
<img className={`tile-image ${!post.availability && "is-available"} `} src={post.img} alt=""/>
</div>
</div>
Any idea on how to fix this and make it responsive would be great so it does not jump from once size to the other automatically. Thank you in advance, please ask if any other information is required.
This happens because you're not using a screen width percentage to reduce the width of tile element. It directly change from 100% width to a specific 432px as you set it inside your #media screen and (min-width: 481px) query. Try to avoid specific pixel width and adjust it with percentage. Something like
#media screen and (max-width: 480px) {
.tile {
position: relative;
height: 192px;
padding-bottom: 12px;
width: 100%;
}
}
#media screen and (min-width: 481px) {
.tile {
position: relative;
width: 80%; // change this with the desired width of the element. Avoid using px
height: 192px;
padding-bottom: 12px;
}
}
A gradual change of width happens when resizing the screen if you're using percentages instead of specific pixels.

CSS problem with tag #media, adaptation position of the button

On the page https://sport-print.online/sample-page/ I have a little button "Сравнить". I'm trying to adapt it using the CSS code:
#media only screen and (max-width: 1920px), only screen and (max-device-
width: 1920px) {
.compare
{
right: -500px;
top: -720px;
width: 70px;
height: 30px;
line-height: 20px;
padding: 0 10px !important;
padding-right: 20px !important;
padding-bottom: 10px !important;
}
}
#media only screen and (max-width: 1024px), only screen and (max-device-
width: 1024px) {
.compare
{
right: -300px;
top: -320px;
width: 70px;
height: 30px;
line-height: 20px;
padding: 0 10px !important;
padding-right: 20px !important;
padding-bottom: 10px !important;
}
}
Only the first part of the code works for width in 1920px. The second part of the code does not work. When the resolution is changed, the button position is not switched. Prompt please in what my error? Please correct my code.
Problem solved, the top and right properties do not affect position: static;
It was necessary to specify position: absolute; or position: relative; in the #media properties. Operating code:
.compare
{
width: 70px;
height: 30px;
line-height: 20px;
padding: 0 10px !important;
padding-right: 20px !important;
padding-bottom: 10px !important;
}
#media only screen and (max-width: 1920px), only screen and (max-device-width:
1920px) {
.compare
{
position: absolute; /* или position: relative;*/
right: -500px;
top: -720px;
}
}
#media only screen and (max-width: 1024px), only screen and (max-device-width:
1024px) {
.compare
{
position: absolute; /* или position: relative;*/
right: -300px;
top: 700px;
}
}

Why is one of the media query rules not applied?

I've recently discovered media queries as a tool, so don't have much experience (seconds day, really).
I have a #logo that uses a margin-left of 7% of the width, but the rule that changes it to 25px doesn't work for some reason. Interestingly, the rest of the particular media query works fine, just the logo one doesn't. What could be the problem?
#media (max-width: 1320px) and (min-width: 105.1px){
#wrapper {
width: 100%;
}
#logo {
width: 195px;
height: 96px;
position: relative;
max-width: 230px;
max-height: 110px;
min-width: 115px;
min-height: 96px;
border: thin solid #000000;
float: left;
margin-left: 25px;
margin-top: -1px;
background-repeat: no-repeat;
background: black;
}
}
#logo {
width: 195px;
height: 96px;
position: relative;
max-width: 230px;
max-height: 110px;
min-width: 115px;
min-height: 96px;
border: thin solid #000000;
float: left;
margin-left: 7%;
margin-top: -1px;
background-repeat: no-repeat;
background: black;
}
Your media query needs to go after the normal set of rules for #logo for your query to override your default rules. You can remove your important flag once you do that.
You need to clean up your media query a bit, as there is some redundancy. Also, you need to decide how you want to structure your CSS, as in mobile first approach or large screen then target down for smaller screens. In your code there isn't really a good reason to use !important as structuring the initial CSS will correct the issue.
Also in your example, a min-width:105 is sort of useless as mobile screens have a min-width of roughly 320px (I'm speaking generally). Here is how you would have it set up if you want all your styles to apply until the screen width goes wider than 1320px.
Here's a jsfiddle as an example: http://jsfiddle.net/disinfor/t9rq36mn/
Note, I changed the (min-width) in the fiddle to 600px so you can actually see it working. You can also change min-width to max-width in the fiddle to see it work in reverse.
MOBILE FIRST APPROACH:
#wrapper {
width: 100%;
}
#logo {
width: 195px;
height: 96px;
position: relative;
max-width: 230px;
max-height: 110px;
min-width: 115px;
min-height: 96px;
border: thin solid #000000;
float: left;
margin-left: 25px;
margin-top: -1px;
background-repeat: no-repeat;
background: black;
}
#media screen and (min-width:1320px) {
#logo {
margin-left: 7%;
}
}
LARGE SCREEN FIRST APPROACH:
#wrapper {
width: 100%;
}
#logo {
width: 195px;
height: 96px;
position: relative;
max-width: 230px;
max-height: 110px;
min-width: 115px;
min-height: 96px;
border: thin solid #000000;
float: left;
margin-left: 7%;
margin-top: -1px;
background-repeat: no-repeat;
background: black;
}
#media screen and (max-width:1319px) {
#logo {
margin-left: 25px;
}
}
Basically, you only need to change the property of the element that actually changes once you reach that media query parameter (min-width, max-width, min-device-width,max-device-width, etc.)
Hopefully that helps clear things up.

Media Query width:100% causing overflow

I am designing my first responsive website. Things were great until now when I set container div to be 100%. The idea was to have to container div to be 100% with max width 768px. If the browser window is less than 768px, the container show take up 100% screen size WITHOUT overflow.
It looks like setting .container {width: 100%} is making the container a fixed 678px even when screen size is smaller, thus causing overflow.
Can someone help?
Example:
/* --- Main style - not media --- */
html, body {
width: 100%;
height: 100%;
}
.main {
position: relative;
width: 100%;
height: 100%;
min-width: 829px;
}
.all-wrapper {
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 1;
position: relative;
}
.container {
position: absolute;
top: 6.0em;
width: 70%;
left: 0;
max-width: 1140px;
min-width: 500px;
margin-bottom: 0;
}
.content-box {
position: relative;
width: 80%;
margin: 2.0em auto 3.0em;
padding: 1.0em;
}
/* -------Media -----------*/
#media screen and (max-width: 767px) {
.main {
max-width: 100%
min-width: 0;
}
.container {
position: relative;
width: 100%;
height: auto;
margin: 0 auto;
padding: 0;
}
.content-box {
position: relative;
width: 90%;
margin: 0 auto;
padding-bottom: 4.0em;
}
}
/*-- html ----*/
<div class= "main">
<div class="container">
<div class="content-box">
<p>stuff</p>
</div>
</div>
</div>

Mediaqueries wont work? Responsive web

I'm currently trying to create a website with different media queries for different device sizes, the problem is, when I add a media query in the media-queries.css file for a different viewport the browser only reads one.
I recently added a media query for a maximum width of 1500 pixels, but now the media query for max-980px wont work.
Here's the css, anyone that can help me?
/************************************************************************************
smaller than 980
*************************************************************************************/
#media screen and (max-width: 980px) {
.slider-container {
width: 100%;
height: 100px;
background-color: white;
overflow: hidden;
-moz-border-radius: 20px;
border-radius: 20px;
}
.wrapper .slider-container #slideshow {
position: relative;
width: 100%;
height: 300px;
}
.wrapper .slider-container #slideshow > div {
position: absolute;
}
.wrapper .slider-container #slideshow > div img {
border: 20px solid #00e800;
height: 100%;
}
}
/************************************************************************************
smaller than 1500
*************************************************************************************/
#media screen and (max-width: 1500px) {
.slider-container {
width: 100%;
height: 400px;
background-color: white;
overflow: hidden;
-moz-border-radius: 20px;
border-radius: 20px;
}
.wrapper .slider-container #slideshow {
position: relative;
width: 100%;
height: 400px;
}
.wrapper .slider-container #slideshow > div {
position: absolute;
}
.wrapper .slider-container #slideshow > div img {
border: 15px solid #f3f70a;
height: 100%;
}
}
Add a min-width component to the larger max-width:
#media screen and (min-width:981px) and (max-width: 1500px) {
This should tell the system to segregate the two choices, as < 980 will also always be < 1500.

Resources