CSS: how to overwrite (unset) previously set max-width property values - css

I have an image element that is styled to scale to the screen size within bound parameters defined in a max-width.
This styling works fine. However I home some images that need this maximum size (a percentage) to be removed on smaller screens (under 480px). To do this I need to "unset" a max-width value for the element set by the template structure.
HTML:
<figure class='image left'>
<div class='boxShadowInner wide190'>
<img src='/images/Bag.jpg' alt='text'>
</div>
<figcaption><strong>caption text</strong></figcaption>
</figure>
(The boxShadowInner class sets a feathered border around the image).
Template CSS (standard):
.image.left {
float: left;
display: block;
width: 70vw;
max-width: 90%;
padding: 0;
top: 0;
margin: 0 auto .5rem;
text-align: center;
}
Template CSS (max-width:480px):
.tableBox .image.left:not(.carrier) {
max-width: none; /** THIS LINE **/
width: auto;
}
.image.right, .image.left {
float: none;
}
Noting the line above; I need to overwrite the max-width property set at the base universal level for the style sheet.
What I've tried already:
max-width: none; as suggested on this question (for -height) does not work in Firefox.
The only max-width property that does work as intended (in Firefox) is: max-width: max-content; but as stated on MDN:
"This is an experimental API that should not be used in production code."
Other possible values for max-width do not give any change.
max-content (and similar keywords) are seeingly not ready for production usage according to caniuse.com.
There are multiple images this rule needs to apply to so setting a value figure (280px, etc) is not a possible solution.
I have tried max-width: auto; and Firefox tells me this is an invalid value.
!important makes no difference.
max-width: initial or max-width: unset also makes no difference.
What I'm trying to achieve can be done with this:
.image.left {
float: left;
display: block;
width: 90%;
/* max-width: 90%; // Disabled */
padding: 0;
top: 0;
margin: 0 auto .5rem;
text-align: center;
}
Screnshot of Firefox Developer Tools shows that it works perfectly when manually disabling the max-width rule setting, but I can't simulate this behavior using CSS rules:
How do I overwrite the set max-width property to return it to browser default?
I made a fiddle but it doesn't seem to be working well with media queries..... (I don't use fiddles much I'm afraid)
https://jsfiddle.net/b8bsxtqu/7/

This may not be the answer you're after for your specific situation, but it's how I would approach it.
Instead of writing your CSS for your desktop/laptop and retrofitting it for smaller devices, write your CSS for the smallest devices first, then use media queries to 'enhance' the CSS (with media queries) and visuals for larger devices. This is what's known as a mobile first approach, or more accurately smaller devices first.
Reasons for developing this way.
You start with a simpler CSS code base.
It's easier to maintain and debug.
It keeps you in the mindset of keeping a priority on mobile browsers (have you looked at mobile usage statistics lately? Whoa.)
It puts the priority on content (where it should be)
Applying this to your situation, rather than "undoing" or "unsetting" a style for a small device. Your CSS is written purposely without that style at the start because you're writing for small devices first. Then, using media queries you begin to add styles for larger, more accommodating devices.
In the snippet below you can see that the img has no max-width style applied to start. Then for devices with a min-width of 480px, you can specify your max-width.
img {
width: auto;
}
#media screen and (min-width: 480px) {
img {
max-width: 500px;
}
}
<img src='https://placehold.it/900x500' alt='text'>

I found the issue was that the above max-width was being set on the container rather than on the <img> tag itself where the <img> tag had its own max- and min-width values set and removing/ adjusting these resolved this CSS.

Related

Fix the position of image in html

I have created an html document. Inside this I've imported an image under a paragraph. But whenever I change the resize my browser. The image disappears by moving below the page.
The image is given inside a card.
.Card{
border:none;
font-family: 'NCS Radhiumz';
border-radius: 15px;
background-color: #000000;
width:90%;
height: 85%;
margin-left: 80px;
padding-left: 5%;
padding-right: 5%;
I just want everything on the page to resize without disappearing from the page. I'm very new to coding.
Anybody know how to fix it?
What you're most likely missing is some styling on your image element. Try this in your css file:
img {
width: 100%;
object-fit: contain;
}
You can experiment with different ways to fit the image in your component. Check out these docs for some options.
you're asking for responsive web design. You can achieve that by implementing CSS Media Query.
Learning source:
https://www.w3schools.com/css/css_rwd_mediaqueries.asp
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
If you want your image to be in one place and don't move you can do that with position style in css like so:
.element {
position: fixed;
}
or
.element {
position: absolute;
}
But image moving below is normal since the browser can't fit the image besides the text.
If you want your image to behave differently you can do that by using #media query and manually write how the app should change based on screen width/height. But generally the best way to do so is using flex-box or grid, since the browser automatically does object moving for you.

Full Width images with minimal CSS

I am (was) looking for a way that my images would be full width despite the body padding. And, I wanted my images to also not be too tall.
Making matters more difficult, (a) all of the images live inside of <p> tags and I can't (easily) assign classes, etc to them. That is because the pages are generated from a Markdown conversion and I wanted to avoid having to apply a ton of regex (though I was willing to if need be).
The requirements were basically that the text be padded inside the body (but not via a p margin so I didn't have to deal with non-paragraph elements). And, I wanted it such that the maximum height was some percentage of the current width. Finally, in addition to trying to avoid touching the HTML, I also didn't want to add any js if I could help it.
The following is my solution. It still needs some tinkering since you can become height-constrained before width-constrained with square images but if nothing else it is a start. Again, this may be super obvious to those more experienced.
Define the folliwng
$pad to be the padding of the body (2em)
$body_width to be the width of the body element (900px
$mh is the fraction to set the max height. Again, this is personal preference for the type of images you care about. I'll do 0.9 in this example.
body {
margin: 1em auto auto;
max-width: 900px; /* $body_width */
padding: 0.1em 2em 2em; /* $pad $pad */
}
video,img{
border: 0;
max-width: calc(100% + 4em); /* + 2*$pad */
max-height: 810px; /* $mh * $body_width
display: block;
/* From [1] */
position: relative;
left: 50%;
transform: translateX(-50%);
display: block;
}
[reference 1]
But I also wanted to handle tall screens with width less than max (i.e. mobile).
#media only screen and (max-width : 900px) { /* $mh*$body_width */
img,video {
max-height: 100vw;
}
}
It works by first making the image max width go beyond the padding and then using transform to center it.
I hope this helps someone in the future. It could have saved me a day!

Responsive site giving me trouble, chrome positioning a div in a different area?

http://www.remotegoatdesign.com/sayhey/pages/edit-valentines-marc-card.html
Doing this site for an assignment due tomorrow. In the proccess of making it responsive.
I am having an issue with the last color block, although its put into its container using percentages, it keeps moving out. In chrome its outside it straight away, whereas in Firefox its only when I resize. Although the difference is only a few pixels, so I'd assume its to do with the monitor size.
Any ideas guys? I'm stumped.
Try add this code snippet into your css file.
#tab-1 > div > div
{
width: 8%;
}
You can change the width.
Good Luck!!
Try using property " display:inline-table " for the class color_container
and give margin for the smaller color divs for space inbetween
try putting slightly smaller percentages(in the color block) and test it until it looks good. also it fits right in wider monitors as you say, because you have one css, that is best for wide screens. the point of responsive design is to have more than one media queries if the one you have breaks the design in smaller screens. so either make the color blocks really small, or myou should make more media queries
Your issue here is display: inline-block;. When you use it, it adds an extra space between elements. If you want to sort out this, you have 2 fixes:
a) negative margin-right
.box {
display: inline-block;
width: 8.74%;
height: 100%;
margin-right: -4px;
}
b) font-size: 0; on the container and default font-size on the elements inside
.color_container {
width: 98%;
height: 60px;
min-height: 60px;
padding: 5px;
background-color: #fff;
font-size: 0;
}
.box {
display: inline-block;
width: 8.74%;
height: 100%;
font-size: 1em; /* or what is your default font-size */
}

Width: 100% not filling up screen on mobile devices

I'm currently trying to optimize a Wordpress site for mobile devices, but I'm struggling with getting the footer of the site to cooperate. The site is here:
http://whitehallrow.com/
When loaded on mobile, the width of the body shrinks in accordance with the screen size and wraps all the contained text within it. However, the footer keeps its width, which I understand is because the width is hard-coded to look good on a computer screen. I've made a media query in the CSS that targets devices with screens 500 pixels wide or smaller, in order to get the footer to resize to the width of the body. Here is a snippet of my CSS that I've been tweaking:
#media screen and (max-width: 500px) {
#customfooter{
width:100%;
}
}
For whatever reason, this is not working - it still shows the footer as being much wider than the body. I've tried max-width:100%, width:auto; max-width:auto, and none of them work.
How do I achieve this without hard-coding anything?
Change your CSS from
#teakfooter {
width: 100%;
}
#verybottom {
width: 100%;
}
add a class so this gets higher priority
.page #teakfooter {
width: 100%;
}
.page #verybottom {
width: 100%;
}
I tried it out using Firebug and it seems to be working well like this.
Edit: After going over a few more things in the comments, I noticed a couple of things causing the footer to not fill out.
.site {
padding: 0 1.71429rem;
}
This is causing #customer footer to have padding on both sides.
#teakfooter {
margin-left: -40px;
}
This is causing #teakfooter to have whitespace on the right side.
also in firebug you can check METRICS (in right column you have Computed Styles, Styles, Metrics, etc.). In METRICS you will see that around your body there is a padding: 24px;
Solution:
body {
padding: 0;
}

Image auto width in CSS for all browsers?

I have my img tag defined with some CSS as:
img
{
width: auto !important;
height: auto !important;
max-width: 100%;
}
It works just fine in some of the Mobile Browsers I have tried, as well as Google Chrome on a desktop. However, it does not seem to work in IE or FireFox. By that, I mean, as you resize the window. Take a look at a sandbox site I am working on: http://terraninstitute.com. I have the image on the home page intentionally set to be a huge picture. Also navigate to the Information (main menu) then Newcomers (side menu) and notice the map image at the bottom. On my Droid (and a few other devices I can get my hands on) as well as in Google Chrome this looks pretty good. In IE and FireFox, not so much.
Am I missing something? Conflicting style definitions? I am not finding them as of yet if it is.
TIA
You're declaring the width of your images multiple times in your document unnecessarily and declaring a max-width the wrong way. Max-width is supposed to define a max size/boundary for your images (600px, for example), if you declare max-width:100% in conjunction with width:100%, you're not really doing anything with that declaration as the images will expand 100% as it is.
Remove the width declarations from your CSS in the following lines:
line 116: delete the img declaration all together, it is not needed.
line 365: remove the max-width:100% and height:auto; attribute as they are not needed (and if you can delete the declaration all together as you can declare that elsewhere)
line 121: Now just stick with this one and just add the following:
img {
height: auto;
width:100%;
}
Bootstrap solution for responsive images:
img {
/* Responsive images (ensure images don't scale beyond their parents) */
/* Part 1: Set a maxium relative to the parent */
max-width: 100%;
/* IE7-8 need help adjusting responsive images */
width: auto\9;
/* Part 2: Scale the height according to the width, otherwise you get stretching */
height: auto;
vertical-align: middle;
border: 0;
-ms-interpolation-mode: bicubic;
}
Don't use !important
Make your CSS more speficic:
body img {
width: auto;
height: auto;
max-width: 100%;
}
in style.css line line 116, 212 and in inuit.css line 365. Remove all those.
img{
height: auto;
max-width: 100%;
}
Thats all you need.

Resources