So I create a container div, then I create a top and bottom div, in the bottom I create 2 more divs, to put separeted items, but, in the middle of them it's a border that a didn't put there, I tried the same code only with divs (whitout text) to see, and it didn't has the border in the middle of them, so, anyone knows why?
Here's my code:
<div class="container">
<div class="top-text">
<h1>Join our community</h1>
<h3>30-day, hassle-free money back guarantee</h3>
<p>Gain access to our full library of tutorials along with expert code reviews. Perfect for any developers who are serious about honing their skills.</p>
</div>
<div class="bottom-text">
<div class="month-sub">
<h3 class="month-sub-title">Monthly Subscription</h3>
<p class="p-dollar"><span class="dollar">$29</span> per month</p>
<p>Full access for less than $1 a day</p>
<button class="btn-sign">Sign Up</button>
</div>
<div class="why-us">
<h3 class="why-us-title">Why Us</h3>
<ul class="specs">
<li>Tutorials by industry experts</li>
<li>Peer & expert code review</li>
<li>Coding exercises</li>
<li>Access to our GitHub repos</li>
<li>Community forum</li>
<li>Flashcard decks</li>
<li>New videos every week</li>
</ul>
</div>
</div>
</div>
.container{
width: 44%;
display: flex;
flex-direction: column;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
overflow: hidden;
border-radius: 10px;
}
.top-text{
background-color: #ffffff;
padding: 35px;
color: var(--cyan);
}
.bottom-text{
display: flex;
justify-content: center;
width: 100%;
color: #ffffff;
}
.month-sub{
width: 50%;
padding: 35px;
background-color: var(--cyan);
}
.why-us{
width: 50%;
padding: 35px;
background-color: var(--grayishBlue);
}
The border
The div I create with the same things, but didn't have the border
I guess it can be due to wrong use of flexbox. If you use display: flex; you shoul use flex: 0 1 50%; in child to benefit from it.
.parent{
display: flex;
}
.child{
//this
flex: 0 1 50$;
//is equivalent of this
flex-grow: 0
flex-shrink: 1;
flex-basis: 50%;
}
My second guess it's box-sizing or some styles are being inherited...
Anyway maybe it'll help you
https://codepen.io/jerrykck/pen/eYZbpGN
Related
I'm working on a project where I have to create an online bookstore webpage using only HTML and CSS . I want to display "about us" information for the shop using a style like in the image below :
Basically I want to put an image and a text in the way displayed above . Using flexbox however I have trouble syncing the image and the text I have because I have difficulties setting the image width to cover half the flex container and the part of my text the other half .
This is my code :
.flex-container {
display: flex;
justify-content: flex-start;
flex-direction: row;
background-color: orange;
opacity: 0.7;
}
.flex-container>div {
margin: 0;
width: 100%;
height: auto;
}
#fleximg>img {
width: 60%;
height: auto;
}
#about {
width: 50%;
margin-right: 300px;
height: auto;
}
<div class="flex-container">
<div id="fleximg"><img src="https://placehold.it/400x400" alt=d esk/></div>
<div id="about">
<!-- text i want to place next to image -->
<h1> ABOUT US </h1>
<p>
In these rough times we experience right now being forced to stay at home staring at nothing does actually nothing.This is why we have created this online book delivery website to provide the company of a book to those who can't go outside to buy or read
a book at a local bookshop .
</p>
</div>
</div>
When i run my page I get the picture below where the image and the text are not next to each other and I have trouble readjusting their sizes
I would appreciate your help with guiding me to solve my problem
First, if the image height is taller than the right-col content - even simple width: 100%; height: auto for the image could work.
div{
border: 2px solid black;
}
#fleximg img{
width: 100%;
height: auto;
vertical-align: middle;
}
.flex-container {
display: flex;
background-color: orange;
}
#fleximg{
flex-basis: 40%;
}
#about {
flex-basis: 60%;
display: flex;
align-items: center; /* align v */
}
<div class="flex-container">
<div id="fleximg" class="col">
tall image
<img src="https://placehold.it/400x700" alt=d esk/>
</div>
<div id="about" class="col">
<!-- text i want to place next to image -->
<div>
<h1> ABOUT US </h1>
</div>
</div>
</div>
But the idea above it less useful for dynamic sites.
More responsive approaches:
By background image
For the "left-col" Use background-image and background-size: cover.
div{
border: 2px solid black;a
}
.flex-container {
display: flex;
background-color: orange;
}
#fleximg{
flex-basis: 40%;
background-image: url("https://picsum.photos/1111/1300");
background-repeat: no-repeat;
background-size: cover
}
#about {
flex-basis: 60%;
padding: 100px 10px;
display: flex;
align-items: center; /* align v */
}
<div class="flex-container">
<div id="fleximg" class="col">
</div>
<div id="about" class="col">
<!-- text i want to place next to image -->
<div>
<h1> ABOUT US </h1>
<p>
In these rough times we experience right now being forced to stay at home staring at nothing does actually nothing.This is why we have created this online book delivery website to provide the company of a book to those who can't go outside to buy or read
a book at a local bookshop .
</p>
</div>
</div>
</div>
By image element
Without using background-image for the left-col this idea is more "tricky".
For img element - One idea/solution is to use this "trick":
https://css-tricks.com/aspect-ratio-boxes/
Set image col "parent" position to relative and put inside an absolute image (100% width/height + object-fit: cover).
div{
border: 2px solid black;
}
.flex-container {
display: flex;
background-color: orange;
}
#fleximg{
flex-basis: 40%;
top-padding: 100%;
position: relative;
}
#fleximg > img {
width: 100%;
height: 100%;
position: absolute;
object-fit: cover ;
}
#about {
flex-basis: 60%;
padding: 100px 10px;
display: flex;
align-items: center; /* align v */
}
<div class="flex-container">
<div id="fleximg" class="col">
<img src="https://placehold.it/400x400" alt=d esk/>
</div>
<div id="about" class="col">
<!-- text i want to place next to image -->
<div>
<h1> ABOUT US </h1>
<p>
In these rough times we experience right now being forced to stay at home staring at nothing does actually nothing.This is why we have created this online book delivery website to provide the company of a book to those who can't go outside to buy or read
a book at a local bookshop .
</p>
</div>
</div>
</div>
Important: From the design aspect - There is no magic solution to suit all situations. It also depends on the amount of content (text/images and so on) you place in the right "content" column and the width of the screen (min-width/height/padding/margin and Media query useful her).
You should set css class flex-container is width: 100% and #fleximg is width:50%, #about is width:50% and use the background in css instead for img tag.
Example:
.flex-container {
display: flex;
justify-content: flex-start;
flex-direction: row;
background-color: orange;
opacity: 0.7;
width: 100%;
height: auto;
};
#fleximg {
width: 50%;
height: auto;
background: url('') center no-repeat;
background-size: cover;
};
#about {
width: 50%;
margin-right: 0;
height: auto;
};
I have created multiple CSS flexboxes on this landing page and I would like to have the SELECT button to be always on the bottom of each box, no matter the size of textual content above. I tried to use multiple properties such as align-self, justify-content, align-items, align-text with various attributes but could not achieve the desired outcome.
Would could I possibly do in this situation to make it work?
Here is the screenshot of flexboxes:
Here is my HTML and CSS for this section:
#pricing-option-1, #pricing-option-2, #pricing-option-3 {
display: flex;
flex-direction: column;
border-style: solid;
border-color: rgb(230, 230, 230);
border-radius: 25px;
padding: 25px;
margin: 10px;
width: 30%;
background-color: yellow;
}
.pricing-amount {
background-color: blue;
}
<div id="pricing-option-2">
<h3 class="pricing-option-header">BASIC ENBALI</h3>
<p class="pricing-amount">$1200/month</p>
<div class="pricing-body">
<p class="pricing-option-description">The most popular option. You wouldn't need to worry about finding a place to live, commute to the classes, cooking or find a right place to eat. We got you!</p>
<ul class="pricing-option-bulletpoints">
<li>80 hours of any classes</li>
<li>Accommodation on campus (4 people per room)</li>
<li>Meal plan (3 times a day)</li>
<li>Unlimited access to our library</li>
<li>Unlimited access to our online courses library</li>
</ul>
</div>
<p class="select-option">
<button type="submit">SELECT</button>
</p>
</div>
First you need a defined height for the container to avoid that the height adjusts to the contents (I added that in the snippet below, and I erased some of the contents to make it more obvious).
Then you can apply margin-top: auto; to the last element (the container for the button) wo automatically move that to the bottom (end of the flex order)
#pricing-option-1,
#pricing-option-2,
#pricing-option-3 {
display: flex;
flex-direction: column;
border-style: solid;
border-color: rgb(230, 230, 230);
border-radius: 25px;
padding: 25px;
margin: 10px;
width: 30%;
background-color: yellow;
}
.pricing-amount {
background-color: blue;
}
#pricing-option-2 {
height: 500px;
}
.v {
margin-top: auto;
}
<div id="pricing-option-2">
<h3 class="pricing-option-header">BASIC ENBALI</h3>
<p class="pricing-amount">$1200/month</p>
<div class="pricing-body">
<p class="pricing-option-description">The most popular option. place to eat. We got you!</p>
<ul class="pricing-option-bulletpoints">
<li>80 hours of any classes</li>
<li>Accommodation on campus (4 people per room)</li>
<li>Unlimited access to our online courses library</li>
</ul>
</div>
<p class=" v">
<button type="submit">SELECT</button>
</p>
</div>
First make the .pricing-body block to be a flexbox.
Then set flex-grow: 1 on .select-option which will make it fill all of the free space
Finally align the content of .select-option with align-items: flex-end to keep the button to the bottom
Just add this piece of code to your CSS:
.pricing-body {
display: flex;
flex-direction: column;
}
.select-option {
flex-grow: 1;
display: flex;
align-items: flex-end;
}
See this codepen example
Hi I am trying to align the bottombar elements so that they are in 2 columns on the side of 102. I was wondering if there is a way to fix it as they are all floating on the right at the moment. I am a beginner html css programmer and I am not very experienced yet. Ill appreciate any help you can give me!
CSS
/*bottom navbar*/
.bottomnav{
width: 100%;
background-color: rgb(248, 138, 180);
display: flex;
flex-direction: row;
}
.navbarlogo2{
display: block;
margin-left: auto;
margin-right: auto;
width: 10%;
text-decoration: none;
}
/*bottombar*/
.nav {
display: flex;
flex-direction: row;
}
.left, .right {
flex: 1;
}
HTML
<div class="bottomnav">
<ul class="bottomlogo">
<li class="navbarimg2"><img class="navbarlogo2" src="img/LOGO.png"></li>
</ul>
<div class='nav'>
<div class='left'>
<ul>
<li>About Us</li>
<li>Affiliates</li>
</ul>
</div>
<div class='right'>
<ul>
<li>TOS</li>
</ul>
</div>
</div>
</div>
END RESULT
WANTED RESULT
I made things like that. CSS Grid is one of the new HTML5 standard you should take a look. In your case, use a grid is better choice against flex because you're looking for a table-like structure.
I choosed to split your needs in 2 parts:
Center your logo
Make a 2 columns grid for your links
Centering your logo
We need to center an element and prevent it to interfere with our incoming links grid. So we'll set our container with a position: relative and place the img tag in position: absolute. Note the image's top right bottom left properties are now relative to the first parent positioned as relative.
And so we only need to make some simple maths. Note the calc() function, we don't want to center the top left corner of your logo but the center. So we need to remove the half of the defined logo's width.
navbarlogo2 {
left: calc(50% - 60px);
}
Make a 2 columns grid for your links
In order make a grid, you have to display your container as grid and set its grid-template-columns to 1fr 1fr. You can translate fr with the word fraction. So here, we're asking for a row split in 2 fractions.
Because we want a place for our logo, we're adding a gap (grid-cap) in out container to make some space between our 2 columns.
Learn more about the fr unit here.
body {
margin:0
}
.bottomnav {
width: 100%;
background-color: rgb(248, 138, 180);
position: relative;
}
.navbarlogo2 {
display: block;
margin-left: auto;
margin-right: auto;
width: 120px;
text-decoration: none;
position: absolute;
filter: brightness(10);
top: 15px;
left: calc(50% - 60px) /*center top left corner then remove half logo width (120px)*/
}
/*bottombar*/
.nav {
display: grid;
grid-gap: 120px;
grid-template-columns: 1fr 1fr;
}
.nav ul {
padding-left: 0;
}
.nav ul li {
list-style: none;
text-align: center;
padding-left: 0;
}
.left,
.right {
flex: 1;
}
<div class="bottomnav">
<div class="bottomlogo">
<img class="navbarlogo2" src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg">
</div>
<div class='nav'>
<div class='left'>
<ul>
<li>About Us</li>
<li>Affiliates</li>
</ul>
</div>
<div class='right'>
<ul>
<li>TOS</li>
<li>Fourth </li>
</ul>
</div>
</div>
</div>
EDIT: I did manage to figure it out in flex! But the links still aren't quite taking up the entire top row:
enter image description here
there's still some extra space to the right and left. Is there any way to force the flex to use all the available space?
I'd like the nav bar to automatically adjust width based on how many links there are.
.top-container {
text-align: center;
display: flex;
}
.top-content-box {
width: 90%;
height: 30%;
margin: 30px auto;
background: #fff;
box-shadow: 0px 0px 10px #000;
justify-content: center;
display: flex;
flex-direction: column;
}
.news-and-twitter {
display: flex;
flex-direction: row;
flex-grow: 1;
}
.twitter-min {
width: 300px;
}
.news-box {
text-align: left;
vertical-align: middle;
padding: 30px;
flex-grow: 2;
}
.nav-bar {
text-align: center;
flex-grow: 1;
}
ul.link-list {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
display: inline-block;
}
<div class="top-container">
<div class="top-content-box">
<div class="nav-bar">
<ul class="link-list">
<li class="link-list"><a class="nav-list" href="default.asp">home</a></li>
<li class="link-list"><a class="nav-list" href="news.asp">about</a></li>
<li class="link-list"><a class="nav-list" href="contact.asp">projects</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">portfolio</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">commissions</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">patreon</a></li>
</ul>
</div>
<div class="news-and-twitter">
<div class="news-box">
<h1>current project</h1>
Arena Circus ch 4 | Inserting voices into The Pretenders Guild
<h1>Current Events</h1>
<ul>
<li>The Pretenders Guild voice update is coming 7/15/19!
</li>
<li>Check the 1 year anniversary special offer here!</li>
</ul>
</div>
<div class="twitter-min">
<a class="twitter-timeline" data-width="300" data-height="450" data-theme="light" data-link-color="#343584" href="https://twitter.com/CapMinyan?ref_src=twsrc%5Etfw">Tweets by CapMinyan</a>
<script async src="https://platform.twitter.com/widgets.js"
charset="utf-8"></script>
</div>
</div>
</div>
</div>
screenshot
You have to make the parent element of the list (ul.link-list) a flex and make the children (li.link-list) have flex-grow of 1
.top-container{
text-align:center;
}
.top-content-box {
width: 90%;
height:30%;
margin:30px auto;
background: #fff;
box-shadow: 0px 0px 10px #000;
}
.news-and-twitter{
display:flex;
flex-direction:row;
flex-grow:1;
}
.twitter-min{
width:300px;
}
.news-box{
text-align:left;
vertical-align: middle;
padding:30px;
flex-grow:2;
}
Some may say "use flex box" but I really prefer not to do such methods!
So my suggestion would be position the nav bar left:50%;
And then give it a margin-left: -100px then change -100px to half of the nav's width, so lets say it has a width of 526px you put it to -263px.
And it may change it's width depending on the device's screen size, so I'd add some Javascript to change that when the page loads, so basically check what the width of the nav is then divide it by 2 and put a "-" in front of it then a "px" after it...
I understand if that sounds too complicated for such a simple task but I like using JavaScript to do my, things
Also many would say this is dumb to do, because the less Javascript the better but I strongly disagree, but that's my opinion... Other than the fact that a lot of Javascript will make the page slower, and a few other things...
Alternatively try doing margin: 0 auto could fix the problem, worth a shot!
Hope this helps!
I'm currently working on my code where i tried making a responsive image gallery with a hover effect. At my first code I created, the effects are working great. However, it is not responsive as it stays in 4 rows always. Please see this Codepen 1
When I tried to change some of the contents of my style.css using flexbox, i was able to make it responsive and change its size depending on the window size. However, whenever you hover to the image, the hover box does not align with the container itself. Please see this Codepen2
The codes are written on the codepen itself. The only code i changed in the 1st code to the 2nd one is this (I commented the previous code instead of removing it to remember which part i changed:
.container
{
/*width: 1280px;
margin: 70px auto 0;
display: flex;
flex-direction: row;
flex-wrap: wrap;*/
margin: .5vw;
font-size: 0;
display: -ms-flexbox;
-ms-flexbox-wrap : wrap;
-ms-flexbox-direction: column;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
display: -webkit-box;
display: flex;
}
.container .box
{
/*position: relative;
width: 300px;
height: 300px;
background: #ff0;
margin: 10px;
box-sizing: border-box;
display: inline-block;*/
-webkit-box-flex: auto;
-ms-flex: auto;
flex: auto;
width: 300px;
margin: .5vw;
}
.container .box .imgBox img
{
/*max-width: 100%;*/
width: 100%;
height: auto; /*added this*/
transition: transform 2s;
}
Please do help me figure out why the hover position is not working. Thanks
You could modify the width's of the container so that it size is defined by relative positioning, this will solve your issue.
The only change that is to be made is to the div with class container, shown below.
.container {
width: 80%;
margin: 0px auto;
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
}
Please check the below example.
body {
margin: 0;
padding: 0;
background: #262626;
font-family: sans-serif
}
.container {
width: 80%;
margin: 0px auto;
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
}
.container .box {
position: relative;
width: 300px;
height: 300px;
background: #ff0;
margin: 10px;
box-sizing: border-box;
display: inline-block;
}
.container .box .imgBox {
position: relative;
overflow: hidden;
}
.container .box .imgBox img {
max-width: 100%;
transition: transform 2s;
}
.container .box:hover .imgBox img {
transform: scale(1.2);
}
.container .box .details {
position: absolute;
top: 10px;
left: 10px;
bottom: 10px;
right: 10px;
background: rgba(0, 0, 0, .8);
transform: scaleY(0);
transition: transform .5s;
}
.container .box:hover .details {
transform: scaleY(1);
}
.container .box .details .content {
position: absolute;
top: 50%;
transform: translateY(-50%);
text-align: center;
padding: 15p;
color: #fff;
}
.container .box .details .content h1 {
margin: 0;
padding: 0;
font-size: 20px;
color: #ff0;
}
.container .box .details .content p {
margin: 10px 0 0;
padding: 0;
}
<div class="container">
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/1.jpg">
</div>
<div class="details">
<div class="content">
<h1> Snow Queen Elsa </h1>
<p>Elsa is the daughter of Agnarr and Iduna, older sister of Anna, and queen of Arendelle. Elsa was born with the powers to manipulate ice and snow and used them to entertain her sister. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/2.jpg">
</div>
<div class="details">
<div class="content">
<h1> The Little Mermaid Ariel </h1>
<p>Ariel as she appears in her mermaid form in Disney's The Little Mermaid. Ariel is a fictional character and the title character of Walt Disney Pictures' 28th animated film The Little Mermaid (1989). She is often rebellious, and in the first film,
she longs to be a part of the human world. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/3.jpg">
</div>
<div class="details">
<div class="content">
<h1> Sleeping Beauty Aurora </h1>
<p>Princess Aurora, also known as Sleeping Beauty or Briar Rose, is a fictional character who appears in Walt Disney Pictures' animated feature film Sleeping Beauty (1959). Originally voiced by singer Mary Costa, Aurora is the only daughter of King
Stefan and Queen Leah. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/4.jpg">
</div>
<div class="details">
<div class="content">
<h1> Aladdin's Jasmine </h1>
<p>Princess Jasmine is the deuteragonist of Disney's 1992 animated feature film, Aladdin. She is from the Middle Eastern kingdom of Agrabah where her father, the Sultan, rules. Jasmine was born into a role and society that treats her as an object
and a tool, rather than a person </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/5.jpg">
</div>
<div class="details">
<div class="content">
<h1> Rupunzel </h1>
<p>Princess Rapunzel (voiced by Mandy Moore) is more assertive in character, and was born a princess. Her long blonde hair has magical healing and restoration powers. A woman named Mother Gothel (voiced by Donna Murphy) kidnaps Rapunzel for her magical
hair which would help maintain her youth. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/5.jpg">
</div>
<div class="details">
<div class="content">
<h1> Megara </h1>
<p>In Greek mythology, Megara was the oldest daughter of Creon, king of Thebes. Megara was offered by her father to Hercules because he defended Thebes. She had two children, a boy and a girl, but was killed with both of them by Hercules in excess
of madness caused by Hera. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="c">
</div>
<div class="details">
<div class="content">
<h1> Belle The Beauty </h1>
<p>Belle is a fictional character who appears in Walt Disney Pictures' animated feature film Beauty and the Beast (1991). Originally voiced by American actress and singer Paige O'Hara, Belle is the non-conforming daughter of an inventor who yearns
to abandon her predictable village life in return for adventure. </p>
</div>
</div>
</div>
<div class="box">
<div class="imgBox">
<img src="http://www.gstatic.com/webp/gallery/1.jpg">
</div>
<div class="details">
<div class="content">
<h1> Mulan The Great Warrior Of China </h1>
<p>Mulan (full name Fa Mulan) is a character, inspired by an actual historic figure, who appears in Walt Disney Pictures' animated feature film Mulan (1998), as well as its sequel Mulan II (2004). </p>
</div>
</div>
</div>
</div>