CSS – Justify Items

CSS – justify-items Property ”; Previous Next CSS justify-items property provides a default alignment along the relevant axis for each box by setting the default justify-self for all box items. This property”s effect varies based on the layout mode in use: It aligns items in block-level layouts along the inline axis within their containing block. When elements are positioned absolutely, the items within the containing block are aligned on the inline axis, considering the specified top, left, bottom, and right offset values. This property is ignored in table cell layouts . This property is ignored in Flexbox layouts. Aligns the items inside the grid areas on the inline axis in grid layouts. Possible Values normal − This keyword”s effect is determined on the layout mode: Same as start in block-level layouts. Absolute positioning uses this keyword as a ”start” for replaced boxes and a ”stretch” for other absolute-positioned boxes. It has no meaning in table cell layouts because its property is ignored. It has no meaning in flexbox layouts because its property is ignored. This term acts as a ”stretch” for grid items, except boxes with an aspect ratio or intrinsic sizes, which act as a ”start”. start − Aligns the items at the start edge of the alignment container in the corresponding axis. end − Aligns the items at the end edge of the alignment container in the corresponding axis. center − Aligns the items at the centre of the alignment container. flex-start − This value is considered as start, by items that are not children of a flex container. flex-end − This value is considered as end, by items that are not children of a flex container. self-start − The items are aligned to the start edge of the alignment container in the appropriate axis. self-end − The items are aligned to the end edge of the alignment container in the appropriate axis. left − The items are aligned to the left edge of the alignment container. This value acts like start if the property”s axis is not parallel to the inline axis. right − The items are aligned to the right edge of the alignment container in the appropriate axis. This value acts like start if the property”s axis is not parallel to the inline axis. baseline, first baseline, last baseline − Defines alignment with the first or last baseline of a box in its baseline-sharing group, aligns the box”s first or last baseline set with the appropriate baseline, with start as the fallback for the first baseline and end for the last baseline. stretch − When the aggregate size of the items is less than the alignment container, auto-sized items are evenly enlarged, according to max-height/max-width limitations, the combined size fills the alignment container. safe − In case the size of the item overflows the alignment container, the alignment mode of the item is set as “start”. unsafe − The specified alignment value is honored regardless of the relative sizes of the item and alignment container. legacy − This value is inherited by box descendants. However, if a descendant has justify-self: auto, only left, right, or centre values are considered, not the legacy keyword. Applies To All elements. Syntax This property can take any of four forms: Basic keyword: The keyword values are normal or stretch. Baseline alignment: The baseline keyword with an additional first or last. Positional alignment: Choose from center, start, end, flex-start, flex-end, self-start, self-end, left, or right. also optionally unsafe or safe. Legacy alignment: Use the legacy keyword with left or right. Basic Keywords justify-items: normal; justify-items: stretch; Positional Alignment justify-items: center; justify-items: start; justify-items: end; justify-items: flex-start; justify-items: flex-end; justify-items: self-start; justify-items: self-end; justify-items: left; justify-items: right; Baseline Alignment justify-items: baseline; justify-items: first baseline; justify-items: last baseline; Overflow Alignment (for positional alignment only) justify-items: safe center; justify-items: unsafe center; Legacy Alignment justify-items: legacy right; justify-items: legacy left; justify-items: legacy center; CSS justify-items – normal Value The following example demonstrates the justify-items: normal property that aligns the items along the row axis within each grid cell with its default behavior − <html> <head> <style> .box { width: 100%; border: 2px solid black; display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 40px; grid-gap: 10px; justify-items: normal; background-color: greenyellow; } .box > div { width: 100px; border: 2px solid blue; background-color: lightcoral; margin: 5px; text-align: center; } </style> </head> <body> <div class=”box”> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> <div>Item 4</div> </div> </body> </html> CSS justify-items – stretch Value The following example demonstrates the justify-items: stretch property that stretches the items along the row axis to fill the entire width of their columns − <html> <head> <style> .box { width: 100%; border: 2px solid black; display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 40px; grid-gap: 10px; justify-items: stretch; background-color: greenyellow; } .box > div { border: 2px solid blue; background-color: lightcoral; margin: 5px; text-align: center; } </style> </head> <body> <div class=”box”> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> <div>Item 4</div> </div> </body> </html> CSS justify-items – start Value The following example demonstrates the justify-items: start property that aligns items against the start edge of the grid container − <html> <head> <style> .box { width: 100%; border: 2px solid black; display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 40px; grid-gap: 10px; justify-items: start; background-color: greenyellow; } .box > div { width: 100px; border: 2px solid blue; background-color: lightcoral; margin: 5px; text-align: center; } </style> </head> <body> <div class=”box”> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div>

CSS – Place Items

CSS – place-items Property ”; Previous Next CSS place-items is a shorthand property used in CSS Grid Layout to set both the align-items and justify-items properties in a single declaration. It allows you to align and justify grid items within the grid container along both the block (column) and inline (row) axes simultaneously This property is a shorthand for the following CSS properties: align-items justify-items Possible Values A single align-items value aligns in both the block and inline directions. An align-items value sets the block direction alignment, followed by justify-items, which specifies inline alignment. Applies To All elements. Syntax Keyword Values place-items: center; place-items: normal start; Positional Alignment place-items: center normal; place-items: start legacy; place-items: end normal; place-items: self-start legacy; place-items: self-end normal; place-items: flex-start legacy; place-items: flex-end normal; Baseline Alignment place-items: baseline normal; place-items: first baseline legacy; place-items: last baseline normal; place-items: stretch legacy; CSS place-items – Placing Items in a Grid Container The following example demonstrates the different behavior of the place-items property in the grid layout − <html> <head> <style> div > div { box-sizing: border-box; border: 2px solid blue; } .row { margin-bottom: 20px; } select { padding: 2px; background-color: yellow; border-radius: 10px; color: blue; } #grid-container { height: 400px; width: 350px; place-items: start; background-color: red; display: grid; grid-template-columns: repeat(3, 100px); } #grid-container > div { width: 60px; min-height: 60px; padding: 5px; margin: 5px; } .gridItem1 { background-color: greenyellow; } .gridItem2 { background-color: violet; } </style> </head> <body> <div class=”row”> <label for=”place-items-values”>place-items: </label> <select id=”place-items-values”> <option value=”start”>start</option> <option value=”center”>center</option> <option value=”end”>end</option> <option value=”stretch”>stretch</option> <option value=”center normal”>center normal</option> <option value=”normal start”>normal start</option> <option value=”center normal”>center normal</option> <option value=”start legacy”>start legacy</option> <option value=”end normal”>end normal</option> <option value=”self-start legacy”>self-start legacy</option> <option value=”self-end normal”>self-end normal</option> <option value=”flex-start legacy”>flex-start legacy</option> <option value=”flex-end normal”>flex-end normal</option> <option value=”baseline”>baseline</option> <option value=”first baseline legacy”>first baseline legacy</option> <option value=”last baseline”>last baseline</option> <option value=”stretch legacy”>stretch legacy</option> </select> </div> <div id=”grid-container”> <div class=”gridItem1″>Grid Item 1</div> <div class=”gridItem2″>Grid Item 2</div> <div class=”gridItem1″>Grid Item 3</div> <div class=”gridItem2″>Grid Item 4</div> <div class=”gridItem1″>Grid Item 5</div> </div> <script> const values = document.getElementById(“place-items-values”); const container = document.getElementById(“grid-container”); values.addEventListener(“change”, () => { container.style.placeItems = values.value; }); </script> </body> </html> CSS place-items – Placing Items in a Flex Container The following example demonstrates the different behavior of the place-items property in the flex layout − <html> <head> <style> div > div { box-sizing: border-box; border: 2px solid blue; display: flex; } .row { margin-bottom: 20px; } select { padding: 2px; background-color: yellow; border-radius: 10px; color: blue; } #flex-container { height: 350px; width: 350px; align-items: start; background-color: red; display: flex; flex-wrap: wrap; } #flex-container > div { width: 60px; min-height: 60px; padding: 5px; margin: 5px; } .flexItem1 { background-color: greenyellow; } .flexItem2 { background-color: violet; } </style> </head> <body> <div class=”row”> <label for=”place-items-values”>place-items: </label> <select id=”place-items-values”> <option value=”start”>start</option> <option value=”center”>center</option> <option value=”end”>end</option> <option value=”stretch”>stretch</option> <option value=”center normal”>center normal</option> <option value=”normal start”>normal start</option> <option value=”center normal”>center normal</option> <option value=”start legacy”>start legacy</option> <option value=”end normal”>end normal</option> <option value=”self-start legacy”>self-start legacy</option> <option value=”self-end normal”>self-end normal</option> <option value=”flex-start legacy”>flex-start legacy</option> <option value=”flex-end normal”>flex-end normal</option> <option value=”baseline”>baseline</option> <option value=”first baseline legacy”>first baseline legacy</option> <option value=”last baseline”>last baseline</option> <option value=”stretch legacy”>stretch legacy</option> </select> </div> <div id=”flex-container”> <div class=”flexItem1″>Flex Item 1</div> <div class=”flexItem2″>Flex Item 2</div> <div class=”flexItem1″>Flex Item 3</div> <div class=”flexItem2″>Flex Item 4</div> <div class=”flexItem1″>Flex Item 5</div> <div class=”flexItem2″>Flex Item 6</div> <div class=”flexItem1″>Flex Item 7</div> </div> <script> const values = document.getElementById(“place-items-values”); const container = document.getElementById(“flex-container”); values.addEventListener(“change”, () => { container.style.placeItems = values.value; }); </script> </body> </html> Print Page Previous Next Advertisements ”;

CSS – min-content

CSS – min-content Property ”; Previous Next The CSS min-content is a value that can be used for sizing properties to specify the minimum size of a box or element based on its content. It specifically suggests that text content will make use of soft-wrapping opportunities, which enable the material to resize to the length of its longest word. The min-content value is calculated based on the intrinsic minimum size required by the content inside the box. Syntax /* Used as a length */ width: min-content; inline-size: min-content; height: min-content; block-size: min-content; /* Used in grid tracks */ grid-template-columns: 100px 2fr min-content; CSS min-content – Box Sizing The following example demonstrates the use of min-content keyword for box sizing. <html> <head> <style> body { background-color: #b9cded; font-family: ”Helvetica Neue”, Arial, sans-serif; margin: 0; padding: 20px; } .container { width: 100%; display: flex; flex-direction: column; align-items: flex-start; } .item { width: min-content; background-color: #ffffff; padding: 5px; margin-bottom: 20px; border: 1px solid #e0e0e0; box-shadow: 0 3px 6px rgba(0, 0, 0, 1); color: #444444; border-radius: 8px; } </style> </head> <body> <div class=”container”> <div class=”item”>Min-Content Example.</div> <div class=”item”>Well-Crafted Min-Content Example.</div> <div class=”item”>Thoughtfully Presented Min-Content Example.</div> </div> </body> </html> CSS min-content – Sizing Grid Columns The following example demonstrates the use of min-content keyword for sizing grid columns. <html> <head> <style> body { margin: 0; font-family: ”Arial”, sans-serif; background-color: #f9f9f9; } #container { display: grid; grid-template-columns: 1fr min-content min-content; grid-gap: 15px; box-sizing: border-box; max-width: 800px; margin: 20px auto; padding: 20px; background-color: #f2f2f2; border: 1px solid #82807f; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5); border-radius: 8px; } .item { background-color: #5f9ea0; color: #141414; padding: 15px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5); } </style> </head> <body> <div id=”container”> <div class=”item”>Flexible Content Area</div> <div class=”item”>Min-Content 1 Content Area</div> <div class=”item”>Min-Content 2 Content Area</div> <div class=”item”>Dynamic Content Area</div> <div class=”item”>Min-Content 3 Content Area</div> <div class=”item”>Min-Content 4 Content Area</div> </div> </body> </html> Print Page Previous Next Advertisements ”;

CSS – All

CSS – all ”; Previous Next The shorthand CSS property all resets all properties of an element, with the exception of unicode bidi, direction and CSS custom properties. It can reset properties to their original or inherited values or to the values explicitly defined in another cascade layer or in the stylesheet origin. Constituent Properties This property serves as a concise representation for all CSS properties, with the exception of unicode bidi, direction and CSS custom properties. The all property is specified with one of the global CSS keyword values. It is important to note that none of these values impact the unicode bidi and direction properties. Possible values The following is the list of possible values of all properties. initial – Indicates that all properties of the element should be reset to their initial values. inherit – Indicates that all properties of the element should be set to their inherited values. unset – Indicates that the element”s properties should set to inherited values in case of default inheritance, else to their initial values. revert – Specifies the behavior depending on the stylesheet origin associated with the declaration: If the rule is linked to the author”s origin, the revert value reverts the cascade to the user level and recalculates the specified values as if no author-level rules were applied to the element. In the context of revert, the author origin includes the override and animation origins. If the rule is part of the user origin, the value revert resets the cascade to the user agent level. This means that the specified values are calculated as if no rules were defined at author or user level for the element. If the rule originates from the user agent origin, the value revert behaves similarly to unset. revert-layer – Specifies the rollback of all properties of the element to a previous cascade layer, if available. This is still in experimental phase. If no other cascade layer is available, the properties of the element are reset to the matching rule in the current layer, if available, or to an earlier style origin. Syntax all = initial | inherit | unset | revert | revert-layer CSS all – Basic Example In the following example, the CSS all property is used to completely adjust all styling properties within specific elements. The first <div> with id=custom1 showcases the default styling without the all property, while subsequent <div> elements (custom2, custom3, and custom4) demonstrate the impact of all: inherit;, all: initial;, and all: unset; respectively. <html> <head> <style> html { font-size: x-large; color: #2c3e50; } #custom1 { background-color: #ecf0f1; color: #e74c3c; font-family: ”Verdana”, sans-serif; font-weight: bold; } #custom2 { background-color: #ecf0f1; color: #e74c3c; font-family: ”Verdana”, sans-serif; font-weight: bold; all: inherit; } #custom3 { background-color: #ecf0f1; color: #e74c3c; font-family: ”Verdana”, sans-serif; font-weight: bold; all: initial; } #custom4 { background-color: #ecf0f1; color: #e74c3c; font-family: ”Verdana”, sans-serif; font-weight: bold; all: unset; } </style> </head> <body> <p>No all property:</p> <div id=”custom1″>Hello from a creative and innovative universe!</div> <p>all: inherit:</p> <div id=”custom2″>Discover the virtually endless possibilities in your head.</div> <p>all: initial:</p> <div id=”custom3″>Welcome the start of an interesting new trip.</div> <p>all: unset:</p> <div id=”custom4″>Use the power of new ideas to realize your full potential.</div> </body> </html> Print Page Previous Next Advertisements ”;

CSS – Combinators

CSS – Combinators ”; Previous Next In CSS (Cascading Style Sheets), combinators are symbols or characters that specify the relationship between different HTML elements that you want to style. Combinators help you target elements based on their position and hierarchy within the HTML document. These combinators allow you to apply styles selectively based on the relationships between elements in your HTML structure, making your CSS more specific and targeted to achieve the desired styling effects. There are four main types of CSS combinators: Descendant Combinator (space) Child Combinator (>) Adjacent sibling Combinator (+) General sibling Combinator (~) CSS Combinators – Descendant Combinator (space) The descendant combinator, often represented by a single space (” “), selects elements that are descendants of specified element. If an element matches the second selector(descendant) and has an ancestor (parent, parent”s parent, etc.) that matches the first selector, then that element will be selected. For example, if you have an HTML structure like this: <div> <p>Paragraph 1</p> <p>Paragraph 2</p> </div> You can target all <p> elements inside the <div> like this: div p { /* CSS styles */ } The following code demonstrates the usage of the descendant combinator. In this example, we have a parent element <div> with the class name parent. Inside this parent element, we have multiple <p> elements. We will apply a some styles to the child elements with the class name child. <html> <head> <title>Descendant Combinator Example</title> <style> .parent { background-color: lightblue; padding: 20px; } .parent p { color: red; font-weight: bold; } </style> </head> <body> <div class=”parent”> <h1>This is the parent element</h1> <p>This is a paragraph inside the parent element.</p> <p>This is a paragraph inside the parent element.</p> </div> <div> <p>This is a paragraph outside the parent element.</p> </div> </body> </html> CSS Combinators – Using List The following example demonstrates the usage of the descendant combinator for list elements. <html> <head> <title>Descendant Combinator Example</title> <style> div ul { background-color: lightblue; padding: 10px; } div ul li { color: white; margin-bottom: 5px; } </style> </head> <body> <div> <h2>Fruits</h2> <ul> <li>Apple</li> <li>Banana</li> <li>Orange</li> </ul> </div> </body> </html> CSS Combinators – Child Combinator (>) The child combinator, represented by the greater-than symbol (>), selects all elements that are immediate children of a specified element. Using the same HTML structure as above, you can target only the immediate child <p> elements like this: div > p { /* CSS styles */ } The following example demonstrates the usage of the child selector(>). In this example, we have a parent element <div> with the class name parent. Inside the parent element, we have multiple <p> elements. We want to apply a specific style to the child elements with the class name child. <html> <head> <title>Child Combinator Example</title> <style> .parent > .child { color: blue; } </style> </head> <body> <div class=”parent”> <p>This is the parent element.</p> <p class=”child”>This is the first child element.</p> <p class=”child”>This is the second child element.</p> <p>This is the third child element.</p> <p>This is the fourth child element.</p> </div> <div> <p>This is another paragraph.</p> </div> </body> </html> The following example demonstrates how the child combinator selector can be used to target specific elements that are direct children of a parent element, allowing for more precise styling and customization. <html> <head> <title>Child Combinator Example</title> <style> div > span { color: red; } ul > li { font-weight: bold; } </style> </head> <body> <div> <span>This text will be red.</span> <span>This text will not be affected.</span> </div> <ul> <li>This list item will have bold font-weight.</li> <li>This list item will also have bold font-weight.</li> </ul> </body> </html> CSS Combinators – Adjacent sibling Combinator (+) The adjacent sibling combinator, represented by the plus sign (+), selects an element that is immediately preceded by a specified element. For example, if you have the following HTML: <h2>Heading 1</h2> <p>Paragraph 1</p> <h2>Heading 2</h2> <p>Paragraph 2</p> You can target the <p> element that directly follows a <h2> like this: h2 + p { /* CSS styles */ } The following code demonstrates the usage of the adjacent sibling selector(+). In this example we are targeting the paragraphs that directly follow <h2> elements. <html> <head> <style> h2 + p { color: red; } </style> </head> <body> <div> <h2>Example of Adjacent Sibling Combinator</h2> <p>This is the first paragraph.</p> <p>This is the second paragraph.</p> </div> <div> <p>This is the third paragraph.</p> <p>This is the fourth paragraph.</p> </div> </body> </html> Here is another example demonstrating the usage of the adjacent sibling selector(+). div + span: This selects the span element that immediately follows a div element. It sets the color of the span text to red. span + button: This selects the button element that immediately follows a span element. It sets the background color of the button to yellow. button + ul: This selects the ul element that immediately follows a button element. It changes the list-style-type of the unordered list to square bullets. <html> <head> <style> div + span { color: red; } span + button { background-color: yellow; } button + ul { list-style-type: square; } </style> </head> <body> <div>This is a div element.</div> <span>This is a span element.</span> <button>This is a button element.</button> <ul> <li>List item 1</li> <li>List item 2</li> <li>List item 3</li> </ul> </body> </html> CSS Combinators – General sibling Combinator (~) TThe general sibling combinator, represented by the tilde symbol (~), selects all elements that are siblings of a specified element. It”s similar to the adjacent sibling combinator but doesn”t require elements to be immediately adjacent. For instance: <h2>Heading 1</h2> <p>Paragraph 1</p> <h2>Heading 2</h2> <p>Paragraph 2</p> You can target all

CSS – Order

CSS – Order ”; Previous Next What is an order? CSS order property is used to specify the order in which flex items appear within a flex container. The order of the flex items is determined by the values of their order property. The flex items with the lower order value will be displayed first. Here are some additional things to keep in mind: The order property is not inherited by child elements. The order property only affects flex items. The default value of the order property is 0. Following are all possible values that can be used for property order: integer: Represents the ordinal group to be used by the item. inherit: Uses the same value of its parent. initial: The element uses default value i.e 0. CSS Order Integer The CSS order property is set to an integer value. The value of the order property can be any positive or negative integer. We can use order property with positive integer value. A positive value means that the item with the highest positive order value will be displayed last. Example Here is an example − <html> <head> <style> .my_flex_container { display: flex; background-color: #0ca14a; height: 90px; } .my_flex_container div { background-color: #FBFF22; padding: 10px; margin: 10px; height: 50px; text-align: center; } </style> </head> <body> <div class=”my_flex_container”> <div style=”order: 2″>Item 1</div> <div style=”order: 6″>Item 2</div> <div style=”order: 4″>Item 3</div> <div style=”order: 1″>Item 4</div> <div style=”order: 3″>Item 5</div> <div style=”order: 5″>Item 6</div> </div> </body> </html> The CSS order property can be set to a negative integer value. A negative value means that the items with the highest negative order value will be displayed first. Example Here is an example − <html> <head> <style> .my_flex_container { display: flex; background-color: #0ca14a; height: 90px; } .my_flex_container div { background-color: #FBFF22; padding: 10px; margin: 10px; height: 50px; text-align: center; } </style> </head> <body> <div class=”my_flex_container”> <div style=”order: 4″>Item 1</div> <div style=”order: 6″>Item 2</div> <div style=”order: -3″>Item 3</div> <div style=”order: 1″>Item 4</div> <div style=”order: -5″>Item 5</div> <div style=”order: 2″>Item 6</div> </div> </body> </html> CSS Order Initial The order: initial value simply sets the order property of the item back to its initial value, which is usually 0. Example Here is an example − <html> <head> <style> .my_flex_container { display: flex; background-color: #0ca14a; height: 90px; } .my_flex_container div { background-color: #FBFF22; padding: 10px; margin: 10px; height: 50px; text-align: center; } </style> </head> <body> <div class=”my_flex_container”> <div style=”order: 5″>Item 1</div> <div style=”order: 3″>Item 2</div> <div style=”order: 1″>Item 3</div> <div style=”order: 6″>Item 4</div> <div style=”order: initial”>Item 5</div> <div style=”order: 4″>Item 6</div> </div> </body> </html> CSS Order Unset If you set the order property to unset value, the flex item will be displayed in it”s default orde based on the HTML markup. Example Here is an example where order property is set to unset to the flex item 3. Then order of the flex item 3 will be displayed in the default order − <html> <head> <style> .my_flex_container { display: flex; background-color: #0ca14a; height: 90px; } .my_flex_container div { background-color: #FBFF22; padding: 10px; margin: 10px; height: 50px; text-align: center; } </style> </head> <body> <div class=”my_flex_container”> <div style=”order: 2″>Item 1</div> <div style=”order: 4″>Item 2</div> <div style=”order: unset”>Item 3</div> <div style=”order: 1″>Item 4</div> <div style=”order: 3″>Item 5</div> <div style=”order: 5″>Item 6</div> </div> </body> </html> CSS Order Revert When we set the order property to the revert value, the flex item will be displayed in the order it appears in the HTML markup, but with the order reversed. Example Here is an example where Order property is set to revert for the fifth flex-item. Then order of the fifth flex-item will be reversed, so it will be displayed first − <!DOCTYPE html> <html> <head> <style> .my_flex_container { display: flex; background-color: #0ca14a; height: 90px; } .my_flex_container div { background-color: #FBFF22; padding: 10px; margin: 10px; height: 50px; text-align: center; } </style> </head> <body> <div class=”my_flex_container”> <div style=”order: 5″>Item 1</div> <div style=”order: 3″>Item 2</div> <div style=”order: 1″>Item 3</div> <div style=”order: 6″>Item 4</div> <div style=”order: revert”>Item 5</div> <div style=”order: 4″>Item 6</div> </div> </body> </html> Print Page Previous Next Advertisements ”;

CSS – Box Model

CSS – Box Model ”; Previous Next CSS Box Model is a fundamental concept in CSS (Cascading Style Sheets) that define how an elements on a web page will be structured and displayed. It defines the properties and behavior of the content, padding, borders, and margins of an element. Here in this article you will learn all about the CSS Box Model. What is CSS box model? The CSS box model is a container that used to structure the elements in a webpage so the element can be displayed visually good. It consists of four essential components content, padding, border, and margin, as shown in the following diagram. CSS Box Model Components Lets understand each components in detail. Content: This is the innermost part of the box and refers to the actual content of an element, such as text, images, or other media. You can set its size using the properties such as inline size and block-size, also known as width and height. Padding: Represents the space between the content and the element”s border. It can be applied separately to each side of the element (top, right, bottom, and left). The size of this box is set using padding and other related properties. Border: Defines a line or boundary around the padding and content of an element. The size, style and color of this box is set using border and other related properties. Margin: Represents the space outside the border of an element. Like padding, margins can also be set separately for each side and are typically used to create space between elements on a webpage. The size of this box is set using margin and other related properties. The total space of an element occupies on the web page is the sum of its content width, padding, border, and margin. Understanding the CSS Box Model is crucial for designing and positioning elements on a webpage, as it allows you to control spacing, layout, and overall design. Types of Box-Model There are two types of box models as listed below. Standard Box Model (Content-Box) Alternative Box Model (Border-Box) Browsers use the standard box model, by default. Let us look into both the types of box models in the following sections. Standard CSS Box Model In standard box-model, height and width properties include only the content area of element. Padding, borders, and margins are added outside the content area. Consider the following styling box. Let us calculate the actual space taken by the box: .box { width: 300px; height: 100px; margin: 20px; padding: 15px; border: 5px solid green; } Standard Box Model Dimension Calculation The box area is only upto the margin, and thus margin area does not add up to the final space taken by the box. Total width = 300(width) + 15(left padding) + 15(right padding) + 5(left border) + 5(right border) = 340px Total height = 100(height) + 15(top padding) + 15(bottom padding) + 5(top border) + 5(bottom border) = 140px Standard Box-Model Example The following example shows how to define a standard box model in HTML. Here we given the default value `content-box` for `box-sizing` property. Here we can notice that width of padding and border is included in offsetWidth of the element. <!DOCTYPE html> <html lang=”en”> <head> <style> .box { width: 300px; height: 100px; padding: 20px; border: 10px solid black; margin: 30px; /* This is the default value */ box-sizing: content-box; } </style> </head> <body> <div class=”box” id=”myBox”> Total Width = 300 (content) + 20 (padding left) + 20 (padding right) + 10 (border left) + 10(border right) = 360 px </div> <p id=”dis”></p> <script> // Get the div element var box = document.getElementById(”myBox”); // Get total width of element var totalWidth = box.offsetWidth; document.getElementById(”dis”).innerText = ”Width of the div using offsetWidth property: ” + totalWidth + ”px”; </script> </body> </html> Alternative Box Model In case of an alternative box model, the actual width of an element is the value of width that is passed to it and same is the case with height. There is no need of adding the padding and border while calculating the actual size of the box. In order to enable or turn on the alternative box model for an element, you need to set box-sizing: border-box on it. .box { box-sizing: border-box; } Let us consider the same dimensions of the box, as mentioned in standard box model example and calculate the actual space taken by the box: .box { width: 300px; height: 100px; margin: 20px; padding: 15px; border: 5px solid green; box-sizing: border-box; } Alternate Box Model Dimension Calculation The space taken by the box having dimensions listed up will be. Total width = width = 300px Total height = height = 100px Alternative Box-Model Example Here is an example alternative box models. Here we changed `box-sizing` property to `border-box`, hence total width of element not include width of border and padding. <!DOCTYPE html> <html lang=”en”> <head> <style> .box { width: 300px; height: 150px; padding: 20px; border: 10px solid black; margin: 30px; /* Change this property to see difference */ box-sizing: border-box; } </style> </head> <body> <div class=”box” id=”myBox”> Total Width = 300 px <br> Total Height = 150 px </div> <p id=”dis”></p> <script> // Get the div element var box = document.getElementById(”myBox”); // Get the total width of element var totalWidth = box.offsetWidth; document.getElementById(”dis”).innerText = ”Total width of div using offsetWidth property: ” + totalWidth + ”px”; </script> </body> </html> Significance of Box-Model Visual Representation: The box model allows developers to visualize and

CSS – Counters

CSS – Counters ”; Previous Next In CSS, counters act as variables that are used for numbering purposes. They can be increased or decreased by css rules. Css counters enable us to modify the presentation of content depending on its position. For instance you can use the counters to automatically assign numbers to paragraphs, headings and lists. CSS Counters – Nesting Counters We can use the counters() function along with the counter-reset and counter-increment properties to create outlined lists with nested counters. This technique allows you to automatically generate counters for different levels of nesting. This following example creates a nested structure using the <ol> element to demonstrate the nesting of counters. <html> <head> <title>Nesting of Counter</title> <style> ol { counter-reset: section; list-style-type: none; } li::before { counter-increment: section; content: counters(section, “.”) ” “; } </style> </head> <body> <ol> <li>Section 1 <ol> <li>Subsection 1.1</li> <li>Subsection 1.2</li> <li>Subsection 1.3</li> </ol> </li> <li>Section 2 <ol> <li>Subsection 2.1</li> <li>Subsection 2.2</li> <li>Subsection 2.3</li> </ol> </li> <li>Section 3 <ol> <li>Subsection 3.1</li> <li>Subsection 3.2</li> <li>Subsection 3.3</li> </ol> </li> </ol> </body> </html> CSS Counters – Reversed counter A reversed counter is a special kind of counter that counts backwards instead of forward. To create a reversed counter, name it with the reversed() function when you set it up with counter-reset. The reversed counters start with a default initial value equal to the number of elements, not zero. This means that it can simply count down from the number of elements to one. Syntax counter-reset: reversed(counter name); The reversed counter property is supported only by Firefox browser The following example demonstrates reversed counter (Execute this example in Firefox browser). <html> <head> <style> body { counter-reset: reversed( section); } p::before { counter-increment: section -1; content: “Section ” counter(section) “: “; } </style> </head> <body> <p>This is fourth paragraph</p> <p>This is Third paragraph</p> <p>This is second paragraph</p> <p>This is first paragraph</p> </body> </html> The name of the counter should not be none, inherit or initial. If this is the case, the declaration is neglected. CSS Counter – Related Properties Following is the list of CSS properties of counter: property value counter-reset It is used to create or reset a counter. counter-set It is used to set a counter to given value. counter-increment It is used to increment the counter value. counter() It provides a string that represents the current value of named counter. counters() It is used to work with nested counters. @counter-styles It is used to create custom counter styles. contain Determines how the content of an element behaves when it is too wide to fit inside its container. Print Page Previous Next Advertisements ”;

CSS – Outlines

CSS – Outlines ”; Previous Next Outlines are very similar to borders, but there are few major differences as well − An outline does not take up space. Outlines do not have to be rectangular. Outline is always the same on all sides. You cannot specify different values for outline on different sides of an element. NOTE − The outline properties are not supported by IE 6 or Netscape 7. You can set the following outline properties using CSS. The outline-width property is used to set the width of the outline. The outline-style property is used to set the line style for the outline. The outline-color property is used to set the color of the outline. The outline-offset property is used to set the space between the outline and border edge of ab element. The outline property is used to set all the above three properties in a single statement. The outline-width Property The outline-width property specifies the width of the outline to be added to the box. Its value should be a length or one of the values thin, medium, or thick, just like the border-width attribute. A width of zero pixels means no outline. Here is an example − <html> <head> </head> <body> <p style = “outline-width: thin; outline-style: solid; padding: 5px”> This text is having thin outline. </p> <p style = “outline-width: thick; outline-style: solid; padding: 5px”> This text is having thick outline. </p> <p style = “outline-width: 5px; outline-style: solid; padding: 5px”> This text is having 5px outline. </p> </body> </html> The outline-style Property The outline-style property specifies the style for the line (solid, dotted, or dashed) that goes around an element. It can take one of the following values − none − No outline. (Equivalent of outline-width:0;) solid − Outline is a single solid line. dotted − Outline is a series of dots. dashed − Outline is a series of short lines. double − Outline is two solid lines. groove − Outline looks as though it is carved into the page. ridge − Outline looks the opposite of groove. inset − Outline makes the box look like it is embedded in the page. outset − Outline makes the box look like it is coming out of the canvas. hidden − Same as none. Here is an example − <html> <head> </head> <body> <p style = “outline-width: thin; outline-style: solid; padding: 5px”> This text is having thin solid outline. </p> <p style = “outline-width: thick; outline-style: dashed; padding: 5px”> This text is having thick dashed outline. </p> <p style = “outline-width: 5px; outline-style: dotted; padding: 5px”> This text is having 5px dotted outline. </p> </body> </html> The outline-color Property The outline-color property allows you to specify the color of the outline. Its value should either be a color name, a hex color, or an RGB value, as with the color and border-color properties. Here is an example − <html> <head> </head> <body> <p style = “outline-width: thin; outline-style: solid; outline-color: red; padding: 5px”> This text is having thin solid red outline. </p> <p style = “outline-width: thick; outline-style: dashed; outline-color: #009900; padding: 5px”> This text is having thick dashed green outline. </p> <p style = “outline-width: 5px; outline-style: dotted; outline-color: rgb(13,33,232); padding: 5px”> This text is having 5px dotted blue outline. </p> </body> </html> The outline-offset Property The outline-offset property is used to specify the space between an outline and the border edge of an element. The space between the outline and the element is transparent. Here is an example − <html> <head> <style> div.example { margin: 20px; border: 2px dotted #000; background-color: #08ff90; outline: 4px solid #666; outline-offset: 10px; } </style> </head> <body> <h2>Outline-offset property</h2> <div class=”example”>The outline-offset is 10px</div> </body> </html> The outline Property The outline property is a shorthand property that allows you to specify values of different properties in a single statement. The values that can be passed are outline-style, outline-color and outline-width. The order in which the values can be passed is not fixed. outline-offset cannot be passed as a shorthand property in outline. It needs to be passed separately. Here is an example − <html> <head> </head> <body> <p style = “outline: thin solid red; padding: 10px;”> This text is having thin solid red outline. </p> <p style = “outline: thick dashed #009900; padding: 10px;”> This text is having thick dashed green outline. </p> <p style = “outline: 5px dotted rgb(13,33,232); padding: 10px;”> This text is having 5px dotted blue outline. </p> </body> </html> Outline vs Border Following table illustrates the differences between outline and border: Outline Border Outline is a non-rectangular shape that surrounds an element, usually with a solid color. Border is a rectangular shape that is drawn around the content of an element, can be solid, dashed, or dotted, and can have different colors and sizes. It does not take up any space in the layout and does not affect the size or position of the element. It affects the size and position of the element, as it adds width to the element. It is typically used to highlight or emphasize an element, such as when an element is focused or activated. It can be used for various purposes, such as separating elements, creating boxes, and adding visual emphasis. It can be created using the outline property in CSS. It can be created using the border property in CSS.

CSS – Isolation

CSS – isolation ”; Previous Next CSS isolation property is used to control how an element”s content interacts with its parent and sibling elements in terms of rendering and stacking context. It essentially determines whether an element establishes a new stacking context or not. Possible Values auto − This is the default value. It indicates that the element”s content does not create a new stacking context. Instead, it inherits the stacking context of its parent. isolate − This value indicates that the element creates a new stacking context, isolating its content from the rest of the document. This means that the element”s content will be rendered independently of its siblings and parent elements. Applies To All elements. In SVG, it applies to container elements, graphics elements, and graphics referencing elements. Syntax isolation: auto | isolate; CSS isolation – auto Value The following example demonstrates the isolation: auto property creating a new stacking context. The mix-blend-mode: difference subtracts the top color from the bottom color and creating in a high-contrast effect − <html> <head> <style> .container { background-color: yellow; width: 250px; height: 200px; padding: 5px; } .box2 { width: 130px; height: 130px; border: 5px solid red; padding: 5px; mix-blend-mode: difference; margin-left: 50px; } .box1 { isolation: auto; } </style> </head> <body> <div class=”container”> <div class=”box1″> <h3 class=”container box2″>isolation: auto;</h3> </div> </div> </body> </html> CSS isolation – isolate Value The following example demonstrates the isolation: isolate property that creates a new stacking context for box1, preventing the box1 from blending with external elements and the blend mode applied to box2 doesn”t impact elements inside box1 − The mix-blend-mode: difference property subtracts the top color from the bottom color and creating high-contrast effect. <html> <head> <style> .container { background-color: yellow; width: 250px; height: 200px; padding: 5px; } .box2 { width: 130px; height: 130px; border: 5px solid red; padding: 5px; mix-blend-mode: difference; margin-left: 50px; } .box1 { isolation: isolate; } </style> </head> <body> <div class=”container”> <div class=”box1″> <h3 class=”container box2″>isolation: isolate;</h3> </div> </div> </body> </html> Print Page Previous Next Advertisements ”;