Reducing gap between heading and paragraph [duplicate] - css

This question already has answers here:
What are the default margins for the html heading tags (<h1>, <h2>, <h3>, etc.)?
(4 answers)
What is the default padding and/or margin for a p element (reset css)?
(5 answers)
Closed 2 years ago.
I've coded a blog website which is functioning absolutely fine and doing what I want it to - I'd say I'm about average with CSS, but for the life of me I can't figure out why there's a huge gap between my blog title and summary / date posted below it. I want them to essentially have no margin or gap between them, but obviously not be overflowing.
However, whenever I set the margin to 0, it doesn't reduce the gap. I've tried playing with sizes, but on the whole I want to keep them auto height sized where possible for mobile responsiveness, and reducing code.
Here's an example:
h1 {
font-size: 8rem;
letter-spacing: 0.5px;
color: #ff4766;
}
h2 {
font-size: 3rem;
letter-spacing: 0.5px;
color: ##ff4766;
}
h3 {
font-size: 2rem;
letter-spacing: 0.5px;
color: #ff4766;
}
h4 {
color: #fff;
}
p {
font-size: 1rem;
letter-spacing: 0.5px;
color: #fff;
}
a {
font-size: 0.8rem;
letter-spacing: 0.5px;
text-decoration: none;
color: #fff;
}
body {
margin: 0;
padding: 0;
font-family: 'Helvetica', sans-serif;
box-sizing: border-box;
background-color: #3b353a;
}
.homepage-blog-section {
height: auto;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.homepage-blog-container {
height: 90%;
width: 90%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.homepage-blog-post {
height: auto;
width: 50%;
display: flex;
align-items: center;
flex-direction: column;
margin: 40px 0;
}
.homepage-blog-title {
height: auto;
width: 100%;
}
.homepage-blog-details {
height: auto;
width: 100%;
}
.homepage-blog-details p {
color: #f9f9f9;
font-style: italic;
font-weight: 200;
font-size: 0.8rem;
}
<section class="homepage-blog-section">
<div class="homepage-blog-container">
<div class="homepage-blog-post">
<div class="homepage-blog-title">
<a href="">
<h3>
Show
</h3>
</a>
</div>
<div class="homepage-blog-details">
<p>
Test summary
</p>
<p>
Test date
</p>
</div>
</div>
</div>
</section>
As you can see when you run that code there's a big gap between the title and the summary/date below it. I've considered putting them into the same div, but to be honest I don't massively want to do that - and it doesn't work exactly how I was hoping.
I'm wanting there to be a similar gap between the title and summary, as there is with the summary and date. I've tried minus margins but that breaks down eventually on smaller devices - and like I said, hoping to keep this code as clean as possible without it being too fiddly.
Is there something obvious I'm missing??

Related

Items are not aligned in the center

enter image description here
i am trying to align all the .features items in the center but for some reason only the first one is doing it. I do not find the reason, please help.
<section>
<div class="features">
<div><i class="fa-solid fa-fire"></i></div>
<div>
<h2>Premium Materials</h2>
<p class="p-description">
Our guitars are built with the best amazonian wood
</p>
</div>
</div>
<div class="features">
<div>
<i class="fa-solid fa-truck-fast"></i>
</div>
<div>
<h2>Shipping</h2>
<p class="p-description">
We make sure you recieve your trombone as soon as we have finished
making it. We also provide free returns if you are not satisfied.
</p>
</div>
</div>
<div class="features">
<div><i class="fa-solid fa-user-check"></i></div>
<div>
<h2>Satisfaction</h2>
<p class="p-description">
For every purchase you make, we will ensure there are no damages or
faults and we will check and test the quality of your instrument.
</p>
</div>
</div>
</section>
css
body {
background-color: #eff1ed;
font-family: "Roboto", sans-serif;
}
/* Header and nav bar */
header {
display: flex;
}
.logo-guitar {
padding: 2% 1% 3% 2%;
}
.luthier-name {
width: 100%;
padding: 1.5% 0 0 1%;
}
#nav-bar {
display: flex;
text-align: center;
justify-content: space-between;
flex-direction: row;
}
.nav-link {
width: 94px;
text-align: center;
margin: 2px auto;
padding-top: 15%;
color: #131b23;
text-decoration: none;
font-weight: bold;
}
h1 {
font-family: "Satisfy", cursive;
font-size: 3rem;
}
/* email form */
.email-form {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
#email {
height: 25px;
width: 100%;
}
#submit {
width: 80%;
margin-top: 5%;
background-color: #ffe121;
border: 0;
font-weight: bold;
height: 35px;
font-family: inherit;
font-size: large;
}
/* Features */
.fa-solid {
color: #e3170a;
font-size: 50px;
}
.features {
display: flex;
align-items: center;
justify-content: center;
text-align: left;
}
.p-description {
width: 80%;
}
section {
padding-top: 10%;
}
If all them have the same class i do not understan why this is happening. I was trying to uso console to find the reason but i'm stuck.
It seems that the .p-description has a width based on its content (it is set in percentage, but its container also does not have a defined width), therefore it gets wider with more text inside, which result in unexpected different looks.
You can define width of .p-description with a value independent to its content and it should make them have same width, such as in px, em, vw, or vh.
Example:
.p-description {
max-width: 350px;
}
Or the layout of its container and parent container can be adjusted to properly contain various length of content in .p-description, but it does take a bit more of refactoring.
Hope that it helps!

CSS Styling both img and paragraph child elements [duplicate]

This question already has answers here:
Vertically align text next to an image?
(26 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
I'm having issues with my 2 child elements both being affected from styles applied to each other. I'm just trying to line up the one with the other better vertically.
I pretty much just went to move the img down a few pixels to line up with the content. Any css thing I'm missing here?
<div id='fileIcon' class='fileIcon'>
<img src='images/Google-Chrome-Logo.svg' class="iconImg"></img>
<p id='fileName'>TestingFileName.pdf</p>
</div>
.fileIcon {
text-align: center;
overflow: auto;
}
.iconImg {
display: inline-block;
z-index: 1;
width: 22px;
height: 22px;
padding: 0px 5px;
}
#fileName {
display: inline-block;
z-index: 1;
color: #999;
font-size: 1.1em;
margin: 0;
}
Add vertical-align: middle to your image (and forget about </img> closing tag)
.fileIcon {
text-align: center;
overflow: auto;
}
.iconImg {
display: inline-block;
z-index: 1;
width: 22px;
height: 22px;
padding: 0px 5px;
vertical-align: middle;
}
#fileName {
display: inline-block;
z-index: 1;
color: #999;
font-size: 1.1em;
margin: 0;
}
<div id='fileIcon' class='fileIcon'>
<img src='' class="iconImg">
<p id='fileName'>TestingFileName.pdf</p>
</div>

Flexbox: while a group of items is at the very center of the page, put at single item at the bottom

I have used Bootstrap 4 and some custom CSS to make a hero section with all its items but one centered horizontally and vertically.
The exception is one item I want to align at the bottom of the page and still keep it centered horizontally.
html,
body {
padding: 0;
margin: 0;
height: 100%;
}
.page-wrapper {
min-height: 100%;
}
a.inherit {
color: inherit;
}
a.nounderline, a.nounderlie:hover {
text-decoration: none;
}
.page-wrapper {
min-height: 100%;
}
.hero {
height: 100vh;
overflow-y: hidden;
padding-top: 3rem;
padding-bottom: 3rem;
justify-content: center;
align-items: center;
}
.hero img {
width: 100%;
}
.hero.type-text h1 {
color: #000;
}
.hero.hero-short {
max-height: 768px;
}
section.type-text h3 {
font-weight: bold;
font-size: 2rem;
margin-bottom: 0;
}
section.type-text h4 {
font-size: 14px;
font-weight: bold;
margin-bottom: 0;
margin-top: 1.5rem;
}
section.type-text p {
font-weight: 500;
}
.allcases {
text-align: center;
font-weight: 700;
margin-top: auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="page-wrapper">
<section class="container d-flex hero type-text">
<div>
<h4 class="text-center m-0">Next item</h4>
<h1 class="display-1 text-center">
<a class="inherit nounderlie" href="#">Lorem</a>
</h1>
</div>
<p class="allcases">
<a class="inherit" href="#">See all items</a>
</p>
</section>
</div>
"See all items" does stay to the bottom (thanks to margin-top: auto), but it is not centered. Changing the flex-direction from row to column messes the whole layout so it is not the way to go.
What is viable solution?
You should be using flex-direction: column for this. This is an ideal use-case for it. Using row layout to achieve what you want, i.e. have the items horizontally centered is not viable. You'd be better off not using flex at all, and using margins instead. If you really wish to use flex-direction: row then the only solution I can think of is either to use position: absolute or a negative margin. Wouldn't recommend it though, since what you want can so easily be accomplished just by using flex-direction: column.
Here is the result I achieved just by changing 2 properties.
Update styling to
// Only added flex-direction: column to this
.hero {
height: 100vh;
overflow-y: hidden;
padding-top: 3rem;
padding-bottom: 3rem;
justify-content: center;
align-items: center;
flex-direction: column;
}
// This is to target the first div, i.e. the div that contains next item and Lorem, since the div doesn't have a class.
.hero > div {
margin-top: auto;
}
The answer is rather simple. Change the flex-flow of your container to column.
flex-flow:column;
Now you have two flex-items: The div, containing your Title and the other links and the footer-p. The first trick is to make your div grow, while the p-tag stays the same. So assign
flex: 1 auto;
to your div and
flex: 0 auto;
to your p-tag.
After doing so you have to add
display: flex;
justify-content: center;
flex-flow: column;
to your div, too. Making it a flex-box itself.
Your p-tag doesnt require more attention, you can also remove the unnecessary margins.
This should do the trick.
You need to allow wrapping and set a width to the div suppose to stand at top
to allow wrapping, use a class
<section class="container d-flex hero type-text flex-wrap">
for the di, you can do :
.hero>div {
width:100%;
}
html,
body {
padding: 0;
margin: 0;
height: 100%;
}
.page-wrapper {
min-height: 100%;
}
a.inherit {
color: inherit;
}
a.nounderline, a.nounderlie:hover {
text-decoration: none;
}
.hero {
height: 100vh;
overflow-y: hidden;
padding-top: 3rem;
padding-bottom: 3rem;
justify-content: center;
align-items: center;
}
.hero>div {
width:100%;
}
.hero img {
width: 100%;
}
.hero.type-text h1 {
color: #000;
}
.hero.hero-short {
max-height: 768px;
}
section.type-text h3 {
font-weight: bold;
font-size: 2rem;
margin-bottom: 0;
}
section.type-text h4 {
font-size: 14px;
font-weight: bold;
margin-bottom: 0;
margin-top: 1.5rem;
}
section.type-text p {
font-weight: 500;
}
.allcases {
text-align: center;
font-weight: 700;
margin: auto auto 0 auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="page-wrapper">
<section class="container d-flex hero type-text flex-wrap">
<div>
<h4 class="text-center m-0">Next item</h4>
<h1 class="display-1 text-center">
<a class="inherit nounderlie" href="#">Lorem</a>
</h1>
</div>
<p class="allcases">
<a class="inherit" href="#">See all items</a>
</p>
</section>
</div>
Another option is to use the flex-column class
html,
body {
padding: 0;
margin: 0;
height: 100%;
}
.page-wrapper {
min-height: 100%;
}
a.inherit {
color: inherit;
}
a.nounderline, a.nounderlie:hover {
text-decoration: none;
}
.hero {
height: 100vh;
overflow-y: hidden;
padding-top: 3rem;
padding-bottom: 3rem;
justify-content: center;
align-items: center;
}
.hero img {
width: 100%;
}
.hero.type-text h1 {
color: #000;
}
.hero.hero-short {
max-height: 768px;
}
section.type-text h3 {
font-weight: bold;
font-size: 2rem;
margin-bottom: 0;
}
section.type-text h4 {
font-size: 14px;
font-weight: bold;
margin-bottom: 0;
margin-top: 1.5rem;
}
section.type-text p {
font-weight: 500;
}
.allcases {
text-align: center;
font-weight: 700;
margin: auto auto 0 auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="page-wrapper">
<section class="container d-flex flex-column hero type-text ">
<div>
<h4 class="text-center m-0">Next item</h4>
<h1 class="display-1 text-center">
<a class="inherit nounderlie" href="#">Lorem</a>
</h1>
</div>
<p class="allcases">
<a class="inherit" href="#">See all items</a>
</p>
</section>
</div>
in both example, margin of allcases is reset to :
margin: auto auto 0 auto;
to stick it at bottom of container, no matter the flex-direction.

HTML/CSS - How do I align three different elements in different way in a heady

I have researched this problem for several days now and have found no solution so far. If this is just a beginners questions with a lot of answers out there I apologise, and please point me in the right directions.
I am trying to developp a "Headline" or "Banner" to be displayed at the top of my website.
I have a colored box, in which i want to display an image (a little off the left side), right next to it some text (the name of the website) and then centered in the middle of the box again some very short text (the name of the document, Home, Contact, things like that).
So it should look like this:
<-space-><-Image-><-Website name-><----------centered text------------------------>
So far whatever I have tried just gets me this:
<-space-><-Image-><-Website name-><-text at the left--------------------------------->
I am currently using a div element for the box with three different elements so i can format them separately in the style sheet. The main problem seems to be that the last header (Centered Text) is just created around the text, and not until the right edge of the box. So aligning left, right or in the center makes no difference.
<div class="box blue-box ">
<img class="icon" src="file://C:/Users/jafa/Desktop/juggling/website/images/white_design.png" alt="icon">
<h8 class="white-text-header">Website name </h8>
<h9 class="white-text-main-header"> Centered Text</h9>
</div>
with css style sheet:
<style>
.blue-box {
background-color: #401841;
padding: 10px;
border:1px solid white;
border-radius: 4px;
.white-text-header {
font-family: Arial;
color: white;
font-size: 24;
vertical-align: middle;
text-align: left;
}
.white-text-header {
font-family: Arial;
color: white;
font-size: 24;
vertical-align: middle;
text-align: center;
}
img.icon{
width: 120px;
height: auto;
align-items: left;
}
</style>
If anyone knows a better solution, I would be very grateful. Thank you for spending your time helping me.
You've duplicated "white-text-header" class definition. Also , left out "blue-box" class' closing brace. The code should be written as followed.
<style>
.blue-box {
background-color: #401841;
padding: 10px;
border:1px solid white;
border-radius: 4px;
}
.white-text-header {
font-family: Arial;
color: white;
font-size: 24;
vertical-align: middle;
text-align: left;
}
.white-text-main-header {
font-family: Arial;
color: white;
font-size: 24;
vertical-align: middle;
text-align: center;
}
img.icon {
width: 120px;
height: auto;
align-items: left;
}
</style>
h8 and h9 probably aren't the elements you want to be using. Update these and you will get what you want. You need to update your css classes. You have some bad syntax and duplicated names.
.blue-box {
background-color: #401841;
padding: 10px;
border:1px solid white;
border-radius: 4px;
}
.white-text-header {
font-family: Arial;
color: white;
font-size: 24;
vertical-align: middle;
text-align: left;
}
.white-text-main-header {
font-family: Arial;
color: white;
font-size: 24;
display: inline-block;
text-align: center;
}
img.icon {
width: 120px;
height: auto;
align-items: left;
}
Here is the fiddle closer to what you want: https://jsfiddle.net/475sdf9w/26/
h8 and h9 are not standard heading tags. Related question
You can reuse text styling properties by adding the rules to the parent (font-family, font-size, and color.
font-size needs a unit -- 24px
You can create your layout with Flexbox. Learn more here.
body {
margin: 0;
}
.blue-box {
background-color: #401841;
padding: 10px;
border: 1px solid white;
border-radius: 4px;
font-family: Arial;
font-size: 24px;
color: white;
display: flex;
align-items: center;
}
.blue-box img {
/* adds space to the left of the image */
margin-left: 20px;
}
.white-text-main-header {
margin: auto;
}
<div class="box blue-box ">
<img class="icon" src="https://unsplash.it/120" alt="icon">
<h5 class="white-text-header">Website name </h5>
<h6 class="white-text-main-header"> Centered Text</h6>
</div>

text background new line padding issue

I am dealing with text blocks (background blocks over text) and face some issues with paddings on new line. The problem occurs when the browser(e.g. mobile) cuts the text into to two lines due to lack of width. text then looks like this:
I don't really know how to set a padding css on the end of the new lines, since it could break up anywhere of the sentence. You could say put a span on it with padding, but it is not fixed where the line will break down. It depends on the width. Any recommendations?
You could apply display: inline-block but that will turn the background color into an ugly box which doesn't look as nice as having an exact width background for each line. Unfortunately CSS doesn't let us target individual lines except for the first one.
If you don't mind getting a little "creative" (or hacky) you could wrap each word in its own element in the backend or using JavaScript and apply the background color to those elements. Adjust the parent's word-spacing accordingly to eliminate gaps.
.main {
font-family: sans-serif;
font-weight: bold;
background-color: #99c;
display: flex;
height: 400px;
flex-direction: row;
align-items: center;
}
.text-container {
max-width: 500px;
display: inline-block;
word-spacing: -15px;
position: relative;
padding-left: 20px;
overflow: hidden;
}
.text-container::before {
content: '';
background-color: black;
position: absolute;
top: 0;
left: 0;
width: 20px;
height: 100%;
z-index: 1;
}
span {
font-size: 36px;
line-height: 1.5em;
color: white;
background-color: black;
padding: 0.25em 0.5em 0.25em 0;
max-width: 360px;
}
<div class="main">
<div class="text-container">
<span>A</span> <span>Movie</span> <span>in</span> <span>the</span> <span>park:</span> <span>Kung</span> <span>Fu</span> <span>Panda</span>
</div>
</div>
You can use box-shadow for this issue and display inline:
<div class="text">
<span class="text-container">A Movie in the park: Kung Fu Panda</span>
</div>
And css:
.text > span {
display: inline;
box-shadow: 25px 0 0 black, -10px 0 0 black;
background-color: black;
color: white;
}
Try to add after "Park:" and before "Kung"
padding workded!!!
change width by console browser and see result:
h1{
background-color: #ff6a6a;
padding: 33px;
display: inline-block;
word-wrap: break-word;
width:300px
}
<h1>rert ert erttttttttttttttt 00000000000000000000 dfgdfgd dfgdfgdft ertert </h1>
Use <p> tag to wrap up the text and it apparently works demo
<div class="main">
<div class="text-container">
<p id="test">A Movie in the park: Kung Fu Panda</p>
</div>
</div>
css
.main {
font-family: sans-serif;
font-weight: bold;
background-color: #99c;
display: flex;
height: 400px;
flex-direction: row;
align-items: center;
}
.text-container {
max-width: 400px;
}
p {
font-size: 36px;
line-height: 2em;
color: white;
background-color: black;
padding: 0.5em;
max-width: 360px;
}

Resources