CSS <div> strange top padding [duplicate] - html

This question already has answers here:
What is the default padding and/or margin for a p element (reset css)?
(5 answers)
Closed 1 year ago.
Im new to web development and currently developing a webapp for my mothers company.
I'm stuck on writing text block above the background image and I'm getting strange top padding in the div. I can't figure out where that comes from so I can remove it and work from there, adding additional padding and applying more css to the .text-block.
Talking about this one:
$(window).scroll(function() {
var sticky = $('.main-banner'),
scroll = $(window).scrollTop();
});
html,
body {
font-family: cursive, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
}
.main-banner {
width: 100%;
height: 60px;
background-color: #51ad89;
box-shadow: 2px 2px 5px grey;
position: fixed;
z-index: 1;
top: 0;
left: 0;
right: 0;
}
.main-nav {
display: flex;
justify-content: center;
align-items: center;
}
.main-nav a {
text-decoration: none;
color: black;
font-size: 18px;
text-shadow: 2px 2px 5px #ccdbd5;
margin: 0px 80px 0px 80px;
}
.main-nav a:hover {
/*text-decoration: underline;*/
color: #e6f0ec;
}
.main-nav img {
display: flex;
align-items: center;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.main-div img {
width: 100%;
}
.main-div {
position: relative;
}
.text-block {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.678);
color: white;
display: flex;
flex-direction: column;
align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header class="main-banner" id="stickyHeader">
<nav class="main-nav">
О студии
Услуги
<img src="/src/tree.png" height="60px" width="60px">
Работы
Контакты
</nav>
</header>
<section class="main-div">
<img src="/src/mainphoto.png">
<div class="text-block">
<p>СТУДИЯ «GREEN ERA»</p>
<p>АВТОРСКИЙ ЛАНДШАФТНЫЙ ДИЗАЙН</p>
<button>Консультация дизайнера</button>
</div>
</section>

You can set the margin on the top to 0 for your <p> elements;
p {
margin-top: 0;
}

It is because of <p> tag's default margin see the picture.
Use a <span> or <div> instead.

Related

How to fix an icon in a corner of hero banner for responsive design?

I am trying to fix an svg icon in the bottom right corner of my hero banner (using flexbox). I am struggling to pin it into the correct position and also need to be able to adjust its position relative to the text and button (its a responsive website and I need to adjust based on screen size). I tried to adjust with the css margin property (left and right), but it does not work well.
.hero {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100vh;
padding: 1em;
box-sizing: border-box;
color: #333333;
background: url(https://images.unsplash.com/photo-1500417148159-68083bd7333a) center center no-repeat;
background-size: cover;
}
.hero-title {
max-width: 17em;
margin: 0;
font-size: 8vh;
font-weight: 100;
line-height: .9;
padding-left: 93px;
padding-top: 150px;
text-transform: none;
color: white;
}
.hero-subtitle {
max-width: 23em;
margin: 0;
font-size: 2vh;
font-weight: 100;
line-height: 1.3;
padding-left: 100px;
padding-bottom: 100px;
padding-top: 60px;
color: white;
}
.hero-footer {
display: flex;
margin-bottom: 2.5em;
}
/* button */
.button-primary {
color: red;
background-color: transparent;
padding: 8px 25px;
margin-left: 100px;
margin-bottom: 350px;
text-decoration: none;
border: .1em solid red;
font-size: 12px;
}
.button-primary:hover {
color: #ffffff;
background-color: #333333;
border: .1em solid #ffffff;
}
#iconheader {
display: flex;
flex-direction: column;
align-items: flex-end;
}
#myicon {
text-decoration: none;
font-size: 5vh;
font-weight: bold;
background: url(../images/test_icon.svg) 50% 50% no-repeat;
color: white;
}
<section class="hero">
<header id="header">
</header>
<header class="hero-header">
<h1 class="hero-title">Wonderful Day<br>Amazing Forum<br>Great Friends</h1>
</header>
<header class="hero-header">
<h2 class="hero-subtitle">Stackoverflow is the #1 forum among developers. Just ask anyone. </h2>
</header>
<footer class="hero-footer">
<a class="button-primary" href="#">Learn More</a>
<div id="iconheader">
<a id="myicon" href="#">Icon</a>
</div>
</footer>
</section>
Any help or suggestions would be greatly, greatly appreciated! Thank you.
Set position absolute and zindex high
.hero {
position: relative;
}
#iconheader {
position: absolute;
right: 0;
bottom: 0;
z-index:99999
}
for set a svg icon bottom in right corner use this css rules instead of yours:
#iconheader {
position: absolute;
right: 0;
bottom: 0;
}
in this way, the svg is always there

Getting a line through the a tag [duplicate]

This question already has answers here:
Create line after text with css
(8 answers)
CSS technique for a horizontal line with words in the middle
(34 answers)
Closed 1 year ago.
I am trying to get a line through the a tag for decorative effect. The line should span the entire width but not go through content of the tag itself.
This is what I want,
This is what I've got so far.
a {
background: none;
}
a::after {
content: "";
border: 3px solid #000;
display: block;
position: relative;
top: -50%;
}
<a class="fw-bold" href="">Explore Services</a>
And here is the jsfiddle of the above code https://jsfiddle.net/68fkvhcw/
Why is the position relative with negative top margin not working?
This would be a possible way to do that. Wrap the a tag all around the elements, make that a flex container and use settings similar to those of my snippet below:
html,
body {
margin: 0;
}
a.link1:link,
a.link1:visited {
text-decoration: none;
font-size: 24px;
color: green;
}
.link1 {
display: flex;
width: 100%;
justify-content: space-between;
align-items: center;
background: #dfd;
padding: 6px 10px;
}
.text1 {
flex-grow: 0;
display: inline-block;
}
.line {
height: 2px;
background: #fa0;
flex-grow: 1;
margin: 0 20px 0;
}
<a class="link1" href="#">
<div class="text1">Explore all Services</div>
<div class="line"></div>
</a>
I have created a code snippet as you want. But here is a suggestion that doesn't use styles directly on <a> tag else it will affect all your <a> tags on the page. So I have defined a style here .my_underline
You can adjust the thickness of the line and the color of the font.
.my_underline {
overflow: hidden;
text-decoration: none;
font-size: 2rem;
font-weight: bold;
color:aqua;
}
.my_underline:after {
content:"";
display: inline-block;
height: 0.5em;
vertical-align: bottom;
width: 100%;
margin-right: -100%;
margin-left: 10px;
border-top: 1px solid black;
}
<a class="fw-bold my_underline" href="">Explore all Services</a>
Use theses styles ,
a {
color: #000000;
font-family: 'collegeregular';
font-size: 20px;
margin-left: 5px;
position: relative;
width: 93%;
}
a::after {
position: absolute;
content: "";
height: 2px;
background-color: #242424;
width: 50%;
margin-left: 15px;
top: 50%;
}
<a class="fw-bold" href="">Explore Services</a>

How to have centered title and an image on just on right in responsive header

First, I'm so sorry because I know that it's possible, but I really suck at CSS.
This is what I'd like to do:
I've managed to do it but it's really messy... The main issue is that my header isn't responsive at all and I'd to know what is the best way to do it (I know that usually flexbox is a good practice when it comes to build something responsive but my issue is that if I create 2 columns thanks to Flexbox I won't be able to align them just next to each other).
This is my current code (I know it's uggly):
header {
background-color: #c16200;
color: white;
margin-top: 1em;
overflow: hidden;
position: sticky;
top: 0;
width: 100%; /* Full width */
z-index: 1;
max-height: 8vh;
}
h1 {
color: white;
text-align: center;
font-size: 2em;
font-family: "Raleway", sans-serif;
text-transform: uppercase;
letter-spacing: 0.08em;
position: relative;
}
.logo {
position: absolute;
top: 10px;
right: 35%;
height: 2.5em;
}
.line {
margin: 0 auto;
width: 17em;
height: 2px;
border-bottom: 2px solid white;
}
.header-sentence {
margin-top: 0.2em;
font-size: 0.8em;
font-style: italic;
text-align: center;
font-family: "Raleway", sans-serif;
}
<header id="myHeader" class="sticky">
<div class="header-title">
<h1>
Title
</h1>
<img
src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517"
class="logo"
alt="logo plane"
/>
<div class="line"></div>
<p class="header-sentence">
subtitle
</p>
</div>
</header>
Thank you guys!
In HTML with CSS it is sometimes a good idea to do some nesting of elements.
I used an wrapper element (.header-title-composition) to layout title, line, and subtitle vertically . This is all wrapped alongside the paper plane inside .header-title, which is responsible for the horizontal layout
header {
background-color: #c16200;
color: white;
margin-top: 1em;
overflow: hidden;
position: sticky;
top: 0;
width: 100%;
/* Full width */
z-index: 1;
/* This destroys everything inside this demonstration */
/* Basing a height on the actual viewport's height is somewhat dangerous */
/* max-height: 8vh; */
}
.header-title {
display: flex;
align-items: center;
}
.header-title :first-child {
margin-left: auto;
}
.header-title :last-child {
margin-right: auto;
}
h1 {
color: white;
text-align: center;
font-size: 2em;
font-family: "Raleway", sans-serif;
text-transform: uppercase;
letter-spacing: 0.08em;
position: relative;
}
.logo {
/* position: absolute;
top: 10px;
right: 35%;*/
height: 2.5em;
}
.line {
margin: 0 auto;
width: 17em;
height: 2px;
border-bottom: 2px solid white;
}
.header-sentence {
margin-top: 0.2em;
font-size: 0.8em;
font-style: italic;
text-align: center;
font-family: "Raleway", sans-serif;
}
<header id="myHeader" class="sticky">
<div class="header-title">
<div class="header-title-composition">
<h1>
Title
</h1>
<div class="line"></div>
<p class="header-sentence">
subtitle
</p>
</div>
<img src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517" class="logo" alt="logo plane" />
</div>
</header>
Here somewhat of a starting point for you. First of all, I added .header-brand as wrapper for title, line, sentence and image. Used display: flex for alignment. The additional media query takes care of the alignment, when the screen size is below 480px (But try it out on your own, since there are probably still some issues with that)
header {
background-color: #c16200;
color: white;
margin-top: 1em;
overflow: hidden;
position: sticky;
top: 0;
width: 100%;
/* Full width */
z-index: 1;
max-height: 80vh;
display: flex;
}
.header-brand {
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
}
h1 {
color: white;
text-align: center;
font-size: 2em;
font-family: "Raleway", sans-serif;
text-transform: uppercase;
letter-spacing: 0.08em;
position: relative;
margin: 0;
}
.logo {
height: 2.5em;
margin-left: 20px;
}
.line {
margin: 0 auto;
width: 17em;
height: 2px;
border-bottom: 2px solid white;
}
.header-title {
margin-top: 10px;
}
.header-sentence {
margin-top: 0.2em;
font-size: 0.8em;
font-style: italic;
text-align: center;
font-family: "Raleway", sans-serif;
}
#media screen and (max-width: 480px) {
.line {
width: 100%;
}
.logo {
margin-left: 0;
}
}
<header id="myHeader" class="sticky">
<div class="header-brand">
<div class="header-title">
<h1>
Title
</h1>
<div class="line"></div>
<p class="header-sentence">
subtitle
</p>
</div>
<img src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517" class="logo" alt="logo plane" />
</div>
</header>
Combine flexbox and a simple wrapper using text-align: center, the decorated line can be a pseudo-element.
h1,
div,
p {
margin: 0;
}
header {
display: flex;
justify-content: center;
align-items: center;
background-color: #c16200;
color: white;
overflow: hidden;
position: sticky;
padding: 0.5rem;
top: 0;
width: 100%;
/* Full width */
z-index: 1;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
.logo-wrapper {
text-align: center;
margin-right: 1rem;
}
header img {
width: 2rem;
height: 2rem;
}
h1 {
font-weight: bold;
font-size: 1.5rem;
}
h1::after {
width: 10rem;
height: 1px;
display: block;
background-color: white;
content: '';
}
<header class="sticky">
<div class="logo-wrapper">
<h1>TITLE</h1>
<p>the tag line</p>
</div>
<img src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517" class="logo" alt="logo plane" />
</header>
I suppose you want the element which contains the title and subtitle centered, and the image aligned right to that, not both together centered. So here's a solution:
The .title-container is centered within the header using display: flex and other flex settings (see below) on the header. Avoiding both the text container and the image to be centered together is done by applying position: absolute to the image, making it a child of .title-container and applying position: relative to .title-container to make it the position reference for the absolutely positioned image. That way the image isn't considered at all when centering the .title-container.
Take a look at the position parameters for the image: Vertically-centered alignement is achieved by top: 50% and transform: translateY(-50%);, the horizontal position is done with a negative right value: -2.5rem, i.e. the width of the image (2rem) plus 0.5rem for the distance to the text container. Adjust all values as needed.
* {
box-sizing: border-box;
}
body {
margin: 0;
}
header {
display: flex;
justify-content: center;
align-items: center;
background-color: #c16200;
color: white;
position: fixed;
padding: 1rem;
top: 0;
width: 100%;
font-family: Calibri, 'Trebuchet MS', sans-serif;
}
.title-container {
text-align: center;
position: relative;
width: 200px;
}
.title-container .line {
border-top: 1px solid white;
width: 100%;
margin: 2px 0 4px;
}
.title-container img {
width: 2rem;
height: 2rem;
position: absolute;
right: -2.5rem;
top: 50%;
transform: translateY(-50%);
}
.title-container h1 {
font-size: 1.5rem;
margin: 0;
}
.title-container p {
margin: 0;
}
<header>
<div class="title-container">
<h1>TITLE</h1>
<div class="line"></div>
<p>SUBTITLE</p>
<img src="https://cdn.glitch.com/33ba966f-5c93-4fa3-969c-a216a9d7629c%2F167931478_735371457343939_8305934260393763828_n.png?v=1617205161517" class="logo" alt="logo plane" />
</div>
</header>

CSS - Add padding only if another div is visible

I have the following html..
.wrapper {
background:wheat;
}
.woocommerce-store-notice, p.demo_store {
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0;
width: 100%;
font-size: 1em;
padding: 1em 0;
text-align: center;
background-color: #a46497;
color: #fff;
z-index: 99998;
box-shadow: 0 1px 1em rgba(0,0,0,.2);
}
<div class="wrapper">
This is my content
</div>
<p class="woocommerce-store-notice demo_store" style="display: block;">DEMO NOTICE CLICK HERE Dismiss</p>
The notice is covering up my content, I know I can add some padding to fix this but how can I add it so that the padding only applies if the .woocommerce-store-notice is visible?
You may solve it using 1 line of jQuery.
In the code, $(".woocommerce-store-notice").outerHeight() gets the height of the notice and applies the value to margin-top of .wrapper
$(".wrapper").css({"margin-top":$(".woocommerce-store-notice").outerHeight()})
.wrapper {
background:wheat;
}
.woocommerce-store-notice, p.demo_store {
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0;
width: 100%;
font-size: 1em;
padding: 1em 0;
text-align: center;
background-color: #a46497;
color: #fff;
z-index: 99998;
box-shadow: 0 1px 1em rgba(0,0,0,.2);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="woocommerce-store-notice demo_store" style="display: block;">DEMO NOTICE CLICK HERE Dismiss</p>
<div class="wrapper">
This is my content
</div>
If you can't change the html markup, you can use flex to change the order of your selectors i.e. .woocommerce-store-notice, p.demo_store would be order: 0; and .wrapper would be order: 1; This way you don't have to depend on absolute positioning to make sure the notice appears at the top of your page.
Try the snippet below.
// dismiss the notice by clicking the "Dismiss" link"
document.querySelector('.woocommerce-store-notice__dismiss-link').addEventListener('click', function(e) {
e.preventDefault();
document.querySelector('.woocommerce-store-notice').style.display = 'none';
});
* {
margin: 0;
}
.flex {
display: flex;
flex-direction: column;
}
.wrapper {
background: wheat;
order: 1;
}
.woocommerce-store-notice,
p.demo_store {
order: 0;
font-size: 1em;
padding: 1em 0;
text-align: center;
background-color: #a46497;
color: #fff;
box-shadow: 0 1px 1em rgba(0, 0, 0, .2);
}
<div class="flex">
<div class="wrapper">
This is my content
</div>
<p class="woocommerce-store-notice demo_store">DEMO NOTICE CLICK HERE Dismiss</p>
</div>

Use before & after Pseudo-element to make a line

I'm using Pseudo-element :before and :after to draw a line before and after a title. It's working with an image:
.mydiv::before {
content: url(img/line.png);}
.mydiv::after {
content: url(img/line.png);}
Here is the result :
But, I would like the line to expand and fill in the whole div before and after the title, like this :
Is there a way to specify a percentage for the image for it to stretch? I try this, but it's not working :
.mydiv img {
width: 100%;
height: auto;
}
You don't need both :before and :after, either of the 2 will be enough and as you've been told, you don't need an image. See the approach below.
#header {
width: 100%;
height: 50px;
margin: 50px 0;
text-align: center;
font-size: 28px;
position: relative;
background-color: #57585C;
}
#header:after {
content: '';
width: 100%;
border-bottom: solid 1px #fff;
position: absolute;
left: 0;
top: 50%;
z-index: 1;
}
h3 {
background-color: #57585C; /* Same as the parents Background */
width: auto;
display: inline-block;
z-index: 3;
padding: 0 20px 0 20px;
color: white;
position: relative;
font-family: calibri;
font-weight: lighter;
margin: 0;
}
<div id="header">
<h3>Last Projects</h3>
</div>
In case you need <h3> title to have transparent background - you can use both :before and :after and display: flex
More about flex-grow you can read here https://developer.mozilla.org/en-US/docs/Web/CSS/flex.
body {
background: linear-gradient(0.25turn, #3f87a6, #000000, #f69d3c); /* example of custom background */
}
#header {
display: flex;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: center; /* making vertical centerign of all children */
}
#header::before, #header::after {
content: '';
flex: 1 1 auto; /* the first digint is 'flex-grow: 1', helps elemet to occupy all free space */
border-bottom: solid 1px #fff;
}
h3 {
flex: 0 1 auto; /* the first digint is flex-grow: 0 */
padding: 0 15px 0 15px;
color: #fff;
}
<div id="header">
<h3>Last Projects</h3>
</div>
<style>
.mydiv::before{
content: '';
position: absolute;
left: 0;
width: 100%;
height: 2px;
bottom: 1px;
background-color: black;
}
</style>
<div class="mydiv">About us</div>