styled-components media queries not working - javascript

I am using styled components to style my react project, components are working fine, but for some reason media queries are not applied. Here's a snippet that works when using regular css:
import styled from 'styled-components';
export const Block = styled.div `
margin: 20px;
padding: 10px;
border-radius: 10px;
display: flex;
flex-direction: column;
align-self: center;
background-color: #DAD870;
flex: 1;
min-width: 200px;
height: auto;
transition-duration: 1s;
font-family: sans-serif;
&:hover {
transform: scale(1.1);
}
#media (max-width: 1024px) {
width: 42%;
min-width: 158px;
}
#media (max-width: 480px) {
width: 40%;
min-width: 148px;
}
`;

I don't really use react but it looks like you are trying to use #media inside of an element, I don't think that works, instead try something like:
div{
min-width: 200px;
}
#media (max-width: 1024px) {
div{
width: 42%;
min-width: 158px;
}
}
#media (max-width: 480px) {
div{
width: 40%;
min-width: 148px;
}
}
I don't know how to show you like the way you did it but basically you need to declare it separately, hope it helped.

Related

My Wrapper Background Image does not show

I'm Transferring some design to a web page, but I can't seem to get the background image to show. I've checked various solutions online and they keep telling me to add the 'background-image' property, which I have done. Yet it doesn't work. I've pasted the code below. I apologise if it's a very obvious mistake as I'm not quite familiar with CSS.
Could someone please point out where my mistake is?
import React from "react";
import styled from "styled-components";
// Assets
import HeaderImage from "../../assets/img/covlie_homepage_illustration.png";
export default function Header() {
return (
<Wrapper id="home" className="container flexSpaceCenter">
<LeftSide className="flexCenter">
<div>
<h1 className="extraBold font60">Some Text</h1>
<HeaderP className="font13 semiBold">
Some Text
</HeaderP>
</div>
</LeftSide>
<RightSide>
<ImageWrapper>
<Img className="radius8" src={HeaderImage} alt="office" style={{zIndex: 9}} />
</ImageWrapper>
<GreyDiv className="lightBg"></GreyDiv>
</RightSide>
</Wrapper>
);
}
const Wrapper = styled.section`
padding-top: 80px;
width: 100%;
min-height: 840px;
background-image: transparent url('../../assets/img/page_heade_rbackground_image.png') 0% 0% no-repeat padding-box;
#media (max-width: 960px) {
flex-direction: column;
}
`;
const LeftSide = styled.div`
width: 50%;
height: 100%;
#media (max-width: 960px) {
width: 100%;
order: 2;
margin: 50px 0;
text-align: center;
}
#media (max-width: 560px) {
margin: 80px 0 50px 0;
}
`;
const RightSide = styled.div`
width: 50%;
height: 100%;
#media (max-width: 960px) {
width: 100%;
order: 1;
margin-top: 30px;
}
`;
const HeaderP = styled.div`
max-width: 470px;
padding: 15px 0 50px 0;
line-height: 1.5rem;
#media (max-width: 960px) {
padding: 15px 0 50px 0;
text-align: center;
max-width: 100%;
}
`;
const GreyDiv = styled.div`
width: 30%;
height: 700px;
position: absolute;
top: 0;
right: 0;
z-index: 0;
#media (max-width: 960px) {
display: none;
}
`;
const ImageWrapper = styled.div`
display: flex;
justify-content: flex-end;
position: relative;
z-index: 9;
#media (max-width: 960px) {
width: 100%;
justify-content: center;
}
`;
const Img = styled.img`
#media (max-width: 560px) {
width: 80%;
height: auto;
}
`;
I finally discovered the issue. I'm using styled components, and the implementation for styled components is different. First I should have imported the image
import bgImg from "../../assets/img/page_heade_rbackground_image.png"
Then implemented like this
const Wrapper = styled.section`
padding-top: 80px;
width: 100%;
min-height: 840px;
background-image: url(${bgImg});
#media (max-width: 960px) {
flex-direction: column;
}
`;
Don't know if this will help but the background image is set to transparent.
background-image: transparent url('../
I see that when you figured it out you didn't use transparent :)

Image not scaling to full width of div element

I have a div element and I want my image within it to scale to the full width of the parent however for some reason its not taking up the full width. I could use object-fit: cover; but I don't want to crop the image I want it to fill up the space of the parent. Id also like to scale it so it remains a square the whole time so it always scales as an equal square. I am a little confused as to why its not taking up the parents container. It seems to be wanting to keep its aspect ratio but I want it to stretch to the parent container both via its height and width properties.
html:
import React from 'react';
const ThreeGridTeaser = (props) => {
return (
<div className="specific-offer-container mt-0">
<div className="three-grid-teaser-wrapper">
<div className="three-grid-teaser-container">
{props.gridData.map((item, index) => (
<div key={index} className="three-grid-teaser-block">
<div className="three-grid-image-container hidden">
<img src={item.image}
/>
</div>
<div className="product-text-container hidden">
<ul>
<li className="product-title">{item.title}</li>
<li className="product-description">{item.description}</li>
</ul>
</div>
</div>
))}
</div>
</div>
</div>
)
}
export default ThreeGridTeaser;
SASS:
.three-grid-teaser-wrapper {
width: 100%;
height: auto;
display: flex;
justify-content: center;
}
.three-grid-teaser-container {
width: 100%;
justify-content: center;
flex-wrap: wrap;
height: auto;
display: flex;
grid-gap: 20px;
gap: 20px;
#media (max-width: 1199.98px) {
gap: 25px;
}
#media (max-width: 767.98px) {
width: 100%;
}
}
.three-grid-teaser-block {
display: flex;
width: 32%;
height: fit-content;
#media (max-width: 1199.98px) {
width: 48%;
}
#media (max-width: 767.98px) {
width:47%;
}
justify-content: center;
flex-direction: column;
}
.hover-text-underline {
text-decoration: underline;
text-underline-position: under;
}
.three-grid-image-container {
height: 300px;
#media (max-width: 1199.98px) {
height: 350px;
}
#media (max-width: 767.98px) {
height: 180px;
}
width: 100%;
max-width: 100%;
img {
display: block;
max-width: 100%;
max-height: 100%;
width: 100%;
height: 100%;
}
}
.product-text-container {
ul {
margin-top: 10px;
padding: 0;
li:last-child {
padding-top: 5px;
}
li {
list-style: none;
}
}
}
.product-title {
font-family: $terminaDemi;
font-size: 12px;
#media (max-width: 767.98px) {
font-size: 8px;
}
}
.product-description {
font-family: $Montserrat;
font-size: 18px;
#media (max-width: 767.98px) {
font-size: 12px;
}
}
This is because the image has reached its maximum height in relation to the div. I mean, an image 300px wide and 400px high inside a 400px square div, in this case if you put a width of 100% in the image, the maximum that it will reach in its width is 300px, in the case of you don't want to resize it.
DemonstraĆ§Ć£o
Update
'object-fit: contain' makes the image fit in the space it has proportionally available, if you want it to maintain its proportion and fill the entire space, use the 'object-fit: cover'
look at this
https://jsfiddle.net/gxnvuq6s/8/
have you tried looking at the background-size: cover; style? you can then also position your image with background-position: center center;

Flexbox that goes full width when forced onto new line?

I have two divs lined next to each other using the following CSS:
.overlay {
display: flex;
flex-wrap: wrap;
}
.mainContent {
flex-grow: 1;
flex-basis: auto;
margin: 5px;
}
.interactions {
flex-basis: auto;
flex-grow: 1;
max-width: 400px;
margin: 5px;
}
When the width of the screen gets too small (i.e. .interactions can no longer fit) a new row is created in which interactions are placed.
What I'd like to do when that happens is for interactions to switch to max-width: 100%, is there any way to do this without using javascript?
There's two situations here
screen gets too small
if screen goes below 400px we're okay with the rules so far
if screen is above 400px but still too small, we end up with conflicting max-widths
is there anyway to do this without using javascript?
It can be achieved with media queries
#media only screen and (max-width: 600px) {
.instructions { min-width: 100% }
}
maybe you should use media queries?
#media (max-width: 400px) {
.interactions {
max-width: 100%;
}
}
Using media queries is a challenge here because the breakpoint at which the problem occurs is dependent on the size of the flexible width .mainContent
So a better approach would be to have a min-width on .mainContent and use a media query for the breakpoint which is a sum of the min-width of .mainContent and max-width of .interactions + margins
.overlay {
display: flex;
flex-wrap: wrap;
justify-content:stretch;
}
.mainContent {
flex-grow: 1;
flex-basis: auto;
min-width:400px;
margin: 5px;
}
.interactions {
flex-basis: auto;
flex-grow: 1;
max-width:400px;
margin: 5px;
}
#media only screen and (max-width: 840px){
.mainContent, .interactions{
max-width:none;
min-width:none;
}
}
See it in action here: https://jsfiddle.net/gnu12Lfs/
you shuld to use the nowrap property,
the flex-wrap: wrap create new line when the intenrnal divs are too little, the nowrap not create, the code to remain in the same line is
.overlay {
display: flex;
flex-wrap: nowrap;
}
.mainContent {
flex-grow: 1;
flex-basis: auto;
margin: 5px;
}
.interactions {
flex-basis: auto;
flex-grow: 1;
max-width: 400px;
margin: 5px;
}
you can find the documentation here https://www.w3schools.com/cssref/css3_pr_flex-wrap.asp
Also you can find other details of the usage and the browser compatibility here https://caniuse.com/#search=flex-wrap

how to make a website go to full width and height in asp.net

I am trying to understand how to make a web page which will go in full height and width in asp.net. I know to use width: 100% for the background but I am not understanding how to change the width and height of other buttons, , and other stuff..
I search on google and youtube for that but I am not getting a good tutorial to explain how I should make it.
Should I use jquery or javascript? I'm trying to stay far from those at the moment.
Can any one please give me a simple explanation about this?
Best way to control ui styles, should be CSS.
For example, setting a element to full width and height
.full_width {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
}
.element_inside {
width: 100px;
border: 1px solid red;
}
.button_inside {
width: 125px;
height: 60px;
line-height: 60px;
}
<div class="full_width">
I am full width element
<div class="element_inside">
Low width
</div>
<button class="button_inside">
Button
</button>
<button style="width: 100%;">
Button full width
</button>
</div>
But it uses absolute position.
You could use something like this too.
html {
display: table;
width: 100%;
min-height: 100%;
}
body {
display: table-row;
}
.full_width {
width: 250px;
display: table-cell;
vertical-align: top;
min-height: 100%;
}
.full_width {
border: 1px solid #f00;
}
<div class="full_width">
full width container
</div>
For responsive layout, and different screen different size, you can use following codes:
#media (min-width: 768px) {
.container {
width: 740px;
}
}
#media (min-width: 992px) {
.container {
width: 960px;
}
}
#media (min-width: 1200px) {
.container {
width: 1160px;
}
}
#media (min-width: SCREEN_WIDTH_IN_PIXELS) {
.container { // my element width for this screen
width: 1160px;
}
}
Use different css container class for buttons. For ex;
.body{
width: 100%
}
button {
color: #666;
border-color: #EBEBEB;
background-color: #EBEBEB;
text-align: center;
height:200px;
width:200px;
}
And then use for button,
<button class="button">Button</button>

change class style of parent element in specific div media query

I want my .container element to be at a width of 100% when it reaches a screen size of 900px. The problem is that I use .container on every page and only want to affect the appearance of .container in the #sub-top div.
the hardest thing is terminology for me but i think this is the closest to what i have the ability to describe.
As you can see below, i am trying to get the .container class to go to 100% rather than 85% when it reaches a screen size of 900px or smaller.
#media
.container{
width: 85%;
height: auto;
}
#sub-top{
background-color: gray;
height: auto;
}
#media (min-width: 900px) {
#sub-top .container {
width: 100% !important;
height: auto;
}
}
<div id="sub-top">
<div class="container">
<div class ="content">
<p> HERE IS CONTENT </p>
</div>
</div>
</div>
Try this instead:
#media screen and (max-width: 900px) {
#sub-top .container {
width: 100%;
height: auto;
}
}
This works:
.container {
width: 85%;
height: auto;
background-color: gray;
}
#sub-top {
height: auto;
}
#media (max-width: 900px) {
#sub-top .container {
width: 100% !important;
height: auto;
}

Categories