This question already has answers here:
How to remove margin space around body or clear default css styles
(7 answers)
Closed 21 days ago.
I have a container with an h2 and h4 element in it centered on an img. I've set the padding to 0 and there's still too much padding. I am very new to this so any help would be appreciated.
I have selected the div to set the padding which hasn't worked but I have also tried to select h2 and h4 by themselves and still no joy.
.main {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.our-mission-img {
margin: 69px;
height: 700px;
z-index: 0;
position: relative;
width: 1200px;
}
.mission-text {
position: absolute;
text-align: center;
background-color: black;
color: white;
width: 1200px;
padding: 0.5px;
}
<div class="main">
<img class="our-mission-img" src="https://content.codecademy.com/courses/freelance-1/unit-4/img-berryblitz.jpg" alt="berry-blitz">
<div class="mission-text">
<h2>Our Mission</h2>
<h4>Handpicked, Artisanllay Currted, Free Range, Sustainable, Small Batch, Fair Trade, Organic Tea </h4>
</div>
</div>
Welcome to the community! I think the issue is with the box-sizing. Setting it to border-box for all elements would get your job done
* {
box-sizing: border-box;
}
'*' selects every element and add the given style
* selects all tags and apply to all.
*{
box-sizing: border-box;
}
Code for it!
*{
box-sizing: border-box;
}
.main {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.our-mission-img {
margin: 69px !important;
height: 700px;
z-index: 0;
position: relative;
width: 1200px;
}
.mission-text {
position: absolute;
text-align: center;
background-color: black;
color: white;
width: 1200px;
padding: 0.5px !important;
}
<div class="main">
<img class="our-mission-img" src="https://content.codecademy.com/courses/freelance-1/unit-4/img-berryblitz.jpg" alt="berry-blitz">
<div class ="mission-text">
<h2>Our Mission</h2>
<h4>Handpicked, Artisanllay Currted, Free Range, Sustainable, Small Batch, Fair Trade, Organic Tea </h4>
</div>
</div>
Related
Been attempting to layer and image over the top left corner of a div container; iv achieved it once but it didn't stick to position if the page was adjusted!
^^ This is what im redesigning
^^ This is what ive managed to design myself
I have come across different posts and answers suggesting that I try to use:
display: block, relative
position: block, relative
margins, float: start
In the end I am stumped an have resorted to removing most displays besides for the p tags an its composed container:
This is the CSS I have been trying to use to make this happen
.roadmap__section__container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
min-height: 100%;
max-height: 100%;
background-color: #383636;
align-items: center;
}
.roadmap__header {
font: 3rem "Nunito";
margin: 3% 50% 3% 50%;
}
.roadmap__phase__one {
width: 100%;
height: 100%;
}
.phase__one__img {
width: 10%;
position: absolute;
margin-left:10%;
}
.phase__one__data {
display: flex;
flex-direction: column;
width: fit-content;
height: fit-content;
align-items: flex-start;
justify-content: center;
text-align: center;
padding: 25px;
margin: 0% 5% 5% 15%;
box-shadow: 1px 1px 40px #ff00e6;
outline-color: #ff00e6;
outline-offset: 0px;
outline-style: solid;
outline-width: 3px;
border-radius: 25px;
}
.phase__one__data p {
font-size: 1.75rem;
color: #fff;
font-family: "Nunito", serif;
padding: 5px 10px;
}
Here is the HTML Code used for my redesign:
<section class="roadmap__section__container">
<h1 class="roadmap__header">Roadmap</h1>
<div class="roadmap__phase__one">
<img class="phase__one__img" src="images/CasinoWRLD__dice1" alt="">
<div class="phase__one__data">
<p>- Working To Perfect The NFT Artwork.</p>
<p>- Finalise The Marketing Plan.</p>
<p>- Plan And Develop Casino WRLD.</p>
</div>
</div>
</section>
.phase__one__img {
grid-column: 3;
width: 11%;
position: absolute;
top: 157.5%;
right: 32%;
}
.phase__zero__img {
grid-column: 1;
width: 11%;
position: absolute;
margin-left: 5%;
top: 119.75%;
}
I've found this to work currently but it is not a 100% answer, if the browser height is adjust the settings regarding ''top:157.5%'' needs to be adjusted accordingly or it will not line up with the top-left corner of the div container
The difference in the two is pertaining to it switches back and forth going down the page
Need to center this text in this way:
Basically, the top part to the left and the bottom part to the right, but with a bit of overlap.
How can I do that in HTML/CSS?
You can center the text vertically by using the centering with transform technique. Then the text should be separated into two lines and aligned to left/right plus a small negative margin so it overflows outside of the circle.
div {
height: 130px;
width: 130px;
background: lightblue;
position: relative;
border-radius: 50%;
}
p {
position: absolute;
top: 50%;
transform: translateY(-50%);
margin: 0;
width: 100%;
}
span {
display: block;
}
span:first-of-type {
text-align: left;
margin-left: -7px;
}
span:last-of-type {
text-align: right;
margin-right: -7px;
}
<div>
<p>
<span>La crèativitè</span>
<span>est contagieuse</span>
</p>
</div>
Simply use 2 elements for those 2 lines and add a margin-left to the second element:
.logo p {
margin: 0;
}
.logo p:nth-of-type(2) {
margin-left: 3.5em;
}
/* for demonstration purpose only */
html {
font-size: 40px;
font-family: Arial;
}
<div class="logo">
<p>La crèativitè</p>
<p>est contagieuse</p>
</div>
Another entirely different (and more complicated) approach is to use flex-box. It requires you to use containers and to know the principles of flex-box. But once you know what you're doing, it becomes fairly simple to center things inside of other things.
If you need somewhere to practice flex-box with simple games, you can visit https://flexboxfroggy.com/.
.container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: gray;
}
.logo {
width: 200px;
height: 200px;
background-color: maroon;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
p {
font-size: 20px;
}
#p1 {
align-self: flex-start;
}
#p2 {
align-self: flex-end;
}
<div class="container">
<div class="logo">
<p id="p1">La crèativitè</p>
<p id="p2"> est contagieuse</p>
</div>
</div>
Bonne Chance!
Beginner here. For some reason in the website I'm coding, the to-be buttons don't align horizontally on the nav bar. I'm using flexbox, but even then it doesn't align them like how I want it to. There doesn't seem to be any problems in the code, either.
.topPart {
position: fixed;
overflow: hidden;
top: 0;
margin: 0px 15px 30px 15px;
width: 1790
padding: 30px;
border: 2px solid #3cc851;
background-color: #1f1f1f;
}
.topButtons {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
align-items: flex-center;
height: 15px;
width: auto;
padding: 15px;
background-color: #fff;
color: #1f1f1f;
}
.topButtons .rightButtons {
text-align: right;
margin: auto 2px auto 1500px;
}
.topButtons .leftButtons {
text-align: left;
margin: auto 2% auto 2%;
}
<div class="topPart">
<h2> Website name I don't want to share </h2> <p> Website slogan I don't want to share </p>
<!-- later, make it so that the boxes of text are lined up like how it looks in the code using css -->
<div class="topButtons">
<div class="leftButtons">
<a href="index.html" class="home">home<a>
<a href="" class="mssg">mssg board<a>
<a href="database.html" class="data">database<a>
<a href="roleplay.html" class="rp">rp<a>
</div>
<div class="rightButtons">
dms
<a class="out">logout</a>
<a class="acc">acc</a>
</div>
</div>
</div>
The right buttons appear to be ~5px lower than the left ones. I'm at a complete loss here, there doesn't seem to be any errors in my code to cause this. Flexbox tags that should fix it don't do anything either, such as flex-direction: row; and justify-content.
What it looks like.
the div is a block element, so maintaining a width of auto would be essentially 100% of the available space which pushed the second div downwards.
try inline-flex or making the width a set amount rather than auto
This is happening due to the CSS properties of "text-align" and "margin" on ".leftButtons" and ".rightButtons" classes. You don't need to use these properties as you are using "display:flex" on ".topButtons" class. Horizontal and vertical alignment of child classes will manage by "justify-content: space-between;" and "align-items: center;" ".topButtons".
I have removed CSS properties of ".leftButtons" and ".rightButtons" classes. Updated "justify-content: space-between;" on ".topButtons" so that child classes horizontal align by leaving equal spaces between element.
I have also made some corrections in your markup which I have found, mentioned below:
(1) Anchor (/a) closure tags was missing of child ".leftButtons".
(2) In ".topPart" ";" was missing after width property declaration. Also added "px" of width value "1790".
Please check added code.
.topPart {
position: fixed;
overflow: hidden;
top: 0;
margin: 0px 15px 30px 15px;
width: 1790px;
padding: 30px;
border: 2px solid #3cc851;
background-color: #1f1f1f;
}
.topButtons {
display: flex;
flex-flow: row wrap;
/* justify-content: space-around; [old code] */
justify-content: space-between;
align-items: center;
height: 15px;
width: auto;
padding: 15px;
background-color: #fff;
color: #1f1f1f;
}
/*---- [old code]----*/
.topButtons .rightButtons {
/*text-align: right;
margin: auto 2px auto 1500px;*/
}
.topButtons .leftButtons {
/*text-align: left;
margin: auto 2% auto 2%;*/
}
<div class="topPart">
<h2> The Blood Tundra Kingdom </h2>
<p> - never talk about the blood tundra kingdom </p>
<!-- later, make it so that the boxes of text are lined up like how it looks in the code using css -->
<div class="topButtons">
<div class="leftButtons">
home
mssg board
database
rp
</div>
<div class="rightButtons">
dms
<a class="out">logout</a>
<a class="acc">acc</a>
</div>
</div>
</div>
You can define display: flex;,justify-content: space-between, align-items: center; in topPart class and equally divided width of inside div by this flex: 1 1 auto; css property. I hope below snippet will help you lot.
*,*:before,*:after{box-sizing: border-box;}
body{margin: 0; padding: 0}
.topPart {
position: fixed;
overflow: hidden;
top: 0;
padding: 30px 20px;
border-bottom: 2px solid #3cc851;
background-color: #fff;
width: 100%;
z-index: 100;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-items: center;
}
.leftButtons {
flex: 1 1 auto;
text-align: left;
}
.centerButtons {
text-align: center;
flex: 1 1 auto;
}
.rightButtons {
text-align: right;
flex: 1 1 auto;
}
.centerButtons a, .rightButtons a{
display: inline-flex;
background-color: #ccc;
padding: 4px 5px;
text-decoration: none;
color: #444;
text-transform: capitalize;
}
<div class="topPart">
<div class="leftButtons">
<div>Website Name<br>Website slogan</div>
</div>
<div class="centerButtons">
home
mssg board
database
rp
</div>
<div class="rightButtons">
dms
<a class="out">logout</a>
<a class="acc">acc</a>
</div>
</div>
After reading something on vertical-align here, i'm decide to do some practice on this feature, but stuck into a problem that really hard to understand. could you please figure out why "the line of text" does not get vertical-aligned in wrapper div.
.wrapper {
height: 200px;
background-color: aquamarine;
margin: 0;
padding: 0;
}
.ctxt {
display: inline-block;
vertical-align: middle;
margin: 0;
padding: 0;
}
.ctxt-before {
display: inline-block;
width: 10px;
height: 200px;
background-color: #f66;
margin: 0;
padding: 0;
}
<div class="wrapper">
<!-- i think the line-box is 200px and div.ctxt should vertical-aligned in div.wrapper -->
<div class="ctxt-before"></div>
<p class="ctxt">this line of text</p>
</div>
I think the line-box is 200px and div.ctxt should vertical-aligned in div.wrapper.
You've almost got the idea, but you're aligning the middle of the ctxt div with the baseline of the ctxt-before div. Because that has no content, its baseline is its bottom edge.
You need to align it with the middle of the ctxt-before div instead:
.wrapper {
height: 200px;
background-color: aquamarine;
margin: 0;
padding: 0;
}
.ctxt {
display: inline-block;
vertical-align: middle;
margin: 0;
padding: 0;
}
.ctxt-before {
display: inline-block;
width: 10px;
height: 200px;
background-color: #f66;
margin: 0;
padding: 0;
vertical-align: middle; /* add this */
}
<div class="wrapper">
<!-- i think the line-box is 200px and div.ctxt should vertical-aligned in div.wrapper -->
<div class="ctxt-before"></div>
<p class="ctxt">this line of text</p>
</div>
The reasons why you got that result is:
div.ctxt-before is not vertical-aligned in middle. By default, inline or inline-block element is aligned on baseline.
div.ctxt-before does not contain text or image, so the baseline is the bottom of its bounding box. If you try adding text inside div.ctxt-before, the text in p.ctxt will display at top, because the baseline belongs to text/image, not the bounding box anymore.
If you just want div.ctxt-before element and p.ctxt element to be vertical-aligned in middle, you miss:
.ctxt-before{vertical-align: middle;}
Using flex box you can do this easily.
.wrapper {
height: 200px;
background-color: aquamarine;
align-items: center;
text-align: center;
display: flex;
justify-content: center;
}
https://codepen.io/ssniranga/pen/dqKaNe
Yours doesn't work because vertical-align is for tables. When display modes, box-model, and positions are considered, it's easy to see that css layout has many aspects. You'll have to read about them to truly understand what's going on.
Try using flexbox, they make this stuff easy:
.wrapper {
height: 200px;
background-color: aquamarine;
display: flex;
justify-content: center;
align-items: center;
}
.ctxt-before {
display: inline-block;
width: 10px;
height: 200px;
background-color: #f66;
margin: 0;
padding: 0;
}
<div class="wrapper">
<!-- i think the line-box is 200px and div.ctxt should vertical-aligned in div.wrapper -->
<div class="ctxt-before"></div>
<p class="ctxt">this line of text</p>
</div>
So I want to build a simple event box, to replace the default one in a calendar (react-big-calendar for reference, but I don't think it matters)
I would like to make it as responsive as possible, but I have started with a very static box, which corresponds to what I would like to see on a big screen.
Simple fiddle
.container {
width: 200px;
height: 200px;
background-color: #dddddd;
margin: 20px;
padding: 5px;
}
.event-slot-component {
width: 100%;
min-height: 30px;
position: relative;
background-color: #64a7DD;
border: 5px;
border-radius: 3px;
padding: 2px;
}
.event-slot-start-time {
font-size: 0.75em;
vertical-align: top;
position: absolute;
top: 0;
left: 0;
}
.event-slot-end-time {
font-size: 0.75em;
float: left;
vertical-align: bottom;
position: absolute;
bottom: 0;
left: 0;
}
.event-slot-label {
font-size: 1em;
top: 8px;
right: 5px;
position: absolute;
}
<div class="container">
<div class="event-slot-component">
<div class="event-slot-start-time">17h</div>
<div class="event-slot-end-time">21h</div>
<div class="event-slot-label">Occupied Slot</div>
</div>
</div>
My goal is to have a 'centered, eventually slightly to the right' label,
and two small indications on the left that correspond to the start and end of the event.
I have tried using flexbox, coming from other StackOverflow answers, and it does seem to be able to do that somehow, but I have not managed to display the three elements properly. Any insight on a clean solution to achieve this result?
The simplest with the existing markup is to use Flexbox with column direction on the 2 date values and then position the label absolute using transform
.container {
width: 200px;
height:200px;
background-color: #dddddd;
margin: 20px;
padding: 5px;
}
.event-slot-component {
width: 100%;
min-height: 30px;
position: relative;
background-color: #64a7DD;
border: 5px;
border-radius: 3px;
padding:2px;
display: flex;
flex-direction: column;
}
.event-slot-start-time,
.event-slot-end-time {
font-size: 0.75em;
flex-grow: 1; /* share the vertical space equal */
}
.event-slot-label {
position:absolute;
font-size: 1em;
top: 50%;
left: calc(50% + 10px); /* adjust px value for horiz. offset */
transform: translate(-50%,-50%); /* vert./hor. center the label */
}
<div class="container">
<div class="event-slot-component">
<div class="event-slot-start-time">17h</div>
<div class="event-slot-end-time">21h</div>
<div class="event-slot-label">Occupied Slot</div>
</div>
</div>
If you want a good responsive solution, use Flexbox all the way, here with a wrapper for the date's
.container {
width: 200px;
height:200px;
background-color: #dddddd;
margin: 20px;
padding: 5px;
}
.event-slot-component {
width: 100%;
min-height: 30px;
position: relative;
background-color: #64a7DD;
border: 5px;
border-radius: 3px;
padding:2px;
display: flex;
}
.event-slot-time {
display: flex;
flex-direction: column;
}
.event-slot-start-time,
.event-slot-end-time {
font-size: 0.75em;
flex-grow: 1; /* share the vertical space equal */
}
.event-slot-label {
flex-grow: 1; /* fill the remaining horizontal space */
font-size: 1em;
display: flex;
align-items: center; /* vertical center the label text */
justify-content: center; /* horizontal center the label text */
}
<div class="container">
<div class="event-slot-component">
<div class="event-slot-time">
<div class="event-slot-start-time">17h</div>
<div class="event-slot-end-time">21h</div>
</div>
<div class="event-slot-label">Occupied Slot</div>
</div>
</div>
You would need to nest your flexboxes. That's what's so wonderful about it!
To explain, what I did was created three wrappers.
One to hold the entire event.
One to hold your event times.
One to hold the status.
We used flex box to butt the event times and status-wrapper against each other. The event times only take up as much space as the text utilizes (plus a little padding). The status wrapper takes up 100% of its usable space.
Then status wrapper is set to flex box using the justify-content and align-items properties. This centers the status.
The status text container is used in the same way to center the status text itself.
.event-wrapper {
background-color: #eee;
display: flex;
}
.event-times-wrapper {
background-color: skyblue;
padding-left: 0.5rem;
padding-right: 0.5rem;
}
.status-wrapper {
width:100%;
display: flex;
align-items:center;
justify-content: center;
}
.status-text {
height: 100%;
display:flex;
align-items: center;
padding-left: 1rem;
padding-right: 1rem;
background-color: tomato;
}
<article class="event-wrapper">
<div class="event-times-wrapper">
<p class="event-start">9:00a</p>
<p class="event-end">10:00a</p>
</div>
<div class="status-wrapper">
<div class="status-text">Busy</div>
</div>
</article>
As OP requested later, a sample without special containers.
.container {
background-color: #eee;
position: relative;
width: 100vw;
height:6rem;
}
.event-slot-component div {
font-family: sans-serif;
color: white;
background-color: skyblue;
height:3rem;
float:left;
padding-left: 1rem;
padding-right: 1rem;
width:10%;
display:flex;
align-items:center;
justify-content:center;
}
.event-slot-component div:nth-child(2) {
background-color:red;
position:absolute;
bottom:0;
left: 0;
}
.event-slot-component div:last-of-type {
margin-left:25%;
background-color: tomato;
float:left;
height: 6rem;
}
<div class="container">
<div class="event-slot-component">
<div class="event-slot-start-time">17h</div>
<div class="event-slot-end-time">21h</div>
<div class="event-slot-label">Occupied Slot</div>
</div>
</div>