image question

Im busy with another FEM Challenge. Now on mobile the image has to take the whole width But on desktop it has a fixed max width Can I do something like this :
img {
width: 100%;
max-width: 1200px
}
img {
width: 100%;
max-width: 1200px
}
or do I have to use media queries to make this work ?
76 Replies
Rägnar O'ock
Rägnar O'ock2mo ago
this would work, but I'm not sure it would do what you want this rule would make all images full width or 1200px whichever is smaller so if the parent element is 800px, the image will be 800px, but if it's 1500px the image will be 1200px
roelof
roelofOP2mo ago
oke, then back to the books
Rägnar O'ock
Rägnar O'ock2mo ago
what do you want to achieve exactly? 'cause this rule might be what you are after, it's just not clear from your question
roelof
roelofOP2mo ago
I will copy the pictures im giving with the challenge
roelof
roelofOP2mo ago
it has to look this way on mobile ;
No description
roelof
roelofOP2mo ago
and like this on desktop :
No description
Rägnar O'ock
Rägnar O'ock2mo ago
looks like the image always fills it's parent container's width, so I would have width: 100% on the image and the max-width on the parent.
roelof
roelofOP2mo ago
for me not on the desktop the top image has some sort of padding and on mobile not
Rägnar O'ock
Rägnar O'ock2mo ago
padding almost never goes on images themselves, but on their parent container. and if you look at the text on the desktop version it has the same padding as the image. which hints at them being in the same container if I had to make this layout I would have something like :
<article>
<img />
<section>intro</section>
<section>ingredients struff</section>
...
</article>
<article>
<img />
<section>intro</section>
<section>ingredients struff</section>
...
</article>
so on desktop the padding goes on the <article> and on mobile it would go on each <section> (could also always put it on the <article> and cancel it on the image with a negative margin on mobile... but I don't like negative margins)
roelof
roelofOP2mo ago
I have done it like this :
<header>
<img>
<h1>
<quote>
</header>
<main>
<div> </div>
<div> </div>
<div> </div>
</main>
<header>
<img>
<h1>
<quote>
</header>
<main>
<div> </div>
<div> </div>
<div> </div>
</main>
Rägnar O'ock
Rägnar O'ock2mo ago
then you don't really have a choice but to use negative margins could also do fancy stuff with grid... but that's overkill and kinda stupid)
roelof
roelofOP2mo ago
yep, this one looks more like a flex one then a grid one
Rägnar O'ock
Rägnar O'ock2mo ago
you can only have one <main> per document, so this markup would not work if you have more than one recipes on the page. nah, you don't need a flex either, all your element's are blocks, they will stack without any need for a layout tool like grid or flex
roelof
roelofOP2mo ago
This challenge has only 1 recipe I do also think you do not need flex but if I understand you right , I need to use a media query here
Rägnar O'ock
Rägnar O'ock2mo ago
or a container query if there's other stuff no the page that could influence the available space (like if you have a nav bar on the left or a table of content on the right for example)
roelof
roelofOP2mo ago
nope , in this case nothing else then shown on the pictures and I have to learn then container queries and maybe im going to use your markup I like the idea of using semantic html
roelof
roelofOP2mo ago
@Rägnar you mean something like this : `
Rägnar O'ock
Rägnar O'ock2mo ago
that's... a lot of code... codepen ?
Rägnar O'ock
Rägnar O'ock2mo ago
quote no adapted to this use, just use a section and you might want to use a a <dl> instead if a <ul> and the nutrition section should be a table the ol is fine, I think
roelof
roelofOP2mo ago
oke, so the <quote> can also be a <section> I have to look what is the difference between a dl and a ul The rest I can change
Rägnar O'ock
Rägnar O'ock2mo ago
I linked you the mdn page just above
roelof
roelofOP2mo ago
where do I have to change this:
and you might want to use a a <dl> instead if a <ul>
and you might want to use a a <dl> instead if a <ul>
?
Rägnar O'ock
Rägnar O'ock2mo ago
for the prep times usually if you have a list of "something: something else" you want a DL
roelof
roelofOP2mo ago
oke So you mean I can do something like this :
<dl>
<dt>Total</dt>
<dd>: Approximately 10 minutes </dd>
<dl>
<dt>Total</dt>
<dd>: Approximately 10 minutes </dd>
?
Rägnar O'ock
Rägnar O'ock2mo ago
the : should be in the <dt> or a pseudo element
roelof
roelofOP2mo ago
oke I did this way because the text should be bold but the : not have to think how to do that What do you mean with a pseudo element ? :before or so ?
Ganesh
Ganesh2mo ago
You can just have two span in dt Put bold style on first span and nothing on other. Or just have one span and remove the bold style from it
roelof
roelofOP2mo ago
oke So like this :
<dl>
<dt> <span class="bold">Total</span>:</dt>
<dd>Approximately 10 minutes </dd>
<dl>
<dt> <span class="bold">Total</span>:</dt>
<dd>Approximately 10 minutes </dd>
?
Rägnar O'ock
Rägnar O'ock2mo ago
that would work too, but in this case the : is presentationnal, it doesn't add anything that the markup doesn't already convey. so you can use a dt::after {content: ':'} and if you want
Ganesh
Ganesh2mo ago
Oh right with span the screen reader will probably pick up the colon
roelof
roelofOP2mo ago
but is the colon very important to hear ?
Rägnar O'ock
Rägnar O'ock2mo ago
no, it's implied by the DT
roelof
roelofOP2mo ago
oke, and then set the font-weight to a non-bold property ?
Rägnar O'ock
Rägnar O'ock2mo ago
yeah
roelof
roelofOP2mo ago
Could work I have to try
roelof
roelofOP2mo ago
@Rägnar I have made it a dl But the first is not bold what I expect and the text is on the next line Do I have to make that good with css https://codepen.io/RoelofWobben/pen/ByNXedq
roelof
roelofOP2mo ago
??
Ganesh
Ganesh2mo ago
dt is not automatically bold if that is what you are asking
roelof
roelofOP2mo ago
oke For the examples I thought it would be
Ganesh
Ganesh2mo ago
were you looking at mdn example? I think that has some font or styles applied
roelof
roelofOP2mo ago
I thought I did
Ganesh
Ganesh2mo ago
No description
Ganesh
Ganesh2mo ago
yes this has some font set and font style oblique
roelof
roelofOP2mo ago
oke I tried this :
dt {
font-weight: 700;
}

dt::after {
content: ':'
font-family: "Outfit";
font-weight: 400;
}
dt {
font-weight: 700;
}

dt::after {
content: ':'
font-family: "Outfit";
font-weight: 400;
}
but as soon as I do the font-x I loose the :
Ganesh
Ganesh2mo ago
font-x or just font family remove font family and see what happens
roelof
roelofOP2mo ago
The same when I do this:
dt::after {
content: ':'
font-weight: 400;
}
dt::after {
content: ':'
font-weight: 400;
}
if I only do the content everything looks fine except the : is also bold what is not what is needed
Ganesh
Ganesh2mo ago
No description
Ganesh
Ganesh2mo ago
works for me you forgot a semi colon after content unlike javascript css will not tolerate missing semi colons
roelof
roelofOP2mo ago
chips, that was it after dinner I have to look why the text is on two lines instead of 1
Ganesh
Ganesh2mo ago
dd is block level I think so should be dt
roelof
roelofOP2mo ago
if I looks here : https://codepen.io/RoelofWobben/pen/ByNXedq it does not look like it
No description
Ganesh
Ganesh2mo ago
what doesn't look like it
roelof
roelofOP2mo ago
it is a block element I see clearly that the text is on two lines
Ganesh
Ganesh2mo ago
then what is the reason dt is block level so it spans the whole width dd is too and does the same what if they were inline what do you think will happen
roelof
roelofOP2mo ago
oke then I have to set a width or use flex to display them next to each other
Ganesh
Ganesh2mo ago
flex is probably better
roelof
roelofOP2mo ago
oke, then I have to figure out where flex schould be working oke this seems to work
dl .row {
display: flex;
flex-direction: row;
gap: 2em;
}
dl .row {
display: flex;
flex-direction: row;
gap: 2em;
}
` so wrap it in a div and that is doing the trick chips. with that I cannot make it work that all items seems to need different gaps sorry not needed if I look at the template but I still think it s looking better that the text after the : are on the same place is there a better way then wrapping everything in a div ?
Ganesh
Ganesh2mo ago
I think you need to elaborate I don't know what you're trying to do right now I might not answer till much later need to go to bed
roelof
roelofOP2mo ago
What I want is that the td and dd are on one row
Chris Bolson
Chris Bolson2mo ago
You could do something like this:
dl {
display: grid;
grid-template-columns: auto 1fr;
gap: 0.5em;
}
dl {
display: grid;
grid-template-columns: auto 1fr;
gap: 0.5em;
}
roelof
roelofOP2mo ago
@Chris thanks
roelof
roelofOP2mo ago
not complete perfect There is now a gap between the two and that is not needed
vince
vince2mo ago
A little off topic, but how much effort / time are you putting in before asking a question? It's fine to ask as many questions as you need but it can get to the point where you aren't learning much
roelof
roelofOP2mo ago
For my idea i do a lot of google and trying Even after asking im still trying things and use google hmm, I can do this:
dd {
display: inline-block;
}
dt {
font-weight: 700;
display: inline-block;
}

dt::after {
content: ':';
font-weight: 400;
}
dd {
display: inline-block;
}
dt {
font-weight: 700;
display: inline-block;
}

dt::after {
content: ':';
font-weight: 400;
}
but then still I have this :
preperation : 10min
preperation : 10min
Instead of
prepration: 10min
prepration: 10min
` So still some work to do @vince do you have a hint ?
vince
vince2mo ago
I'm not sure either but in situations like this I try to make the problem as simple as possible by reducing the complexity So make a separate codepen or file that only has the issue you have and try to experiment with different styles or elements
roelof
roelofOP2mo ago
I did already and at this moment, out of ideas Tried padding and margin on the dt and on the dd but no luck oke, it seems that wrapping in a div is the best solution I can find PIty that I do not know a better way
Chris Bolson
Chris Bolson2mo ago
Sorry, my mistake. I assumed that you were referring to the bottom Nutrition data. I honestly think that you have overcomplicated this for yourself by making the preparation time a <dl>. Sticking with a <ul> would have been so much simpler. I can see why it makes sense for the nutrition list but not for this one.
roelof
roelofOP2mo ago
@Chris Bolson I understand your point but another person ( Ragnar) said I had to be a <dl>
Chris Bolson
Chris Bolson2mo ago
I am not sure why they suggested that as it really is just a bullet list of points. I am not saying that Ragnar is wrong, no doubt they know more about this than me, but sometimes you need to use the best tool for the job. In terms of following the design, if you use <dl> for this you are creating more work for yourself (and the browser) as you need to add things in that would already be there if you used <ul> eg: - Make the items inline in groups of 2 - Add the bullet points
roelof
roelofOP2mo ago
@Rägnar what do you think about this ?
Rägnar O'ock
Rägnar O'ock2mo ago
you have a lost > before the image and a lost closing </p> tag and you have 2 unclosed <section> tags (line 2 and 7)
Chris Bolson
Chris Bolson2mo ago
you could simplify the CSS without having to use absolute positioning on the bullets if you added them as pseudo elements on the wrapper rather than on the dt, something like this:
dl > .wrapper {
position: relative;
display: flex;
gap: 0.5em;
}
dl > .wrapper::before {
content: "•";
padding-inline-end: .5em; /* space between bullet and dt*/
}
dt::after {
content: ':'
}
dl > .wrapper {
position: relative;
display: flex;
gap: 0.5em;
}
dl > .wrapper::before {
content: "•";
padding-inline-end: .5em; /* space between bullet and dt*/
}
dt::after {
content: ':'
}
roelof
roelofOP2mo ago
oke, I changed everything and hope everything is well

Did you find this page helpful?