The float property indicates in which direction to display an element being inserted on a web page.
The author selected the Diversity in Tech Fund to receive a donation as part of the Write for DOnations program. Show IntroductionThere are many ways to work with layout on the web. The modern approaches tend to rely on CSS Grid and Flexbox properties to create many web layouts. However, before Grid and Flexbox existed, web developers relied heavily on the float and columns properties. The float property is used to allow inline elements to wrap around a floating element, and column sets parameters for arranging text in columns. Because these properties also have properties that neither Grid and Flexbox can replicate, learning this method of layout can add options and flexibility to a designer’s set of skills. In this tutorial you will work with the float and columns properties to create various layout styles and effects. You will use the float property to float a pull quote and an image and cause the content to wrap around those elements. Then you will use the columns property to adjust vertically long content to spread across the horizontal axis. Additionally, the concepts and methods of responsive web design will be covered as you work to apply the various styles. Prerequisites
Setting Up the Initial HTML and CSSIn this section you will set up the base HTML and CSS that will be used throughout the tutorial. You will use demo text content from Cupcake Ipsum and the font Comfortaa from Google Fonts. To begin, open the index.html file in your text editor. Then, add the following HTML to the file: index.html doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Cupcake Layoutstitle> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Comfortaa:wght@400;700&display=swap" rel="stylesheet"> <link rel="stylesheet" href="styles.css" /> head> <body> body> html>There are a lot of page settings being defined inside the element. The first element defines the characters set to use for the text. This way most special characters, such as an accent mark, will render without special HTML codes. The second element tells browsers (mobile browsers in particular) to treat the width of the device as the width of the content; otherwise the browser will simulate a 960 pixel desktop width. TheNext, the page will need content to style. You will use sample content from Cupcake Ipsum as filler copy to work with the styles. Throughout the tutorial, new code will be highlighted. Return to index.html in your text editor and add the highlighted HTML from the following code block: index.html doctype html> <html> <head> ... head> <body> <article> <h2>Carrot Cake Pie Toffee Cheesecakeh2> <p>Gummi bears powder tootsie roll biscuit. Icing jelly-o tootsie roll powder pie dessert biscuit tiramisu. Jelly beans pudding lemon drops sesame snaps jujubes sesame snaps gummi bears tiramisu. Apple pie danish soufflé soufflé cupcake cookie donut. Cookie gummies powder dragée powder chocolate bar muffin jelly marshmallow. Cheesecake dessert chocolate bar chocolate cake gingerbread chocolate bar gingerbread wafer brownie. Bear claw cake sugar plum muffin candy canes tart cheesecake croissant candy canes. Gummi bears carrot cake sweet chocolate. Carrot cake cake sweet bear claw. Gummi bears chocolate pie croissant chocolate cake bear claw dragée.p> <p>Tootsie roll cotton candy cake tiramisu pastry. Croissant oat cake ice cream dragée sweet roll soufflé marshmallow powder marzipan. Lemon drops cake bear claw cake sweet lollipop. Lollipop toffee liquorice apple pie gingerbread pastry tiramisu.p> <h2>Jelly Beans Wafer Apple Pie Icingh2> <p>Jelly cotton candy cake pastry carrot cake. Topping icing danish. Lollipop sesame snaps muffin bonbon icing. Lollipop chocolate bar jelly-o sweet roll. Topping bear claw sweet. Apple pie bonbon sweet lemon drops caramels topping pastry. Biscuit jelly tootsie roll cookie tiramisu cotton candy. Icing chocolate cake liquorice ice cream sesame snaps cupcake chocolate bar pudding gingerbread. Donut toffee ice cream chocolate biscuit.p> <p>Carrot cake pie halvah toffee cheesecake chocolate cake gingerbread liquorice. Marshmallow danish candy fruitcake chocolate dessert. Bear claw chocolate bar sugar plum pudding cake cheesecake sweet. Marzipan lemon drops croissant gummies. Dessert carrot cake carrot cake cookie candy canes toffee. Biscuit bear claw candy canes muffin. Lollipop gummies tart danish tart sugar plum biscuit. Lemon drops muffin sugar plum sweet. Cotton candy pastry topping caramels gummies carrot cake marzipan tiramisu sesame snaps.p> <p>Ice cream donut cookie. Donut icing cookie sweet roll topping cookie. Jelly beans marzipan jujubes jelly beans ice cream marzipan apple pie cake. Sweet croissant sweet roll carrot cake oat cake tiramisu ice cream wafer wafer. Candy canes jelly beans toffee danish. Liquorice donut macaroon caramels. Donut pie cupcake gummies. Icing jujubes pudding apple pie pastry muffin sweet roll ice cream chocolate bar. Liquorice icing sugar plum marshmallow icing.p> <h2>Jelly Sugar Plum Chocolate Danishh2> <p>Topping marzipan sesame snaps soufflé chupa chups cookie wafer cupcake. Jelly beans lollipop jelly beans. Chocolate cake lemon drops chupa chups candy icing tootsie roll danish liquorice. Gummies danish dragée apple pie jelly pie topping icing tootsie roll. p> <p>Apple pie bear claw muffin tiramisu gummi bears cake muffin. Candy canes oat cake croissant cake liquorice topping halvah dessert cheesecake. Candy fruitcake muffin. Cookie oat cake gummies brownie dessert candy canes icing. Soufflé chocolate cake pastry toffee cheesecake macaroon liquorice gummi bears. Halvah tiramisu jujubes. Bear claw candy danish. Macaroon chocolate wafer soufflé biscuit. Bear claw biscuit sesame snaps biscuit liquorice sweet roll lollipop. Marshmallow bear claw dragée icing liquorice candy donut chupa chups. Halvah dessert bonbon cupcake cupcake sugar plum cotton candy toffee muffin. Bonbon gummi bears jujubes chupa chups oat cake candy canes.p> <p>Gummies bonbon marzipan icing pudding. Jujubes croissant carrot cake. Pastry halvah pudding toffee. Lemon drops gingerbread chocolate apple pie jelly cheesecake.p> <p>Fruitcake dessert chocolate cupcake carrot cake dessert candy canes chocolate soufflé. Cookie cupcake marzipan sesame snaps biscuit tart pie jelly-o. Halvah tiramisu gummies biscuit powder donut. Chocolate cake bear claw macaroon.p> <h2>Gingerbread Macaroon Fruitcakeh2> <p>Jelly chocolate gummies cupcake liquorice chocolate. Sugar plum donut tiramisu muffin chupa chups. Cake caramels fruitcake jelly beans pudding I love pie lollipop jelly beans. Gummi bears cheesecake dragée I love fruitcake sesame snaps I love. Apple pie muffin donut. Fruitcake donut chocolate bar cotton candy topping candy macaroon I love. Fruitcake sesame snaps jelly-o. Tart wafer sugar plum I love apple pie biscuit. Pastry sesame snaps tart. Apple pie gingerbread chocolate candy canes.p> <p>I love cake sweet roll toffee lemon drops bonbon sweet. Oat cake lemon drops candy canes topping macaroon.p> <p>Sweet cotton candy danish. Lollipop bear claw pudding carrot cake. Gummi bears candy canes chocolate cake chupa chups. Sugar plum jelly cake cookie cheesecake croissant muffin carrot cake. Pudding I love liquorice jelly-o caramels I love sweet roll bonbon oat cake. Caramels gummi bears gingerbread.p> article> <body> html>Save these additions to your index.html file then open it in your web browser. The content will use the browser default styles, as shown in the following image: Create a file called styles.css in the same folder as index.html and open it in your text editor. This file will contain all the styles used throughout the tutorial. The first set of styles will apply a general aesthetic that you will build on later. Apply the CSS from the following code block to your styles.css file: styles.css body { font: 100%/1.5 Comfortaa, sans-serif; color: hsl(300, 100%, 10%); background-color: hsl(300, 100%, 98%); } article { width: 90%; max-width: 75ch; margin: 2rem auto; } h2 { font-size: 1.75rem; color: hsl(204, 100%, 29%); margin: 1em 0 0.5em; } h2 { font-size: 1.5rem; color: hsl(204, 100%, 29%); margin: 1em 0 0.5em; }These styles add a minimal set of styles, using a shorthand font property on the body selector to set a new default font family and line height between lines of the text. The styles set to the article selector make the container have a width slightly smaller than the full screen width, but keep that content centered with the margin property set to auto for the left and right values. The article also has a maximum with of 75ch, which means it only gets as wide as 75 zero characters of the set font-size value. Lastly, the h2 and h2 selectors set a new font-size, color, and margin for each. Save your changes to styles.css, then refresh index.html in your web browser. The following image shows how these new base styles will appear in the browser: In this section you set up the the base HTML and CSS needed to begin adding float and columns content. In the next section, you will create a block quote that floats, allowing content to wrap around it. You will also create a horizontal rule line that stops content from wrapping around the floating element. Floating ContentNext you will add a element and apply a float property to cause the content to wrap around the floating content. |