How to override the universal selector *? - html

so I am trying to set all the margins and the paddings for the upper part to 0 then add padding and margin to the elements individually whenever I need to that's why I would like to override the color and the margin set by the universal selector * but it doesn't work, everything is staying deeppink and also the margin doesn't change, please help.
/*------------------------All Margin & Padding-------------------------*/
.upperPart {
margin: 0px;
padding: 0px;
background-color: darksalmon;
}
.upperPart * {
margin: 0px;
padding: 0px;
background-color: deeppink;
}
body {
margin: 0px;
padding: 0px;
}
/*-----------------------------------Colors-------------------------*/
header {
background-color: darkolivegreen;
}
h1,
h2,
h3 {
background-color: mediumspringgreen;
}
nav {
background-color: teal;
}
ul {
background-color: magenta;
}
li {
background-color: yellow;
margin: 5px;
}
li {
background-color: blue;
margin: 2px;
}
div {
background-color: brown;
}
section {
background-color: lightsteelblue;
}
nav ul {
list-style-type: none;
display: flex;
flex-direction: row;
}
<!----------------------------THE UPPPER PART------------------------------------------->
<div class="upperPart">
<!---------------------------HEADER-------------------------------------->
<header>
<h1>Untitled</h1>
<img src="" alt="logo" name="logo" />
<h3>description in form of a slogan</h3>
</header>
<!---------------------------NAVIGATION----------------------------------->
<nav>
<ul>
<li>Home</li>
<li>Shops and Products</li>
<li>Find Work/Worker</li>
<li>New Shops</li>
<li>Contact</li>
</ul>
</nav>
<!---------------------------SEARCH--------------------------------------->
<div>
<form method="post" action="searchAppPhp.php">
<label>Search </label>
<input type="text" id="search" size="50" />
<input type="submit" name="search"/>
</form>
</div>
</div>
<!----------------------------THE MIDDLE PART------------------------------------------->
<div class="middlePart">
<section>
<article>
</article>
</section>
</div>
<div class="downPart">
<footer>
</footer>
</div>

CSS Specificity.
Since you used .upperPart * to null styles instead of arguably-preferably using * {}.
And since we are not supposed to ever use !important (unless we really, really know what we're doing)...
you'll need to use a more specific Rule as override. I.e:
.upperPart header {
background-color: darkolivegreen;
}
Etc. for all your nulled .upperPart descendant Elements (*).
Suggestion:
Don't style TAGS directly (or keep it minimal, margins, font-size, paddings etc).
Style only Classes or IDs.
/* QuickReset */
* {
margin:0;
box-sizing: border-box;
padding: 0;
}
/* DEFAULT STYLES FOR TAGS */
h1,
h2,
h3 {
/* Keep it minimal */
}
nav {
/* Keep it minimal */
}
ul {
/* Keep it minimal */
list-style-type: none;
display: flex;
flex-direction: row;
}
li {
/* Keep it minimal */
}
/* STYLES FOR SPECIFIC CLASSES */
.upperPart {
background-color: darksalmon;
}
/* ...Etc... */
<div class="upperPart">
<header>
<h1>Untitled</h1>
<img src="" alt="logo" name="logo" />
<h3>description in form of a slogan</h3>
</header>
<nav>
<ul>
<li>Home</li>
<li>Shops and Products</li>
<li>Find Work/Worker</li>
<li>New Shops</li>
<li>Contact</li>
</ul>
</nav>
<div>
<form method="post" action="searchAppPhp.php">
<label>Search </label>
<input type="text" id="search" size="50" />
<input type="submit" name="search">
</form>
</div>
</div>
<div class="middlePart">
<section>
<article>
<p>ARTICLE HERE</p>
</article>
</section>
</div>
<div class="downPart">
<footer>
<p>FOOTER HERE</p>
</footer>
</div>

Related

Unexplainable vertical gap between my navbar and the div below it

There is currently an unexplainable gap between my nav element and the div element that's below it.
Using inspect element on said gap refers me to the body. Meaning that there's literally nothing in that gap. I've used Chrome developer tools to check the margin and padding of every element that exists in the html file already and from what I can see there really isn't anything that should be causing the gap.
Here's the code. Running the code snippet in a new window would be best:
/* Load required font faces */
#font-face {
font-family: Nunito;
src: url("../fonts/Nunito/Nunito-Regular.ttf") format('truetype');
font-weight: normal;
}
/* CSS for text */
p, a, h1, h2, h3 {
font-family: Nunito;
}
/* CSS Styles for body */
body {
margin: 0 !important;
}
/* CSS Styles for the Navbar */
nav {
height: 8vh;
padding: 0% 2% 0% 2%;
background-image: linear-gradient(to right, #00B9FF, #9316FF);
}
nav a {
color: white;
font-size: 1.5em;
text-decoration: none;
}
nav a:hover {
color: #adadad;
}
#nav-wrapper {
height: 100%;
display: flex;
align-items: center;
}
#nav-content {
width: 100%;
}
.nav-logo {
float: left;
}
#nav-navigation {
width: 20%;
float: right;
margin: 0;
}
.nav-button {
display: inline-block;
margin: 0% 2% 0% 2%;
}
/* CSS for the page header */
#page-header {
height: 9vh;
background-color: #F3F3F3;
}
<html>
<head>
<title>Webpage</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<!-- Navbar -->
<nav>
<div id="nav-wrapper" class="align-middle">
<div id="nav-content">
<!-- Company name on the left of navbar -->
<div class="nav-logo">
Company Name
</div>
<!-- Navigation buttons for the navbar -->
<ul id="nav-navigation">
<li class="nav-button">
Nav 1
</li>
<li class="nav-button">
Nav 2
</li>
<li class="nav-button">
Nav 3
</li>
<li class="nav-button">
Nav 4
</li>
<li class="nav-button">
Nav 5
</li>
</ul>
</div>
</div>
</nav>
<!-- Main content -->
<div id="page-header">
<div>
<div>
<p>About Company Name</p>
</div>
<div>
<p>Over here at company....</p>
</div>
</div>
</div>
</body>
</html>
Any help would be welcome. Thank you.
The "p" element in the "page-header" div is causing the gap. Elements may have a default margin or padding. You can reset all default margins and paddings at the beginning of your css file like this:
* {
margin: 0;
padding: 0;
}
or you can select that certain "p" element and remove its margin.
Your p element is getting a margin on it. that's what's causing your gap. If you set a new css rule of:
p {
margin-top:0;
}
or even:
p {
margin:0;
}
then it'll work as expected. At least - it works for me running your snippet - there may be other stuff on your page doing that, but it's worth a shot I think.
Always remember to reset the rules that browsers set by default. A typical reset is
* {
margin: 0,
padding: 0,
box-sizing: border-box,
}

CSS Div not centreing

I'm new to HTML and CSS and to improve my skills i've been trying to recreate differnt sites by myself. I'm currently trying to recreate the notion home page but i have an issue. my img div is not centring? This is probably an easy fix but if someone could help i'd apprectiate it :)
Here is my HTML:
#import url("https://fonts.googleapis.com/css2?family=Inter:wght#400;500;700&display=swap");
:root {
--primary-text: #111111;
--secondary-text: #91908f;
--background: #fffefc;
--btn-primary: #e16259;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: var(--b);
font-family: "Inter", sans-serif;
}
/* NAV */
.nav {
width: 100;
}
.logo {
max-width: 100%;
height: 55px;
margin-right: 70px;
margin: 15px 50px;
}
a,
a:link,
a:visited {
color: #111111;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.nav ul {
display: flex;
padding: 35px;
list-style: none;
}
.nav ul li {
padding-right: 20px;
text-decoration: none;
color: #91908f;
}
.nav {
float: right;
}
/* HERO */
.nav-img {
display: flex !important;
align-items: center;
justify-content: center;
}
<img class="logo" src="img/logo.png" alt="Notion Logo">
<div class="nav">
<ul>
<li>Product</li>
<li>Download</li>
<li>Resources</li>
<li>Pricing</li>
<li>Careers</li>
<li><span>|</span></li>
<li>Sign Up</li>
<li>Log In</li>
</ul>
</div>
<div class="hero">
<div class="hero-img">
<img src="img/hero.webp" alt="Illustration of 3 people using laptop computers on seperate desks with different expressions">
</div>
<h1>All-in-one workspace</h1>
<h4>One tool for your whole team. Write, plan, and get organized.</h4>
<form action="">
<input type="text" placeholder="Enter your email...">
<button class='btn-primary' type="submit">Sign Up</button>
</form>
<p>For teams & individuals - web, mobile, Mac, Windows</p>
</div>
..
There are different things you need to consider when learning to code, first of all, it's important to use the new html5 semantic element header where you can place your logo and nav
<header>
<img src="logo.png" alt="Notion Logo">
<nav>
Product
Download
Resources
Pricing |
Careers
Sign Up
Log In
</nav>
</header>
*note: a nav element doesn’t have to contain a list as mentioned in MDN, and you can style them easily. The img centering issue came from the header wrong styling .nav{float: right;} .nav{ width: 100} needs to have px, %, or vw example .nav{width: 100px;}, .nav{width: 60vw;}, .nav{width: 60%;} then you can easily center the other elements vertically like below
.center-vertically {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
<header>
<div class="header-sub">
<img class="logo" src="img/logo.png" alt="Notion Logo" />
<nav>
Product
Download
Resources
Pricing |
Careers
Sign Up
Log In
</nav>
</div>
</header>
<div class="center-vertically">
<div class="centered-item">
<img src="https://www.notion.so/cdn-cgi/image/f=auto,w=1080,q=100/front-static/pages/product/hero.png" alt="Illustration of 3 people using laptop computers on seperate desks with different expressions" />
</div>
<h1>All-in-one workspace</h1>
<h4>One tool for your whole team. Write, plan, and get organized.</h4>
<form action="">
<input type="text" placeholder="Enter your email..." />
<button class="btn-primary" type="submit">Sign Up</button>
</form>
<p>For teams & individuals - web, mobile, Mac, Windows</p>
<div class="centered-item">anything you want to add</div>
</div>
You can style your header, and there is an awesome resource to follow to learn properly Freecodecamp

Why adding 1px padding adding 10px in height of div [duplicate]

This question already has answers here:
Why does this CSS margin-top style not work?
(14 answers)
Does UL have default margin or padding [duplicate]
(2 answers)
Closed 2 years ago.
I am trying to learn CSS basics. Below is the HTML and CSS for which I am unable to understand something
* {
box-sizing: border-box;
}
body {
font-family: 'Montserrat', sans-serif;
margin: 0;
}
.section-title {
color: #2ddf5c;
}
main {
background-color: burlywood;
}
#product-overview {
background: #ff1b68;
width: 100%;
height: 528px;
padding: 10px;
}
.main-header {
background: #2ddf5c;
padding: 0px 16px;
}
<header class="main-header">
<div>
<a href="index.html">
uhost
</a>
</div>
<nav class="main-nav">
<ul class="main-nav__items">
<li class="main-nav__item">Packages</li>
<li class="main-nav__item">Customers</li>
<li class="main-nav__item">Start hosting</li>
</ul>
</nav>
</header>
<main>
<section id="product-overview">
<h1>Get the freedom you deserve</h1>
</section>
<section id="plans">
<h1>Choose your plan</h1>
</section>
</main>
We can see that there is a small gap between red and green area.
As per my understanding, this gap exists because both nav and main are block elements.
Right now, height of header element is 90px(approx.)
But if I am trying to update padding of main-header, then browser is removing blank area between header and main elements, and also updating the height of header to 106px
.main-header {
background: #2ddf5c;
padding: 1px 16px;
}
Please help me understand why this is happening.
This has nothing to do with block elements. What happens: The ul at the bottom of your header has a default margin, top and bottom. The margin-bottom goes out of the header and creates the white space (this is known as "collapsing margins").
If you manually set that margin to zero:
.main-nav ul {
margin-bottom: 0;
}
, the space disappears. But when you add a padding-bottom to the header (regardless which value), the (default) margin-bottom of the ul remains inside the header element, also causing the white space to disappear.
Here's the situation without the added padding, but with header's margin-bottom set to zero:
* {
box-sizing: border-box;
}
body {
font-family: 'Montserrat', sans-serif;
margin: 0;
}
.section-title {
color: #2ddf5c;
}
main {
background-color: burlywood;
}
#product-overview {
background: #ff1b68;
width: 100%;
height: 528px;
padding: 10px;
}
.main-header {
background: #2ddf5c;
padding: 0px 16px;
}
.main-nav ul {
margin-bottom: 0;
}
<header class="main-header">
<div>
<a href="index.html">
uhost
</a>
</div>
<nav class="main-nav">
<ul class="main-nav__items">
<li class="main-nav__item">Packages</li>
<li class="main-nav__item">Customers</li>
<li class="main-nav__item">Start hosting</li>
</ul>
</nav>
</header>
<main>
<section id="product-overview">
<h1>Get the freedom you deserve</h1>
</section>
<section id="plans">
<h1>Choose your plan</h1>
</section>
</main>
Well, I'm not sure If I understood you right,
you are bothered about the white gap between the two elements? (I don't have the Privilege to comment your question)
If so the reason of the white gap is because UL elements have "default" style that the browser gives them.
you need to change those default style settings by doing:
ul {
margin: 0;
}
Hope I got you right :)

html form submit in navigation bar

Need advice on html as i am not expert on this.
I need to display following form buttons on a navigation bar on top of a web page but they are coming on different lines. I want this to be responsive so for smaller screens they adjust on different lines but if enough area , should display on nav bar in a single line. Tried few css but was not able to get it right.
Suggestion....
<body class="body">
<header>
<nav>
<ul>
<li><form action="/create" ><button type="submit">Create</button></form></li>
<li><form action="/update" ><button type="submit">Update</button></form></li>
<li><form action="/delete" ><button type="submit">Delete</button></form></li>
</ul>
</nav>
</header>
<div>
</div>
</body>
You Add CSS like this
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<body class="body">
<header>
<nav>
<ul>
<li><form action="/create" ><button type="submit">Create</button></form></li>
<li><form action="/update" ><button type="submit">Update</button></form></li>
<li><form action="/delete" ><button type="submit">Delete</button></form></li>
</ul>
</nav>
</header>
<div>
</div>
</body>
You can use:
li, form { display: inline; }

my inline CSS under a box (segment) of my webpage is affecting other parts of the same page

I am using this software (Swiiit Website Builder) purchased by my company, im doing intranet page for my department (im a total learn on the job dude being tasked to do this) please help!
<style><!--
<html>
<head>
<style>
ul {
float: left;
width: 100%;
padding: 0;
margin: 0;
list-style-type: none;
}
a {
float: center;
width: 6em;
text-decoration: none;
color: white;
background-color: #000080;
padding: 0.6em 4.5em;
border-right: 1px solid white;
}
a:hover {
background-color: #0000cd;
}
li {
display: inline;
}
--></style>
the style will affect other area of the page; how do i edit it?
i tried to understand this Apply different css stylesheet for different parts of the same web page but im still cracking my head...
I have amended according to the recommendation (see below) and turn out great!!! Thanks!!
doesnt affect other parts of page now, but now the space between blocks of links are so wide apart when i launch the code... looks fine in preview mode though... please assist :)
.something ul {
float: left;
width: 100%;
padding: 0;
margin: 0;
list-style-type: none;
}
.something li {
display: inline;
}
.something a {
float: center;
width: 6em;
text-decoration: none;
color: white;
background-color: #000080;
padding: 0.6em 4.5em;
border-right: 1px solid green;
}
.something a:hover {
background-color: #0000cd;
}
<div class="something">
<ul>
<li>Organisation</li>
<li>XXX</li>
<li>Organisational Chart</li>
<li>Contact Us</li>
</ul>
</div>
<p><img style="display: block; text-align: center; vertical-align: top;" src="/wbn/slot/u3733/Org%20Chart.png" alt="" width="800" height="473" /></p>
Set class for every ul, a and li otherwise
<div class="something">
<ul>
<li>....</li>
<li>....</li>
</ul>
...
</div>
Give styles using the class name like,
.something ul { }
.something li { }
.something a { }
Set specific class or id to the elements like
text
You can style this a in your css by
#a{
style
}
Rather than adding styling to your elements, add them to a class or id
class definition:
html
<div class="myClassIsReusableToAllWithThisClass"></div>
css
.myClassIsReusableToAllWithThisClass{
/*add styling to all which have a class called 'myClassIsReusableToAllWithThisClass'*/
}
ID definition:
html
<div id="uniqueID"></div>
css
#uniqueID{
/*add styling to only one element*/
}
element definition: (what you're doing presently)
html
<div></div>
css
div{
/*add styling to all 'div' elements*/
}
As #James Donnelly has already mentioned, you should also remove these:
<!-- and -->
as these are 'comments' in html, and will render all within them void/ go unnoticed by your browser.
DEMO:
#myID {
color: red;
}
.myClass {
background: blue;
}
a {
font-weight: bold;
}
<div >I'm just a div</div>
<br/>
<div id="myID">I have a specific id</div>
<br/>
<div class="myClass">I have a specific class which is reusable to all with this class</div>
<br/>
<div class="myClass">I have the myClass class</div>
<br/>
<a>I'm an a tag with no class or id. But all 'a' tags will have this styling</a>