Trying to left-align text in flex containers with a flex rule - html

I have a section that has a list of jokes where each joke is individually contained within divs. In each joke are two paragraphs that I'm trying to align to display at the left (start of the flex container). Right now the two main issues I'm facing is that the first paragraph (Q:) is not aligning to the very start of the container
I'm trying to align the paragraphs so that both the abbreviation and question wrap to leftmost boundary of the container's content.
The second issue I'm facing is that the second paragraph (A:) keeps centering itself in the container instead of displaying on the left side. I am not sure why the first paragraph does not center itself but the second paragraph does when displaying the webpage. I've been told that this can be achieved with a single flex rule which I've been trying to find in my div p ruleset.
Overall, I am trying to achieve this expected display:
HTML
<section id="jokes">
<h2>Out Of This World Joke Inventory!</h2>
<p>Hover over each joke to see the answer!</p>
<div id="joke-cards">
<div id="sun-joke">
<img src="img/icon1.png" alt="Icon of shooting star">
<hr>
<p><span class="abbrv">Q:</span> Why did the sun go to school?</p>
<p><span class="abbrv">A:</span> <span class="answer">To get brighter!</span></p>
</div>
<div id="tick-joke">
<img src="img/icon2.png" alt="Icon of rocket blasting off">
<hr>
<p><span class="abbrv">Q:</span> What do you call a tick on the moon?</p>
<p><span class="abbrv">A:</span> <span class="answer">A luna-tick</span></p>
</div>
<div id="restaurant-joke">
<img src="img/icon3.png" alt="Icon of flag on the Moon">
<hr>
<p><span class="abbrv">Q:</span> Why did the people not like the restaurant on the moon?</p>
<p><span class="abbrv">A:</span> <span class="answer">Because there was no atmosphere.</span></p>
</div>
</div>
</section>
CSS
#joke-cards {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-evenly;
}
#joke-cards div {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 15px;
margin-left: 15px;
margin-right: 15px;
margin-bottom: 15px;
height: 400px;
width: 300px;
background-color: #9B580D;
opacity: 80%;
padding: 20px;
}
#joke-cards div img {
height: 150px;
width: 150px;
}
hr {
width: 65%;
}
div p {
align-content: flex-start;
}
.abbrv {
font-size: 28px;
color: #E0DBD7;
}
.answer {
display: none;
font-size: 24px;
color: #191919;
}
#joke-cards div:hover .answer {
display: inline;
}

Use align-items: flex-start; for the divs, width + margin for img and text-align: left; for p
img{ width: 50%; outline: 1px solid blue; margin: 0 auto; }
#joke-cards {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-evenly;
}
#joke-cards div {
display: flex;
flex-direction: column;
align-items: flex-start;
margin-top: 15px;
margin-left: 15px;
margin-right: 15px;
margin-bottom: 15px;
height: 400px;
width: 300px;
background-color: #9B580D;
opacity: 80%;
padding: 20px;
}
#joke-cards div img {
height: 150px;
width: 150px;
}
hr {
width: 65%;
}
div p {
text-align: left;
}
.abbrv {
font-size: 28px;
color: #E0DBD7;
}
.answer {
display: none;
font-size: 24px;
color: #191919;
}
#joke-cards div:hover .answer {
display: inline;
}
<section id="jokes">
<h2>Out Of This World Joke Inventory!</h2>
<p>Hover over each joke to see the answer!</p>
<div id="joke-cards">
<div id="sun-joke">
<img src="img/icon1.png" alt="Icon of shooting star">
<hr>
<p><span class="abbrv">Q:</span> Why did the sun go to school?</p>
<p><span class="abbrv">A:</span> <span class="answer">To get brighter!</span></p>
</div>
<div id="tick-joke">
<img src="img/icon2.png" alt="Icon of rocket blasting off">
<hr>
<p><span class="abbrv">Q:</span> What do you call a tick on the moon?</p>
<p><span class="abbrv">A:</span> <span class="answer">A luna-tick</span></p>
</div>
<div id="restaurant-joke">
<img src="img/icon3.png" alt="Icon of flag on the Moon">
<hr>
<p><span class="abbrv">Q:</span> Why did the people not like the restaurant on the moon?</p>
<p><span class="abbrv">A:</span> <span class="answer">Because there was no atmosphere.</span></p>
</div>
</div>
</section>

You have to wrap your paragraph tags into a container which will have place-items attribute and not each paragraph. Also, you need change display value from block to inherit for each content paragraph.
<div class="content">
<p><span class="abbrv">Q:</span> Why did the sun go to school?</p>
<p><span class="abbrv">A:</span> <span class="answer">To get brighter!</span></p>
</div>
div.content{
display: flex;
flex-direction: column;
width: 100%;
place-self: flex-start;
}
div.content p {
display:inherit;
}

Related

Align text to flex box without containing it within the item

I am trying to achieve this design
I used flex box to achieve this but facing some problems to align the contents the way the design demanding
I used self-align property of flex box to align the three items to achieve the steps like design.
My code:
#main {
background: #fff2fd;
padding: 120px 0;
}
#main h2 {
font-family: 'Circular-Std-Bold';
text-transform: capitalize;
font-size: 50px;
max-width: 700px;
}
#main div.contents {
display: flex;
align-items: flex-start;
justify-content: space-between;
height: 100vh;
}
#main div.contents div.item {
width: 32%;
border-bottom: 2px solid #c8c8c8;
margin-right: 50px;
}
#main div.contents div.item:last-child {
margin-right: 0;
}
#main div.contents div.item div.img-container {
width: 300px;
}
#main div.contents div.item div.img-container img {
display: block;
width: 100%;
}
#main div.contents div.item h3 {
font-family: 'Circular-Std-Bold';
font-size: 25px;
margin-top: 40px;
text-transform: capitalize;
}
#main div.contents div.item p {
font-size: 21px;
margin-top: 20px;
text-transform: capitalize;
}
#main div.contents div.item:first-child {
align-self: flex-end;
}
#main div.contents div.item:nth-child(2) {
align-self: center;
}
#main div.contents div.item:last-child {
align-self: flex-start;
}
<body>
<section id="main">
<section class="wrapper">
<h2>how patch works for everyone involved?</h2>
<div class="contents">
<div class="item bottom">
<div class="img-container"><img src="https://i.ibb.co/26bwqT5/image1.png" alt="image" /></div>
<h3>patch can you a new deposit or unlock an existing one</h3>
<p>giving tenants choice to spend their money on whatever they like</p>
</div>
<div class="item middle">
<div class="img-container"><img src="https://i.ibb.co/C9DNWWh/image2.png" alt="image" /></div>
<h3>patch can you a new deposit or unlock an existing one</h3>
<p>giving tenants choice to spend their money on whatever they like</p>
</div>
<div class="item top">
<div class="img-container"><img src="https://i.ibb.co/ZT6f77F/image3.png" alt="image" /></div>
<h3>patch can you a new deposit or unlock an existing one</h3>
<p>giving tenants choice to spend their money on whatever they like</p>
</div>
</div>
</section>
</section>
</body>
I'm having trouble to aligning text with the third item where the text says "the how patch works for everyone involved?" Are there other ways to achieve this?
If you're referring to the image with the text (it seems that's the main difference) you simply need to change your selector:
#main div.contents div.item div.img-container {
width: 100%;
justify-content: center;
display: flex;
}
Then you'll have to fix the size of each block, giving it a fixed width or working with padding.
I hope this was what you meant.

my texts keep breaking down into new paragraphs in css and html

HTML CODE
<div class="wrapper">
<div class="flex-container">
<div class="box one">
<img src="img\xii.jpg" alt="phone1" />
<section class="section1">
<p>Xiaomi X15</p>
<br />
<h2>New Powerhouse Phone From The Xiaomi Brand</h2>
<br />
<button> BUY NOW</button>
</section>
</div>
</div>
</div>
CSS CODE
.flex-container {
display: flex;
background-color: #fff;
border: 2px solid black;
flex-wrap: wrap;
}
.wrapper {
width: 100px;
max-width: 960px;
margin: 0 auto;
}
.one p {
position: absolute;
bottom: -350px;
color: white;
font-size: 50px;
left: 500px;
text-align: center;
font-weight: lighter;
font-family: calibri;
}
.section1 h2 {
position: absolute;
bottom: -500px;
left: 300px;
color: white;
}
please help im a beginner in css and this is my first stackoverflow post
my texts are not horizantally in place and keeps breaking down that each word forms a new paragraph how can i solve this please? i have tried the display:inline- block but it didnt work.as you can see from my code i made the div tag that the image is on to be position relative so i can move the h2 and span and button elements to be nn the image. i intend on using flexbox because on i want to position more images to be on the side of the initial image
What you can do is:
Define some width and height (max-width and min-width to be more flexible) for your box;
Use background-image CSS prop instead of HTML <img />, which will ensure the image fills the background of your container;
Use flex-box with its all properties to lay out your elements. There will be no need for position: absolute for the text and header.
Other notes to your code: there are some closing tags missing in your HTML as well as closing } braces in the CSS. That is never a good practice to leave your code without them, even though in some cases the browser can fill them out for you.
Here is an example of how you can achieve what you were asking about:
.wrapper {
width: 100%;
max-width: 960px;
margin: 0 auto ;
display: flex;
justify-content: space-between;
}
.flex-container{
display: flex;
background-color:#fff;
border: 2px solid black;
flex-wrap: wrap;
width: auto;
height: 200px;
min-width: 100px;
max-width: 400px;
}
.one {
background-image: url('https://cdn.thewirecutter.com/wp-content/uploads/2018/05/smartphone-tp-top-2x1-lowres1024-7951.jpg');
background-size: cover;
}
.section1 {
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.section1 p{
color: white;
font-size: 25px;
text-align: left;
font-weight: lighter;
font-family: calibri;
padding: 10px;
margin: 0
}
.section1 h2{
color: white;
padding: 10px;
margin: 0
}
button {
max-width: 100px
}
<div class="wrapper">
<div class="flex-container">
<div class="box one">
<section class="section1">
<p>Xiaomi X15 </p>
<h2>New Powerhouse Phone From The Xiaomi Brand</h2>
<button> BUY NOW</button>
</section>
</div>
</div>
<div class="flex-container">
<div class="box one">
<section class="section1">
<p>Xiaomi X15 </p>
<h2>New Powerhouse Phone From The Xiaomi Brand</h2>
<button> BUY NOW</button>
</section>
</div>
</div>
</div>

position text on image correctly {CSS HTML}

[How to position text on the image correctly? i got three blocks of text which i want to position them on the same line on the image but i struggle with it :(
#banner {
justify-content: center;
height: 600px;
margin-top: 100px;
margin-left: 10%; }
.banner-text {
color: white;
justify-content: center;
justify-content: space-around;
text-align: center;
display: inline-block;
position: absolute;
flex-direction: column; }
/*DownTown*/
.flex-text {
background-color: grey;
text-align: center;
}
html
<div id="banner"><img src="2525.jpeg">
<div class="banner-text">
<div class="flex-text text1">
<h1><b>DownTown</b> 384 West 4th St Suite 108</h1>
<div class="flex-text text2">
<h1><b>East Bayside</b> 3433 Phisherman Avenue </h1>
<div class="flex-text text3">
<h1><b>Oakdale</b> 515 Crecent avenue Second Floor </h1>
</div>
</div>
</div>
</div>
</div>
here is how it should look
]2
and that's how i did it -_-
Your html markup is not correct and instead of adding image as an img element in the #banner, add the image as a background image using css. That way, you don't need position absolute to place text over the image. After that, use flexbox for aligning the elements.
#banner {
background-image: url(https://picsum.photos/500);
background-repeat: no-repeat;
background-size: cover;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.banner-text {
color: white;
justify-content: space-around;
display: flex;
width: 100%;
flex-wrap: wrap;
}
.flex-text {
background-color: #222;
display: flex;
align-items: center;
flex-direction: column;
justify-content: space-between;
height: 180px;
padding: 20px;
width: 200px;
margin: 5px;
}
h1 {
margin: 0;
}
<div id="banner">
<div class="banner-text">
<div class="flex-text text1">
<h1>DownTown</h1>
<span>384 West 4th St</span>
<span>Suite 108</span>
<span>Portland, Maine</span>
</div>
<div class="flex-text text1">
<h1>DownTown</h1>
<span>384 West 4th St</span>
<span>Suite 108</span>
<span>Portland, Maine</span>
</div>
<div class="flex-text text1">
<h1>DownTown</h1>
<span>384 West 4th St</span>
<span>Suite 108</span>
<span>Portland, Maine</span>
</div>
</div>
</div>
Your markup is not right. So I changed it a bit. And I also changed the css accordingly and use flex for aliigning items. And instead of using img in html I use background property.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
max-width: 1200px;
height: 100vh;
margin: auto;
overflow: hidden;
padding: 1rem;
background: #333;
}
#banner {
margin-top: 100px;
}
.banner-text {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
color: white;
}
/*DownTown*/
.flex-text {
background-color: rgba(0, 0, 0, 0.384);
line-height: 4rem;
padding: 4rem;
margin: 0rem 2rem;
text-align: center;
}
<div class="container">
<div id="banner">
<!-- <img src="2525.jpeg" /> -->
<div class="banner-text">
<div class="flex-text text1">
<h1>DownTown</h1>
<p>384 West 4th St</p>
<p>Suite 108</p>
</div>
<div class="flex-text text2">
<h1>East Bayside</h1>
<p>3433 Phisherman Avenue</p>
<p>(Norway Corner)</p>
</div>
<div class="flex-text text3">
<h1>Oakdale</h1>
<p>
515 Crecent avenue
</p>
<p>
Second Floor
</p>
</div>
</div>
</div>
</div>
The issue you have is the root diff i.e #banner has two child. The properties on #banner doesn't indicate in any way that the two items should overlap. You are trying to achieve that through position:absolute. Which is also an approach. But here's how you can achieve what you want in two ways:
Put that image as the background property for #banner and provide the flexbox properties to banner.
body {
margin: 0;
max-width: 100%;
}
#banner {
position: relative;
height: 600px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#banner img {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: 1;
}
.banner-text {
position: relative;
width: 100%;
z-index: 2;
display: flex;
justify-content: space-around;
}
.flex-text {
background: #efefef;
}
h1 {
font-size: 17px;
}
<div id="banner">
<img src="https://cdn.slashgear.com/wp-content/uploads/2019/07/coffee_main_envat-1280x720.jpg">
<div class="banner-text">
<div class="flex-text text1">
<h1><b>DownTown</b><br/> 384 West 4th St Suite 108</h1>
</div>
<div class="flex-text text2">
<h1><b>East Bayside</b><br/> 3433 Phisherman Avenue </h1>
</div>
<div class="flex-text text3">
<h1><b>Oakdale</b><br/> 515 Crecent avenue Second Floor </h1>
</div>
</div>
</div>
Position your banner div relative. Then position your image as absolute so that it occupies whole width and banner-text should have relative positioning. One other thing you need to do if the text doesn't come to center is provide 100% width to the banner-text.
Hope this solves the problem

how to align avatar circle and text content css

I want to align text content and the avatar circle. Here are the classes that I have defined
.user-detail {
display: inline-block;
text-transform: uppercase;
text-align: right;
.user-detail__username {
margin: 18px 16px 0px 10px;
display: block;
float: left;
font-size: $font-size;
font-weight: 500;
}
.user-detail__role {
margin: 18px 16px 0px 10px;
display: block;
font-size: $font-size * 0.9;
font-style: normal;
line-height: 13px;
font-weight: 300;
}
.user-detail__avatar-circle {
display: inline-block;
margin: 8px 11px 0px 0px;
width: 45px;
height: 45px;
border-radius: 50%;
border: 1px solid $lightstroke;
}
.user-detail__avatar {
position: relative;
font-size: 51px;
line-height: 43px;
left:-4px;
}
}
and here is the HTML markup
<div class="user-detail right">
<div style="display:inline-block">
<span class="user-detail__username">
Adminstrator
</span>
<span class="user-detail__role">Adminstrator</span>
</div>
<div class="user-detail__avatar-circle">
<i class="material-icons user-detail__avatar">account_circle</i>
</div>
</div>
it shows like this here
I want to text and avatar circle should be bottom aligned. If I inspect element there is space on the top of text div. If I could remove this space problem will be solved. But I don't know how could I do that? Even changing the margin of text div not working? Any help?
As usual, what is a pain to achieve with traditional CSS (float, margin, inline-block, etc.) is a breeze with Flexbox.
.user-detail {
width: 300px;
height: 100px;
border: blue solid 2px;
display: flex;
align-items: center;
justify-content: center;
}
.user-detail>div {
border: green solid 2px;
}
.user-detail .details {
display: flex;
flex-direction: column;
}
<div class="user-detail right">
<div class="details">
<span class="user-detail__username">
Adminstrator
</span>
<span class="user-detail__role">Adminstrator</span>
</div>
<div class="user-detail__avatar-circle">
<i class="material-icons user-detail__avatar">account_circle</i>
</div>
</div>
You should use Flexbox is less pain.
Try making your
.user-detail {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
More about flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
The vertical-align: top setting normally align and positions your div element to top , in your case the "Administrator" text.
You need to change your html as (check the second <div> tag in the below code for the change),
<div class="user-detail right">
<div style="display:inline-block;vertical-align: top">
<span class="user-detail__username">
Adminstrator
</span>
<span class="user-detail__role">Adminstrator</span>
</div>
<div class="user-detail__avatar-circle">
<i class="material-icons user-detail__avatar">account_circle</i>
</div>
</div>
Hope this helps!

Make flex child to grow according to sibling's height

I have two article summaries contained in the same parent, and I need one of the child elements (the one with the background image) to stretch if the other article has longer content.
I figured if I walked down from the parent ensuring that everything is set to display flex, and then on my child set the flex grow property everything would work out, but instead of growing the content's height to match it's sibling, it pushed out the width of the area (unacceptable).
Please take a look at my fiddle (looks like its only "working" in chrome as of now) and let me know if you can be of any help.
Fiddle
article {
float: left;
width: 50%;
padding-right: 0.75em;
display: flex;
flex-direction: column;
}
.article-type-wrapper {
display: block;
background-color: #999999;
color: white;
padding-top: 15px;
padding-bottom: 15px;
height: 60px;
}
header {
display: flex;
padding: 0 1.5em 20px 1.5em;
background: url(//i.istockimg.com/file_thumbview_approve/83599047/6/stock-photo-83599047-sun-orange-yellow-and-rays-background.jpg);
background-size: cover;
}
h3 {
flex: 1 0 auto;
margin: 0;
padding-top: 60px;
padding-bottom: 60px;
padding-left: 25px;
font-weight: lighter;
color: black;
}
* {
box-sizing: border-box;
}
.flex-wrapper {
display: flex;
flex-direction: row;
}
<div class="flex-wrapper">
<article>
<div class="article-type-wrapper">
<div class="article-types">
<span class="article-type">Image Gallery</span>
</div>
</div>
<a href="#">
<header class="article-header">
<h3>Article title in background area, longer title that wraps and makes this one longer </h3>
</header>
</a>
</article>
<article>
<div class="article-type-wrapper">
<div class="article-types">
<span class="article-type">Image Gallery</span>
</div>
</div>
<a href="#">
<header class="article-header">
<h3>Short title</h3>
</header>
</a>
</article>
</div>
P.S. JavaScript IS NOT an acceptable solution to this question.
You can try using nested flexbox layout. See the demo and comments below.
jsFiddle
article {
/* float: left; */
width: 50%;
padding-right: 0.75em;
display: flex;
flex-direction: column;
}
article > a {
flex: 1; /* added */
display: flex; /* added */
}
.article-type-wrapper {
display: block;
background-color: #999999;
color: white;
padding-top: 15px;
padding-bottom: 15px;
height: 60px;
}
header {
/* display: flex; */
padding: 0 1.5em 20px 1.5em;
background: url(//i.istockimg.com/file_thumbview_approve/83599047/6/stock-photo-83599047-sun-orange-yellow-and-rays-background.jpg);
background-size: cover;
width: 100%; /* added */
box-sizing: border-box; /* added */
}
h3 {
flex: 1 0 auto;
margin: 0;
padding-top: 60px;
padding-bottom: 60px;
padding-left: 25px;
font-weight: lighter;
color: black;
}
* {
box-sizing: border-box;
}
.flex-wrapper {
display: flex;
flex-direction: row;
}
<div class="flex-wrapper">
<article>
<div class="article-type-wrapper">
<div class="article-types">
<span class="article-type">Image Gallery</span>
</div>
</div>
<a href="#">
<header class="article-header">
<h3>Article title in background area, longer title that wraps and makes this one longer </h3>
</header>
</a>
</article>
<article>
<div class="article-type-wrapper">
<div class="article-types">
<span class="article-type">Image Gallery</span>
</div>
</div>
<a href="#">
<header class="article-header">
<h3>Short title</h3>
</header>
</a>
</article>
</div>