CSS Box Model

Example

Width and Height of an Element

In order to set the width and height of an element correctly in all browsers, you need to know how the box model works. The following <div> element will have a total width of 350px:
div {
  width: 320px;
  padding: 10px;
  border: 5px solid gray;
  margin: 0;
}
		
Here is the calculation:

			320px (width)
			+ 20px (left + right padding)
			+ 10px (left + right border)
			+ 0px (left + right margin)
			= 350px

Total element width = width + left padding + right padding + left border + right border + left margin + right margin

Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin
		

Example



CSS Margins

Example

		div {
		        border: 1px solid black;
  			margin-top: 100px;
  			margin-bottom: 100px;
  			margin-right: 150px;
  			margin-left: 80px;
		}

		<div>This div element has a top margin of 100px, a right margin of 150px,
			a bottom margin of 100px, and a left margin of 80px.</div>
		

Margin - Shorthand Property

CSS Borders

			border-width: 2px;
			border-style: dotted;
			border-color: red;

			or

			border: 2px dotted red;
		

CSS Padding

Example

		div {
		border: 1px solid black;
  		background-color: lightblue;
  		padding-top: 50px;
  		padding-right: 30px;
  		padding-bottom: 50px;
  		padding-left: 80px;
		}

		<div>This div element has a top padding of 50px, a right padding of 30px, 
			a bottom padding of 50px, and a left padding of 80px.</div>
		

Padding - Shorthand Property

CSS Layout - The display Property

Block-level Elements

A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can).
Examples of block-level elements:
			<div>
			<h1> - <h6>
			<p>
			<form>
		

Inline Elements

An inline element does not start on a new line and only takes up as much width as necessary.
Examples of inline elements:
			<span>
			<a>
			<img>
		

Override The Default Display Value

Example:

CSS Layout - float and clear

float

			/*In CSS file*/
			img {
  			float: right;
			}

			/*In HTML file */
			<p>In this example, the image will float to the right in the paragraph, 
			and the text in the paragraph will wrap around the image.</p>

			<p><img src="https://www.w3schools.com/css/pineapple.jpg" 
			alt="Pineapple" style="width:170px;height:170px;margin-left:15px;">
			Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, 
			nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim 
			ligula venenatis dolor. Maecenas nisl est, ultrices neccongue eget, auctor 
			vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, 
			facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus 
			interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper 
			ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. 
			Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non
			fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis 
			diam velit.</p>
		

clear

The clear property is used to stop elements from wrapping around a floated element. When we use the float property, and we want the next element below (not on right or left), we will have to use the clear property.

CSS Layout - The position Property

CSS Navigation Bar

CSS Dropdown Navbar

Create a Dropdown Menu inside a Navigation Bar.
		CSS:
		ul {
			  list-style-type: none;
			  margin: 0;
			  padding: 0;
			  overflow: hidden; /* Prevents content from overflowing */
			  background-color: #333;
		}

		li {
		  float: left; /* Aligns list items horizontally */
		}

		li a, .dropbtn {
		  display: inline-block;
		  color: white;
		  text-align: center;
		  padding: 14px 16px;
		  text-decoration: none;
		}

		li a:hover, .dropdown:hover .dropbtn {
		  background-color: red;
		}
		
		li.dropdown {
		  display: inline-block;
		}

		.dropdown-content {
		  display: none; /* Hides dropdown content initially */
		  position: absolute; /* Positions dropdown content below the button */
		  background-color: #f9f9f9;
		  min-width: 160px;
		  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
		  z-index: 1; /* Ensures dropdown appears above other elements */
		}

		.dropdown-content a {
		  color: black;
		  padding: 12px 16px;
		  text-decoration: none;
		  display: block;
		  text-align: left;
		}

		.dropdown-content a:hover {background-color: #f1f1f1;}
		
		.dropdown:hover .dropdown-content {
		  display: block;
		}

		HTML:
		<ul>
			  <li><a href="#home">Home</a></li>
			  <li><a href="#news">News</a></li>
			  <li class="dropdown">
				    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
				    <div class="dropdown-content">
				      <a href="#">Link 1</a>
				      <a href="#">Link 2</a>
				      <a href="#">Link 3</a>
				    </div>
			  </li>
		</ul>
		<!--javascript:void(0) is commonly used to prevent a link from navigating 
			to another page or refreshing the page when clicked. -->
		

Reference