Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using CSS to set an image size

Participant ,
Feb 01, 2022 Feb 01, 2022

Hi,

I have an image placed inside <header> tags with a class named "main-head" asigned to the <header> as follows...

<header class="main-head"><img src="images/MyLogo.svg" alt="My logo"/> </header>

 ... Currently, when I start out with a small browser viewport and make it larger, the image grows in size as I make the browser viewport larger. I would rather keep it at a fixed size of my choosing.

I am wondering if anyone here can tell me if/how I can do this using CSS?

 

Ultimately, I'd like to use media queries to increase the size of the image at specific breakpoints rather than having it grow progressively.  Any advice will be greatly appreciated.

 

Thanks in advance,

Paul

 

PS... As this is more of a general web design / HTML / CSS question more than it is specific to Dreamweaver, please feel free to tell me if this forum is not an appropriate place for me to have posted this.

TOPICS
How to
681
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 01, 2022 Feb 01, 2022

If you only have one image in the header tag, you could add a selector called "header img" to your css, then give a height and width setting in pixels. For excample:

header img {

   height:50px;
   width:50px;

}

...then change the size in your various media queries.

Translate
Community Expert ,
Feb 01, 2022 Feb 01, 2022

If you only have one image in the header tag, you could add a selector called "header img" to your css, then give a height and width setting in pixels. For excample:

header img {

   height:50px;
   width:50px;

}

...then change the size in your various media queries.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 01, 2022 Feb 01, 2022

Thank you Jon and Nancy for your replies. Jon's suggestions worked for what I was trying to accomplish today. 

 

Nancy's point about using the <picture> element is well taken. I will learn how to take advantage of that.

 

I appreciate both of your help!

 

Best regards,

Paul

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2022 Feb 01, 2022

The correct way to do this is with the <picture> element and SRC SET attributes.   A different sized image is served to different sized devices thus eliminating excessive bandwidth consumption to small devices.  Some manual coding and a set of different sized images required. 

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 01, 2022 Feb 01, 2022
quote

The correct way to do this is with the <picture> element and SRC SET attributes.   A different sized image is served to different sized devices thus eliminating excessive bandwidth consumption to small devices.  Some manual coding and a set of different sized images required. 

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture

 


By Nancy OShea

 

Does anyone really bother to use srcset, particulary for a logo, which should'nt be that big anyway????

 

I know for larger photographic images it's the correct way to go but I cant help feeling because of the extra image management needed,  in a real world situation, where time and budget are zero and a website uses lots of images, this doesnt get used very often! 

 

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2022 Feb 01, 2022

Most CMS systems do it on the server. 

PHP can resize images server-side.

 

 

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 01, 2022 Feb 01, 2022
quote

Most CMS systems do it on the server. 

PHP can resize images server-side.

 

By Nancy OShea

 

 

Not really efficiently for various devices, one would need to specifically crop/size the images to get the best visual solution, not mearly reduce its physical size, a large hero image for desktop could look distinctly stupid on mobile unless it was sized/cropped correctly.

 

I think a lot of websites are using 'object-fit' cover now for images with a focus on the center of the image if possible, that can be applied well across all devices BUT of course using the one big image!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2022 Feb 01, 2022

It all depends on the CMS and how it was programmed.  Some allow multiple image uploads while others rescale and/or clip images to fit. 

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 01, 2022 Feb 01, 2022
quote

It all depends on the CMS and how it was programmed.  Some allow multiple image uploads while others rescale and/or clip images to fit. 

 


By Nancy OShea

 

Most only give the option of 1x 2x or 3x otherwise you do have to mess around cropping each image before or after uploading to the server, which is probably not what most are going to do.

 

I cant see my client, who has probably 400 locations on their website making 1200 different image size/crops - they are just going to upload 400 same sized images and be done with it!

 

I guess IF the images can retain their aspect ratio then yes, php can resize on the fly BUT if images need to be specifically cropped and sized that wont happen without a lot of human intervention.

 

I think its all down to time/money. The first website I investigated was a big UK insurance company and they indeed are using srcset (I admit I was surprised!). The next website I investigated, one of the biggest cottage holiday letting companies in the UK, but nowhere near having the finances of the big insurance company, are just using the one large image for all devices.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 01, 2022 Feb 01, 2022

If one uses artboards and/or scripts in XD, Illustrator or Photoshop, it's not hard to generate multiple sized assets. 

https://www.adobe.com/creativecloud/business/enterprise/xd-artboards.html

https://helpx.adobe.com/xd/help/export-design-assets.html

 

Most amateurs can't be bothered learning new techniques or don't know why they should.

 

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 01, 2022 Feb 01, 2022
quote

If one uses artboards and/or scripts in XD, Illustrator or Photoshop, it's not hard to generate multiple sized assets. 

https://www.adobe.com/creativecloud/business/enterprise/xd-artboards.html

https://helpx.adobe.com/xd/help/export-design-assets.html

 

Most amateurs can't be bothered learning new techniques or don't know why they should.

 


By Nancy OShea

 

Its not hard, its more time consuming than most either get paid for or want to do or even know its what they should be doing.

 

If a developer was to apply every single thing they are supposed to be doing they would never drop a job or be so poorly paid in terms of the time consumed, it becomes a labour or love,  that's ok IF you like doing it and dont need the money. The client dont give a monkies, they just want the cheapest, or most do.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 02, 2022 Feb 02, 2022
LATEST

There is a lot of missinformation, not correct information in this thread unfortnatly.

Some out dated, some too new.

 

Picture is great if you can provide the varied assets and not using a CMS or application sollution as most still do not support that. And there are a number of compatbility reasons why as well. You will not see it used on the web as a whole as much as you may think considering how long it has been around. No point listing all the reasons why but that is how things are.

If you are using a CMS or web application everything handles images differently. A system like Umbraco returns an object. You upload an image and to render you do not just get the URL, you get an object with data. Of which, as people like Nancy has mentioned you get abiltiies like multi image creation, thumbnails etc. The way Umbraco does it with amazing client side UX for focus point, scaling and editing is second to none for example. Adobe's now EOL Business Catalyst like others offered parameter otpions at the end of the URL to system scale the image as well as compression option for quality.
This is actually why picture is not adopted because your serving more images and HTML/client side VS what you can do server side - you want server side for perfomance.
Any comment in terms of server side being bad here in 2022 is actually not true.

Then considering mobile and retinna displays but not only that, scaling of a browser. A desktop view on an imac vs a laptop or notebook and tablet rendering as a desktop IS NOT the same width, height and pixel density so images need to scale and look good regardless. Many cases an image small on desktop may be larger in width of a mobile with the responsive aspect.

So again, like some have said sticking with <img> utilising what ever CMS or server tools is still the most common and constant option and as shown can still be the better performing.

When it comes to size and scale and also considering SEO and Social media and more putting the width and height on the img tag should be done. Then any corrisponding CSS style in your css can be applied.

In most cases for a good true modern scalable image if its a photo in content for example then your CSS is actuall more like....

img{
max-width:500px;

width:100%;

height:auto;
}

It may be max-width is 100vw.
You may want to scale based on the height of the viewable window so
width:100vh;
height:auto;

OR after all the posts what your after is the basic

header img{
width:50px;
height:50px;
}
That does not change.
You should really do an image 100px by 100px so when it is compressed like this it looks better for responsive.

IF the image is an icon it SHOULD NOT be an image.
SVG or a font icon for example is the better solution in that regard as well.

Logos for example I never do as an image. Text is actual text or an SVG as more often than not elements change based on the mobile responsive, there is animation going on etc.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines