I'm new to styling, sorry if this is too basic.
In my React app, I am trying to import an image from file, like so:
import cup from './img/cup.png'
and style it alongside text and everything else contained in the <div> like so:
<div style={{display:'inline-block',
textDecoration:'underline',
cursor:'pointer'}}>
<img src={cup} alt=''/>
<h1 className="title is-4">"Item"</h1>
</div>
But this works badly. Image display is too big.
I would like to reference image right into style and manage 'height' and 'width' (or 'size') within it as well, but haven't found a way.
is this possible? how so?
Try this if you want in vertical
<div>
<div style={{ display: "inline-block",
textDecoration: "underline",cursor: "pointer"}}>
<img
style={{ height: 100 }} src={
"https://www.belightsoft.com/products/imagetricks/img/intro-video-poster#2x.jpg"
} alt=""
/>
<h1 className="title is-4">"Item"</h1>
</div>
</div>
and if you want in horizontal
<div>
<div style={{ display: "flex", textDecoration: "underline", cursor: "pointer"}}>
<img style={{ height: 100 }} src={
"https://www.belightsoft.com/products/imagetricks/img/intro-video-poster#2x.jpg"
} alt=""
/>
<h1 className="title is-4">"Item"</h1>
</div>
</div>
just add height and width size to your image, like this
<img src={cup} alt='' height={50} width={50} />
also, you can add auto to and set the size to another it will work best
<img src={cup} alt='' height={50} width='auto' />
<img src={cup} alt='' height='auto' width={50} />
Basically the first step of you is to learn what a selector is. In short, it is a naming convention to link style with the HTML tag(s)(or dom, whatever). For example, let's say you put a class to the image.
<div>
<img src={cup} alt='' className="my-image"/>
<h1 className="title is-4">"Item"</h1>
</div>
And a CSS file, which you have to import it as a dependency of the component at the header of source file, next to the import cup from './img/cup.png'.
style.css
img.my-image {
// css rules here
}
Then the magic occurs, the CSS rule, e.g., the absolute/relative width/height written in the file will be applied to the image accordingly.
Related
<script>
import { Img } from 'flowbite-svelte';
</script>
<Img src={image_url} alt="Listing" class="rounded-lg"/>
How do I define the height of the image? I have tried <Img src={image_url} alt="Listing" size="h-10" class="rounded-lg"/> but it had no effect.
Per docs, you can use imgClass to add styles to Image.
imgClass="h-auto"
<Img src={image_url} alt="Listing" class="rounded-lg" imgClass="h-10"/>
You can use class attribute :
<Img src={image_url} alt="Listing" class="rounded-lg h-10" />
I'd like to align some text with a logo on the same line:
<div id="container" style="white-space:nowrap">
<div id="image" style="display:inline;">
<Link href="" target="_blank">
<img
src={img}
loading="lazy"
width="40px"
height="40px"
alt=""
/>
</Link>
</div>
<div id="texts" style="display:inline; white-space:nowrap;">
<strong> 75 </strong>
</div>
</div>
But when I try to run it, I receive these errors:
Line 61:41: Style prop value must be an object react/style-prop-object
Line 62:41: Style prop value must be an object react/style-prop-object
Line 73:41: Style prop value must be an object
In react you need to use in style in object,
https://reactjs.org/docs/dom-elements.html#style
style={{
whiteSpace:'nowrap',
display:'inline',
}}
style prop takes an object.
Full code:
<div id="container" style={{ whiteSpace: "nowrap" }}>
<div id="image" style={{ display: "inline" }}>
<Link href="" target="_blank">
<img src={img} loading="lazy" style={{ width: "40px", height: "40px" }} alt="" />
</Link>
</div>
<div id="texts" style={{ display: "inline", whiteSpace: "nowrap" }}>
<strong> 75 </strong>
</div>
</div>
CodeSandbox Demo
Before we can answer the question you ask in the title of this post, you have to fix your inline styles, hence the error you receive from React. The answer to your question using CSS will be at the bottom of the post.
When using inline styles in React, you will need to create an object. An object is the use of key, value pairs. So, using the code given in your example, style="display:inline;" is not an object, and thus will not work as you have seen. To make this inline style work, you will need to do one of the following.
Create an object within the JSX
This method can get messy, so if you are planning to write all your styles as inline styles, I suggest using method 2.
To do this, you can follow #RiTeSh 's example. You will need to create an object and pass that to the element's style prop, WHICH CANNOT BE A STRING, as you can see from the errors you are getting. You can do the following:
// Notice how the value is the only string in the object.
style={{
whiteSpace:'nowrap',
display:'inline',
}}
And to see what this would look like when used in an element:
<div style={{whiteSpace:'nowrap', display:'inline'}} >
Hello World
</div>
Store the styles in a variable
Compared to method 1, this is a much cleaner way to add inline styles as it doesn't create a jumbled mess in your render() function.
Before you reach the render() function, create an object and store it in a variable like the one below.
const styleObject = {
whiteSpace:'nowrap',
display:'inline',
};
return (
// Your JSX here
);
And when you apply the styleObject to the JSX element, it should look like the following:
return(
<div style={styleObject} >
Hello World
</div>
);
Make img and text appear on the same line
This is quite a simple answer if you use the display: flex property on the wrapper element, which, in your case, is the div with an id of container. Here is a simple read about flexbox from W3 Schools
With inline styles on the container element:
style={{display: 'flex'}}
With CSS:
#container {
display: flex;
}
I've got the following code, that's basically an Auth0 login form and it includes the logo image as an img element, which has it's own shadown tree: It looks like this:
<div class="auth0-lock-header-welcome">
<img alt="" class="auth0-lock-header-logo" src="/supp/pixel.png">
#shadow-root (user-agent)
<span>
<img />
</span>
<div class="auth0-lock-name" title="Log in">Log in</div>
</div>
How do I select the <span> in the #shadow-root, the <img> or even both in a classic .css file? Or do I have to write some React code for it?
Here's what I tried (none worked):
.auth0-lock-header .auth0-lock-header-logo::shadow #shadow-rootspan{
display: none;
}
:host(.auth0-lock-header .auth0-lock-header-logo #alttext-image) {
display: none;
}
PS: Sorry if it's a stupid question.
I have an html structure defined like this:
<div class="container">
<div class=photoItems>
<div class=photoWrapper>
<img class="image" ... />
</div>
<div class=photoWrapper>
<img class="image" ... />
</div>
<div class=photoWrapper>
<img class="image" ... />
</div>
...
</div>
</div>
What I would like to be able to do is use the "container" style to recursively go through its children and locate the first instance of type <img/> and force a display:none;. Kind of like this (non-working css):
.container {
width: 100%;
> img:first-of-type {
display: none;
}
}
From what I understand the 'first-of-type' selector only works at the sibling level, however for reasons I am only able to operate at the "container" level - so is there a way to recursively select the first instance of an image from the styling at the great-grandfather level?
On my own understanding, ">" selector selects the elements that is first descendant of an element. Like for example div > img, this one selects all the img that are first descendant of the div and not the img that is on its second successor.
So, if I didn't misunderstood your question, what you are trying to accomplish is to find the very first img inside the .container class by using the :first-of-type selector. If we base on the structure of your elements, it will never happen. This is because you are using ">" selector but there are no direct descendants of img inside since images are being wrapped inside .photoWrapper class. And if you use div img:first-of-type, this will select all the first instance of image inside the div as well as in its successors.
Sadly, I think there's still no feature/selector on CSS that can find elements to all its successor in accordance to your question. You can read about selectors here:
https://www.w3schools.com/cssref/css_selectors.asp /
https://www.w3schools.com/cssref/css_selectors.asp
Hmm I don't know if you prefer this, but here's my workaround for your question. I will use find('img:first') function of jquery.
Hope this will help you.
$(document).ready(function(){
$('.container').find('img:first').hide();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class=photoItems>
<div class=photoWrapper>
<img class="image" alt="1" />
</div>
<div class=photoWrapper>
<img class="image" alt="2" />
</div>
<div class=photoWrapper>
<img class="image" alt="3" />
</div>
...
</div>
</div>
What about:
.container .photoWrapper:first-child img {
display:none;
}
I have a single div with several images next to one another. Let’s say I have 7 in a row for this example.
Now, when I resize the browser, I’d like them to stay in the same row next to each other and resize their height and width accordingly.
How would I go about this?
Current example
<div>
<img src=“img1.jpg” />
<img src=“img2.jpg” />
<img src=“img3.jpg” />
<img src=“img4.jpg” />
<img src=“img5.jpg” />
<img src=“img6.jpg” />
<img src=“img7.jpg” />
<\div>
Try this:
div { display: grid; grid-template-columns: repeat(auto-fit, minmax(1px, 1fr)) }
img { width: 100%; height: auto; }
<div>
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
<img src="https://m.media-amazon.com/images/M/MV5BMTU5Mzk0ODk1MF5BMl5BanBnXkFtZTcwNjA0NDQyNA##._V1_UX100_CR0,0,100,100_AL_.jpg" />
</div>
Explanation:
display: grid is the newest addition to the CSS layout techniques, and is the most elegant. It's widely supported in evergreen browsers, so all is good.
grid-template-columns: repeat(auto-fit, minmax(1px, 1fr)) finds a way to fit within 1 fraction of the whole width (so variable with the container size, which if you place this in an empty page, will vary with screen size too) with a minimum of 1px and auto-fit to whole width, each image.
width: 100% is needed to make sure the image isn't skewed.
height: auto has the same anti-distorting function if you want to use a full IMG tag with the width and height properties (which I fully recommend due to performance issues if you aren't building an HTTP/2 compatible site) or if you edit the height accidentally using some other CSS.
By the way, this works no matter how many images you have here, so no need to change the fractions or the percentages every time!
One last thing: when you have lists of any sort, I'd replace the <div> with an <ul> or <ol> if I were you, and have <li>s spread like this with some more CSS magic needed but a much more semantic DOM.
Image credits: IMDB.
A quick and dirty way is to use calc and float
div img {max-width: calc(100%/7);float:left;}
<div>
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
<img src="https://www.fillmurray.com/600/600" />
</div>
As suggested by Jon P below, you can use calc function of css. The other better way could be running media queries.
#media (min-width:1200px){
div{
width:500px;
}
div > img{
width:20%;
height:auto;
}
}
The very simplest way is to turn the container into a flexbox (which is also more widely supported than css-grid).
#imgs {
display: flex;
}
#imgs img {
width: 100%;
}
<div id="imgs">
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
<img src="https://imgs.xkcd.com/comics/angular_momentum.jpg" />
</div>