I'm building carousel with react, and content images are horizontally aligned.
If I say 3 tags are in div with width 1200px, how can I control these images what I want whether it's overflow container div or not? ( cf. It's not important, however I'm using React.js)
sorry for skipping my codes so far. I'm trying to handle this with styled-components"
const SliderContainer = styled.div`
overflow: hidden;
width: 100%;
position:relative;
`;
const Slider = styled.div`
width:100%;
overflow:hidden;
display: flex;
`
const IMG = styled.img`
width: 100%;
height: 70vh;
`;
export const Slide: SFC<{
src?: string,
alt?: string
}> =({
src,
alt
}) => {
return(
<IMG src={src} alt={alt}/>
)
}
export const Carousel: FC = () => {
return(
<SliderContainer>
<Slider>
<Slide src={"1.png"}/>
<Slide src={"2.png"}/>
<Slide src={"3.png"}/>
</Slider>
</SliderContainer>
)
<div class="parent">
<div class="container">
<img id="img1" />
<img id="img2" />
<img id="img3" />
</div>
</div>
when we want those images( or contents) in div which has container as a classname over flow <div class="parent"></div>
we should specify the length of <div class="container"></div>
so in this case
.parent{
width: 100%;
overflow;
}
.container{
display: flex; // so we have 3 images horizontally
width: 1028px; // whatever but more than .parent widh
}
.img{
width:100%;
height: auto;
}
the reason we should specify .container div width is, we have flex display in this case, so whole img width might be auto resize through this flex properties.
and .parent's overflow: hidden property only make sense when it's child tag has large length more than itself.
Related
So I wrapped my content in the layout component and all my other pages push the footer to the bottom because the content is bigger than the actual screen. However, I have one page that has less content and now my footer is awkwardly in the middle if I view it on a bigger height screen.
So here is my footer css
export const FooterContainer = styled.div`
background-color: #101522;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: absolute;
bottom: 0;
width: 100%;
`
My layout div css
.layout {
padding-bottom: 160px;
position: relative;
}
My layout component
return (
<>
<Sidebar isOpen={isOpen} toggle={toggle} />
<Navbar toggle={toggle} />
<div className="layout">
<main>{children}</main>
<Footer />
</div>
</>
)
}
Then in my page where my footer isn't sticking to the bottom this is my code
<Layout>
<div className="resource__container">
<h1 className="post__heading">
Here are some resources to learn web development
</h1>
<Posts posts={posts} key={posts.id} />
</div>
</Layout>
So here is a pic showing what is happening
The top black bar is the Navbar
The red section is my .resource__container
The white space below that is the padding-bottom: 160px from the .layout <div>
Then the last black space is the footer
After that it's just <main> but technically nothing there, so I don't why it isn't sticking to the bottom
Given a HTML structure like:
<body>
<header>…</header>
<main>…</main>
<footer>…</footer>
</body>
The solution to stick the footer at the bottom is using flexbox:
body {
display: flex;
min-height: 100vh;
flex-direction: column;
}
main {
flex: 1;
}
The key is the min-height: 100vh as well as flex: 1 of the main tag.
Useful resources:
https://dev.to/mokkapps/sticky-footer-in-gatsbyjs-using-flexbox-5162
https://css-tricks.com/couple-takes-sticky-footer/
https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/
I'm trying to understand why the following code does not creates a scrollable flexbox.
var firstData = ["One", "Two", "Three", "Four"]
FillDiv();
FillDiv();
$("button").on('click', function() {
FillDiv();
});
function FillDiv() {
var newData = GetData();
for (i = 0; i < newData.length; i++) {
$(".wrapper").append('<div>Test</div>');
}
}
function GetData() {
return firstData;
}
.wrapper {
display: flex;
overflow-x: auto;
}
.wrapper div {
display: block;
width: 200px;
height: 200px;
background-color: black;
color: white;
text-align: center;
margin-left: 1%;
}
button {
margin-top: 20px;
margin-left: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
</div>
<button>
NEXT
</button>
However, the following example does:
.container {
display: flex;
overflow-x: auto;
}
<div class="container">
<img src="https://as1.ftcdn.net/jpg/02/12/43/28/500_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" alt="">
<img src="https://as1.ftcdn.net/jpg/02/12/43/28/500_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" alt="">
<img src="https://as1.ftcdn.net/jpg/02/12/43/28/500_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" alt="">
<img src="https://as1.ftcdn.net/jpg/02/12/43/28/500_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" alt="">
<img src="https://as1.ftcdn.net/jpg/02/12/43/28/500_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" alt="">
<img src="https://as1.ftcdn.net/jpg/02/12/43/28/500_F_212432820_Zf6CaVMwOXFIylDOEDqNqzURaYa7CHHc.jpg" alt="">
</div>
Why does the scroll breaks when I add width and height in first example?
Add flex-shrink: 0 to your Test divs. Flex items are set, by default, to flex-shrink: 1, which enable them to shrink in order to prevent an overflow of the container.
For more details see "The flex-shrink factor" section in my answer here:
What are the differences between flex-basis and width?
The problem addressed above doesn't exist in your second example (with the images) because another default setting of flex items is min-width: auto. This means that flex items cannot, by default, be smaller than their content. If you override this default with img { min-width: 0 }, you'll get the same behavior as in your first example.
For more details about the flex minimum sizing algorithm see:
Why don't flex items shrink past content size?
This question already has answers here:
How to match width of text to width of dynamically sized image/title?
(2 answers)
Closed 2 years ago.
I have a component (Container) that contains an icon (marked with an X below), a title and a child component (Message) that contains a long message. I would like Container's width to wrap around the icon and the title so both are on one line as much as window's width allows for it.
Message component has a button that toggles display of a long text. This text should not stretch the parent Container and it should be aligned with title's width. The message content can be broken and wrapped at any point:
I used a flex-grow: 1; width: 0; style on a dummy div in Message as suggested
here to prevent it from growing. This works well on all browsers except for MS Edge, where the message content stretches the parent:
How can I fix this issue on MS Edge?
Is there alternative way using only CSS that I can prevent the message content from stretching its parent?
Style.css:
.box {
display: table;
margin: auto;
border: 1px solid black;
}
.container {
display: flex;
justify-content: center;
}
.icon {
margin-right: 10px;
}
.message {
display: flex;
}
.message > div {
flex-grow: 1;
width: 0;
word-break: break-all;
}
Container.jsx:
export const Container = () => {
return (
<div className='box'>
<div className='container'>
<div className='icon'>
X
</div>
<div className='content'>
<div className='title'>
Some title
</div>
<Message>
Long message that should not make parent wider
</Message>
</div>
</div>
</div>
);
}
Message.jsx:
export const Message = ({children}) => {
const [isExpanded, setExpanded] = React.useState(false);
const handleClick = () => setExpanded(!isExpanded);
return (
<div>
<div>
<button onClick={handleClick}>Click</button>
</div>
{isExpanded &&
<div className='message'>
<div>{children}</div>
</div>
}
</div>
);
}
Try width:0;min-width:100%; on the message container:
.box {
display: table;
margin: auto;
border: 1px solid black;
}
.container {
display: flex;
justify-content: center;
}
.icon {
margin-right: 10px;
}
message {
display:block;
width:0;
min-width:100%;
}
<div class='box'>
<div class='container'>
<div class='icon'>
X
</div>
<div class='content'>
<div class='title'>
Some title
</div>
<message>
<div>Long message that should not make parent wider</div>
</message>
</div>
</div>
</div>
Or to the div inside the message:
.box {
display: table;
margin: auto;
border: 1px solid black;
}
.container {
display: flex;
justify-content: center;
}
.icon {
margin-right: 10px;
}
message {
display:block;
}
message > div {
width:0;
min-width:100%;
}
<div class='box'>
<div class='container'>
<div class='icon'>
X
</div>
<div class='content'>
<div class='title'>
Some title
</div>
<message>
<div>Long message that should not make parent wider</div>
</message>
</div>
</div>
</div>
I'm trying to add a background image behind my Form component in a react App, but the background shows up only on part of the screen. I want it to be spread across the entire screen. This is the code for my Signup component that nests the Form component inside of it.
Is there an easier/better way to do this? I am new to React and web development. Thanks!
import React from "react";
import Form from "../Form";
import logo from "../../images/logo.png";
import background from "../../images/login_background.png";
class Signup extends React.Component {
onSubmit = fields => {
console.log("Got", fields);
};
render() {
return (
<div
style={{
backgroundImage: `url(${background})`,
backgroundSize: "cover"
}}
>
<div
style={{ position: "relative", top: "200px" }}
className="ui centered aligned grid"
>
<div>{/* <img src={logo} alt="intecreate logo" /> */}</div>
<div className="ui raised segment">
<Form onSubmit={fields => this.onSubmit(fields)} />
</div>
</div>
</div>
);
}
}
export default Signup;
Give more 200px to the background
Because you use top: 200px you have to make up for it and increase the div of the background.
One way is to add height: calc(100% + 200px);
It will help to get a work link in codepen for instance.
And better you use class and not inline style for the beginning :)
After comment adding example
Simple example that show what I mean.
The father must have height.
body{
margin: 0;
}
.centered{
text-align: center;
height: 200px;
}
.wrap{
background-image:url(https://images.pexels.com/photos/255379/pexels-photo-255379.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500);
background-size: cover;
height: calc(100% + 50px);
}
.segment{
background-color: #3791e1;
height: 200px;
width: 150px;
top: 50px;
position: relative;
margin: 0 auto;
}
<div class="ui centered aligned grid">
<div class="wrap">
<div class="ui raised segment">
Form
</div>
</div>
</div>
I have a flexbox container with two flex items in it. One is an image and the other a paragraph. I've been trying to resize the image proportionally by giving width:some-percentage and height:auto but it's not working. Please help me solve this.
.item{
display:flex;
}
img{
width: 25%; /* not working */
height: auto;
}
<div class="item">
<img
src=
"https://png.pngtree.com/element_origin_min_pic/16/10/16/105802ebe43fe0f.jpg"/>
<p>some paragraph</p>
</div>
JSFiddle link - https://jsfiddle.net/dizzyramen/xfot3Lwv/2/
A default setting on a flex container is align-items: flex-start.
In a row-direction container, this makes flex items, not having a defined height (e.g. height: auto), extend the full height of the container (full explanation).
In this particular case, however, the image is stretching to its fullest, and expanding the size of the container along with it.
The solution is to set a height limit on the container or override the default with align-items: flex-start on the container or align-self: flex-start on the item.
jsFiddle demo
.item {
display: flex;
}
img {
width: 25%;
align-self: start; /* new */
}
<div class="item">
<img src="https://png.pngtree.com/element_origin_min_pic/16/10/16/105802ebe43fe0f.jpg" />
<p>some paragraph</p>
</div>
here is another option:
wrap your img in a div <div class="image-wrapper"> and set manage the width in this node.
asign width: 100%; height: auto; to the img so it adjust proportionally to its parent.
Here you have it in a snippet. Hope it helps.
<div class="item">
<div class="image-wrapper">
<img
src=
"https://png.pngtree.com/element_origin_min_pic/16/10/16/105802ebe43fe0f.jpg"/>
</div>
<p>some paragraph</p>
</div>
<style>
.item{
display:flex;
}
.image-wrapper {
width: 25%; /* means 25% of .item */
}
img{
width: 100%; /* means 100% of its parent .image-wrapper */
height: auto;
}
</style>
It happens because, per default the tag <img>is inline. You need to change it to block or flex