How can I display these images in a 2x2 grid? - html

My goal here is to display 4 images in a 2x2 grid at the center of the page that will have text underneath all of the images. These images will then serve as a link to other pages. However, I am having trouble getting these images to display how I would like them to! Any help would be greatly appreciated. Please see relevant code. Thank you!
#main img{
max-width: 500px;
height: auto;
margin-top: 10px;
display: block;
grid-template-columns: auto;
grid-template-rows: auto;
grid-gap: 10px;
margin-left: auto;
margin-right: auto;
align-items: center;
border: 3px lightblue solid;
position: relative;
}
<div id="main">
<div class="category-1">
<img src="./images/apples.jpg" alt="Sandwich"/>
<h3>Candy Apples!</h3>
<p>Just some random text, lorem ipsum text praesent tincidunt ipsum lipsum.</p>
</div>
<div class="category-2">
<img src="./images/santacookie.jpg" alt="Steak">
<h3>Christmas Cookies!</h3>
<p>Once again, some random text to lorem lorem lorem lorem ipsum text praesent tincidunt ipsum lipsum.</p>
</div>
<div class="category-3">
<img src="./images/eastertreats.jpg" alt="Cherries">
<h3>Easter Treats!</h3>
<p>Lorem ipsum text praesent tincidunt ipsum lipsum.</p>
</div>
<div class="category-4">
<img src="/w3images/wine.jpg" alt="Pasta and Wine">
<h3>Category 4</h3>
<p>Lorem ipsum text praesent tincidunt ipsum lipsum.</p>
</div>
</div>

You have placed and used the grid properties wrongly.
Try this:
#main {
display: grid;
grid-template-columns: auto auto;
grid-gap: 10px;
}
#main img{
max-width: 500px;
height: auto;
margin-top: 10px;
display: block;
margin: auto;
border: 3px lightblue solid;
}

Related

One Column to extend to edge of the window and other Column to remain within container

I want to create a two-column layout where the column on the left is 40% of the container and stays/aligns with the container but the column on the right expands to the edge of the window. I am able to do it with position absolute but I am sure there are better ways to do it. Following is the image and the code snippet of what I did. Is there a better way to do the same thing?
div {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: arial;
}
.header {
padding: 20px 10px;
background: #EEE;
}
.container {
max-width: 1000px;
margin: auto;
display: flex;
gap: 0;
position: relative;
}
.container.wide {
width: 100%;
max-width: unset;
}
.relativediv {
position: relative;
}
.absolute-container {
width: 100%;
max-width: 1000px;
padding: 20px 10px;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
}
.container.wide :nth-child(1) {
width: 40%;
}
.container.wide :nth-child(2) {
width: 60%;
min-height: 500px;
}
.container.wide :nth-child(2) img {
height: 100%;
width: 100%;
object-fit: cover;
display: block;
}
<div class="container header">
Header
</div>
<div class="relativediv">
<div class="container wide">
<div></div>
<div>
<img src="https://i.picsum.photos/id/0/5616/3744.jpg?hmac=3GAAioiQziMGEtLbfrdbcoenXoWAW-zlyEAMkfEdBzQ" />
</div>
</div>
<div class="absolute-container">
<div class="content">
<p>Lorem ipsum dolar sit smit is dummy text. </p>
<p>Lorem ipsum dolar sit smit is dummy text. </p>
<p>Lorem ipsum dolar sit smit is dummy text. </p>
<p>Lorem ipsum dolar sit smit is dummy text. </p>
<p>Lorem ipsum dolar sit smit is dummy text. </p>
</div>
</div>
</div>
Simply use Flexbox to create a 2 column layout. To center the text vertically inside your left column, you can use flexbox too:
.relativediv {
display: flex;
flex-direction: row-reverse;
}
.container {
width: 60%;
max-width: 1400px;
}
.container img {
width: 100%;
}
.absolute-container {
flex-grow: 1;
display: flex;
align-items: center;
}
<div class="container header">
Header
</div>
<div class="relativediv">
<div class="container wide">
<div></div>
<div>
<img src="https://i.picsum.photos/id/0/5616/3744.jpg?hmac=3GAAioiQziMGEtLbfrdbcoenXoWAW-zlyEAMkfEdBzQ" />
</div>
</div>
<div class="absolute-container">
<div class="content">
<p>Lorem ipsum dolar sit smit is dummy text. </p>
<p>Lorem ipsum dolar sit smit is dummy text. </p>
<p>Lorem ipsum dolar sit smit is dummy text. </p>
<p>Lorem ipsum dolar sit smit is dummy text. </p>
<p>Lorem ipsum dolar sit smit is dummy text. </p>
</div>
</div>
</div>

Make <div> with childs always fit in parent <div>

I'm trying to make a div with some children inside always fit to the parent div which covers the whole page. It already works that the child div's width fits into the parent's. But the height should also fit inside the parent div. I want to avoid using overflow: auto because then the user will have to scroll in some cases. The child div (with it's children inside) should always fit in the parent's height and width.
How can I make this in CSS?
<div class="parent">
<div class="child">
<button>Toggle</button>
<img />
<p>Some text here...</p>
</div>
</div>
The <div class="parent"> covers the whole page. The <div class="child"> should scale into the <div class="parent"> width and height.
You can try this. You would need a media query for mobile but does what I think you are asking...
<html>
<head>
<style>
body {
font-weight: 400;
background-color: black;
color: white;
font-family: sans-serif;
margin: 0;
}
.grid {
min-height: 100%;
grid-column: 1 / -1;
grid-gap: 0;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(33%, 1fr));
}
.grid>span {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}
.image {
background-image: url('https://live.staticflickr.com/7206/6859864719_5d68aedbd7.jpg');
background: cover;
background-position: center center;
background-repeat: no-repeat;
}
.toggle {
background: #4096C0;
border-radius: 2px;
padding: 12px 24px;
color: white;
width: auto;
text-align: center;
cursor: pointer;
}
.toggle:hover {
background: #00537C;
}
.text {
background: #40B6C0;
padding: 72px;
color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="grid">
<span class="toggle">TOGGLE</span>
<span class="image"></span>
<span class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eu fermentum dolor. Cras aliquet tempus elit ut eleifend. In commodo malesuada nisi non vulputate.</span>
</div>
</div>
</body>
</html>

Rectangular inside circle responsive with contents

I'm trying to make rectangular inside the circle on the left , when im resizing the screen of the website the shapes moving out and the texts checking out from the shapes , any ways to make texts and shapes responsive to all screens
The code : https://codepen.io/enespro/pen/rNWmLvP
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="circle">
<h3 class="mt-4">Bzns Monster School<br><span style="font-size: 20px; color: #DDDD;">From Zero To Hero</span></h3>
</div>
<div class='rect-box'>
</div>
<div class='rect-content'>
<div class="content">
<span>Lorem ipsum dolor sit amet, consetetur<br> sadipscing elitr, sed diam nonumy eirmod<br> tempor invidunt ut labore et dolore magna.</span>
</div>
</div>
<button class="btn btn-outline-secondary ml-1">Read More</button>
</div>
I see in your code there's a lot of position absolute that will just drive you crazy in long term to maintain that code. What about using the magic of flex, instead? :)
I rewrote your code here below. Check it clicking on "Run Code Snippet" to see it in action.
I suggest on the mobile to separate the banner from the circle to make it responsive and readable.
Enjoy!
.wrapper {
position: relative;
display: flex;
align-items: center;
min-height: 300px;
flex-direction: column;
}
.circle {
border-radius: 50%;
background-color: #000;
min-width: 200px;
min-height: 200px;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.circle span {
color: #fff;
}
.rectangle {
width: 100%;
padding: 10px;
text-align: center;
background-color: #eee;
}
#media screen and (min-width: 600px) {
.circle {
position: absolute;
}
.rectangle {
padding: 30px 0 30px 220px;
}
.wrapper {
flex-direction: row;
}
}
<div class='wrapper'>
<div class='circle'><span>Lorem Ipsum</span></div>
<div class='rectangle'>Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum</div>
</div>

On mobile screen sizes, how can I increase the width of a child element while not affecting the size of its sibling?

How can I expand the width of the text group while not affecting the sizing of the image? When I add a width on the text group, it makes the image smaller.
body {
background: #0A0B5B;
}
.hero {
display: flex;
justify-content: space-between;
}
.text-group {
background-color: green;
}
img {
position: relative;
right: -40%;
}
<section class="hero">
<div class="text-group">
<h2>Space Enthusiast & JavaScript Developer</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing.</p>
</div>
<img src="https://raw.githubusercontent.com/iamshaunjp/responsive-css-grid-build/lesson-14/assets/banner_image.png" alt="">
</section>
I think this is more suitable for a background:
body {
background: #0A0B5B;
}
.hero {
display: flex;
background:
url(https://raw.githubusercontent.com/iamshaunjp/responsive-css-grid-build/lesson-14/assets/banner_image.png)
right/
auto 100%
no-repeat;
}
.text-group {
min-height: 300px;
color:#fff;
}
<section class="hero">
<div class="text-group">
<h2>Space Enthusiast & JavaScript Developer</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing.</p>
</div>
</section>
You can apply a fixed width and flex-shrink: 0; to the .text-group. That way it won't become any smaller than the defined width.
body {
background: #0A0B5B;
}
.hero {
display: flex;
justify-content: space-between;
}
.text-group {
background-color: green;
width: 300px;
flex-shrink: 0;
}
img {
position: relative;
right: -40%;
}
<section class="hero">
<div class="text-group">
<h2>Space Enthusiast & JavaScript Developer</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing.</p>
</div>
<img src="https://raw.githubusercontent.com/iamshaunjp/responsive-css-grid-build/lesson-14/assets/banner_image.png" alt="">
</section>
I modified the code with more flex configuration. I hope this is what you are looking for.
body {
background: #0A0B5B;
}
.hero {
display: flex;
justify-content: space-between;
}
.text-group {
background-color: green;
display: flex;
flex-direction: column;
justify-content: center;
flex: 1 0 500px;
}
img {
flex: 0 0 auto;
}
<section class="hero">
<div class="text-group">
<h2>Space Enthusiast & JavaScript Developer</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing.</p>
</div>
<img src="https://raw.githubusercontent.com/iamshaunjp/responsive-css-grid-build/lesson-14/assets/banner_image.png" alt="">
</section>

Flexbox divs overlap and responsive

I am trying to make some elements overlap and staggered using flexbox, but I also need it responsive to stack when on mobile. I am new to flexbox and want to see if I can get some help making this responsive. Below is my html and css. As you can see when you run the snippet, it is not how I want it to look. This is my codepen and that is the desktop layout I would like, then I would like the divs to stack as it gets to mobile: https://codepen.io/ascarb1/full/zWZVRw/
Any help is really appreciated.
body {
max-width: 1600px;
width: 95%;
margin: 0 auto;
}
.home-story-container {
display: flex;
margin: 10em auto;
flex-direction: column;
justify-content: space-between;
align-items: left;
float: none;
}
.home-story-container .home-story-text-block {
height: 373px;
width: 618px;
background: #ddd;
align-self: flex-start;
flex-shrink: 0;
margin-left: 15em;
}
.home-story-container .home-story-text-block .home-story-text-content {
width: 60%;
margin: 4em 0 0 4em;
}
.home-story-container .home-story-image-block {
width: 640px;
align-self: flex-end;
flex-shrink: 0;
margin-right: 14em;
margin-top: -23em;
}
.home-story-container .home-story-video-block {
width: auto;
align-self: flex-start;
flex-shrink: 0;
margin-left: 21em;
margin-top: -10em;
}
.home-story-container .home-story-quote-block {
align-self: flex-end;
flex-shrink: 0;
margin-right: 16em;
margin-top: -9.5em;
width: 413px;
}
<div class="col-md-12">
<div class="home-story-container">
<div class="home-story-text-block">
<div class="home-story-text-content">
<h1>Lorem ipsum dolor sit amet</h1>
<br>
<p>Lorem ipsum dolor sit amet, malesuada quisque sit consectetuer adipiscing odit, sed tortor leo nunc, a vel erat ultricies.</p>
</div>
</div>
<div class="home-story-image-block"><img src="http://via.placeholder.com/640x373"></div>
<div class="home-story-video-block"><iframe width="560" height="315" src="https://www.youtube.com/embed/SkgTxQm9DWM?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen=""></iframe></div>
<div class="home-story-quote-block">
<p>Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet<br><span>Lorem ipsum dolor sit amet.</span></p>
</div>
</div>
</div>