Why Does the Div Wrapper Alter My Page In This Way? - html

If I comment out my Div wrapper, the page becomes a jumbled mess, if the wrapper is not commented out, it is not a jumbled mess.
Why? My guess is that is is somehow shielded from the CSS, but I am not sure.
I am new so, I apologize in advance.
This exercise is part of the OdinProject, and is the first one I'm actually struggling with understanding.
html,
body {
height: 100%;
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
font-size: 18px;
font-weight: 700;
margin-bottom: 8px;
}
body {
font-family: Roboto, sans-serif;
margin: 0;
background: #aaa;
color: #333;
/* I'll give you this one for free lol */
display: flex;
align-items: center;
justify-content: center;
}
.modal {
display: flex;
gap: 16px;
padding: 16px;
background: white;
width: 480px;
border-radius: 10px;
box-shadow: 2px 4px 16px rgba(0, 0, 0, .2);
}
.icon {
flex-shrink: 0;
color: royalblue;
font-size: 26px;
font-weight: 700;
background: lavender;
width: 42px;
height: 42px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
.close-button {
background: #eee;
border-radius: 50%;
color: #888;
font-weight: 400;
font-size: 16px;
height: 24px;
width: 24px;
display: flex;
align-items: center;
justify-content: center;
}
button {
padding: 8px 16px;
border-radius: 8px;
}
button.continue {
background: royalblue;
border: 1px solid royalblue;
color: white;
}
button.cancel {
background: white;
border: 1px solid #ddd;
color: royalblue;
}
.text {
margin-bottom: 16px;
}
<div class="modal">
<div class="icon">!</div>
<div class="wrapper">
<div class="header">Are you sure you want to do that?
<div class="close-button">✖</div>
</div>
<div class="text">text</div>
<button class="continue">Continue</button>
<button class="cancel">Cancel</button>
</div>

Your .modal has the display: flex property. This property applies to all direct children of the .modal element.
If you remove the .wrapper, the elements in the modal are no longer grouped together and they are treated as separate flex items. This is why they appear side to side (.icon, then .header, then .text, then .button all on the same line).
Here is a great guide on the display: flex property.

All HTML elements have default properties. A div has one: display: block;. In this case, when your wrapper div is removed, this property is removed it breaks the appearance because this default is battling the parent element's display: flex property.
Here's the default property list: https://www.w3schools.com/cssref/css_default_values.asp

Related

how to make style button in top right corner using css

I have created on dashboard page which using css style
But i want to make logout button in top right corner using css like this image using css
How can we do that logout button in right corner top using css?
I tried to do that but it did not happen
CSS:
.dashboard {
height: 100vh;
width: 100vw;
/* display: flex; */
align-items: center;
justify-content: center;
}
.dashboard__container {
display: flex;
flex-direction: column;
text-align: center;
background-color: #dcdcdc;
padding: 30px;
height: 100vh;
width: 100vw;
}
.dashboard__btn {
padding: 10px;
font-size: 18px;
margin-top: 10px;
border: none;
color: white;
background-color: black;
}
.dashboard div {
margin-top: 7px;
}
the html render code
return (
<div className="dashboard">
<div className="dashboard__container">
welcome
<div>{name}</div>
<div>{user?.email}</div>
<button className="dashboard__btn" onClick={logout}>
Logout
</button>
</div>
</div>
);
I think this might work
.dashboard__btn {
position: absolute;
right: 10px;
padding: 10px;
font-size: 18px;
margin-top: 10px;
border: none;
color: white;
background-color: black;
}
you can do this I am just not sure if it's the best practice, I am also a beginner,
.welcome { position: absolute; } //in css
and
<div className="welcome">welcome</div> //in react
Is this what you want?
Just removed some code which does nothing and added justify-content: space-between; to the dashboard__container and that does the trick
.dashboard {
height: 100vh;
width: 100%;
background: #3e295a;
}
.dashboard__container {
display: flex;
flex-direction: row;
justify-content: space-between;
text-align: left;
background-color: #7015b8;
padding: 1em 20px 1em 20px;
color: white;
font-weight: bold;
}
.dashboard__btn {
margin-top: -5px;
font-size: 18px;
padding: 0.25em 0.5em;
border: none;
color: white;
background-color: #402958;
}
<div class="dashboard">
<div class="dashboard__container">
Welcome
<div>{name}</div>
<div>{user?.email}</div>
<button class="dashboard__btn" onclick="{logout}">
Logout
</button>
</div>
</div>
Tell me if its not working...

Adding a focus state to a div inside a link

I'm trying to make the link have a focus state around the whole .container-covid-alert div, when the link is tabbed onto, I'm sure this must be possible? The below 2 selectors are not working.
.container-covid-alert:focus {
outline: solid 3px #FFAD16;
}
.container-covid-alert a:focus {
outline: solid 3px #FFAD16;
}
.container-covid-alert {
background-color: #206497 !important;
color: #FFFFFF;
padding-top: 10px;
padding-bottom: 10px;
display: block;
flex-flow: row wrap;
align-items: center;
align-content: center;
justify-content: flex-start;
font-size: 1.1rem;
}
.container-covid-alert a {
color: #FFFFFF;
}
.covid-inner {
margin: auto;
width: 1170px;
}
<a id="covid-banner-link" title="Coronavirus" class="sys_16" href="/">
<div class="container-covid-alert">
<div class="covid-inner">
<p>Coronavirus (COVID-19)updates and advice.</p>
</div>
</div>
</a>
Your hyperlink collapses as it behaves as an inline element.
The solution is to make it act like a block element with display: block;.
That way you can just add the :focus state to the hyperlink itself and make it really simple.
Also an orange focus indicator is not high enough contrast - it is only 1.86:1 and you need 3:1 as a minimum so you may want to change that.
#covid-banner-link{
display:block;
}
#covid-banner-link:focus {
outline: solid 3px #FFAD16;
}
.container-covid-alert {
background-color: #206497 !important;
color: #FFFFFF;
padding-top: 10px;
padding-bottom: 10px;
display: block;
flex-flow: row wrap;
align-items: center;
align-content: center;
justify-content: flex-start;
font-size: 1.1rem;
}
.container-covid-alert a {
color: #FFFFFF;
}
.covid-inner {
margin: auto;
width: 1170px;
}
<a id="covid-banner-link" title="Coronavirus" class="sys_16" href="/">
<div class="container-covid-alert">
<div class="covid-inner">
<p>Coronavirus (COVID-19)updates and advice.</p>
</div>
</div>
</a>

How do I flex 1 item of a flex div all the way right when its inside another flex div?

I seem to be havin some issues with the bottom portion of this blog card for my hobby website.
As you can see im trying to go for the standard author name on the left side with the like symbol on the right side.
For whatever reason, I can't flex the heart all the way to the right. I made the parent container 100% width. I've tried all the align-self. But I can't seem to get it to work and have come to the pros for help.
.container {
width: 20%;
display: flex;
flex-direction: column;
margin-left: 10px;
position: relative;
margin: 10rem auto;
background-color: white;
box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, .25);
align-items: flex-start
}
.picture {
width: 100%;
height: 25rem;
background-image: url(./images/adventure.jpg);
overflow: hidden;
background-size: cover;
background-position: center bottom;
padding-bottom: 2rem;
}
.container__p {
font-size: 1rem;
font-weight: 700;
align-self: center;
color: #777;
letter-spacing: 1px;
text-transform: uppercase;
padding-bottom: .5rem;
padding-left: .5rem;
padding-right: .5rem;
padding-top: 1rem;
border-bottom: 1px solid lightgrey;
}
.container__header {
font-size: 3rem;
text-align: center;
padding: 1rem;
align-self: center;
font-weight: 600;
}
.container__blogsummary {
font-size: 1.3rem;
padding-top: 5rem;
padding: 1rem;
text-align: center;
align-self: center;
color: #777;
}
/*Problem CSS below*/
.icon-basic-heart {
font-size: 2rem;
color: red;
align-self: flex-end;
}
.test {
display: inline-flex;
align-items: flex-start;
}
<div class='container'>
<div class='picture'></div>
<p class='container__p'><span>Trending<span></p>
<h1 class='container__header'>Survivng the Amazon Forest</h1>
<i class='icon-basic-world'></i>
<p class='container__blogsummary'>
A matchbox, cantene of water and pocket knife. How I survived the Amazon rainforest for 3 days with just the bare necessaties and how you can too!
</p>
<!-- this is the problem div below -->
<div class='test'>
<p class='container_author'>James</p>
<i class='icon-basic-heart'></i>
</div>
</div>
It's probably a fundamental mistake as I am learning flexbox throuh creating this on my own. Or maybe I havent had enough coffe today. Thanks in advance!
.icon-basic-heart {
float: left;
}
.container_author {
float: right;
}
.test {
width: 100%;
}
You overcomplicate things. There is no need in flex for only 2 objects - flex doesnt fit in here.
Change your .test CSS to this:
.test {
display: inline-flex;
align-items: flex-start;
// Add the following
justify-content: space-between;
width: 100%;
}

Having trouble positioning a button at the right side of the div in which it resides using flexbox

As you can see in the snippet below, I have 1 div which has display property set to flex and then this div has 2 children divs which take flex:1 space ( 50% ). Each of those divs contains 1 button each.
Now the problem is this. I want the first button to be at the start of the first div ( so left side ) and the second button to be at the end of the second div ( so right side ). Currently both buttons are at the left side of their respective divs.
And while we're at it, is using flexbox the best way to create side by side divs like I've done nowadays?
.edit-btn, .submit-btn {
display: block;
color: black;
cursor: pointer;
text-align: center;
border-radius: 4px;
border: none;
padding: 8px 10px 8px 10px;
line-height: 1.2;
outline:none;
}
.flex-row {
display: flex;
margin-bottom: 10px;
}
.flex-column {
flex: 1;
}
.like, .edit {
width: 150px;
font-size: 13px;
}
.submit-btn {
background-color: #4CAF50;
}
.edit-btn {
background-color: #13aff0;
}
<div class="flex-row">
<div class="flex-column">
<button class="submit-btn like">Like</button>
</div>
<div class="flex-column">
<a class='edit-btn edit' href="#">Edit</a>
</div>
</div>
You don't need .flex-column wrappers. And use justify-content: space-between;
.edit-btn,
.submit-btn {
display: block;
color: black;
cursor: pointer;
text-align: center;
border-radius: 4px;
border: none;
padding: 8px 10px 8px 10px;
line-height: 1.2;
outline: none;
}
.flex-row {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
.like,
.edit {
width: 150px;
font-size: 13px;
}
.submit-btn {
background-color: #4CAF50;
}
.edit-btn {
background-color: #13aff0;
}
<div class="flex-row">
<button class="submit-btn like">Like</button>
<a class='edit-btn edit' href="#">Edit</a>
</div>

Getting flex to have equal width columns to the widest element

I have a bunch of buttons and I want them to all be the same width without having to set a specific width, so naturally you would want the width of all buttons to take the width of the widest element, but I am having a hard time achieving this with flex as it seems it just wants them all to be 100%; I also tried it with a wrapper around the anchors but that didn't help as then the buttons were all varying widths.
CodePen: https://codepen.io/gutterboy/pen/MZWroj?editors=1100
So in that example, all the buttons should match the natural width of what the "Groundskeeping" would be.
HTML:
<div class="container">
<div class="row">
<div class="offset-md-4 col-md-4">
<div class="buttons">
Plumbing
Electrical
Groundskeeping
Construction
Cleaning
Security
Trades Assistant
General Duties
</div>
</div>
</div>
</div>
CSS:
.buttons {
display: flex;
flex-direction: column;
padding: 15px;
background-color: gray;
.btn {
display: block;
margin-bottom: 11px;
&:last-child {
padding-bottom: 21px;
}
}
}
a.btn {
display: inline-block;
text-align: center;
height: 35px;
padding: 0 20px;
min-width: 128px;
font-weight: bold;
color: #fff;
background-color: orange;
border: 2px solid #000;
white-space: nowrap;
text-decoration: none;
}
Is there any way this can be done in Flex or anything else?
You are almost good, you should use inline-flex instead of flex to have the shrink-to-fit behavior thus the biggest button will define the width of the container and all the elements are by default stretched to that width:
.container {
margin-top: 15px;
text-align:center;
}
.buttons {
display: inline-flex;
flex-direction: column;
padding: 15px;
background-color: gray;
}
.buttons .btn {
display: block;
margin-bottom: 11px;
}
.buttons .btn:last-child {
padding-bottom: 21px;
}
a.btn {
display: inline-block;
text-align: center;
height: 35px;
padding: 0 20px;
min-width: 128px;
font-weight: bold;
color: #fff;
background-color: orange;
border: 2px solid #000;
white-space: nowrap;
text-decoration: none;
}
<div class="container">
<div class="buttons">
Plumbing
Electrical
Groundskeeping
Construction
Cleaning
Security
Trades Assistant
General Duties
</div>
</div>