CSS – Clearfix ”; Previous Next Clearfix is a technique to ensure that a container properly encloses and contains floated elements within it. It prevents layout issues by adding an empty element to the container, which clears both left and right floats, allowing the container to expand and maintain its intended layout. Clearfix helps to prevent the problems like container collapse, uneven heights, overlapping content, inconsistent alignment. This chapter will explore, how the clearfix technique ensures that container elements properly contains its floated child elements. As mentioned above ,the CSS clearfix fixes the overflow elements from the desired element. The following three properties can be worked upon for this: Overflow and float Property height Property clear Property The following diagram demonstrates the clearfix layout for reference: CSS Clearfix – With overflow & float Property Let us see an example where an image is larger than its container”s height, causing the image to extend beyond the boundaries of its container and potentially disrupting the layout. <html> <head> <style> div { border: 2px solid #f0610e; padding: 5px; background-color: #86f00e; } .tutimg { float: right; border: 3px solid #f0610e; } </style> </head> <body> <h2>Without clearfix</h2> <div> <img class=”tutimg” src=”images/tutimg.png” width=”200″ height=”200″> There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don”t look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn”t anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. </div> </body> </html> CSS Clearfix – With overflow Property To resolve this overflow, we can set overflow: auto; property to the corresponding element, ensuring that the image is fully contained within the container. <html> <head> <style> div { border: 2px solid #f0610e; padding: 5px; background-color: #86f00e; } .tutimg { float: right; border: 3px solid #f0610e; } .custom-clearfix { overflow: auto; } </style> </head> <body> <h2>With clearfix</h2> <div class=”custom-clearfix”> <img class=”tutimg” src=”images/tutimg.png” width=”200″ height=”200″> There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don”t look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn”t anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. </div> </body> </html> CSS Clearfix – With height Property You can also acheive clearfix by setting the height of <div> element similar to the height of the floated image. <html> <head> <style> div { border: 2px solid #f0610e; padding: 10px; height: 120px; background-color: #86f00e; } .tutimg { float: right; border: 3px solid #f0610e; } </style> </head> <body> <div> <img class=”tutimg” src=”images/tutimg.png” width=”120″ height=”120″> There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don”t look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn”t anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. </div> </body> </html> CCS Clear Property The clear CSS property affects the float concept by moving elements past it instead of having them move up to take the space available. The clear property applies to floating and non-floating elements. Possible values are: none: Is a keyword indicating that the element is not moved down to clear past floating elements. left: Is a keyword indicating that the element is moved down to clear past left floats. right: Is a keyword indicating that the element is moved down to clear past right floats. both: Is a keyword indicating that the element is moved down to clear past both left and right floats. inline-start: Is a keyword indicating that the element is moved down to clear floats on start side of its containing block, that is the left floats on ltr scripts and the right floats on rtl scripts. inline-end: Is a keyword indicating that the element is moved down to clear floats on end side of its containing block, that is the right floats on ltr scripts and the left floats on rtl scripts. CSS clear – left Value Following example demonstrates clearfix using clear:left property: <html> <head> <style> .mainbody{ border: 1px solid black; padding: 10px; } .left { border: 1px solid black; clear: left; } .aqua{ float: left; margin: 0; background-color: aqua; color: #000; width: 20%; } .pink{ float: left; margin: 0; background-color: pink; width: 20%; } p { width: 50%; } </style> </head> <body> <div class=”mainbody”> <p class=”aqua”> There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don”t look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn”t anything embarrassing hidden in ton the Internet. </p> <p class=”pink”>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p> <p class=”left”>This paragraph clears left.</p> </div> </body> </html> CSS clear – right Value Following example demonstrates clearfix using clear:right property: <html> <head> <style> .mainbody{ border: 1px solid black; padding: 10px; } .right { border: 1px solid black; clear: right; } .aqua{ float: right; margin: 0; background-color: black; color: #fff; width: 20%; } .pink{ float: right; margin: 0;
Category: css
CSS – Tables
CSS – Tables ”; Previous Next A table is an HTML element used to display data in a structured format with rows and columns. It is created using the <table> tag in HTML and can be styled using CSS properties. This chapter discusses how to set different properties of an HTML table using CSS. Let us see an example below which demonstrates a simple HTML table : <html> <head> <style> </style> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> </tbody> </table> </body> </html> The table in the above example is a simple display of data and can be styled using CSS. You can set following CSS properties of a table − The border-collapse specifies whether the browser should control the appearance of the adjacent borders that touch each other or whether each cell should maintain its style. The border-spacing specifies the width that should appear between table cells. The caption-side specifies where the table caption should be displayed (top or bottom). The empty-cells specifies whether the border should be shown if a cell is empty. The table-layout allows browsers to speed up layout of a table by using the first width properties it comes across for the rest of a column rather than having to load the whole table before rendering it. The width and height properties set the height and width of a table. The text-align property sets the horizontal alignment of the text content within table cells. The border property can be used to set border to table and its cells. The background-color property can be applied to the table itself, the table cells, or table rows. The font-size, font-family, font-weight, font-coloretc style the table font. Let us see how to use these properties with examples in the following sections. CSS Tables – Border Collapse The property border-collapse ensures that borders between table cells collapse into a single border, creating a cleaner look. Property border-collapse can have values collapse or separate (default). The following example only adds border to cells and body of table − <html> <head> <style> table { border: 3px solid purple; } th, td { border: 1px solid black; } </style> <body> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> </tbody> </table> </body> </html> Now let us add border-collapse:collapse and see how this removes the spacing between the table cells and causes the borders to overlap. <html> <head> <style> table { border-collapse: collapse; border: 3px solid purple; } th, td { border: 1px solid black; } </style> <body> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> </tbody> </table> </body> </html> CSS Tables – Border Spacing The border-spacing property specifies the distance that separates adjacent cells” borders in a table. This property may be specified as either one or two values.: border-spacing: 2px;: If one value is passed, the spacing is applied to both vertical and horizontal borders. border-spacing: 1cm 2em;: If two values are passed, the first value defines the horizontal spacing between cells (i.e., the space between cells in adjacent columns), and the second value defines the vertical spacing between cells (i.e., the space between cells in adjacent rows). Now let”s modify the previous example and see the effect − <html> <head> <style> table { border-collapse: separate; border-spacing: 1em 0.5em; padding: 0 2em 1em 0; border: 3px solid purple; } td { width: 1.5em; height: 1.5em; background: #d2d2d2; text-align: center; vertical-align: middle; } </style> <body> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> </tbody> </table> </body> </html> Note: The border-spacing property only works when border-collapse is set to separate. If you set border-collapse to collapse, the border-spacing property will have no effect, and the borders will collapse into a single line. Here is an example to demonstrate the above point: <html> <head> <style> table { border-collapse: collapse; border-spacing: 1em 0.5em; padding: 0 2em 1em 0; border: 3px solid purple; } td { width: 1.5em; height: 1.5em; background: #d2d2d2; text-align: center; vertical-align: middle; } </style> <body> <table> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> <tr> <td>Data 1</td> <td>Data 2</td> <td>Data 3</td> </tr> </tbody> </table> </body> </html> CSS Tables – Caption Side The caption-side property in CSS is used to control the placement or alignment of the table caption in relation to the table. The caption-side property can have one of following values: top: The caption placed above the table. bottom: The caption placed below the table. block-start: The caption box is positioned at the block start edge of the table. block-end: The caption box sis positioned at the block end edge of the table. inline-start: The caption box is positioned at the inline start edge of the table. inline-end: The caption box is positioned at the inline end edge of the table. inherit: The value is inherited from the caption-side property of the parent element. Let us see an example: <html> <head> <style> .top caption { caption-side: top; } .bottom caption { caption-side: bottom; } table { border: 1px solid red; } td { border: 1px solid blue; } </style> <body> <table class=”top”> <caption> Caption ABOVE the table </caption> <thead>
CSS – Borders
CSS – Borders Property ”; Previous Next A border, in the context of design and styling, refers to a decorative or functional element that surrounds the content of an object, such as a text box, image, or any other HTML element on a web page. The border property is used to create a border around an element, such as a div, image, or text. It allows you to customize the appearance of the border, including its color, width, and style. Borders play a vital role in the overall aesthetics and design of the webpage. Importance of borders The importance of using borders in CSS can be summarized as follows: Visual separation: Borders help to visually separate different elements on a webpage, making it easier for users to understand the layout and navigation. Organization and structure: Borders can be given to grids, tables or even boxes that makes the content look more organized and structured. Emphasis and focus: Borders can be given to elements to emphasize and highlight them. Design and aesthetics: Borders allow to add you to add decoration to the elements to enhance the visual appeal. This can be achieved using the style, color and width of border. Borders – Properties Following table describes the various properties of border, their description and default values they hold: Property Description Default value style specifies whether a border should be solid, dashed line, double line, or one of the other possible values none width specifies the width of a border medium color specifies the color of a border foreground color of the element and if element is blank, then color of the parent element Now, we will see how to use these properties with examples. CSS Borders – border-style Property The border-style property is one of the essential properties of border. Following values can be passed to border-style: Value Description none No border hidden A hidden border, same as ”none” except for table elements dotted A series of dots dashes A series of short dashes solid A single solid line double Two parallel lines with a small gap between them groove A border that appears to be carved into the page ridge A border that appears to be slightly raised above the page inset A border that appears embedded into the page outset A border that appears slightly raised out of the page initial Sets the border-style property to its default value inherit Inherits the border-style property from its parent element Let us see an example for these values of border-style: <html> <head> <style> p.none {border-style: none;} p.hidden {border-style: hidden;} p.dotted {border-style: dotted;} p.dashes {border-style: dashed;} p.solid {border-style: solid;} p.double {border-style: double;} p.groove {border-style: groove;} p.ridge {border-style: ridge;} p.inset {border-style: inset;} p.outset {border-style: outset;} p.mixed {border-style: none dashed solid dotted;} </style> </head> <body> <h2>border-style Property</h2> <p class=”none”>No border.</p> <p class=”hidden”>Hidden border.</p> <p class=”dotted”>Dotted border.</p> <p class=”dashes”>Dashed border.</p> <p class=”solid”>Solid border.</p> <p class=”double”>Double border.</p> <p class=”groove”>Groove border.</p> <p class=”ridge”>Ridge border.</p> <p class=”inset”>Inset border.</p> <p class=”outset”>Outset border.</p> <p class=”mixed”>A mixed border.</p> </body> <html> Single Side – Border Style The property border-style can be exclusively specified for each single-side. The same set of values can be passed to each single-side for border-style: border-top-style border-right-style border-bottom-style border-left-style Let us see an example: <html> <head> <style> p {border-top-style: dotted; border-right-style: solid; border-bottom-style: dashed; border-left-style: double; padding: 2em;} </style> </head> <body> <h2>border-style (single-side)</h2> <p>Different border styles on all sides.</p> </body> <html> CSS BorderS – width Property The border-width property is next in line after setting border style. Following values can be passed to border-width: Value Description thin a thin border medium a medium width border thick a thick border length any length specified (like 0.1em, 5px) Declare a border-style before declaring border-width, else the border effect will not be seen. Let us see an example (with and without specifying border-style): <html> <head> <style> p.thin {border-width: thin;} p.medium {border-width: medium;} p.thick {border-width: thick;} p.length {border-width: 100px;} p.thin1 {border-style: double; border-width: thin;} p.medium1 {border-style: dashed; border-width: medium;} p.thick1 {border-style: solid; border-width: thick;} p.length1 {border-style: dotted; border-width: 10px;} </style> </head> <body> <h2>border-width without <i>border-style</i> property</h2> <p class=”thin”>Thin border.</p> <p class=”medium”>Medium border.</p> <p class=”thick”>Thick border.</p> <p class=”length”>Specific length border.</p> <h2>border-width with <i>border-style</i> property</h2> <p class=”thin1″>Thin width.</p> <p class=”medium1″>Medium width.</p> <p class=”thick1″>Thick width.</p> <p class=”length1″>Specific length width border.</p> </body> </html> Single Side – Border Width The property border-width can be exclusively specified for each single-side. The same set of values can be passed to each single-side for border-width: border-top-width border-right-width border-bottom-width border-left-width Let us see an example: <html> <head> <style> p {border-style: solid; border-top-width: thin; border-right-width: thick; border-bottom-width: medium; border-left-width: 10px; padding: 2em;} </style> </head> <body> <h2>border-width (single-side)</h2> <p>Different border widths on all sides.</p> </body> </html> CSS Borders – color Property The border-color is the third constituent property of border. It sets the color of the border. border can have one, two, three or all four values. If no color is specified for border, the default value is currentcolor i.e. the foreground color. Any type of
CSS – Text
CSS – Text ”; Previous Next A text refers to a piece of written or printed information in the form of words or characters that can be read and understood. Texts can include content such as books, articles, emails, messages, web pages, etc. Styling a text involves modifying its appearance to make it more visually appealing or to convey a particular message. This chapter demonstrates how to manipulate text using CSS properties. There are several ways to style text. Following properties provided by CSS can be used for the styling purpose: color: Sets the color of the text. text-align Sets the alignment of the text. text-align-last: Sets the alignment of the last line of a block of text. direction: Sets the direction of the text of an element. text-indent: Sets the indentation of the first line of the text. letter-spacing: Specifies the space between the letters or characters that make up a word or text. word-spacing: Specifies the space between the words in a text. white-space: Controls the white space flow inside the text in an element. text-decoration: Adds an underline, overline, and strikethrough over a piece of text. text-decoration-skip: Determines what part of the content of the element, where text decoration is affecting, needs to be skipped. text-decoration-skip-ink: Specifies how the overline and underline text decoration lines are drawn around glyph ascenders and descenders. text-transform: Converts text to uppercase or lowercase letters. text-emphasis: Applies emphasis marks to text (except spaces and control characters). text-shadow: Adds shadow to the text. line-break: Controls how to set the rule for a line break. word-break: Controls how to set the rule for a word break. CSS Text – Text Color Altering the color of the text can add visual interest or align with a specific design scheme. The color property is used to set the color of the text. The possible values for this property are as follows: Color Name: Example = red, blue, green. Hexadecimal value: Example = #ff00ff. RGB value: Example = rgb(125, 255, 0). Here is an example: <html> <head> </head> <body> <h2>Text Color</h2> <p style = “color: blueviolet;”> Color Name </p> <p style = “color:#ff00ff;”> Hexadecimal value </p> <p style = “color: rgb(255,124,100);”> RGB value </p> </body> </html> CSS Text – Text Alignment The position or placement of text on a page is called its alignment. The text is aligned based on the left and right margins of the page. The CSS property that defines the alignment of the text is text-align. It sets the horizontal alignment of the text. Following are the possible values that this property can hold: start: Same as left, if direction is LTR and right if direction is RTL. end: Same as right, if direction is LTR and left if direction is RTL. left: Aligned with the left-margin. right: Aligned with the right-margin. center: Aligned at the center of the page. justify: Aligned with both the margins. justify-all: Same as justify, making the last line justified as well. match-parent: Similar to inherit. Values of start and right taken from parent”s value and replaced by left and right. When the direction is ltr, the default alignment is left-aligned and when the direction is rtl, the default alignment is right-aligned. Here is an example: <html> <head> </head> <body> <h2>Text Alignment</h2> <p style=”text-align: left;”>Text Left Alignment.</p> <p style=”text-align: right;”>Text Right Alignment.</p> <p style=”text-align: center;”>Text Center Alignment.</p> <p style=”text-align: justify; border: 2px solid red; width: 200px; height: 100px;”> Text Justify Alignment. This alignment aligns the text based on both the margins, left and right. </p> </body> </html> CSS Text – Vertical Alignment The vertical-align property is used to align the text vertically in an inline, inline-block and a table cell. The vertical-align property can have one of the following values: baseline: Baseline of the element is aligned with the baseline of the parent element. sub: Baseline of the element is lowered to the point appropriate for subscripted text. super: Baseline of the element is raised to the point appropriate for superscripted text. top: Top of the element”s box is aligned with the top of the line box, in the context of inline content, or with the top of the table cell in the context of tables. text-top: Top of the element”s box is aligned with the top of the highest inline box in the line. middle: Baseline of the element is aligned with the point defined by the baseline of the parent element plus half the x-height of the parent element”s font, in the context of inline content. bottom: Bottom of the element”s box is aligned with the bottom of the line box, in the context of inline content, or with the bottom of the table cell in the context of tables. text-bottom: Bottom of the element”s box is aligned with the bottom of the lowest inline box in the line. percentage: Baseline of the element is raised or lowered by the given percentage of the value for the property line-height. length: Baseline of the element is raised or lowered by the given length value. Negative length values are permitted for this property. A length value of 0 for this property has the same effect as the value baseline. Here is an example: <html> <head> <style> table,td {height:100px; width:400px; border:1px solid red;} </style> </head> <body> <table> <tr> <td style=”vertical-align: baseline”>baseline</td> <td style=”vertical-align: top”>top</td> <td style=”vertical-align: middle”>middle</td> <td style=”vertical-align: bottom”>bottom</td> <td> <p> No vertical alignment </p> </td> </tr> </table> <p> top: <img style=”vertical-align: top” src=”images/smiley.png” alt=”star”/> middle: <img style=”vertical-align: middle” src=”images/smiley.png” alt=”star”/> bottom: <img style=”vertical-align: bottom”
CSS – Border Block
CSS – border-block Property ”; Previous Next CSS border-block property is a shorthand property for assigning values to border-block-color, border-block-style and border-block-width in one go. The border-block-style is required parameter. If other parameters are not mentioned then default values will be used. This property depends on the direction of the block which is determined by the writing mode. Syntax border-block: border-block-width border-block-style border-block-color | initial | inherit; Property Values Value Description border-block-width It specifies the width of the border in the block direction. Default is medium. border-block-style It specifies the style of the border in the block direction. Default is none. border-block-color It specifies the color of the border in the block direction. Default is text”s color. initial This sets the property to its default value. inherit This inherits the property from the parent element. Examples of CSS Border Block Property The following examples explain the border-block property with different values. Setting the Width of Border To set the width of the border, we use the border-block-width component of the border-block property. In the following example, we have used ”thick” and 10px width for border-block-width property. Example <!DOCTYPE html> <html> <head> <style> #width1 { padding: 30px; border-block-style: solid; border-block-width: thick; } #width2 { padding: 30px; border-block-style: solid; border-block-width: 10px; } </style> </head> <body> <h2>CSS border-block property</h2> <p id=”width1″> This first example shows the width property of the border-block with thick value. </p> <p id=”width2″> This second example shows the width property of the border-block with 10px value. </p> </body> </html> Setting the Style of Border To set the style of the border, we use the border-block-style component of the border-block property. In the following example, we have used ”solid” and ”dashed” styles for border-block-style property. Example <!DOCTYPE html> <html> <head> <style> #style1 { padding: 30px; border-block-style: solid; } #style2 { padding: 30px; border-block-style: dashed; } </style> </head> <body> <h2>CSS border-block property</h2> <p id=”style1″> This first example shows the style property of the border-block with solid value. </p> <p id=”style2″> This second example shows the style property of the border-block with dashed value. </p> </body> </html> Setting the Color of Border To set the color of the border, we use the border-block-color component of the border-block property. In the following example, we have used ”red” and ”blue” colors for border-block-color property. Example <!DOCTYPE html> <html> <head> <style> #color1 { padding: 30px; border-block-style: solid; border-block-color:red; } #color2 { padding: 30px; border-block-style: solid; border-block-color:blue; } </style> </head> <body> <h2>CSS border-block property</h2> <p id=”color1″> This first example shows the color property of the border-block with red value. </p> <p id=”color2″> This second example shows the color property of the border-block with blue value. </p> </body> </html> Setting all Values at Once To set the values of width, style and color at once, we can simply use the border-block property by providing all the values at once. In the following example, the width has been taken as 5px, style as dashed and color as green. Example <!DOCTYPE html> <html> <head> <style> #block { padding: 30px; border-block: 5px dashed green; } </style> </head> <body> <h2>CSS border-block property</h2> <p id=”block”> This example shows the border-block property defining all values at once. </p> </body> </html> Setting Border Block with Writing Mode The writing-mode in the context of border-block influences the direction of the border. By default, the border appears horizontally, however by changing the writing mode, the border”s direction can be changed Horizontal-tb: border appears horizontally. Vertical-lb: border appears vertically. These are shown in the following example. Example <!DOCTYPE html> <html> <head> <style> #horizontal { inline-size: 200px; padding: 10px; writing-mode: horizontal-tb; border-block: 5px dashed red; } #vertical { inline-size: 200px; padding: 10px; writing-mode: vertical-rl; border-block: 5px dashed green; } </style> </head> <body> <h2>CSS border-block property</h2> <div> <p id=”horizontal”>This example shows the horizontal boder.</p> </div> <div> <p id=”vertical”> This example shows the vertical border.</p> </div> </body> </html> Supported Browsers Property border-block 87.0 87.0 66.0 14.1 73.0 css_properties_reference.htm Print Page Previous Next Advertisements ”;
CSS – Focus
CSS – focus ”; Previous Next CSS focus The :focus pseudo-class in CSS represents an element that has received focus. This class is generally triggered when a user clicks, taps on an element or selects an element using the tab key of keyboard. :focus pseudo-class is applied to the focussed element itself. In order to select an element that contains a focussed element, use :focus-within pseudo-class. Applies to All HTML elements, such as buttons, dropdowns, input fields, etc. Syntax selector:focus{ properties } CSS Focus – Link Here is an example where focus is set on a link: <html> <head> <style> a { color: darkviolet; transition: all 0.3s ease; } a:focus { outline: none; color: red; background-color: yellow; } body { margin: 5px; padding: 2rem; display: grid; font: 20px/1.4 system-ui, -apple-system, sans-serif; } </style> </head> <body> <p>Click on the link or press the tab key.</p> <p>The link here has a <a href=”#0″>change in background and foreground color</a> as it is focussed.</p> </body> </html> CSS Focus – Button Here is an example where focus is set on a button: <html> <head> <style> button { display: block; margin: 2em; padding: 2px; font-weight: bold; font-size: 18px; text-align: center; color: black; background-color: #fff; border: 2px solid black; border-radius: 24px; cursor: pointer; transition: all 0.3s ease; width: 150px; height: 50px; } button:hover { color: blue; border-color: black; background-color: lightpink; } button:focus { background-color: darkred; color: white; border-radius: 2px; } </style> </head> <body> <p>Click on the button or press the tab key.</p> <button>Click Me!!</button> </body> </html> CSS Focus – Input Box Here is an example where focus is set on an input: <html> <head> <style> label { display: block; font-size: 20px; color: black; width: 500px; } input[type=”text”] { padding: 10px 16px; font-size: 16px; color: black; background-color: #fff; border: 1px solid #597183; border-radius: 8px; margin-top: 5px; width: 500px; transition: all 0.3s ease; } input[type=”text”]:focus { background-color: lightyellow; } </style> </head> <body> <p>Click on the text box or press the tab key.</p> <form> <label> Full Name <input type=”text”> </label> </form> </body> </html> CSS Focus – Dropdown Box Here is an example where focus is set on a dropdown: <html> <head> <style> label { display: block; font-size: 18px; color: black; width: 500px; } select { padding: 10px 16px; font-size: 16px; color: black; background-color: #fff; border: 1px solid #597183; border-radius: 8px; margin-top: 25px; width: 300px; transition: all 0.3s ease; } select:focus { background-color: rgb(173, 233, 209); box-shadow: 20px 15px 30px yellow, -20px 15px 30px lime, -20px -15px 30px blue, 20px -15px 30px red; } </style> </head> <body> <p>Select an item from the dropdown box.</p> <form> <label> Ice cream Flavors: <select name=”flavor”> <option>Cookie dough</option> <option>Pistachio</option> <option>Cookies & Cream</option> <option>Cotton Candy</option> <option>Lemon & Raspberry Sorbet</option> </select> </label> </form> </body> </html> CSS Focus – Toggle Button Here is an example where focus is set on a toggle: <html> <head> <style> .toggle { position: relative; display: block; width: 100%; margin: 2em; } .toggle label { padding-right: 8px; font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: 16px; line-height: 28px; color: black; } .toggle span { position: relative; display: inline-block; vertical-align: middle; cursor: pointer; } .toggle span:before { display: block; width: 50px; height: 30px; content: “”; background-color: #a9adaf; border-radius: 28px; transition: background-color 0.3s ease; } .toggle span:after { position: absolute; top: 1px; left: 1px; display: block; width: 30px; height: 28px; visibility: visible; content: “”; background-color: #fff; border-radius: 28px; transition: left 0.3s ease; } input[type=”checkbox”]:checked ~ span:before { background-color: orange; } input[type=”checkbox”]:checked ~ span:after { top: 1px; left: 21px; } input[type=”checkbox”]:hover ~ span:before { background-color: #1194d1; } input[type=”checkbox”]:not(:checked):hover ~ span:before { background-color: #afbec9; } input[type=”checkbox”]:focus ~ span:before { outline: none; box-shadow: 0 0 0 4px red; } </style> </head> <body> <div class=”toggle”> <label for=”toggle1″>Check the box to toggle me</label> <input type=”checkbox” id=”toggle1″ /> <span></span> </div> </body> </html> CSS Focus – Associated Properties Apart from :focus, there are two more pseudo-classes that are associated with the :focus pseudo-class. They are listed in the table given below: sr.no pseudo-class description 1 :focus-visible Focus is made evident on an element and it is shown to the user. 2 :focus-within sets focus on the element that contains a focussed element. Print Page Previous Next Advertisements ”;
CSS – Margins
CSS – Margins ”; Previous Next CSS Margins are used to create space around outer part of an element. In this tutorial we will learn how to add different types of margins to HTML elements and properties associated with it. What is CSS Margin? A Margin in CSS make layout visually appealing by adding extra space between elements. You can set margin for individual sides using properties margin-bottom, margin-top, margin-left and margin-right Negative Margin: A margin with negative value indicate the elements overlap with each other. CSS Margins Example You can try different ways to use to create margins in the below section and you can change the values as well. margin: 1em margin: 10px 0 margin: 10px 50px 20px margin: 10px 50px 20px 40px margin: 30px Try Different Margin Options Table of Contents Define Margin Margin Individual Sides Different Ways to Apply Margin Margin Percentage Values Margin Mix up Units Margin Properties Reference Define Margin To define any margin on any HTML element you can use CSS margin property, this property is shorthand property of ”margin-top”, ”margin-right”, ”margin-bottom” and ”margin-left” properties. A single value will generate margin all around the element. Syntax margin: “value”; Example As you can see in this below example we have made a 10px and 20px margin surrounding the paragraph element and highlight the margin area with light-green background. <!DOCTYPE html> <html> <head> <title>CSS Margin</title> <style> div{ background-color: lightgray; border: 1px solid black; } </style> </head> <body> <h1>Tutorialspoint</h1> <h3>CSS Margin</h3> <div> <div style=”margin: 20px; background: white;”> CSS Margin 20px all sides </div> <hr color=”blue”> <div style=”margin: 10px; background: white;”> CSS Margin 10px all sides </div> </div> </body> </html> Margin Individual Sides As we have mentioned earlier the margin is shorthand property for all Individual sides margin. You can set different margin values for top, right, bottom and left sides. margin-top: This property is used to set the margin top on any element. margin-right: This property is used to set the margin right on any element. margin-bottom: This property is used to set the margin bottom on any element. margin-left: This property is used to set the margin left on any element. You can check the attached image for more clarity on individual side margins. Syntax margin-top: “value”; margin-right: “value”; margin-bottom: “value”; margin-left: “value”; Example In this example we have create 4 different element and generated margin on each element”s individual sides with the above mentioned properties. <!DOCTYPE html> <html> <head> <title>CSS Margin</title> <style> div{ background-color: lightgray; border: 1px solid black; } p { background-color: lightgreen; border: 1px solid black; } span { background-color: white; } </style> </head> <body> <h1>Tutoriaslpoint</h1> <h3>CSS Margin</h3> <div> <p style=”margin-top: 15px;”> <span>CSS Margin Top Applied on Paragraph Element</span> </p> <hr> <p style=”margin-right: 15px;”> <span>CSS Margin Right Applied on Paragraph Element</span> </p> <hr> <p style=”margin-bottom: 15px;”> <span>CSS Margin Bottom Applied on Paragraph Element</span> </p> <hr> <p style=”margin-left: 15px;”> <span>CSS Margin Left Applied on Paragraph Element</span> </p> </div> </body> </html> All the left over space on the right side of the element can be confusing to identify the margin, if you try on your own by changing values it will clear the margin concept. And always remember negative values are not allowed for margin in CSS. Different Ways to Apply Margin on HTML Elements There are four ways to provide value to the CSS margin property all of them are mentioned and described bellow with the complete example code. Single Values: Here you can provide a single value to the margin property that same value will be applicable on four sides of the the element. Two Values: Here you have to provide two values that will be used as top, bottom and right, left margin value. Three Values: In this way you can provide three values that will define top, left, right and bottom value. Like if you set margin: 20px 40px 10px. In this case top margin will be 20px, right and left margins will be 40px and bottom margin will be 10px. Four Values: If you provide four values to the margin property it will generate top margin from first value, right margin from 2nd value and so on. Syntax margin: “value” // Single Value margin: “value value” // Two Values margin: “value value value” // Three Values margin: “value value value value” // Four Values Example In this following example we have crated a four different element and used inline css to generate margin around the element in different ways. <!DOCTYPE html> <html> <head> <title>CSS Margin</title> <style> div{ background-color: lightgray; border: 1px solid black; } p { background-color: lightgreen; border: 1px solid black; padding: 10px; } </style> </head> <body> <h1>Tutoriaslpoint</h1> <h3>CSS Margin</h3> <div> <p style=”margin: 20px”> <span>Margin property with Single Value</span> </p> <hr/> <p style=”margin: 10px 20px”> <span>Margin property with two Values</span> </p> <hr/> <p style=”margin: 10px 15px 20px”> <span>Margin property with three Values</span> </p> <hr/> <p style=”margin: 5px 10px 15px 20px”> <span>Margin property with four Values</span> </p> </div> </body> </html> Margin Mix up Units CSS does not restrict usage of multiple units for specifying the values of margin, while specifying in shorthand property. That means one can pass values of length as pixel along with ems or inches, etc. Syntax h2 { margin: 20px 4ex .5in 3em; } Example In this following example we will
CSS – Hover
CSS – :hover ”; Previous Next The :hover pseudo-class in CSS is used to target an element when the user hovers over it with the mouse cursor. Its purpose is to apply styles or trigger specific behaviors to enhance the user experience or provide additional visual feedback. :hover is a tool to make interactive elements more dynamic and engaging without requiring any user input beyond moving the mouse. Applies to HTML elements, such as buttons, links, images, etc. DOM Syntax :hover { /* … */ } CSS :hover – With Background-color Property Here is an example of changing the background color of a link: <html> <head> <style> div { padding: 1rem; } a { background-color: rgb(238, 135, 9); font-size: 1rem; padding: 5px; } a:hover { background-color: rgb(235, 235, 169); } </style> </head> <body> <div> <h3>Hover example – link</h3> <a href=”#”>Hover over to see the color change!!!</a> </div> </body> </html> CSS :hover – With Button Effect Here is an example of changing the background color of a button: <html> <head> <style> div { padding: 1rem; } button { background-color: greenyellow; font-size: large; } button:hover { background-color: gold; } </style> </head> <body> <h3>Hover example – button</h3> <button>Hover me!!!</button> </div> </body> </html> CSS :hover – With Border Effect Here is an example, where border of the link is changing on hover: <html> <head> <style> .link { color: blue; text-transform: uppercase; padding: 20px; border: 4px solid blue; border-radius: 10px; display: inline-block; } .link:hover { color: #494949; border-radius: 45px; border-color: #494949; } </style> </head> <body> <h3>Hover effect on border of the link</h3> <div class=”button”><a class=”link” href=”#”>Check my borders on hover</a></div> </body> </html> CSS :hover – With box-shadow Here is an example, where box-shadow is added to the link on hover: <html> <head> <style> a { font-size: 2rem; color: #071402; margin: 0 .25rem; padding: 0 .25rem; transition: color .3s ease-in-out, box-shadow .3s ease-in-out; } a:hover { box-shadow: inset 100px 0 0 100px #9ce2cc; color: rgb(240, 32, 32); } </style> </head> <body> <div> <a href=”#”>Hover over me!!</a> </div> </body> </html> CSS :hover – With Background Effect Here is an example, where the background grows over a link on hover: <html> <head> <style> a { text-decoration-line: underline; color: #18272F; font-size: 2rem; position: relative; } a::before { content: ””; background-color: rgba(118, 213, 83, 0.75); font-size: 2rem; position: absolute; left: 0; bottom: 3px; width: 100%; height: 10px; z-index: -1; transition: all .5s ease-in-out; } a:hover::before { bottom: 0; height: 100%; } </style> </head> <body> <div> <a href=”#”>Hover over me!!</a> </div> </body> </html> CSS :hover – Rainbow Color Effect Here is an example, where the underline of the link gives a rainbow color effect on hover: <html> <head> <style> a { color: black; text-decoration: none; } a { background: linear-gradient( to right, rgb(179, 232, 168), rgb(185, 162, 215) ), linear-gradient( to right, rgba(255, 0, 0, 1), rgba(255, 0, 180, 1), rgba(0, 200, 50, 1) ); background-size: 100% 5px, 0 5px; background-position: 100% 100%, 0 100%; background-repeat: no-repeat; transition: background-size 700ms; } a:hover { background-size: 0 5px, 100% 5px; } body { display: grid; font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: 2rem; height: 100vh; } </style> </head> <body> <div> <a href=”#”>Hover over me!!</a> </div> </body> </html> CSS :hover – Shadow Effect Here is an example, where the a shadow effect is given to a button on hover: <html> <head> <style> body { width: 100%; height: 100vh; display: grid; justify-content: center; align-items: center; } .glow { padding: 10px; width: 250px; height: 50px; border: none; outline: none; color: #fff; background: #111; cursor: pointer; position: relative; z-index: 0; border-radius: 20px; } .glow:before { content: ””; background: linear-gradient(60deg, #ff0000, #ff7300, #fffb00, #48ff00, #00ffd5, #002bff, #7a00ff, #ff00c8, #ff0000); position: absolute; top: -4px; left:-4px; background-size: 400%; z-index: -1; filter: blur(15px); width: calc(100% + 6px); height: calc(100% + 6px); animation: glowing 20s linear infinite; opacity: 0; transition: opacity .3s ease-in-out; border-radius: 10px; } .glow:active { color: rgb(246, 235, 235) } .glow:active:after { background: transparent; } .glow:hover:before { opacity: 1; } .glow:after { z-index: -1; content: ””; position: absolute; width: 100%; height: 100%; background: #111; left: 0; top: 0; border-radius: 10px; } @keyframes glowing { 0% { background-position: 0 0; } 50% { background-position: 400% 0; } 100% { background-position: 0 0; } } </style> </head> <body> <button class=”glow” type=”button”>HOVER OVER & CLICK!</button> </body> </html> Print Page Previous Next Advertisements ”;
CSS – Inclusion
CSS – Inclusion ”; Previous Next There are four ways to associate styles with your HTML document. Most commonly used methods are inline CSS and External CSS. Embedded CSS – The <style> Element You can put your CSS rules into an HTML document using the <style> element. This tag is placed inside the <head>…</head> tags. Rules defined using this syntax will be applied to all the elements available in the document. Here is the generic syntax − Live Demo <!DOCTYPE html> <html> <head> <style type = “text/css” media = “all”> body { background-color: linen; } h1 { color: maroon; margin-left: 40px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> </body> </html> It will produce the following result − Attributes Attributes associated with <style> elements are − Attribute Value Description type text/css Specifies the style sheet language as a content-type (MIME type). This is required attribute. media screen tty tv projection handheld print braille aural all Specifies the device the document will be displayed on. Default value is all. This is an optional attribute. Inline CSS – The style Attribute You can use style attribute of any HTML element to define style rules. These rules will be applied to that element only. Here is the generic syntax − <element style = “…style rules….”> Attributes Attribute Value Description style style rules The value of style attribute is a combination of style declarations separated by semicolon (;). Example Following is the example of inline CSS based on the above syntax − Live Demo <html> <head> </head> <body> <h1 style = “color:#36C;”> This is inline CSS </h1> </body> </html> It will produce the following result − External CSS – The <link> Element The <link> element can be used to include an external stylesheet file in your HTML document. An external style sheet is a separate text file with .css extension. You define all the Style rules within this text file and then you can include this file in any HTML document using <link> element. Here is the generic syntax of including external CSS file − <head> <link type = “text/css” href = “…” media = “…” /> </head> Attributes Attributes associated with <style> elements are − Attribute Value Description type text css Specifies the style sheet language as a content-type (MIME type). This attribute is required. href URL Specifies the style sheet file having Style rules. This attribute is a required. media screen tty tv projection handheld print braille aural all Specifies the device the document will be displayed on. Default value is all. This is optional attribute. Example Consider a simple style sheet file with a name mystyle.css having the following rules − h1, h2, h3 { color: #36C; font-weight: normal; letter-spacing: .4em; margin-bottom: 1em; text-transform: lowercase; } Now you can include this file mystyle.css in any HTML document as follows − <head> <link type = “text/css” href = “mystyle.css” media = ” all” /> </head> Imported CSS – @import Rule @import is used to import an external stylesheet in a manner similar to the <link> element. Here is the generic syntax of @import rule. <head> @import “URL”; </head> Here URL is the URL of the style sheet file having style rules. You can use another syntax as well − <head> @import url(“URL”); </head> Example Following is the example showing you how to import a style sheet file into HTML document − <head> @import “mystyle.css”; </head> CSS Rules Overriding We have discussed four ways to include style sheet rules in a an HTML document. Here is the rule to override any Style Sheet Rule. Any inline style sheet takes highest priority. So, it will override any rule defined in <style>…</style> tags or rules defined in any external style sheet file. Any rule defined in <style>…</style> tags will override rules defined in any external style sheet file. Any rule defined in external style sheet file takes lowest priority, and rules defined in this file will be applied only when above two rules are not applicable. Handling old Browsers There are still many old browsers who do not support CSS. So, we should take care while writing our Embedded CSS in an HTML document. The following snippet shows how you can use comment tags to hide CSS from older browsers − <style type = “text/css”> <!– body, td { color: blue; } –> </style> CSS Comments Many times, you may need to put additional comments in your style sheet blocks. So, it is very easy to comment any part in style sheet. You can simple put your comments inside /*…..this is a comment in style sheet…..*/. You can use /* ….*/ to comment multi-line blocks in similar way you do in C and C++ programming languages. Example Live Demo <!DOCTYPE html> <html> <head> <style> p { color: red; /* This is a single-line comment */ text-align: center; } /* This is a multi-line comment */ </style> </head> <body> <p>Hello World!</p> </body> </html> It will produce the following result − Print Page Previous Next Advertisements ”;
CSS – Backgrounds
CSS – Background Property ”; Previous Next The background property of CSS is used to set the background of an element. It can be used to apply a single background image or multiple background images, as well as defining the background color, size, position, repeat behavior, and other related properties. It is a versatile tool for styling the appearance of elements and adding visual interest to web pages. The background is a shorthand for the following properties: background-attachment: Specifies the position of the background relative to the viewport, either fixed or scrollable. background-clip: Controls how far a background image extends beyond the element”s padding or content box. background-color: Sets the background color of an element. background-image: Sets one or more background image(s) on an element. background-origin: Sets the origin of the background. background-position: Sets the initial position of each image in a background. background-repeat: Controls the repetition of an image in the background. background-size: Controls the size of the background image. Overview The background property allows to set one or multiple layers of background, separated by commas. Each layer in the background may have zero or atleast one occurrence of the following values: <attachment> <bg-image> <position> <bg-size> <repeat-style> If <bg-size>is to be added, it must be included immediately after <position>, separated with ”/”. For example: “left/50%”. Value of <box> can be included zero, one or two times. Based on the number of occurrences, following values are set: once – sets the values for both background-origin and background-clip. twice – first sets the value of background-origin and second sets the value of background-clip. The value of background-color may be included in the last layer specified as background. Possible Values Following are the possible values that the background shorthand property can have: <attachment>: Specifies the position of the background, whether fixed or scrollable. Default is scroll. <box>: Default is border-box and padding-box respectively. <background-color>: Sets the color of the background. Default is transparent. <bg-image>: Sets the one or more background image. Default is none. <position>: Sets the initial position of the background. Default is 0% 0%. <repeat-style>: Controls the repetition of the image in background. Default is repeat. <bg-size>: Controls the size of the background image. Default is auto. Syntax background = background-color | bg-image | bg-position / bg-size | repeat-style | attachment | box; CSS Background – With Image And Background color Here is an example of setting a background using the shorthand property background, where an image and background color is specified: <html> <head> <style> body { background: url(”images/scenery2.jpg”) center/40% 50% no-repeat fixed padding-box content-box lightblue; } </style> </head> <body> <h2>Shorthand Background</h2> </body> </html> CSS Background – Image Repeat Here is an example of setting a background using the shorthand property background, where image is repeated: <html> <head> <style> body { background: url(”images/scenery2.jpg”) repeat fixed padding-box content-box lightblue; } </style> </head> <body> <h2 style=”color: white;”>Shorthand Background – background repeated</h2> </body> </html> CSS Background – Image With Background Color Here is an example of setting a background using the shorthand property background, where two images are added and a background color: <html> <head> <style> body { background: url(”images/orange-flower.jpg”) right bottom/30% 60% no-repeat, url(”images/pink-flower.jpg”) left top/30% 60% no-repeat lightgoldenrodyellow; } </style> </head> <body> <h2 style=”color: black; text-align: center;”>Shorthand Background – two images</h2> </body> </html> CSS Backgrounds- Related Properties All the properties related to background are listed in the table below: Property Description background Shorthand for background related properties. background-attachment Specifies the position of the background relative to the viewport, either fixed or scrollable. background-clip Controls how far a background image extends beyond the element”s padding or content box. background-color Sets the background color of an element. background-image Sets one or more background image(s) on an element. background-origin Sets the origin of the background. background-position Sets the initial position of each image in a background. background-position-x Sets the initial horizontal position of each image in a background. background-position-y Sets the initial vertical position of each image in a background. background-repeat Controls the repetition of an image in the background. background-size Controls the size of the background image. background-blend-mode Determines how an element”s background images blend with each other. Print Page Previous Next Advertisements ”;