cant float two items inside of a grid element - css

I have a grid with a bunch of items. One of the items contains an image and some text. I want to float the text next to the image but can't get it to work...
See code below:
<div id="blog-post-item" class="blog-post">
<h1>Some amazing blogpost title about something super cool!</h1>
<div class="blog-post-content">
<div class="content-flex-container clearfix">
<img src="Data/Content/food_post5.jpg">
<p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione, repellendus natus! Corporis, architecto laboriosam natus doloremque corrupti sunt a incidunt! Eligendi soluta beatae neque reprehenderit ipsa perspiciatis architecto accusantium incidunt.</p>
</div>
</div>
</div>
body {
display: grid;
background: tomato;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: minmax(max-content, max-content);
column-gap: 20px;
row-gap: 20px;
}
.blog-post {
background: #fff;
border: 5px solid #fff;
border-radius: 10px;
display: grid;
grid-template-rows: minmax(auto, max-content) 1fr;
height: 100%;
grid-column: 2/-1;
width: 100%;
}
.blog-post h1 {
grid-row: 1;
grid-column: 1/-1;
}
.blog-post-content {
grid-row: 2;
}
.content-flex-container {
height: 100%;
width: 100%;
display: block;
}
.content-flex-container img {
height: 100%;
float: left;
}
.content-flex-container p {
float: left;
}
But this is the result i get:
as you can see the text is below the image not to the right of the image in the big white open space..
If more information is needed please let me know so i can clarify.

Related

flex column and wrap will let flex-item overflow

A simple layout that I want to achieve with minimal html tags
Only <img> & <h1> & <p> and no other extra tags
flex + column + wrap
The first column has only one image
The second column contains the title and crossword
The width and height of the parent layer are fixed
The result is that part of the text will overflow
Only add width to <p> to prevent
Is there any way to automatically break text without adding width?
HTML
*{
margin: 0;
padding: 0;
}
.out{
width: 600px;
height: 200px;
border: 1px solid #ccc;
padding: 20px;
margin: 50px auto;
font-family: Verdana;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
img{
/* margin-bottom: 20px; */
margin-right: 20px;
}
p{
line-height: 1.6;
overflow-wrap: break-word;
}
<div class="out">
<img src="https://picsum.photos/id/230/200/200" alt="">
<h1>This is Title</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta iure iusto cupiditate sequi aperiam, nostrum optio ipsam dicta modi officiis eligendi vel. Dignissimos delectus exercitationem nemo. Enim id sed corrupti!</p>
</div>
Another solution as per your expecation:
* {
margin: 0;
padding: 0;
}
.out {
width: 600px;
height: 200px;
border: 1px solid #ccc;
padding: 20px;
margin: 50px auto;
font-family: Verdana;
display: flex;
}
img {
/* margin-bottom: 20px; */
margin-right: 20px;
}
p {
line-height: 1.6;
overflow-wrap: break-word;
margin-left: -200px;
margin-top: auto;
margin-bottom: auto;
}
h1 {
position: relative;
white-space: nowrap;
}
p::before {
content: "";
width: 100%;
}
<div class="out">
<img src="https://picsum.photos/id/230/200/200" alt="">
<h1>This is Title</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta iure iusto cupiditate sequi aperiam, nostrum optio ipsam dicta modi officiis eligendi vel. Dignissimos delectus exercitationem nemo. Enim id sed corrupti!</p>
</div>
Here is my solution
* {
font-family: 'poppins';
}
.card {
display: flex;
padding: 15px;
border: 1px solid #8f8f8f;
}
.content {
margin-left: 10px;
}
.content h6 {
margin-top: 0;
font-size: 32px;
margin-bottom: 5px;
}
<div class="card">
<img src="//via.placeholder.com/150">
<div class="content">
<h6>This is title</h6>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content
here, content here', making it look like readable English.</p>
</div>
</div>

how to make a heading always stay horizontal inplace of overflow

I am working on a portfolio project and I am creating the projects section.
The width of the projects section is around 350px so only some of the title could be visible but if the text is not able to fit in 350 px it is making the text overflow vertically but I want it to overflow horizontally.
here is my HTML&CSS:
.project-container {
display: grid;
height: 350px;
width: 350px;
background: #c4c4c4;
margin-bottom: 3%;
}
.project-title{
height: 45px;
width: 100%;
background-color: red;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
overflow: auto;
}
<div class="project-container">
<h1 class="project-title">Lorem ipsum dolor sit amet consectetur adipisicing elit. Asperiores dignissimos praesentium dolorem saepe velit at libero a consectetur atque molestias.</h1>
</div>
and here is my result when the text can't fit in 350px:
but I want the overflow to be horizontal
white-space: nowrap;
I think you just want this?
.project-container {
display: grid;
height: 350px;
width: 350px;
background: #c4c4c4;
margin-bottom: 3%;
}
.project-title{
height: 45px;
width: 100%;
background-color: red;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
overflow: auto;
white-space: nowrap;
}
<div class="project-container">
<h1 class="project-title">Lorem ipsum dolor sit amet consectetur adipisicing elit. Asperiores dignissimos praesentium dolorem saepe velit at libero a consectetur atque molestias.</h1>
</div>
You've given your .project-title a width which automatically means the text inside will wrap at the end of the line. If you want it to overflow horizontally instead of vertically, you need to:
Allow space for it to overflow.
Prevent it from wrapping.
This can be achieved by changing .project-title[width] to .project-title[min-width] (meaning the space is at least the width of the container, but may be larger) and setting .project-title[white-space]=nowrap (meaning text is not allowed to break across lines).
.project-container {
display: grid;
height: 350px;
width: 350px;
background: #c4c4c4;
margin-bottom: 3%;
}
.project-title{
height: 45px;
min-width: 100%;
white-space: nowrap;
background-color: red;
margin: 0;
display: flex;
overflow: auto;
}
<div class="project-container">
<h1 class="project-title">Lorem ipsum dolor sit amet consectetur adipisicing elit. Asperiores dignissimos praesentium dolorem saepe velit at libero a consectetur atque molestias.</h1>
</div>
just add white-space: nowrap; to class project-title

Is there a way to create this layout using CSS?

I have two sections: a hero and an about section. There is a container that I am using but for the about section, the image on the left will go outside of the container line, while the content on the right will stay within the container. I have attached a photo of what I am trying to accomplish.
.container {
max-width: 1280px;
margin: 0 auto;
}
#about {
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.img img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.content {
background-color: #1A969F;
padding: 2rem 8rem 2rem 3rem;
}
<section id="about">
<div class="img">
<img src="about-img.jpg" alt="">
</div>
<div class="content">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente, vitae! Quam, ipsam quidem autem numquam ipsa atque debitis similique pariatur aliquid, quibusdam labore mollitia dolorum temporibus et magni, commodi blanditiis.</p>
</div>
</section>
from comments:
yes, with grid you are missing 2 columns. – G-Cyrillus
So we would use two extra grid columns for the about section? Would we still be able to use the container class for the other sections and navbar? - Webdev1995
i made you an example below of the idea of 2 extra columns (but on the parent ;) ) that #about can span through – G-Cyrillus
extra column shoul be used from .container where you did set the max-width inside the grid via minmax() instead max-width on .container.
here is an idea:
.container {
margin: 0 auto;
display: grid;
grid-template-columns: 1fr minmax(auto, 1280px) 1fr
}
section {
grid-column: 2;
background: #bee
}
#about {
grid-column: 1 /span 3;
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.img img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
background: #eee
}
.content {
background-color: #1A969F;
padding: 2rem 8rem 2rem 3rem;
}
<div class="container">
<section>whatever of 1280px max-width</section>
<section id="about">
<div class="img">
<img src="about-img.jpg" alt="about illustration">
</div>
<div class="content">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente, vitae! Quam, ipsam quidem autem numquam ipsa atque debitis similique pariatur aliquid, quibusdam labore mollitia dolorum temporibus et magni, commodi blanditiis.</p>
</div>
</section>
<section>whatever of 1280px max-width</section>
</div>

Background-Image behind Header Section with CSS Grid

i'm trying to recreate this template through the use of Grid. I can't figure how to create the background-image behind what i've already created
in my markup. Image what i'm after refrence image any tips?
check my website, what have i already done, i thought to use z-indexing, but it didn't worked.. also if i apply it to the body, it will stretch with the additional content after the header section.
*{
box-sizing: border-box;
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
font-family: 'Montserrat', sans-serif, arial;
}
a{
color: black;
}
body{
background-color: red;
margin: 0 auto;
width: 80%;
}
/* .background-image{
background-image: url(../img/bgheader.jpg);
background-repeat: no-repeat;
background-size: cover;
grid-column: 1 / -1;
grid-row: 1 / 4;
} */
#grid-container{
display: grid;
grid-template-columns: repeat(12,1fr);
grid-gap: 1rem;
}
header{
grid-column: 1 / 13;
display: grid;
grid-template-columns: 1fr 11fr;
grid-template-rows: 1fr 10fr 1fr 4fr;
margin-top: 5rem;
}
header ul{
display: flex;
justify-content: flex-end;
}
header ul li:nth-child(-n+3){
margin-right: 3rem;
}
header ul a{
text-transform: uppercase;
font-weight: 400;
color: white;
}
header a:hover{
color: grey;
}
.slogan{
grid-row: 2 / 3;
grid-column: 1 / 3;
justify-self: center;
align-self: center;
text-align: center;
}
.slogan h1{
font-size: 2.5rem;
color: white;
margin-bottom: 1rem;
text-transform: uppercase;
}
.more{
color: white;
}
.services{
color: #4a4747;
grid-row: 4 / 5;
grid-column: 1 / 3;
justify-self: center;
align-self: center;
text-align: center;
padding: 0 20rem;
}
.services h2{
margin-bottom: 1rem;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
<title>Brackets Junior</title>
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
</head>
<body>
<div id="grid-container">
<!-- Header Section -->
<header>
<img class="logo" src="img/logo.png" alt="logo">
<nav>
<ul>
<li>Home</li>
<li>Services</li>
<li>Portfolio</li>
<li>Blog</li>
</ul>
</nav>
<div class="slogan">
<h1>Always a step ahead go pro</h1>
Learn more..
</div>
<div class="services">
<h2> We provide the best services</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia voluptatum blanditiis expedita ducimus fugit rem, qui repellat, quos totam ipsa reprehenderit eum laboriosam maiores. Eum fugit provident error velit soluta, praesentium enim dolorum architecto, quis quisquam magnam! Sequi voluptas consequatur dolor asperiores rem eaque voluptatem necessitatibus laborum alias, omnis atque.</p>
</div>
</header>
</div>
</body>
</html>
Just apply your background to the header.
header{
grid-column: 1 / 13;
display: grid;
grid-template-columns: 1fr 11fr;
grid-template-rows: 1fr 10fr 1fr 4fr;
margin-top: 5rem;
background-image: url(../img/bgheader.jpg);
background-repeat: no-repeat;
background-size: cover;
}
I am not sure why your header includes your page contents, nor why you are placing the header grid inside the page grid. These things might be revisited.
write some text in header or li components and then apply min padding of 400px
or top and bottom padding of 200px. this will resolve your problem

How to make an element fill the remaining viewport height?

I'd like to use CSS Grid. Something like this I think…
html {
height: 100%;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: auto auto [whatever's left of the vh] auto auto;
position: relative;
}
Set the viewport with display: flex and height: 100vh and add to the last element
flex-grow: 1
.viewportDiv {
display: flex;
flex-direction: column;
height: 100vh;
}
.div1{
background-color: yellow;
height: 100px;
}
.remainingDiv{
background-color: red;
flex-grow: 1;
}
<div class="viewportDiv">
<div class="div1"></div>
<div class="remainingDiv"></div>
</div>
Using CSS Grid you need to wrap the top two elements and the remaining space and then apply display: grid to that.
In other words, your diagram actually was the solution.
The wrapper should have a height of 100vh…
html {
height: 100%;
}
body {
margin: 0;
min-height: 100%;
background: pink;
display: grid;
grid-template-rows: 100vh auto auto;
position: relative;
}
.wrapper {
display: grid;
grid-template-rows: auto auto 1fr;
}
header {
background: green;
padding: .25em;
}
nav {
background: orangered;
padding: .25em;
}
main {
background: rebeccapurple;
}
footer {
background: yellow;
}
.subfooter {
background: blue;
}
<div class="wrapper">
<header>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Ratione magnam placeat quia iusto, quisquam cum temporibus modi, ex dolorem velit fuga! Minima, ex.
</header>
<nav>
Lorem ipsum dolor, sit amet consectetur adipisicing elit.
</nav>
<main></main>
</div>
<footer>Lorem, ipsum.</footer>
<div class="subfooter">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Ex dignissimos ratione maxime officia eum. ea!
</div>
You can do it using flex.
.a {
width: 100%;
height: auto;
}
.remaining {
width: 100%;
flex-grow: 1;
}
.holder {
display: flex;
flex-direction: column;
height: 100%;
}
html,
body {
height: 100%;
}
HTML code:
<div class="holder">
<div class="a">
Content here
</div>
<div class="a">
Content here
</div>
<div class="remaining">
Content here
</div>
</div>

Resources