I am using a Grid layout, and I am trying to place an image next to a text inside of the same griditem. The image is like an icon and supposed to be right next to the text (as it's placed in the code below). The text next to it is also supposed to have a different color and font than the rest of the text. I previously tried this with a span. However, when I use any of those tags inside the griditem it kinda breaks the grid. Instead of being a normal wall of text, it's suddenly all next to each other. I've been trying to figure this out for a couple hours now, can anyone help?
.container {
display: grid;
grid-template: repeat(4, auto) 300px 150px / repeat(6, 1fr);
grid-gap: 3px;
grid-template-areas: ". . h h . ." ". . img img . ." "a a a a a a" ". t t t t ." ". p1 p2 p3 p4 ." ". sm sm hp hp .";
}
.text {
display: flex;
grid-area: t;
font-size: 1vw;
font-family: Verdana;
border: 1px solid #3f3f3f;
border-radius: 25px;
padding: 2%;
}
<div class="container">
<div class="text">Hi!<br><br> Just a quick note to say thank you for making an account with SuperBath.co.uk!
<br><br> Having a SuperBath.co.uk account will give you the following perks:<br>
<br>
<img src="..scratches/Drop.jpg">Saved personal details<br> Personal data is instantly available as you log in with your email address and password. <br><br>
</div>
</div>
The main problem here is that because you use display: flex on text, the img becomes a flex row item and the text before and after the image, will become anonymous flex items, hence they line up side-by-side.
There is mainly two ways to solve that, where one solution is to wrap the whole text/image chunk in a span, and then, to enable a different color on the text next to the img, also wrap it with a span.
Stack snippet
.container {
display: grid;
grid-template: repeat(4, auto) 300px 150px / repeat(6, 1fr);
grid-gap: 3px;
grid-template-areas: ". . h h . ." ". . img img . ." "a a a a a a" ". t t t t ." ". p1 p2 p3 p4 ." ". sm sm hp hp .";
}
.text {
display: flex;
grid-area: t;
font-size: 1vw;
font-family: Verdana;
border: 1px solid #3f3f3f;
border-radius: 25px;
padding: 2%;
}
.text span span {
color: blue /* added */
}
<div class="container">
<div class="text"><span>Hi!
<br>
<br> Just a quick note to say thank you for making an account with SuperBath.co.uk!
<br>
<br> Having a SuperBath.co.uk account will give you the following perks:
<br>
<br>
<img src="http://placehold.it/15/00f"><span>Saved personal details</span>
<br> Personal data is instantly available as you log in with your email address and password.
<br>
<br></span>
</div>
</div>
Or as suggested in a comment, simply remove display: flex from the text, and only wrap the text that should have different color with a span.
Stack snippet
.container {
display: grid;
grid-template: repeat(4, auto) 300px 150px / repeat(6, 1fr);
grid-gap: 3px;
grid-template-areas: ". . h h . ." ". . img img . ." "a a a a a a" ". t t t t ." ". p1 p2 p3 p4 ." ". sm sm hp hp .";
}
.text {
grid-area: t;
font-size: 1vw;
font-family: Verdana;
border: 1px solid #3f3f3f;
border-radius: 25px;
padding: 2%;
}
.text span {
color: blue /* added */
}
<div class="container">
<div class="text">Hi!
<br>
<br> Just a quick note to say thank you for making an account with SuperBath.co.uk!
<br>
<br> Having a SuperBath.co.uk account will give you the following perks:
<br>
<br>
<img src="http://placehold.it/15/00f"><span>Saved personal details</span>
<br> Personal data is instantly available as you log in with your email address and password.
<br>
<br>
</div>
</div>
Related
This question already has an answer here:
Why aren't my grid-template-areas with ASCII art not working?
(1 answer)
Closed 2 years ago.
I built a website,
.info {
width: 29.5%;
display: grid;
align-items: center;
grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
grid-template-areas: 'number title empty' 'empty para para' 'empty para para';
}
.round {
background-color: hsl(12, 88%, 59%);
width: 60px;
height: 35px;
padding: 5px 20px;
border-radius: 30px / 100%;
color: white;
grid-area: number;
}
.title {
grid-area: title;
}
.about {
background-color: aquamarine;
grid-area: para;
}
<div class='info'>
<div class='round'>01</div>
<h4 class='title'>Track company-wide progress</h4>
<p class='about'>See how your day-to-day tasks fit into the wider vision. Go from tracking progress at the milestone level all the way done to the smallest of details. Never lose sight of the bigger picture again.</p>
</div>
<div class='info'>
<div class='round'>02</div>
<h4 class='title'>Advanced built-in reports</h4>
<p class='about'>Set internal delivery estimates and track progress toward company goals. Our customisable dashboard helps you build out the reports you need to keep key stakeholders informed.</p>
</div>
<div class='info'>
<div class='round'>03</div>
<h4 class='title'>Everything you need in one place</h4>
<p class='about'>Stop jumping from one service to another to communicate, store files, track tasks and share documents. Manage offers an all-in-one team productivity solution.</p>
</div>
this gives
I am not able to understand why is my code giving an output like this. The element with the class 'round' and 'title' are not visible only element with the class 'about' is visible.
Use dots instead of the word empty. Example here.
.info {
/* width: 29.5%; */
display: grid;
align-items: center;
grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
grid-template-areas: "number title ......" "...... para para" "...... para para";
}
.round {
background-color: hsl(12, 88%, 59%);
width: 60px;
height: 35px;
padding: 5px 20px;
border-radius: 30px / 100%;
color: white;
grid-area: number;
}
.title {
grid-area: title;
}
.about {
background-color: aquamarine;
grid-area: para;
}
<div class="info">
<div class="round">01</div>
<h4 class="title">Track company-wide progress</h4>
<p class="about">See how your day-to-day tasks fit into the wider vision. Go from tracking progress at the milestone level all the way done to the smallest of details. Never lose sight of the bigger picture again.</p>
</div>
<div class="info">
<div class="round">02</div>
<h4 class="title">Advanced built-in reports</h4>
<p class="about">Set internal delivery estimates and track progress toward company goals. Our customisable dashboard helps you build out the reports you need to keep key stakeholders informed.</p>
</div>
<div class="info">
<div class="round">03</div>
<h4 class="title">Everything you need in one place</h4>
<p class="about">Stop jumping from one service to another to communicate, store files, track tasks and share documents. Manage offers an all-in-one team productivity solution.</p>
</div>
So I am having trouble setting up a CSS Grid inside another CSS grid which is working fine. The first (outer) CSS grid is defined with a grid template column and row in addition to a grid row and grid column called on every element.
The difficult part is that I want to re-use many of the elements I am using in the first sub-grid in future sub-grids but different layouts. That is why I decided to use the grid-area call. Here is the part of my code that is causing problems:
.wrapperFour {
height: 900vh;
background-color: tan;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(9, 100vh);
}
.wrapperFour>div {
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
}
.iPhone {
grid-area: iPhone;
height: 77.4074vh;
}
.featureLogo {
grid-area: featureIcon;
height: 116px;
}
.featureInfo {
color: #4F4F4F;
}
.feature_header {
grid-area: featureHeader;
font-family: montserrat_bold;
font-style: normal;
font-weight: normal;
font-size: 55px;
line-height: 66px;
}
.feature_infotext {
grid-area: featureInfotext;
font-family: montserrat_regular;
font-style: normal;
font-weight: 300;
font-size: 35px;
line-height: 42px;
margin-top: 40px;
}
.feature_cta {
grid-area: featureCTA;
height: 80px;
width: 400px;
background-color: #2F60A2;
color: #FFFFFF;
}
.feature_cta_text {
font-family: montserrat_regular;
font-style: normal;
font-weight: normal;
font-size: 35px;
line-height: 42px;
}
.feature_left {
display: grid;
grid-template-columns: 13.85vw 21vw 7.81vw 43.49vw 13.85vw;
grid-template-rows: 11.2vh 11.38vh 9.54vh 27.5252vh 16.66vh 12.3148vh 11.38vh;
grid-template-areas: ". . . . ." ". iPhone . featureIcon ." ". iPhone . featureHeader ." ". iPhone . featureInfotext ." ". iPhone . featureCTA ." ". iPhone . . ." ". . . . .";
background-color: white;
}
.featureOne {
background-color: rgb(31, 141, 155);
grid-column: 1;
grid-row: 1;
}
.featureTwo {
background-color: rgb(126, 31, 155);
grid-column: 1;
grid-row: 2;
}
.featureThree {
background-color: rgb(103, 119, 148);
grid-column: 1;
grid-row: 3;
}
<div class="wrapperFour">
<div class="featureOne">
<div class="feature_left">
<div class="iPhone">
<img src="Assets/iPhone Mockup 1.png" alt="Mockup1" class="iPhone">
</div>
<div class="featureInfo">
<div class="featureLogo">
<img src="Assets/Feature1Icon.png" alt="Feature 1 Icon" class="featureLogo">
</div>
<div class="feature_header">
Lorem, ipsum dolor.
</div>
<div class="feature_infotext">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Optio eum fugit error voluptatem tempora! Soluta sit ipsa possimus quidem totam.
</div>
<div class="feature_cta">
<div class="feature_cta_text">
CTA Button
</div>
</div>
</div>
</div>
</div>
<div class="featureTwo">
</div>
<div class="featureThree">
</div>
</div>
The issue is that only the iPhone image is going in the right grid area. The rest is just sticking to the left, so the first column. What am I doing wrong?
Could you describe what your goal is? Maybe make a screenshot of what you want it to look like?
Also, I've noticed you're using huge dimensions in your classes.
For example:
.wrapperFour {
height: 900vh;
background-color: tan;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(9,100vh) ;
}
you set the wrapper height to be 900vh.
This means the height is 9 times your screen's height (vh = view height)
Did you mean to set it this way?
another hint that might help you:
If you go to dev tools in chrome, and place the marker on your grid, you can see the layout that's rendered to the screen. Then you can try and play with the css properties and watch how your elements are aligned.
This is what it looks like with your snippet
I have the grid area template with correct formating, but for some reason, when I add an element and assign it's grid-area, it goes into the next available space in the column. eg: supposed to be middle column bottom row, but instead showed up on the middle column a space away from the privious element. (example shown in code) Also, the spacing is not working eg: 3fr is the same size as a 1fr. (This is in code pen)
fixed this problem in comments
New problem: the numbers are surrounding everything, why?
body {
background-color: rgb(90, 250, 190);
text-align: center;
font-family: 'Alegreya', serif;
min-width: 320px;
}
.container {
min-height: 500px;
width: 98%;
margin: 10px;
display: grid;
background: rgb(255, 255, 255);
grid-template-columns: 30px 3fr 2fr;
grid-template-rows: 0.5fr 1fr 1fr 1fr 3fr 3fr 1fr 3fr 2fr;
grid-template-areas: "description description description" "1 name nameinput"
"2 email emailinput"
"3 age ageinput"
"4 redblueoryellow redblueoryellowinput"
"5 mixedwith mixedwithinput"
"6 lighterordarkerdropdown lighterordarkerdropdowninput"
"7 radiobuttons radiobuttonsinput"
"8 questionsorcomments questionsorcommentsinput"
}
.description {
grid-area: description;
font-size: 30px;
}
.\31 {
grid-area: \31;
}
.\32 {
grid-area: \32;
}
.\33 {
grid-area: \33;
}
.\34 {
grid-area: \34;
}
.\35 {
grid-area: \35;
}
.\36 {
grid-area: \36;
}
.\37 {
grid-area: \37;
}
.\38 {
grid-area: \38;
}
.name {
grid-area: name;
}
.email {
grid-area: email;
}
.questionsorcomments {
grid-area: questionsorcomments;
}
<div class="container">
<p id="description" class="description">
What is your favorite color?
</p>
<p class="1">1.</p>
<p class="2">2.</p>
<p class="3">3.</p>
<p class="4">4.</p>
<p class="5">5.</p>
<p class="6">6.</p>
<p class="7">7.</p>
<p class="8">8.</p>
<p class="name">What is your name?</p>
<p class="email">What is your email?</p>
<p class="questionsorcomments"> Any questions or comments?</p>
</div>
The numbers are supposed to be running down the side. If you want a link to the full codepen then here it is: https://codepen.io/Homburg_Molly/pen/arbzeN
I have the parent element set to newTableForm with labelColumn and mainFormColumn as children - but with the current CSS everything collapses into one row:
.newTableForm {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-rows: 30px 30px 30px 40px;
grid-gap: 10px;
grid-template-areas: "... main" "labels main" "labels main" "... main";
border: 2px dashed deeppink;
}
.labelColumn {
grid-area: labels;
}
.mainFormColumn {
grid-area: main;
}
<form method='post' action="/admin-post.php" class="newTableForm">
<h4 class="mainFormColumn">
Add a new price compare table
</h4>
<label for="store" class="labelColumn">Store</label>
<input type="text" name="store" placeholder="" class="mainFormColumn"/>
<label for="product-page" class="labelColumn">Product Page Link</label>
<input type="text" name="product-page" placeholder="Copy address here" class="mainFormColumn" />
<button class="mainFormColumn">Save new price compare table</button>
</form>
Is there a way to undo the single column stacking behavior here? I would remove the template areas altogether if not for the blank sections with ... (above and below the label section)
Again you can drop grid-template-areas as multiple grid-areas overlap - and you can use pseudo elements for this:
place labelColumn into the first column using grid-column: 1 and mainFormColumn into the second column using grid-column: 2.
after element will be the first column in the last row using grid-row: -2 and grid-column: 1
before will be first column in the first row using grid-column: 1
See demo below:
.newTableForm {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-rows: 30px 30px 30px 40px;
grid-gap: 10px;
border: 2px dashed deeppink;
}
.labelColumn {
grid-column: 1;
}
.mainFormColumn {
grid-column: 2;
}
.newTableForm:after, .newTableForm:before {
content: '';
display: block;
grid-column: 1;
}
.newTableForm:after {
grid-row: -2;
}
<form method='post' action="/admin-post.php" class="newTableForm">
<h4 class="mainFormColumn">
Add a new price compare table
</h4>
<label for="store" class="labelColumn">Store</label>
<input type="text" name="store" placeholder="" class="mainFormColumn"/>
<label for="product-page" class="labelColumn">Product Page Link</label>
<input type="text" name="product-page" placeholder="Copy address here" class="mainFormColumn" />
<button class="mainFormColumn">Save new price compare table</button>
</form>
I just update your .newTableForm css with some updates. I hope it'll help you out. Thanks
.newTableForm {
display: flex;
flex-direction: column;
border: 2px dashed deeppink;
}
This question already has answers here:
How do I remove the first empty column in a CSS grid?
(3 answers)
Closed 4 years ago.
Problem / Misunderstanding:
I've created an input type="radio" for radio choices. The radio choices should align to the left at the beginning of column 3, but instead they are in the middle, between column 3 and 4.
Expected behavior:
Radio input options should be at the beginning of column 3, under aligning with the text input bar of the Name label.
Minimal, complete and verifiable code below.
MCV.html code:
<link rel="stylesheet" href="MCP.css">
<div class="grid-container">
<form id="survey-form">
<label for="name" id="name-label">Name:</label>
<input type="text" id="name">
<div class="radio-title">
<p>Gender:</p>
</div>
<div class="radio-options">
<ul>
<li>
<input type="radio" id="choice-1">
<label for="choice-1">Option A</label>
</li>
<li>
<input type="radio" id="choice-1">
<label for="choice-1">Option B</label>
</li>
</ul>
</div>
</form>
</div>
MCV.css code:
.grid-container {
display: grid;
grid-template-areas:
". . . ."
". c c ."
". . . .";
grid-template-columns: 0.7fr 1.5fr 1.5fr 0.7fr;
grid-template-rows: 0.7fr 1.5fr 0.7fr;
}
#survey-form {
display: grid;
grid-area: c;
grid-template-columns: repeat(4, 1fr);
grid-gap: 5px;
}
label {
grid-column: 2 / 3;
text-align: right;
}
input {
text-align: left;
}
ul {
list-style: none;
}
.radio-title {
grid-column: 2;
text-align: right;
}
.radio-options {
grid-column: 3 / 4;
text-align: left;
}
Here's an image with line numbers and form grid:
All feedback is appreciated, thank you!
NOTE: This issue was already answered in How do I remove the first empty column in a css grid?. Take a look at second answer.
I believe this is more of a List issue. Most browsers attach some sort of padding / margin to an element so most people clear these out before starting on a new page.
Going to your codepen use:
//css
.gender ul {
padding: 0;
}
and you'll see the buttons move over to your desired location.